This is a JavaScript implementation of zkSNARK schemes. It allows the original 8points protocol and the Groth Protocol (3 point only and 3 pairings)
This library allows to do the trusted setup, generate proofs and verify the proofs.
This library uses the compiled circuits generated by the jaz compiler.
A good starting point is this tutorial
Also this video is a good starting point.
npm install snarkjssnarkjs --helpWill show all the info in how to use the cli.
constzkSnark=require("snarkjs");// "myCircuit.cir" is the output of the jaz compilerconstcircuitDef=JSON.parse(fs.readFileSync("myCircuit.cir","utf8"));constcircuit=newzkSnark.Circuit(circuitDef);// `signalId` can always be a number or an alias stringcircuit.nConstraints;// number of constraintscircuit.nSignals;// number of signalscircuit.nPublic;// number of public signals (nOutputs + nPublicInputs)// The array of signals is always sorted in this order:// [ 1, outputs, publicInputs, privateInputs, internalSignals, constants]// returns a,b and c coeficients of the `signalId` on a given `constraint`circuit.a(constraint,signalId)circuit.b(constraint,signalId)circuit.c(constraint,signalId)circuit.nOutputs// number of public outputscircuit.pubInputs// number of public inputscircuit.nPrvInputs// number of private inputscircuit.nInputs// number of inputs ( nPublicInputs + nPrivateInputs)circuit.nVars// number of variables ( not including constants (one is a variable) )circuit.nSignals// number of signals ( including constants )circuit.outputIdx(i)// returns the index of the i'th outputcircuit.inputIdx(i)// returns the index of the i'th inputcircuit.pubInputIdx(i)// returns the index of the i'th public inputcircuit.prvInputIdx(i)// returns the index of the i'th private inputcircuit.varIdx(i)// returns the index of the i'th variablecircuit.constantIdx(i)// returns the index of the i'th constantcircuit.signalIdx(i)// returns the index of the i'th signal// returns signal Idx given a signalId// if the idx >= n , it is a constant// if the idx == -1, the signal does not existcircuit.getSignalIdx(name);// returns an array aliases names of the i'th signalcircuit.signalNames(i)// input is a key value object where keys are the signal names// of all the inputs (public and private)// returns an array of values representing the witnesscircuit.calculateWitness(input)constsetup=zkSnark.setup(circuit);fs.writeFileSync("myCircuit.vk_proof",JSON.stringify(setup.vk_proof),"utf8");fs.writeFileSync("myCircuit.vk_verifier",JSON.stringify(setup.vk_verifier),"utf8");setup.toxic// Must be discarded.constcircuitDef=JSON.parse(fs.readFileSync("myCircuit.cir","utf8"));constcircuit=newzkSnark.Circuit(circuitDef);constinput={"main.pubIn1": "123","main.out1": "456"}constwitness=circuit.calculateWitness(input);constvk_proof=JSON.parse(fs.readFileSync("myCircuit.vk_proof","utf8"));const{proof, publicSignals}=zkSnark.genProof(vk_proof,witness);constvk_verifier=JSON.parse(fs.readFileSync("myCircuit.vk_verifier","utf8"));if(zkSnark.isValid(vk_verifier,proof,publicSignals)){console.log("The proof is valid");}else{console.log("The proof is not valid");}snarkjs is part of the iden3 project copyright 2018 0KIMS association and published with GPL-3 license. Please check the COPYING file for more details.