-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
43 lines (34 loc) · 1.34 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
const snarkjs = require("snarkjs");
const fs = require("fs");
const { execSync } = require("child_process");
async function run() {
// Compile the circuit
console.log("Compiling the circuit...");
execSync("circom circuit.circom --r1cs --wasm --sym --c");
// Generate the witness
console.log("Generating the witness...");
execSync("node circuit_js/generate_witness.js circuit_js/circuit.wasm input.json witness.wtns");
// Setup the trusted setup
console.log("Running setup...");
const { zkey: { data: zkeyData } } = await snarkjs.powersOfTau.newZKey(
"circuit.r1cs",
"pot12_final.ptau"
);
// Export verification key
console.log("Exporting verification key...");
const vKey = await snarkjs.zKey.exportVerificationKey(zkeyData);
fs.writeFileSync("verification_key.json", JSON.stringify(vKey));
// Generate proof
console.log("Generating proof...");
const { proof, publicSignals } = await snarkjs.groth16.fullProve(
fs.readFileSync("witness.wtns"),
zkeyData
);
console.log("Proof: ", proof);
console.log("Public Signals: ", publicSignals);
// Verify proof
console.log("Verifying proof...");
const isValid = await snarkjs.groth16.verify(vKey, publicSignals, proof);
console.log("Proof is valid:", isValid);
}
run().catch(console.error);