snarkjs: JavaScript implementation of zkSNARKs.
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.
Install.
npm install snarkjs
Usage from command line.
snarkjs --help
Will show all the info in how to use the cli.
Usage from javascript
Import.
const zkSnark = require("snarkjs");
Load a circuit.
const circuitDef = JSON.parse(fs.readFileSync("myCircuit.cir", "utf8"));
const circuit = new zkSnark.Circuit(circuitDef);
Inspect the circuit.
circuit.nConstraints;
circuit.nSignals;
circuit.nPublic;
circuit.a(constraint, signalId)
circuit.b(constraint, signalId)
circuit.c(constraint, signalId)
circuit.nOutputs
circuit.pubInputs
circuit.nPrvInputs
circuit.nInputs
circuit.nVars
circuit.nSignals
circuit.outputIdx(i)
circuit.inputIdx(i)
circuit.pubInputIdx(i)
circuit.prvInputIdx(i)
circuit.varIdx(i)
circuit.constantIdx(i)
circuit.signalIdx(i)
circuit.getSignalIdx(name);
circuit.signalNames(i)
circuit.calculateWitness(input)
Trusted setup.
const setup = 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
Generate proof.
const circuitDef = JSON.parse(fs.readFileSync("myCircuit.cir", "utf8"));
const circuit = new zkSnark.Circuit(circuitDef);
const input = {
"main.pubIn1": "123",
"main.out1": "456"
}
const witness = circuit.calculateWitness(input);
const vk_proof = JSON.parse(fs.readFileSync("myCircuit.vk_proof", "utf8"));
const {proof, publicSignals} = zkSnark.genProof(vk_proof, witness);
Verifier.
const vk_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");
}
License
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.