snarkjs/test/zksnark.js
test/zksnark.jsRaw
1/*
2 Copyright 2018 0kims association
3
4 This file is part of zksnark javascript library.
5
6 zksnark javascript library is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 zksnark javascript library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with zksnark javascript library. If not, see <https://www.gnu.org/licenses/>.
18*/
19const chai = require("chai");
20const fs = require("fs");
21const path = require("path");
22const bigInt = require("../src/bigint.js");
23
24const Circuit = require("../src/circuit.js");
25const zkSnark = require("../index.js");
26
27const assert = chai.assert;
28
29
30function stringifyBigInts(o) {
31 if ((typeof(o) == "bigint") || (o instanceof bigInt)) {
32 return o.toString(10);
33 } else if (Array.isArray(o)) {
34 return o.map(stringifyBigInts);
35 } else if (typeof o == "object") {
36 const res = {};
37 for (let k in o) {
38 res[k] = stringifyBigInts(o[k]);
39 }
40 return res;
41 } else {
42 return o;
43 }
44}
45
46function unstringifyBigInts(o) {
47 if ((typeof(o) == "string") && (/^[0-9]+$/.test(o) )) {
48 return bigInt(o);
49 } else if (Array.isArray(o)) {
50 return o.map(unstringifyBigInts);
51 } else if (typeof o == "object") {
52 const res = {};
53 for (let k in o) {
54 res[k] = unstringifyBigInts(o[k]);
55 }
56 return res;
57 } else {
58 return o;
59 }
60}
61
62describe("zkSnark", () => {
63 it("Load a circuit, create trusted setup, create a proof and validate", () => {
64
65 const cirDef = JSON.parse(fs.readFileSync(path.join(__dirname, "circuit", "sum_old.json"), "utf8"));
66 const cir = new Circuit(cirDef);
67
68 console.log("Signals: " + cir.nSignals);
69 console.log("Vars: " + cir.nVars);
70 console.log("Constants: " + cir.nConstants);
71
72 const setup = zkSnark.setup(cir);
73 const strSetup = stringifyBigInts(setup);
74 fs.writeFileSync("vk_proof.json", JSON.stringify(strSetup.vk_proof), "utf-8");
75 fs.writeFileSync("vk_verifier.json", JSON.stringify(strSetup.vk_verifier), "utf-8");
76
77/*
78 const setup = {};
79 setup.vk_proof = unstringifyBigInts(JSON.parse(fs.readFileSync("vk_proof.json", "utf8")));
80 setup.vk_verifier = unstringifyBigInts(JSON.parse(fs.readFileSync("vk_verifier.json", "utf8")));
81*/
82 const witness = cir.calculateWitness({"a": "33", "b": "34"});
83
84 assert.equal(witness[cir.getSignalIdx("main.out")].toString(), "67");
85
86 const {proof, publicSignals} = zkSnark.genProof(setup.vk_proof, witness);
87
88 assert( zkSnark.isValid(setup.vk_verifier, proof, publicSignals));
89 }).timeout(10000000);
90});