You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

263 lines
8.4 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
4 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. # circom and snarkjs tutorial
  2. This tutorial will guide you in creating your first zero-knowledge SNARK circuit. It will take you through the various techniques to write circuits and show you how to create and verify proofs off-chain and on-chain on Ethereum.
  3. ## 1. Installing the tools
  4. ### 1.1 Pre-requisites
  5. If you don't have it installed yet, you need to install `Node.js`.
  6. The last stable version of `Node.js` (or 8.12.0) works just fine, but if you install the latest current version `Node.js` (10.12.0) you will see a significant increase in performance. This is because last versions of node includes Big Integer Libraries nativelly. The `snarkjs` library makes use of this feature if available, and this improves the performance x10 (!).
  7. ### 1.2 Install **circom** and **snarkjs**
  8. Run:
  9. ```sh
  10. npm install -g circom
  11. npm install -g circom_runtime
  12. npm install -g snarkjs
  13. ```
  14. ## 2. Working with a circuit
  15. Let's create a circuit that will allow you to prove that you are able to factor a number!
  16. ### 2.1 Create a circuit in a new directory
  17. 1. Create an empty directory called `factor` where you will put all the files that you will use in this tutorial.
  18. ```
  19. mkdir factor
  20. cd factor
  21. ```
  22. > In a real circuit, you will probably want to create a `git` repository with a `circuits` directory and a `test` directory with all your tests, and the needed scripts to build all the circuits.
  23. 2. Create a new file named `circuit.circom` with the following content:
  24. ```
  25. template Multiplier() {
  26. signal private input a;
  27. signal private input b;
  28. signal output c;
  29. c <== a*b;
  30. }
  31. component main = Multiplier();
  32. ```
  33. This circuit has 2 private input signals named `a` and `b` and one output named `c`.
  34. The only thing that the circuit does is forcing the signal `c` to be the value of `a*b`
  35. After declaring the `Multiplier` template, we instantiate it with a component named`main`.
  36. Note: When compiling a circuit, a component named `main` must always exist.
  37. ### 2.2 Compile the circuit
  38. We are now ready to compile the circuit. Run the following command:
  39. ```sh
  40. circom circuit.circom --r1cs --wasm --sym
  41. ```
  42. The `--r1cs` option will generate `circuit.r1cs` (the r1cs constraint system of the circuit in binary format).
  43. The `--wasm` option will generate `circuit.wasm` (the wasm code to generate the witness).
  44. The `--sym` option will generate `circuit.sym` (a symbols file required for debugging or if you want to print the constraint system in an annotated mode).
  45. ## 3. Taking the compiled circuit to *snarkjs*
  46. Now that the circuit is compiled, we will continue with `snarkjs`.
  47. Please note that you can always access the help of `snarkjs` by typing:
  48. ```sh
  49. snarkjs --help
  50. ```
  51. ### 3.1 View information and stats regarding a circuit
  52. To show general statistics of this circuit, you can run:
  53. ```sh
  54. snarkjs info -r circuit.r1cs
  55. ```
  56. You can also print the constraints of the circuit by running:
  57. ```sh
  58. snarkjs printconstraints -r circuit.r1cs -s circuit.sym
  59. ```
  60. ### 3.2 Setting up using *snarkjs*
  61. Ok, let's run a setup for our circuit:
  62. ```sh
  63. snarkjs setup
  64. ```
  65. > By default `snarkjs` will look for and use `circuit.r1cs`. You can always specify a different circuit file by adding `-r <circuit R1CS file name>`.
  66. The output of the setup will be in the form of 2 files: `proving_key.json` and `verification_key.json`.
  67. ### 3.3. Calculating a witness
  68. Before creating any proof, we need to calculate all the signals of the circuit that match (all) the constraints of the circuit.
  69. `circom` generates a wasm module that calculates those for you. You need to provide a file with the inputs and it will execute the circuit and calculate all the intermediate signals and the output. This set of signals is the *witness*.
  70. The zero-knowledge proofs prove that you know a set of signals (witness) that match all the constraints without revealing any of the signals except the public inputs and the outputs.
  71. For example, imagine you want to prove you are able to factor the number 33. It means that you know two numbers `a` and `b` that when you multiply them, it results in 33.
  72. > Of course you can always use the number one and the same number as `a` or `b`. We will deal with this problem later.
  73. So you want to prove that you know 3 and 11.
  74. Let's create a file named `input.json`
  75. ```json
  76. {"a": 3, "b": 11}
  77. ```
  78. Now let's calculate the witness:
  79. ```sh
  80. snarkjs calculatewitness --wasm circuit.wasm --input input.json --witness witness.json
  81. ```
  82. `calcwit` is part of the circom_runtime package and it's just a wrapper in JS to call the wasm module.
  83. You can use `circom_runtime` from your own project to calulate the witness.
  84. You may want to take a look at `witness.json` file with all the signals.
  85. ### Create the proof
  86. Now that we have the witness generated, we can create the proof.
  87. ```sh
  88. snarkjs proof
  89. ```
  90. This command will use the `proving_key.json` and the `witness.json` files by default to generate `proof.json` and `public.json`
  91. The `proof.json` file will contain the actual proof and the `public.json` file will contain just the values of the public inputs and the outputs.
  92. ### Verifying the proof
  93. To verify the proof run:
  94. ```sh
  95. snarkjs verify
  96. ```
  97. This command will use `verification_key.json`, `proof.json` and `public.json` to verify that is valid.
  98. Here we are verifying that we know a witness that the public inputs and the outputs matches the ones in the `public.json` file.
  99. If the proof is ok, you will see `OK` or `INVALID` if not ok.
  100. ### Generate the solidity verifier
  101. ```sh
  102. snarkjs generateverifier
  103. ```
  104. This command will take the `verification_key.json` and generate solidity code in `verifier.sol` file.
  105. You can take the code in `verifier.sol` and cut and paste it in remix.
  106. This code contains two contracts: Pairings and Verifier. You only need to deploy the `Verifier` contract.
  107. > You may want to use a test net like Rinkeby, Kovan or Ropsten. You can also use the Javascript VM, but in some browsers the verification takes long and it may hang the page.
  108. ### Verifying the proof on-chain
  109. The verifier contract deployed in the last step has a `view` function called `verifyProof`.
  110. This function will return true if the proof and the inputs are valid.
  111. To facilitate the call, you can use `snarkjs` to generate the parameters of the call by typing:
  112. ```sh
  113. snarkjs generatecall
  114. ```
  115. Just cut and paste the output to the parameters field of the `verifyProof` method in Remix.
  116. If every thing works ok, this method should return true.
  117. If you change any bit in the parameters, the result will be verifiably false.
  118. ## Bonus track
  119. We can fix the circuit to not accept the number 1 as any of the input values by adding some extra constraints.
  120. Here, the trick is that we use the property that 0 has no inverse. So `(a-1)` should not have an inverse.
  121. That means that `(a-1)*inv = 1` will be inpossible to match if `a` is 1.
  122. We just calculate inv by `1/(a-1)`.
  123. So, let's modify the circuit:
  124. ```
  125. template Multiplier() {
  126. signal private input a;
  127. signal private input b;
  128. signal output c;
  129. signal inva;
  130. signal invb;
  131. inva <-- 1/(a-1);
  132. (a-1)*inva === 1;
  133. invb <-- 1/(b-1);
  134. (b-1)*invb === 1;
  135. c <== a*b;
  136. }
  137. component main = Multiplier();
  138. ```
  139. A nice thing of the circom language is that you can split a `<==` into two independent actions: `<--` and `===`.
  140. The `<--` and `-->` operators assign a value to a signal without creating any constraints.
  141. The `===` operator adds a constraint without assigning any value to a signal.
  142. The circuit also has another problem: the operation works in `Z_r`, so we need to guarantee the multiplication does not overflow. This can be done by converting the inputs to binary and checking the ranges, but we will reserve it for future tutorials.
  143. ## Where to go from here
  144. You may want to read the [README](https://github.com/iden3/circom) to learn more features about `circom`.
  145. You can also check a library with many basic circuits lib binarizations, comparators, eddsa, hashes, merkle trees etc [here](https://github.com/iden3/circomlib) (Work in progress).
  146. Or a exponentiation in the Baby Jubjub curve [here](https://github.com/iden3/circomlib) (Work in progress).
  147. # Final note
  148. There is nothing worse for a dev than working with a buggy compiler. This is a very early stage of the compiler, so there are many bugs and lots of work needs to be done. Please have it present if you are doing anything serious with it.
  149. And please contact us for any isue you have. In general, a github issue with a small piece of code with the bug is very useful to us.
  150. Enjoy zero-knowledge proving!