Skip to content

Commit fafa1b0

Browse files
authored
Merge pull request iden3#92 from victorcrrd/plonk-custom-gates-doc
Added some documentation regarding custom templates
2 parents 3fa5592 + 1382091 commit fafa1b0

File tree

4 files changed

+38
-3
lines changed

4 files changed

+38
-3
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Custom templates in [snarkjs](../index.md#snarkjs)
2+
3+
[snarkjs](https://github.com/iden3/snarkjs) provides an implementation of the PLONK's zkSNARK. An extension of the scheme, [turbo-PLONK](https://docs.zkproof.org/pages/standards/accepted-workshop3/proposal-turbo_plonk.pdf), allows the definition of the so called custom gates: more general transition gates than the ones defined by default for the regular PLONK zkSNARK, that allows the circuit's designer to, maybe, reduce the number of used gates, probably resulting in shorter proofs size or verification times. This document will contain a list of the custom gates implemented in snarkjs that can me used in the circom language. Note that the list may grow over time with the new implementations from the iden3 collaborators or thanks to contributions from the community.
4+
5+
## List of custom gates implemented in snarkjs
6+
At the moment there are no custom gates implemented in snarkjs yet.
Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
11
# Pragma
22

3-
All files with .circom extension should start with a first `pragma`instruction specifying the compiler version, like this:
3+
## Version pragma
4+
5+
All files with .circom extension should start with a first `pragma` instruction specifying the compiler version, like this:
46

57
```text
68
pragma circom xx.yy.zz;
79
```
810

9-
This is to ensure that the circuit is compatible with the compiler version indicated after the `pragma` instruction. Otherwise, the compiler throws a warning.
11+
This is to ensure that the circuit is compatible with the compiler version indicated after the `pragma` instruction. Otherwise, the compiler throws a warning.
1012

1113
If a file does not contain this instruction, it is assumed that the code is compatible with the latest compiler's version and a warning is thrown.
1214

15+
## Custom templates pragma
16+
17+
Since circom 2.0.6, the language allows the definition of custom templates (see [this](../circom-language/templates-and-components.md#custom-templates) for more information). This `pragma` allows the circom programmer to easily tell if it's using custom templates: if any file declaring a custom template or including a file declaring any custom template doesn't use this `pragma`, the compiler will produce an error. Moreover, it will inform the programmer about which files should include this pragma.
18+
19+
To use it simply add the following instruction at the beginning (and after the version `pragma`) of the .circom files that needs it:
20+
21+
```text
22+
pragma custom_templates;
23+
```

mkdocs/docs/circom-language/reserved-keywords.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ The list of reserved keywords is the following:
2020
* **assert:** Check the condition at construction time.
2121
* **include:** Include code of the indicated file.
2222
* **pragma circom**: Instruction to check the compiler version.
23-
23+
* **pragma custom_templates**: Instruction to indicate the usage of custom templates.
2424

2525

mkdocs/docs/circom-language/templates-and-components.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,3 +213,21 @@ template parallel NameTemplate(...){...}
213213

214214
If this tag is used, the resulting C++ file will contain the parallelized code to compute the witness. Parallelization becomes particularly relevant when dealing with large circuits.
215215

216+
## Custom templates
217+
218+
Since version 2.0.6, the language allows the definition of a new type of templates, custom templates. This new construction works similarly to standard templates: they are declared analogously, just adding the keyword `custom` in its declaration after `template`; and are instantiated in the exact same way. That is, a custom template `Example` is defined and then instantiated as follows:
219+
220+
```text
221+
pragma circom 2.0.6; // note that custom templates are only allowed since version 2.0.6
222+
pragma custom_templates;
223+
224+
template custom Example() {
225+
// custom template's code
226+
}
227+
228+
template UsingExample() {
229+
component example = Example(); // instantiation of the custom template
230+
}
231+
```
232+
233+
However, the way in which their computation is encoded is different from the one for standard templates. Instead of producing r1cs constraints, the usage of each defined custom template will be treated in a later stage by [snarkjs](../index.md#snarkjs) to generate and validate the zk proof, in this case using the PLONK scheme (and using the custom template's definitions as PLONK's custom gates, see [here](../circom-language/custom-templates-snarkjs.md) how). Information about the definition and usages of custom templates will be exported in the `.r1cs` file (see [here](https://github.com/iden3/r1csfile/blob/master/doc/r1cs_bin_format.md) sections 4 and 5). This means that custom templates cannot introduce any constraint inside their body, nor declare any subcomponent.

0 commit comments

Comments
 (0)