snarkjs/README.md
README.mdRaw
1# 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 zksnark
13```
14
15## Usage.
16
17### Import.
18
19```js
20const zkSnark = require("zksnark");
21```
22
23### Load a circuit.
24
25```js
26// "myCircuit.cir" is the output of the jaz compiler
27
28const circuitDef = JSON.parse(fs.readFileSync("myCircuit.cir", "utf8"));
29const circuit = new zkSnark.Circuit(circuitDef);
30```
31
32### Inspect the circuit.
33
34```js
35 // `signalId` can always be a number or an alias string
36
37 circuit.nConstraints; // number of constraints
38 circuit.nSignals; // number of signals
39 circuit.nPublic; // number of public signals (nOutputs + nPublicInputs)
40
41 // The array of signals is always sorted in this order:
42 // [ 1, outputs, publicInputs, privateInputs, internalSignals, constants]
43
44 // returns a,b and c coeficients of the `signalId` on a given `constraint`
45 circuit.a(constraint, signalId)
46 circuit.b(constraint, signalId)
47 circuit.c(constraint, signalId)
48
49 circuit.nOutputs // number of public outputs
50 circuit.pubInputs // number of public inputs
51 circuit.nPrvInputs // number of private inputs
52 circuit.nInputs // number of inputs ( nPublicInputs + nPrivateInputs)
53 circuit.nVars // number of variables ( not including constants (one is a variable) )
54 circuit.nSignals // number of signals ( including constants )
55
56 circuit.outputIdx(i) // returns the index of the i'th output
57 circuit.inputIdx(i) // returns the index of the i'th input
58 circuit.pubInputIdx(i) // returns the index of the i'th public input
59 circuit.prvInputIdx(i) // returns the index of the i'th private input
60 circuit.varIdx(i) // returns the index of the i'th variable
61 circuit.constantIdx(i) // returns the index of the i'th constant
62 circuit.signalIdx(i) // returns the index of the i'th signal
63
64 // returns signal Idx given a signalId
65 // if the idx >= n , it is a constant
66 // if the idx == -1, the signal does not exist
67 circuit.getSignalIdx(name);
68
69 // returns an array aliases names of the i'th signal
70 circuit.signalNames(i)
71
72 // input is a key value object where keys are the signal names
73 // of all the inputs (public and private)
74 // returns an array of values representing the witness
75 circuit.calculateWitness(input)
76```
77
78### Trusted setup.
79
80```js
81const setup = zkSnark.setup(circuit);
82fs.writeFileSync("myCircuit.vk_proof", JSON.stringify(setup.vk_proof), "utf8");
83fs.writeFileSync("myCircuit.vk_verifier", JSON.stringify(setup.vk_verifier), "utf8");
84setup.toxic // Must be discarded.
85```
86
87### Generate proof.
88
89```js
90const circuitDef = JSON.parse(fs.readFileSync("myCircuit.cir", "utf8"));
91const circuit = new zkSnark.Circuit(circuitDef);
92const input = {
93 "main.pubIn1": "123",
94 "main.out1": "456"
95}
96const witness = circuit.calculateWitness(input);
97const vk_proof = JSON.parse(fs.readFileSync("myCircuit.vk_proof", "utf8"));
98
99const {proof, publicSignals} = zkSnark.genProof(vk_proof, witness);
100```
101
102### Verifier.
103
104```js
105const vk_verifier = JSON.parse(fs.readFileSync("myCircuit.vk_verifier", "utf8"));
106
107if (zkSnark.isValid(vk_verifier, proof, publicSignals)) {
108 console.log("The proof is valid");
109} else {
110 console.log("The proof is not valid");
111}
112```