mirror of
https://github.com/arnaucube/snarkjs.git
synced 2026-02-27 21:46:47 +01:00
zkSnarks working
This commit is contained in:
@@ -31,7 +31,7 @@ describe("Curve G1 Test", () => {
|
||||
it("r*one == 0", () => {
|
||||
const bn128 = new BN128();
|
||||
|
||||
const res = bn128.G1.mulEscalar(bn128.G1.g, bn128.r);
|
||||
const res = bn128.G1.mulScalar(bn128.G1.g, bn128.r);
|
||||
|
||||
assert(bn128.G1.equals(res, bn128.G1.zero), "G1 does not have range r");
|
||||
});
|
||||
@@ -43,12 +43,12 @@ describe("Curve G1 Test", () => {
|
||||
const r1 = bigInt(33);
|
||||
const r2 = bigInt(44);
|
||||
|
||||
const gr1 = bn128.G1.mulEscalar(bn128.G1.g, r1);
|
||||
const gr2 = bn128.G1.mulEscalar(bn128.G1.g, r2);
|
||||
const gr1 = bn128.G1.mulScalar(bn128.G1.g, r1);
|
||||
const gr2 = bn128.G1.mulScalar(bn128.G1.g, r2);
|
||||
|
||||
const grsum1 = bn128.G1.add(gr1, gr2);
|
||||
|
||||
const grsum2 = bn128.G1.mulEscalar(bn128.G1.g, r1.add(r2));
|
||||
const grsum2 = bn128.G1.mulScalar(bn128.G1.g, r1.add(r2));
|
||||
|
||||
assert(bn128.G1.equals(grsum1, grsum2));
|
||||
});
|
||||
@@ -58,7 +58,7 @@ describe("Curve G2 Test", () => {
|
||||
it ("r*one == 0", () => {
|
||||
const bn128 = new BN128();
|
||||
|
||||
const res = bn128.G2.mulEscalar(bn128.G2.g, bn128.r);
|
||||
const res = bn128.G2.mulScalar(bn128.G2.g, bn128.r);
|
||||
|
||||
assert(bn128.G2.equals(res, bn128.G2.zero), "G2 does not have range r");
|
||||
});
|
||||
@@ -69,12 +69,12 @@ describe("Curve G2 Test", () => {
|
||||
const r1 = bigInt(33);
|
||||
const r2 = bigInt(44);
|
||||
|
||||
const gr1 = bn128.G2.mulEscalar(bn128.G2.g, r1);
|
||||
const gr2 = bn128.G2.mulEscalar(bn128.G2.g, r2);
|
||||
const gr1 = bn128.G2.mulScalar(bn128.G2.g, r1);
|
||||
const gr2 = bn128.G2.mulScalar(bn128.G2.g, r2);
|
||||
|
||||
const grsum1 = bn128.G2.add(gr1, gr2);
|
||||
|
||||
const grsum2 = bn128.G2.mulEscalar(bn128.G2.g, r1.add(r2));
|
||||
const grsum2 = bn128.G2.mulScalar(bn128.G2.g, r1.add(r2));
|
||||
|
||||
/*
|
||||
console.log(G2.toString(grsum1));
|
||||
@@ -148,11 +148,11 @@ describe("Pairing", () => {
|
||||
for (let i=0; i<1; i++) {
|
||||
const bn128 = new BN128();
|
||||
|
||||
const g1a = bn128.G1.mulEscalar(bn128.G1.g, 25);
|
||||
const g2a = bn128.G2.mulEscalar(bn128.G2.g, 30);
|
||||
const g1a = bn128.G1.mulScalar(bn128.G1.g, 25);
|
||||
const g2a = bn128.G2.mulScalar(bn128.G2.g, 30);
|
||||
|
||||
const g1b = bn128.G1.mulEscalar(bn128.G1.g, 30);
|
||||
const g2b = bn128.G2.mulEscalar(bn128.G2.g, 25);
|
||||
const g1b = bn128.G1.mulScalar(bn128.G1.g, 30);
|
||||
const g2b = bn128.G2.mulScalar(bn128.G2.g, 25);
|
||||
|
||||
const pre1a = bn128.precomputeG1(g1a);
|
||||
const pre2a = bn128.precomputeG2(g2a);
|
||||
|
||||
20
test/calculatewitness.js
Normal file
20
test/calculatewitness.js
Normal file
@@ -0,0 +1,20 @@
|
||||
const chai = require("chai");
|
||||
const fs = require("fs");
|
||||
|
||||
const Circuit = require("../src/circuit.js");
|
||||
const BN128 = require("../src/BN128.js");
|
||||
const F1Field = require("../src/zqfield.js");
|
||||
|
||||
const assert = chai.assert;
|
||||
|
||||
|
||||
describe("Calculate witness", () => {
|
||||
it("Should calculate the witness of a sum circuit", () => {
|
||||
|
||||
const cirDef = JSON.parse(fs.readFileSync("../jaz/sum.json", "utf8"));
|
||||
const cir = new Circuit(cirDef);
|
||||
const witness = cir.calculateWitness({"a": "33", "b": "34"});
|
||||
|
||||
assert.equal(witness[cir.getSignalIdx("main.out")].toString(), "67");
|
||||
});
|
||||
});
|
||||
60
test/pols.js
60
test/pols.js
@@ -2,6 +2,7 @@ const chai = require("chai");
|
||||
|
||||
const bigInt = require("../src/bigint.js");
|
||||
const PolField = require("../src/polfield.js");
|
||||
const ZqField = require("../src/zqfield");
|
||||
|
||||
const assert = chai.assert;
|
||||
|
||||
@@ -9,7 +10,7 @@ const r = bigInt("2188824287183927522224640574525727508854836440041603434369820
|
||||
|
||||
describe("Polinomial field", () => {
|
||||
it("Should compute a multiplication", () => {
|
||||
const PF = new PolField(r);
|
||||
const PF = new PolField(new ZqField(r));
|
||||
|
||||
const a = [bigInt(1), bigInt(2), bigInt(3)];
|
||||
const b = [bigInt(1), bigInt(2), bigInt(3)];
|
||||
@@ -18,7 +19,7 @@ describe("Polinomial field", () => {
|
||||
assert(PF.equals(res, [bigInt(1), bigInt(4), bigInt(10), bigInt(12), bigInt(9)]));
|
||||
});
|
||||
it("Should compute a multiplication 2", () => {
|
||||
const PF = new PolField(r);
|
||||
const PF = new PolField(new ZqField(r));
|
||||
|
||||
const a = [bigInt(5), bigInt(1)];
|
||||
const b = [bigInt(-5), bigInt(1)];
|
||||
@@ -27,7 +28,7 @@ describe("Polinomial field", () => {
|
||||
assert(PF.equals(res, [bigInt(-25), bigInt(0), bigInt(1)]));
|
||||
});
|
||||
it("Should compute an addition", () => {
|
||||
const PF = new PolField(r);
|
||||
const PF = new PolField(new ZqField(r));
|
||||
|
||||
const a = [bigInt(5), bigInt(1)];
|
||||
const b = [bigInt(-5), bigInt(1)];
|
||||
@@ -36,7 +37,7 @@ describe("Polinomial field", () => {
|
||||
assert(PF.equals(res, [bigInt(0), bigInt(2)]));
|
||||
});
|
||||
it("Should compute a substraction", () => {
|
||||
const PF = new PolField(r);
|
||||
const PF = new PolField(new ZqField(r));
|
||||
|
||||
const a = [bigInt(5), bigInt(3), bigInt(4)];
|
||||
const b = [bigInt(5), bigInt(1)];
|
||||
@@ -45,7 +46,7 @@ describe("Polinomial field", () => {
|
||||
assert(PF.equals(res, [bigInt(0), bigInt(2), bigInt(4)]));
|
||||
});
|
||||
it("Should compute reciprocal", () => {
|
||||
const PF = new PolField(r);
|
||||
const PF = new PolField(new ZqField(r));
|
||||
|
||||
const a = [bigInt(4), bigInt(1), bigInt(-3), bigInt(-1), bigInt(2),bigInt(1), bigInt(-1), bigInt(1)];
|
||||
const res = PF._reciprocal(a, 3, 0);
|
||||
@@ -53,7 +54,7 @@ describe("Polinomial field", () => {
|
||||
assert(PF.equals(res, [bigInt(12), bigInt(15), bigInt(3), bigInt(-4), bigInt(-3), bigInt(0), bigInt(1), bigInt(1)]));
|
||||
});
|
||||
it("Should div2", () => {
|
||||
const PF = new PolField(r);
|
||||
const PF = new PolField(new ZqField(r));
|
||||
|
||||
// x^6
|
||||
const a = [bigInt(0), bigInt(0), bigInt(0), bigInt(0), bigInt(0),bigInt(0), bigInt(1)];
|
||||
@@ -67,7 +68,7 @@ describe("Polinomial field", () => {
|
||||
assert(PF.equals(res2, [bigInt(0), bigInt(1)]));
|
||||
});
|
||||
it("Should div", () => {
|
||||
const PF = new PolField(r);
|
||||
const PF = new PolField(new ZqField(r));
|
||||
|
||||
const a = [bigInt(1), bigInt(2), bigInt(3), bigInt(4), bigInt(5),bigInt(6), bigInt(7)];
|
||||
const b = [bigInt(8), bigInt(9), bigInt(10), bigInt(11), bigInt(12), bigInt(13)];
|
||||
@@ -79,7 +80,7 @@ describe("Polinomial field", () => {
|
||||
});
|
||||
|
||||
it("Should div big/small", () => {
|
||||
const PF = new PolField(r);
|
||||
const PF = new PolField(new ZqField(r));
|
||||
|
||||
const a = [bigInt(1), bigInt(2), bigInt(3), bigInt(4), bigInt(5),bigInt(6), bigInt(7)];
|
||||
const b = [bigInt(8), bigInt(9)];
|
||||
@@ -90,18 +91,55 @@ describe("Polinomial field", () => {
|
||||
assert(PF.equals(a, d));
|
||||
});
|
||||
it("Should div random big", () => {
|
||||
const PF = new PolField(r);
|
||||
const PF = new PolField(new ZqField(r));
|
||||
|
||||
const a = [];
|
||||
const b = [];
|
||||
for (let i=0; i<1000; i++) a.push(bigInt(Math.floor(Math.random()*100000) -500000));
|
||||
for (let i=0; i<300; i++) b.push(bigInt(Math.floor(Math.random()*100000) -500000));
|
||||
for (let i=0; i<900; i++) b.push(bigInt(Math.floor(Math.random()*100000) -500000));
|
||||
|
||||
const c = PF.mul(a,b);
|
||||
|
||||
const d = PF.div(c,b);
|
||||
|
||||
assert(PF.equals(a, d));
|
||||
}).timeout(10000000);
|
||||
}).timeout(10000);
|
||||
it("Should evaluate and zero", () => {
|
||||
const PF = new PolField(new ZqField(r));
|
||||
const p = [PF.F.neg(bigInt(2)), bigInt(1)];
|
||||
const v = PF.eval(p, bigInt(2));
|
||||
assert(PF.F.equals(v, bigInt(0)));
|
||||
});
|
||||
it("Should create lagrange polynomial minmal", () => {
|
||||
const PF = new PolField(new ZqField(r));
|
||||
|
||||
const points=[];
|
||||
points.push([bigInt(1), bigInt(1)]);
|
||||
points.push([bigInt(2), bigInt(2)]);
|
||||
points.push([bigInt(3), bigInt(5)]);
|
||||
|
||||
const p=PF.lagrange(points);
|
||||
|
||||
for (let i=0; i<points.length; i++) {
|
||||
const v = PF.eval(p, points[i][0]);
|
||||
assert(PF.F.equals(v, points[i][1]));
|
||||
}
|
||||
});
|
||||
it("Should create lagrange polynomial", () => {
|
||||
const PF = new PolField(new ZqField(r));
|
||||
|
||||
const points=[];
|
||||
points.push([bigInt(1), bigInt(2)]);
|
||||
points.push([bigInt(2), bigInt(-2)]);
|
||||
points.push([bigInt(3), bigInt(0)]);
|
||||
points.push([bigInt(4), bigInt(453345)]);
|
||||
|
||||
const p=PF.lagrange(points);
|
||||
|
||||
for (let i=0; i<points.length; i++) {
|
||||
const v = PF.eval(p, points[i][0]);
|
||||
assert(PF.F.equals(v, points[i][1]));
|
||||
}
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
70
test/ratzqfield.js
Normal file
70
test/ratzqfield.js
Normal file
@@ -0,0 +1,70 @@
|
||||
const chai = require("chai");
|
||||
|
||||
const bigInt = require("../src/bigint.js");
|
||||
const ZqField = require("../src/zqfield.js");
|
||||
const RatZqField = require("../src/ratzqfield.js");
|
||||
|
||||
const q = bigInt("21888242871839275222246405745257275088548364400416034343698204186575808495617");
|
||||
const Z = new ZqField(q);
|
||||
const R = new RatZqField(Z);
|
||||
|
||||
const assert = chai.assert;
|
||||
|
||||
function r(a,b) {
|
||||
return [bigInt(a), bigInt(b)];
|
||||
}
|
||||
|
||||
|
||||
describe("Rational zq Field", () => {
|
||||
it("Should compare correctly", () => {
|
||||
assert( R.equals(r(3,5), r(6,10)));
|
||||
assert(!R.equals(r(3,5), r(6,11)));
|
||||
});
|
||||
it("Should add correctly", () => {
|
||||
const a = r(7,4);
|
||||
const b = r(5,12);
|
||||
|
||||
assert(R.equals( R.add(a,b), r(13, 6)));
|
||||
});
|
||||
it("Should substract", () => {
|
||||
const a = r(7,4);
|
||||
const b = r(5,12);
|
||||
|
||||
assert(R.equals( R.sub(a,b), r(4, 3)));
|
||||
});
|
||||
it("Should multiply", () => {
|
||||
const a = r(7,4);
|
||||
const b = r(5,12);
|
||||
|
||||
assert(R.equals( R.mul(a,b), r(35, 48)));
|
||||
});
|
||||
it("Should div", () => {
|
||||
const a = r(7,4);
|
||||
const b = r(5,12);
|
||||
|
||||
assert(R.equals( R.div(a,b), r(7*12, 5*4)));
|
||||
});
|
||||
it("Should square", () => {
|
||||
const a = r(7,4);
|
||||
|
||||
assert(R.equals( R.square(a), r(49, 16)));
|
||||
});
|
||||
it("Should affine", () => {
|
||||
const a = r(12,4);
|
||||
const aa = R.affine(a);
|
||||
assert(Z.equals( aa[0], bigInt(3)));
|
||||
assert(Z.equals( aa[1], Z.one));
|
||||
});
|
||||
it("Should convert from Z to R", () => {
|
||||
const vz = bigInt(34);
|
||||
const vr = R.fromF(vz);
|
||||
|
||||
assert(R.equals( vr, r(34,1)));
|
||||
});
|
||||
it("Should convert from R to Z", () => {
|
||||
const vr = r(32, 2);
|
||||
const vz = R.toF(vr);
|
||||
|
||||
assert(Z.equals( vz, bigInt(16)));
|
||||
});
|
||||
});
|
||||
67
test/zksnark.js
Normal file
67
test/zksnark.js
Normal file
@@ -0,0 +1,67 @@
|
||||
const chai = require("chai");
|
||||
const fs = require("fs");
|
||||
const bigInt = require("../src/bigint.js");
|
||||
|
||||
const Circuit = require("../src/circuit.js");
|
||||
const zkSnark = require("../index.js");
|
||||
|
||||
const assert = chai.assert;
|
||||
|
||||
|
||||
function stringifyBigInts(o) {
|
||||
if ((typeof(o) == "bigint") || (o instanceof bigInt)) {
|
||||
return o.toString(10);
|
||||
} else if (Array.isArray(o)) {
|
||||
return o.map(stringifyBigInts);
|
||||
} else if (typeof o == "object") {
|
||||
const res = {};
|
||||
for (let k in o) {
|
||||
res[k] = stringifyBigInts(o[k]);
|
||||
}
|
||||
return res;
|
||||
} else {
|
||||
return o;
|
||||
}
|
||||
}
|
||||
|
||||
function unstringifyBigInts(o) {
|
||||
if ((typeof(o) == "string") && (/^[0-9]+$/.test(o) )) {
|
||||
return bigInt(o);
|
||||
} else if (Array.isArray(o)) {
|
||||
return o.map(unstringifyBigInts);
|
||||
} else if (typeof o == "object") {
|
||||
const res = {};
|
||||
for (let k in o) {
|
||||
res[k] = unstringifyBigInts(o[k]);
|
||||
}
|
||||
return res;
|
||||
} else {
|
||||
return o;
|
||||
}
|
||||
}
|
||||
|
||||
describe("zkSnark", () => {
|
||||
it("Load a circuit, create trusted setup, create a proof and validate", () => {
|
||||
|
||||
const cirDef = JSON.parse(fs.readFileSync("../jaz/sum.json", "utf8"));
|
||||
const cir = new Circuit(cirDef);
|
||||
|
||||
const setup = zkSnark.setup(cir);
|
||||
const strSetup = stringifyBigInts(setup);
|
||||
fs.writeFileSync("vk_proof.json", JSON.stringify(strSetup.vk_proof), "utf-8");
|
||||
fs.writeFileSync("vk_verifier.json", JSON.stringify(strSetup.vk_verifier), "utf-8");
|
||||
|
||||
/*
|
||||
const setup = {};
|
||||
setup.vk_proof = unstringifyBigInts(JSON.parse(fs.readFileSync("vk_proof.json", "utf8")));
|
||||
setup.vk_verifier = unstringifyBigInts(JSON.parse(fs.readFileSync("vk_verifier.json", "utf8")));
|
||||
*/
|
||||
const witness = cir.calculateWitness({"a": "33", "b": "34"});
|
||||
|
||||
assert.equal(witness[cir.getSignalIdx("main.out")].toString(), "67");
|
||||
|
||||
const {proof, publicSignals} = zkSnark.genProof(setup.vk_proof, witness);
|
||||
|
||||
assert( zkSnark.isValid(setup.vk_verifier, proof, publicSignals));
|
||||
}).timeout(10000000);
|
||||
});
|
||||
Reference in New Issue
Block a user