arnaucube
arnaucube/snarkjsPublic Self-hosted repository
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 bigInt = require("../src/bigint.js");
22
23const Circuit = require("../src/circuit.js");
24const zkSnark = require("../index.js");
25
26const assert = chai.assert;
27
28
29function stringifyBigInts(o) {
30    if ((typeof(o) == "bigint") || (o instanceof bigInt))  {
31        return o.toString(10);
32    } else if (Array.isArray(o)) {
33        return o.map(stringifyBigInts);
34    } else if (typeof o == "object") {
35        const res = {};
36        for (let k in o) {
37            res[k] = stringifyBigInts(o[k]);
38        }
39        return res;
40    } else {
41        return o;
42    }
43}
44
45function unstringifyBigInts(o) {
46    if ((typeof(o) == "string") && (/^[0-9]+$/.test(o) ))  {
47        return bigInt(o);
48    } else if (Array.isArray(o)) {
49        return o.map(unstringifyBigInts);
50    } else if (typeof o == "object") {
51        const res = {};
52        for (let k in o) {
53            res[k] = unstringifyBigInts(o[k]);
54        }
55        return res;
56    } else {
57        return o;
58    }
59}
60
61describe("zkSnark", () => {
62    it("Load a circuit, create trusted setup, create a proof and validate", () => {
63
64        const cirDef = JSON.parse(fs.readFileSync("../jaz/sum.json", "utf8"));
65        const cir = new Circuit(cirDef);
66
67        const setup = zkSnark.setup(cir);
68        const strSetup = stringifyBigInts(setup);
69        fs.writeFileSync("vk_proof.json", JSON.stringify(strSetup.vk_proof), "utf-8");
70        fs.writeFileSync("vk_verifier.json", JSON.stringify(strSetup.vk_verifier), "utf-8");
71
72/*
73        const setup = {};
74        setup.vk_proof = unstringifyBigInts(JSON.parse(fs.readFileSync("vk_proof.json", "utf8")));
75        setup.vk_verifier = unstringifyBigInts(JSON.parse(fs.readFileSync("vk_verifier.json", "utf8")));
76*/
77        const witness = cir.calculateWitness({"a": "33", "b": "34"});
78
79        assert.equal(witness[cir.getSignalIdx("main.out")].toString(), "67");
80
81        const {proof, publicSignals} = zkSnark.genProof(setup.vk_proof, witness);
82
83        assert( zkSnark.isValid(setup.vk_verifier, proof, publicSignals));
84    }).timeout(10000000);
85});