snarkjs/README.md
README.mdRaw
1# snarkjs: JavaScript implementation of zkSNARKs.
2
3This is a JavaScript implementation of zkSNARK schemes.
4
5This library allows to do the trusted setup, generate proofs and verify the proofs.
6
7This library uses the compiled circuits generated by the jaz compiler.
8
9## Install.
10
11```sh
12npm install snarkjs
13```
14
15## Usage from command line.
16
17
18```sh
19snarkjs --help
20```
21
22Will show all the info in how to use the cli.
23
24## Ussage from jacascript
25
26
27### Import.
28
29```js
30const zkSnark = require("snarkjs");
31```
32
33### Load a circuit.
34
35```js
36// "myCircuit.cir" is the output of the jaz compiler
37
38const circuitDef = JSON.parse(fs.readFileSync("myCircuit.cir", "utf8"));
39const circuit = new zkSnark.Circuit(circuitDef);
40```
41
42### Inspect the circuit.
43
44```js
45 // `signalId` can always be a number or an alias string
46
47 circuit.nConstraints; // number of constraints
48 circuit.nSignals; // number of signals
49 circuit.nPublic; // number of public signals (nOutputs + nPublicInputs)
50
51 // The array of signals is always sorted in this order:
52 // [ 1, outputs, publicInputs, privateInputs, internalSignals, constants]
53
54 // returns a,b and c coeficients of the `signalId` on a given `constraint`
55 circuit.a(constraint, signalId)
56 circuit.b(constraint, signalId)
57 circuit.c(constraint, signalId)
58
59 circuit.nOutputs // number of public outputs
60 circuit.pubInputs // number of public inputs
61 circuit.nPrvInputs // number of private inputs
62 circuit.nInputs // number of inputs ( nPublicInputs + nPrivateInputs)
63 circuit.nVars // number of variables ( not including constants (one is a variable) )
64 circuit.nSignals // number of signals ( including constants )
65
66 circuit.outputIdx(i) // returns the index of the i'th output
67 circuit.inputIdx(i) // returns the index of the i'th input
68 circuit.pubInputIdx(i) // returns the index of the i'th public input
69 circuit.prvInputIdx(i) // returns the index of the i'th private input
70 circuit.varIdx(i) // returns the index of the i'th variable
71 circuit.constantIdx(i) // returns the index of the i'th constant
72 circuit.signalIdx(i) // returns the index of the i'th signal
73
74 // returns signal Idx given a signalId
75 // if the idx >= n , it is a constant
76 // if the idx == -1, the signal does not exist
77 circuit.getSignalIdx(name);
78
79 // returns an array aliases names of the i'th signal
80 circuit.signalNames(i)
81
82 // input is a key value object where keys are the signal names
83 // of all the inputs (public and private)
84 // returns an array of values representing the witness
85 circuit.calculateWitness(input)
86```
87
88### Trusted setup.
89
90```js
91const setup = zkSnark.setup(circuit);
92fs.writeFileSync("myCircuit.vk_proof", JSON.stringify(setup.vk_proof), "utf8");
93fs.writeFileSync("myCircuit.vk_verifier", JSON.stringify(setup.vk_verifier), "utf8");
94setup.toxic // Must be discarded.
95```
96
97### Generate proof.
98
99```js
100const circuitDef = JSON.parse(fs.readFileSync("myCircuit.cir", "utf8"));
101const circuit = new zkSnark.Circuit(circuitDef);
102const input = {
103 "main.pubIn1": "123",
104 "main.out1": "456"
105}
106const witness = circuit.calculateWitness(input);
107const vk_proof = JSON.parse(fs.readFileSync("myCircuit.vk_proof", "utf8"));
108
109const {proof, publicSignals} = zkSnark.genProof(vk_proof, witness);
110```
111
112### Verifier.
113
114```js
115const vk_verifier = JSON.parse(fs.readFileSync("myCircuit.vk_verifier", "utf8"));
116
117if (zkSnark.isValid(vk_verifier, proof, publicSignals)) {
118 console.log("The proof is valid");
119} else {
120 console.log("The proof is not valid");
121}
122```
123
124## License
125
126snarkjs is part of the iden3 project copyright 2018 0KIMS association and published with GPL-3 license. Please check the COPYING file for more details.
127
128