mirror of
https://github.com/arnaucube/circomlib.git
synced 2026-02-06 18:56:43 +01:00
Merge branch 'c_build'
This commit is contained in:
@@ -1,11 +1,11 @@
|
||||
const chai = require("chai");
|
||||
const path = require("path");
|
||||
const snarkjs = require("snarkjs");
|
||||
const compiler = require("circom");
|
||||
|
||||
const assert = chai.assert;
|
||||
|
||||
const bigInt = snarkjs.bigInt;
|
||||
const bigInt = require("big-integer");
|
||||
|
||||
const tester = require("circom").tester;
|
||||
|
||||
function print(circuit, w, s) {
|
||||
console.log(s + ": " + w[circuit.getSignalIdx(s)]);
|
||||
@@ -14,7 +14,7 @@ function print(circuit, w, s) {
|
||||
function getBits(v, n) {
|
||||
const res = [];
|
||||
for (let i=0; i<n; i++) {
|
||||
if (v.shr(i).isOdd()) {
|
||||
if (v.shiftRight(i).isOdd()) {
|
||||
res.push(bigInt.one);
|
||||
} else {
|
||||
res.push(bigInt.zero);
|
||||
@@ -25,46 +25,45 @@ function getBits(v, n) {
|
||||
|
||||
const q = bigInt("21888242871839275222246405745257275088548364400416034343698204186575808495617");
|
||||
|
||||
describe("Aliascheck test", () => {
|
||||
let circuit;
|
||||
describe("Aliascheck test", function () {
|
||||
this.timeout(100000);
|
||||
|
||||
let cir;
|
||||
before( async() => {
|
||||
const cirDef = await compiler(path.join(__dirname, "circuits", "aliascheck_test.circom"));
|
||||
|
||||
circuit = new snarkjs.Circuit(cirDef);
|
||||
|
||||
console.log("NConstrains: " + circuit.nConstraints);
|
||||
cir = await tester(path.join(__dirname, "circuits", "aliascheck_test.circom"));
|
||||
});
|
||||
|
||||
it("Satisfy the aliastest 0", async () => {
|
||||
const inp = getBits(bigInt.zero, 254);
|
||||
circuit.calculateWitness({in: inp});
|
||||
await cir.calculateWitness({in: inp}, true);
|
||||
});
|
||||
|
||||
it("Satisfy the aliastest 3", async () => {
|
||||
const inp = getBits(bigInt(3), 254);
|
||||
circuit.calculateWitness({in: inp});
|
||||
await cir.calculateWitness({in: inp}, true);
|
||||
});
|
||||
|
||||
it("Satisfy the aliastest q-1", async () => {
|
||||
const inp = getBits(q.sub(bigInt.one), 254);
|
||||
circuit.calculateWitness({in: inp});
|
||||
const inp = getBits(q.minus(bigInt.one), 254);
|
||||
await cir.calculateWitness({in: inp}, true);
|
||||
});
|
||||
|
||||
it("Nhot not satisfy an input of q", async () => {
|
||||
it("Should not satisfy an input of q", async () => {
|
||||
const inp = getBits(q, 254);
|
||||
try {
|
||||
circuit.calculateWitness({in: inp});
|
||||
await cir.calculateWitness({in: inp}, true);
|
||||
assert(false);
|
||||
} catch(err) {
|
||||
assert(/Constraint\sdoesn't\smatch(.*)1\s!=\s0/.test(err.message) );
|
||||
}
|
||||
});
|
||||
|
||||
it("Nhot not satisfy all ones", async () => {
|
||||
it("Should not satisfy all ones", async () => {
|
||||
|
||||
const inp = getBits(bigInt(1).shl(254).sub(bigInt(1)), 254);
|
||||
const inp = getBits(bigInt(1).shiftLeft(254).minus(bigInt.one), 254);
|
||||
try {
|
||||
circuit.calculateWitness({in: inp});
|
||||
await cir.calculateWitness({in: inp}, true);
|
||||
assert(false);
|
||||
} catch(err) {
|
||||
assert(/Constraint\sdoesn't\smatch(.*)1\s!=\s0/.test(err.message) );
|
||||
|
||||
103
test/babyjub.js
103
test/babyjub.js
@@ -1,103 +1,89 @@
|
||||
const chai = require("chai");
|
||||
const path = require("path");
|
||||
const snarkjs = require("snarkjs");
|
||||
const compiler = require("circom");
|
||||
|
||||
const createBlakeHash = require("blake-hash");
|
||||
const eddsa = require("../src/eddsa.js");
|
||||
|
||||
const assert = chai.assert;
|
||||
|
||||
const bigInt = require("snarkjs").bigInt;
|
||||
const bigInt = require("big-integer");
|
||||
const tester = require("circom").tester;
|
||||
const utils = require("../src/utils.js");
|
||||
|
||||
describe("Baby Jub test", function () {
|
||||
let circuitAdd;
|
||||
let circuitTest;
|
||||
let circuitPbk;
|
||||
|
||||
this.timeout(100000);
|
||||
|
||||
before( async() => {
|
||||
const cirDefAdd = await compiler(path.join(__dirname, "circuits", "babyadd_tester.circom"));
|
||||
circuitAdd = new snarkjs.Circuit(cirDefAdd);
|
||||
console.log("NConstrains BabyAdd: " + circuitAdd.nConstraints);
|
||||
circuitAdd = await tester(path.join(__dirname, "circuits", "babyadd_tester.circom"));
|
||||
|
||||
const cirDefTest = await compiler(path.join(__dirname, "circuits", "babycheck_test.circom"));
|
||||
circuitTest = new snarkjs.Circuit(cirDefTest);
|
||||
console.log("NConstrains BabyTest: " + circuitTest.nConstraints);
|
||||
|
||||
const cirDefPbk = await compiler(path.join(__dirname, "circuits", "babypbk_test.circom"));
|
||||
circuitPbk = new snarkjs.Circuit(cirDefPbk);
|
||||
console.log("NConstrains BabyPbk: " + circuitPbk.nConstraints);
|
||||
circuitTest = await tester(path.join(__dirname, "circuits", "babycheck_test.circom"));
|
||||
|
||||
circuitPbk = await tester(path.join(__dirname, "circuits", "babypbk_test.circom"));
|
||||
});
|
||||
|
||||
it("Should add point (0,1) and (0,1)", async () => {
|
||||
|
||||
const input={
|
||||
x1: snarkjs.bigInt(0),
|
||||
y1: snarkjs.bigInt(1),
|
||||
x2: snarkjs.bigInt(0),
|
||||
y2: snarkjs.bigInt(1)
|
||||
x1: bigInt(0),
|
||||
y1: bigInt(1),
|
||||
x2: bigInt(0),
|
||||
y2: bigInt(1)
|
||||
};
|
||||
|
||||
const w = circuitAdd.calculateWitness(input);
|
||||
const w = await circuitAdd.calculateWitness(input, true);
|
||||
|
||||
const xout = w[circuitAdd.getSignalIdx("main.xout")];
|
||||
const yout = w[circuitAdd.getSignalIdx("main.yout")];
|
||||
|
||||
assert(xout.equals(0));
|
||||
assert(yout.equals(1));
|
||||
await circuitAdd.assertOut(w, {xout: bigInt(0), yout: bigInt(1)});
|
||||
});
|
||||
|
||||
it("Should add 2 same numbers", async () => {
|
||||
|
||||
const input={
|
||||
x1: snarkjs.bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
|
||||
y1: snarkjs.bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
|
||||
x2: snarkjs.bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
|
||||
y2: snarkjs.bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475")
|
||||
x1: bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
|
||||
y1: bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
|
||||
x2: bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
|
||||
y2: bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475")
|
||||
};
|
||||
|
||||
const w = circuitAdd.calculateWitness(input);
|
||||
const w = await circuitAdd.calculateWitness(input, true);
|
||||
|
||||
const xout = w[circuitAdd.getSignalIdx("main.xout")];
|
||||
const yout = w[circuitAdd.getSignalIdx("main.yout")];
|
||||
await circuitAdd.assertOut(w, {
|
||||
xout: bigInt("6890855772600357754907169075114257697580319025794532037257385534741338397365"),
|
||||
yout: bigInt("4338620300185947561074059802482547481416142213883829469920100239455078257889")
|
||||
});
|
||||
|
||||
assert(xout.equals(snarkjs.bigInt("6890855772600357754907169075114257697580319025794532037257385534741338397365")));
|
||||
assert(yout.equals(snarkjs.bigInt("4338620300185947561074059802482547481416142213883829469920100239455078257889")));
|
||||
});
|
||||
|
||||
it("Should add 2 different numbers", async () => {
|
||||
|
||||
const input={
|
||||
x1: snarkjs.bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
|
||||
y1: snarkjs.bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
|
||||
x2: snarkjs.bigInt("16540640123574156134436876038791482806971768689494387082833631921987005038935"),
|
||||
y2: snarkjs.bigInt("20819045374670962167435360035096875258406992893633759881276124905556507972311")
|
||||
x1: bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
|
||||
y1: bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
|
||||
x2: bigInt("16540640123574156134436876038791482806971768689494387082833631921987005038935"),
|
||||
y2: bigInt("20819045374670962167435360035096875258406992893633759881276124905556507972311")
|
||||
};
|
||||
|
||||
const w = circuitAdd.calculateWitness(input);
|
||||
const w = await circuitAdd.calculateWitness(input, true);
|
||||
|
||||
const xout = w[circuitAdd.getSignalIdx("main.xout")];
|
||||
const yout = w[circuitAdd.getSignalIdx("main.yout")];
|
||||
await circuitAdd.assertOut(w, {
|
||||
xout: bigInt("7916061937171219682591368294088513039687205273691143098332585753343424131937"),
|
||||
yout: bigInt("14035240266687799601661095864649209771790948434046947201833777492504781204499")
|
||||
});
|
||||
|
||||
/*
|
||||
console.log(xout.toString());
|
||||
console.log(yout.toString());
|
||||
*/
|
||||
|
||||
assert(xout.equals(snarkjs.bigInt("7916061937171219682591368294088513039687205273691143098332585753343424131937")));
|
||||
assert(yout.equals(snarkjs.bigInt("14035240266687799601661095864649209771790948434046947201833777492504781204499")));
|
||||
});
|
||||
|
||||
it("Should check 0 is a valid poiny", async() => {
|
||||
const w = circuitTest.calculateWitness({x: 0, y:1});
|
||||
assert(circuitTest.checkWitness(w));
|
||||
it("Should check (0,1) is a valid point", async() => {
|
||||
const w = await circuitTest.calculateWitness({x: 0, y:1}, true);
|
||||
|
||||
await circuitTest.checkConstraints(w);
|
||||
});
|
||||
|
||||
it("Should check 0 is an invalid poiny", async() => {
|
||||
it("Should check (1,0) is an invalid point", async() => {
|
||||
try {
|
||||
circuitTest.calculateWitness({x: 1, y: 0});
|
||||
await circuitTest.calculateWitness({x: 1, y: 0}, true);
|
||||
assert(false, "Should be a valid point");
|
||||
} catch(err) {
|
||||
assert(/Constraint\sdoesn't\smatch(.*)168700\s!=\s1/.test(err.message) );
|
||||
@@ -108,18 +94,19 @@ describe("Baby Jub test", function () {
|
||||
|
||||
const rawpvk = Buffer.from("0001020304050607080900010203040506070809000102030405060708090021", "hex");
|
||||
const pvk = eddsa.pruneBuffer(createBlakeHash("blake512").update(rawpvk).digest().slice(0,32));
|
||||
const S = bigInt.leBuff2int(pvk).shr(3);
|
||||
const S = utils.leBuff2int(pvk).shiftRight(3);
|
||||
|
||||
const A = eddsa.prv2pub(rawpvk);
|
||||
|
||||
const input = {
|
||||
in : S,
|
||||
Ax : A[0],
|
||||
Ay : A[1]
|
||||
}
|
||||
in : S
|
||||
};
|
||||
|
||||
const w = circuitPbk.calculateWitness(input);
|
||||
assert(circuitPbk.checkWitness(w));
|
||||
const w = await circuitPbk.calculateWitness(input, true);
|
||||
|
||||
await circuitPbk.assertOut(w, {Ax : A[0], Ay: A[1]});
|
||||
|
||||
await circuitPbk.checkConstraints(w);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
const chai = require("chai");
|
||||
const path = require("path");
|
||||
const snarkjs = require("snarkjs");
|
||||
const compiler = require("circom");
|
||||
const bigInt = require("big-integer");
|
||||
const babyjub = require("../src/babyjub.js");
|
||||
|
||||
const assert = chai.assert;
|
||||
@@ -16,14 +14,14 @@ describe("Baby Jub js test", function () {
|
||||
it("Should add point (0,1) and (0,1)", () => {
|
||||
|
||||
const p1 = [
|
||||
snarkjs.bigInt(0),
|
||||
snarkjs.bigInt(1)];
|
||||
bigInt(0),
|
||||
bigInt(1)];
|
||||
const p2 = [
|
||||
snarkjs.bigInt(0),
|
||||
snarkjs.bigInt(1)
|
||||
bigInt(0),
|
||||
bigInt(1)
|
||||
];
|
||||
|
||||
const out = babyjub.addPoint(p1, p2)
|
||||
const out = babyjub.addPoint(p1, p2);
|
||||
assert(out[0].equals(0));
|
||||
assert(out[1].equals(1));
|
||||
});
|
||||
@@ -41,43 +39,43 @@ describe("Baby Jub js test", function () {
|
||||
it("Should add 2 same numbers", () => {
|
||||
|
||||
const p1 = [
|
||||
snarkjs.bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
|
||||
snarkjs.bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
|
||||
bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
|
||||
bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
|
||||
];
|
||||
const p2 = [
|
||||
snarkjs.bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
|
||||
snarkjs.bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
|
||||
bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
|
||||
bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
|
||||
];
|
||||
|
||||
const out = babyjub.addPoint(p1, p2)
|
||||
assert(out[0].equals(snarkjs.bigInt("6890855772600357754907169075114257697580319025794532037257385534741338397365")));
|
||||
assert(out[1].equals(snarkjs.bigInt("4338620300185947561074059802482547481416142213883829469920100239455078257889")));
|
||||
const out = babyjub.addPoint(p1, p2);
|
||||
assert(out[0].equals(bigInt("6890855772600357754907169075114257697580319025794532037257385534741338397365")));
|
||||
assert(out[1].equals(bigInt("4338620300185947561074059802482547481416142213883829469920100239455078257889")));
|
||||
});
|
||||
|
||||
it("Should add 2 different numbers", () => {
|
||||
|
||||
const p1 = [
|
||||
snarkjs.bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
|
||||
snarkjs.bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
|
||||
bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
|
||||
bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
|
||||
];
|
||||
const p2 = [
|
||||
snarkjs.bigInt("16540640123574156134436876038791482806971768689494387082833631921987005038935"),
|
||||
snarkjs.bigInt("20819045374670962167435360035096875258406992893633759881276124905556507972311"),
|
||||
bigInt("16540640123574156134436876038791482806971768689494387082833631921987005038935"),
|
||||
bigInt("20819045374670962167435360035096875258406992893633759881276124905556507972311"),
|
||||
];
|
||||
|
||||
const out = babyjub.addPoint(p1, p2)
|
||||
const out = babyjub.addPoint(p1, p2);
|
||||
|
||||
assert(out[0].equals(snarkjs.bigInt("7916061937171219682591368294088513039687205273691143098332585753343424131937")));
|
||||
assert(out[1].equals(snarkjs.bigInt("14035240266687799601661095864649209771790948434046947201833777492504781204499")));
|
||||
assert(out[0].equals(bigInt("7916061937171219682591368294088513039687205273691143098332585753343424131937")));
|
||||
assert(out[1].equals(bigInt("14035240266687799601661095864649209771790948434046947201833777492504781204499")));
|
||||
});
|
||||
|
||||
it("should mulPointEscalar 0", () => {
|
||||
const p = [
|
||||
snarkjs.bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
|
||||
snarkjs.bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
|
||||
bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
|
||||
bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
|
||||
];
|
||||
|
||||
const r = babyjub.mulPointEscalar(p, snarkjs.bigInt("3"));
|
||||
const r = babyjub.mulPointEscalar(p, bigInt("3"));
|
||||
let r2 = babyjub.addPoint(p, p);
|
||||
r2 = babyjub.addPoint(r2, p);
|
||||
assert.equal(r2[0].toString(), r[0].toString());
|
||||
@@ -88,65 +86,65 @@ describe("Baby Jub js test", function () {
|
||||
|
||||
it("should mulPointEscalar 1", () => {
|
||||
const p = [
|
||||
snarkjs.bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
|
||||
snarkjs.bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
|
||||
bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
|
||||
bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
|
||||
];
|
||||
|
||||
const r = babyjub.mulPointEscalar(p, snarkjs.bigInt("14035240266687799601661095864649209771790948434046947201833777492504781204499"));
|
||||
const r = babyjub.mulPointEscalar(p, bigInt("14035240266687799601661095864649209771790948434046947201833777492504781204499"));
|
||||
assert.equal(r[0].toString(), "17070357974431721403481313912716834497662307308519659060910483826664480189605");
|
||||
assert.equal(r[1].toString(), "4014745322800118607127020275658861516666525056516280575712425373174125159339");
|
||||
});
|
||||
|
||||
it("should mulPointEscalar 2", () => {
|
||||
const p = [
|
||||
snarkjs.bigInt("6890855772600357754907169075114257697580319025794532037257385534741338397365"),
|
||||
snarkjs.bigInt("4338620300185947561074059802482547481416142213883829469920100239455078257889"),
|
||||
bigInt("6890855772600357754907169075114257697580319025794532037257385534741338397365"),
|
||||
bigInt("4338620300185947561074059802482547481416142213883829469920100239455078257889"),
|
||||
];
|
||||
|
||||
const r = babyjub.mulPointEscalar(p, snarkjs.bigInt("20819045374670962167435360035096875258406992893633759881276124905556507972311"));
|
||||
const r = babyjub.mulPointEscalar(p, bigInt("20819045374670962167435360035096875258406992893633759881276124905556507972311"));
|
||||
assert.equal(r[0].toString(), "13563888653650925984868671744672725781658357821216877865297235725727006259983");
|
||||
assert.equal(r[1].toString(), "8442587202676550862664528699803615547505326611544120184665036919364004251662");
|
||||
});
|
||||
|
||||
it("should inCurve 1", () => {
|
||||
const p = [
|
||||
snarkjs.bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
|
||||
snarkjs.bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
|
||||
bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
|
||||
bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
|
||||
];
|
||||
assert(babyjub.inCurve(p));
|
||||
});
|
||||
|
||||
it("should inCurve 2", () => {
|
||||
const p = [
|
||||
snarkjs.bigInt("6890855772600357754907169075114257697580319025794532037257385534741338397365"),
|
||||
snarkjs.bigInt("4338620300185947561074059802482547481416142213883829469920100239455078257889"),
|
||||
bigInt("6890855772600357754907169075114257697580319025794532037257385534741338397365"),
|
||||
bigInt("4338620300185947561074059802482547481416142213883829469920100239455078257889"),
|
||||
];
|
||||
assert(babyjub.inCurve(p));
|
||||
});
|
||||
|
||||
it("should inSubgroup 1", () => {
|
||||
const p = [
|
||||
snarkjs.bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
|
||||
snarkjs.bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
|
||||
bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
|
||||
bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
|
||||
];
|
||||
assert(babyjub.inSubgroup(p));
|
||||
});
|
||||
|
||||
it("should inSubgroup 2", () => {
|
||||
const p = [
|
||||
snarkjs.bigInt("6890855772600357754907169075114257697580319025794532037257385534741338397365"),
|
||||
snarkjs.bigInt("4338620300185947561074059802482547481416142213883829469920100239455078257889"),
|
||||
bigInt("6890855772600357754907169075114257697580319025794532037257385534741338397365"),
|
||||
bigInt("4338620300185947561074059802482547481416142213883829469920100239455078257889"),
|
||||
];
|
||||
assert(babyjub.inSubgroup(p));
|
||||
});
|
||||
|
||||
it("should packPoint - unpackPoint 1", () => {
|
||||
const p = [
|
||||
snarkjs.bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
|
||||
snarkjs.bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
|
||||
bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
|
||||
bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
|
||||
];
|
||||
const buf = babyjub.packPoint(p);
|
||||
assert.equal(buf.toString('hex'), '53b81ed5bffe9545b54016234682e7b2f699bd42a5e9eae27ff4051bc698ce85');
|
||||
assert.equal(buf.toString("hex"), "53b81ed5bffe9545b54016234682e7b2f699bd42a5e9eae27ff4051bc698ce85");
|
||||
const p2 = babyjub.unpackPoint(buf);
|
||||
assert.equal(p2[0].toString(), "17777552123799933955779906779655732241715742912184938656739573121738514868268");
|
||||
assert.equal(p2[1].toString(), "2626589144620713026669568689430873010625803728049924121243784502389097019475");
|
||||
@@ -154,11 +152,11 @@ describe("Baby Jub js test", function () {
|
||||
|
||||
it("should packPoint - unpackPoint 2", () => {
|
||||
const p = [
|
||||
snarkjs.bigInt("6890855772600357754907169075114257697580319025794532037257385534741338397365"),
|
||||
snarkjs.bigInt("4338620300185947561074059802482547481416142213883829469920100239455078257889"),
|
||||
bigInt("6890855772600357754907169075114257697580319025794532037257385534741338397365"),
|
||||
bigInt("4338620300185947561074059802482547481416142213883829469920100239455078257889"),
|
||||
];
|
||||
const buf = babyjub.packPoint(p);
|
||||
assert.equal(buf.toString('hex'), 'e114eb17eddf794f063a68fecac515e3620e131976108555735c8b0773929709');
|
||||
assert.equal(buf.toString("hex"), "e114eb17eddf794f063a68fecac515e3620e131976108555735c8b0773929709");
|
||||
const p2 = babyjub.unpackPoint(buf);
|
||||
assert.equal(p2[0].toString(), "6890855772600357754907169075114257697580319025794532037257385534741338397365");
|
||||
assert.equal(p2[1].toString(), "4338620300185947561074059802482547481416142213883829469920100239455078257889");
|
||||
|
||||
@@ -1,55 +1,50 @@
|
||||
const chai = require("chai");
|
||||
const path = require("path");
|
||||
const snarkjs = require("snarkjs");
|
||||
const compiler = require("circom");
|
||||
|
||||
const assert = chai.assert;
|
||||
|
||||
const bigInt = snarkjs.bigInt;
|
||||
const bigInt = require("big-integer");
|
||||
const tester = require("circom").tester;
|
||||
|
||||
function print(circuit, w, s) {
|
||||
console.log(s + ": " + w[circuit.getSignalIdx(s)]);
|
||||
}
|
||||
|
||||
function checkSub(_a,_b, circuit) {
|
||||
async function checkSub(_a,_b, circuit) {
|
||||
let a=bigInt(_a);
|
||||
let b=bigInt(_b);
|
||||
if (a.lesser(bigInt.zero)) a = a.add(bigInt.one.shl(16));
|
||||
if (b.lesser(bigInt.zero)) b = b.add(bigInt.one.shl(16));
|
||||
const w = circuit.calculateWitness({a: a, b: b});
|
||||
if (a.lesser(bigInt.zero)) a = a.add(bigInt.one.shiftLeft(16));
|
||||
if (b.lesser(bigInt.zero)) b = b.add(bigInt.one.shiftLeft(16));
|
||||
const w = await circuit.calculateWitness({a: a, b: b}, true);
|
||||
|
||||
let res = a.sub(b);
|
||||
if (res.lesser(bigInt.zero)) res = res.add(bigInt.one.shl(16));
|
||||
assert( w[circuit.getSignalIdx("main.out")].equals(bigInt(res)) );
|
||||
let res = a.minus(b);
|
||||
if (res.lesser(bigInt.zero)) res = res.add(bigInt.one.shiftLeft(16));
|
||||
await circuit.assertOut(w, {out: bigInt(res)});
|
||||
}
|
||||
|
||||
describe("BinSub test", () => {
|
||||
describe("BinSub test", function () {
|
||||
|
||||
this.timeout(100000);
|
||||
|
||||
let circuit;
|
||||
before( async() => {
|
||||
const cirDef = await compiler(path.join(__dirname, "circuits", "binsub_test.circom"));
|
||||
|
||||
circuit = new snarkjs.Circuit(cirDef);
|
||||
|
||||
console.log("NConstrains BinSub: " + circuit.nConstraints);
|
||||
circuit = await tester(path.join(__dirname, "circuits", "binsub_test.circom"));
|
||||
});
|
||||
|
||||
it("Should check variuos ege cases", async () => {
|
||||
checkSub(0,0, circuit);
|
||||
checkSub(1,0, circuit);
|
||||
checkSub(-1,0, circuit);
|
||||
checkSub(2,1, circuit);
|
||||
checkSub(2,2, circuit);
|
||||
checkSub(2,3, circuit);
|
||||
checkSub(2,-1, circuit);
|
||||
checkSub(2,-2, circuit);
|
||||
checkSub(2,-3, circuit);
|
||||
checkSub(-2,-3, circuit);
|
||||
checkSub(-2,-2, circuit);
|
||||
checkSub(-2,-1, circuit);
|
||||
checkSub(-2,0, circuit);
|
||||
checkSub(-2,1, circuit);
|
||||
checkSub(-2,2, circuit);
|
||||
checkSub(-2,3, circuit);
|
||||
await checkSub(0,0, circuit);
|
||||
await checkSub(1,0, circuit);
|
||||
await checkSub(-1,0, circuit);
|
||||
await checkSub(2,1, circuit);
|
||||
await checkSub(2,2, circuit);
|
||||
await checkSub(2,3, circuit);
|
||||
await checkSub(2,-1, circuit);
|
||||
await checkSub(2,-2, circuit);
|
||||
await checkSub(2,-3, circuit);
|
||||
await checkSub(-2,-3, circuit);
|
||||
await checkSub(-2,-2, circuit);
|
||||
await checkSub(-2,-1, circuit);
|
||||
await checkSub(-2,0, circuit);
|
||||
await checkSub(-2,1, circuit);
|
||||
await checkSub(-2,2, circuit);
|
||||
await checkSub(-2,3, circuit);
|
||||
});
|
||||
|
||||
|
||||
|
||||
@@ -1,35 +1,37 @@
|
||||
const chai = require("chai");
|
||||
const path = require("path");
|
||||
const snarkjs = require("snarkjs");
|
||||
const crypto = require("crypto");
|
||||
|
||||
const compiler = require("circom");
|
||||
const tester = require("circom").tester;
|
||||
|
||||
const bigInt = require("big-integer");
|
||||
|
||||
const assert = chai.assert;
|
||||
|
||||
describe("Sum test", () => {
|
||||
describe("Binary sum test", function () {
|
||||
|
||||
this.timeout(100000000);
|
||||
|
||||
it("Should create a constant circuit", async () => {
|
||||
const circuit = await tester(path.join(__dirname, "circuits", "constants_test.circom"));
|
||||
await circuit.loadConstraints();
|
||||
|
||||
const cirDef = await compiler(path.join(__dirname, "circuits", "constants_test.circom"));
|
||||
assert.equal(cirDef.nVars, 2);
|
||||
assert.equal(circuit.nVars, 2);
|
||||
assert.equal(circuit.constraints.length, 1);
|
||||
|
||||
const circuit = new snarkjs.Circuit(cirDef);
|
||||
const witness = await circuit.calculateWitness({ "in": bigInt("d807aa98", 16)}, true);
|
||||
|
||||
const witness = circuit.calculateWitness({ "in": "0xd807aa98" });
|
||||
|
||||
assert(witness[0].equals(snarkjs.bigInt(1)));
|
||||
assert(witness[1].equals(snarkjs.bigInt("0xd807aa98")));
|
||||
assert(witness[0].equals(bigInt(1)));
|
||||
assert(witness[1].equals(bigInt("d807aa98", 16)));
|
||||
});
|
||||
it("Should create a sum circuit", async () => {
|
||||
const circuit = await tester(path.join(__dirname, "circuits", "sum_test.circom"));
|
||||
await circuit.loadConstraints();
|
||||
|
||||
const cirDef = await compiler(path.join(__dirname, "circuits", "sum_test.circom"));
|
||||
assert.equal(cirDef.nVars, 97); // 32 (in1) + 32(in2) + 32(out) + 1 (carry)
|
||||
assert.equal(circuit.constraints.length, 97); // 32 (in1) + 32(in2) + 32(out) + 1 (carry)
|
||||
|
||||
const circuit = new snarkjs.Circuit(cirDef);
|
||||
const witness = await circuit.calculateWitness({ "a": "111", "b": "222" }, true);
|
||||
|
||||
const witness = circuit.calculateWitness({ "a": "111", "b": "222" });
|
||||
|
||||
assert(witness[0].equals(snarkjs.bigInt(1)));
|
||||
assert(witness[1].equals(snarkjs.bigInt("333")));
|
||||
assert(witness[0].equals(bigInt(1)));
|
||||
assert(witness[1].equals(bigInt("333")));
|
||||
});
|
||||
});
|
||||
|
||||
@@ -6,6 +6,8 @@ template A() {
|
||||
signal input b;
|
||||
signal output out;
|
||||
|
||||
var i;
|
||||
|
||||
component n2ba = Num2Bits(16);
|
||||
component n2bb = Num2Bits(16);
|
||||
component sub = BinSub(16);
|
||||
@@ -14,9 +16,12 @@ template A() {
|
||||
n2ba.in <== a;
|
||||
n2bb.in <== b;
|
||||
|
||||
for (var i=0; i<16; i++) {
|
||||
for (i=0; i<16; i++) {
|
||||
sub.in[0][i] <== n2ba.out[i];
|
||||
sub.in[1][i] <== n2bb.out[i];
|
||||
}
|
||||
|
||||
for (i=0; i<16; i++) {
|
||||
b2n.in[i] <== sub.out[i];
|
||||
}
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ template Main() {
|
||||
signal input in;
|
||||
signal output out[2];
|
||||
|
||||
var base = [5299619240641551281634865583518297030282874472190772894086521144482721001553,
|
||||
var base[2] = [5299619240641551281634865583518297030282874472190772894086521144482721001553,
|
||||
16950150798460657717958625567821834550301663161624707787222815936182638968203];
|
||||
|
||||
|
||||
|
||||
@@ -7,8 +7,8 @@ template Main() {
|
||||
|
||||
var i;
|
||||
|
||||
var base = [5299619240641551281634865583518297030282874472190772894086521144482721001553,
|
||||
16950150798460657717958625567821834550301663161624707787222815936182638968203];
|
||||
var base[2] = [5299619240641551281634865583518297030282874472190772894086521144482721001553,
|
||||
16950150798460657717958625567821834550301663161624707787222815936182638968203];
|
||||
|
||||
component escalarMul = EscalarMul(256, base);
|
||||
|
||||
|
||||
@@ -6,8 +6,8 @@ template Main() {
|
||||
signal input e;
|
||||
signal output out[2];
|
||||
|
||||
var base = [5299619240641551281634865583518297030282874472190772894086521144482721001553,
|
||||
16950150798460657717958625567821834550301663161624707787222815936182638968203]
|
||||
var base[2] = [5299619240641551281634865583518297030282874472190772894086521144482721001553,
|
||||
16950150798460657717958625567821834550301663161624707787222815936182638968203]
|
||||
|
||||
|
||||
component n2b = Num2Bits(253);
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
include "../../circuits/escalarmulw4table.circom";
|
||||
|
||||
var base = [5299619240641551281634865583518297030282874472190772894086521144482721001553,
|
||||
var base[2] = [5299619240641551281634865583518297030282874472190772894086521144482721001553,
|
||||
16950150798460657717958625567821834550301663161624707787222815936182638968203]
|
||||
|
||||
component main = EscalarMulW4Table(base, 0);
|
||||
|
||||
@@ -4,10 +4,10 @@ include "../../circuits/escalarmulw4table.circom";
|
||||
template Main() {
|
||||
signal input in;
|
||||
signal output out[16][2];
|
||||
var base = [5299619240641551281634865583518297030282874472190772894086521144482721001553,
|
||||
var base[2] = [5299619240641551281634865583518297030282874472190772894086521144482721001553,
|
||||
16950150798460657717958625567821834550301663161624707787222815936182638968203];
|
||||
|
||||
var escalarMul = EscalarMulW4Table(base, 0);
|
||||
var escalarMul[16][2] = EscalarMulW4Table(base, 0);
|
||||
for (var i=0; i<16; i++) {
|
||||
out[i][0] <== escalarMul[i][0]*in;
|
||||
out[i][1] <== escalarMul[i][1]*in;
|
||||
|
||||
@@ -4,10 +4,10 @@ include "../../circuits/escalarmulw4table.circom";
|
||||
template Main() {
|
||||
signal input in;
|
||||
signal output out[16][2];
|
||||
var base = [5299619240641551281634865583518297030282874472190772894086521144482721001553,
|
||||
16950150798460657717958625567821834550301663161624707787222815936182638968203];
|
||||
var base[2] = [5299619240641551281634865583518297030282874472190772894086521144482721001553,
|
||||
16950150798460657717958625567821834550301663161624707787222815936182638968203];
|
||||
|
||||
var escalarMul = EscalarMulW4Table(base, 3);
|
||||
var escalarMul[16][2] = EscalarMulW4Table(base, 3);
|
||||
for (var i=0; i<16; i++) {
|
||||
out[i][0] <== escalarMul[i][0]*in;
|
||||
out[i][1] <== escalarMul[i][1]*in;
|
||||
|
||||
258
test/circuits/in.json
Normal file
258
test/circuits/in.json
Normal file
@@ -0,0 +1,258 @@
|
||||
{
|
||||
"in": [
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1",
|
||||
"1"
|
||||
]
|
||||
}
|
||||
@@ -6,6 +6,8 @@ template A() {
|
||||
signal input b;
|
||||
signal output out;
|
||||
|
||||
var i;
|
||||
|
||||
component n2ba = Num2Bits(32);
|
||||
component n2bb = Num2Bits(32);
|
||||
component sum = BinSum(32,2);
|
||||
@@ -14,9 +16,12 @@ template A() {
|
||||
n2ba.in <== a;
|
||||
n2bb.in <== b;
|
||||
|
||||
for (var i=0; i<32; i++) {
|
||||
for (i=0; i<32; i++) {
|
||||
sum.in[0][i] <== n2ba.out[i];
|
||||
sum.in[1][i] <== n2bb.out[i];
|
||||
}
|
||||
|
||||
for (i=0; i<32; i++) {
|
||||
b2n.in[i] <== sum.out[i];
|
||||
}
|
||||
|
||||
|
||||
@@ -1,193 +1,184 @@
|
||||
const chai = require("chai");
|
||||
const path = require("path");
|
||||
const snarkjs = require("snarkjs");
|
||||
const crypto = require("crypto");
|
||||
|
||||
const compiler = require("circom");
|
||||
const tester = require("circom").tester;
|
||||
|
||||
const bigInt = require("big-integer");
|
||||
|
||||
const assert = chai.assert;
|
||||
|
||||
describe("Sum test", () => {
|
||||
it("Should create a iszero circuit", async() => {
|
||||
const cirDef = await compiler(path.join(__dirname, "circuits", "iszero.circom"));
|
||||
describe("Comparators test", function () {
|
||||
|
||||
const circuit = new snarkjs.Circuit(cirDef);
|
||||
this.timeout(100000);
|
||||
|
||||
it("Should create a iszero circuit", async() => {
|
||||
const circuit = await tester(path.join(__dirname, "circuits", "iszero.circom"));
|
||||
|
||||
let witness;
|
||||
witness = circuit.calculateWitness({ "in": 111});
|
||||
assert(witness[0].equals(snarkjs.bigInt(1)));
|
||||
assert(witness[1].equals(snarkjs.bigInt(0)));
|
||||
witness = await circuit.calculateWitness({ "in": 111}, true);
|
||||
assert(witness[0].equals(bigInt(1)));
|
||||
assert(witness[1].equals(bigInt(0)));
|
||||
|
||||
witness = circuit.calculateWitness({ "in": 0 });
|
||||
assert(witness[0].equals(snarkjs.bigInt(1)));
|
||||
assert(witness[1].equals(snarkjs.bigInt(1)));
|
||||
witness = await circuit.calculateWitness({ "in": 0 }, true);
|
||||
assert(witness[0].equals(bigInt(1)));
|
||||
assert(witness[1].equals(bigInt(1)));
|
||||
});
|
||||
it("Should create a isequal circuit", async() => {
|
||||
const cirDef = await compiler(path.join(__dirname, "circuits", "isequal.circom"));
|
||||
|
||||
const circuit = new snarkjs.Circuit(cirDef);
|
||||
const circuit = await tester(path.join(__dirname, "circuits", "isequal.circom"));
|
||||
|
||||
let witness;
|
||||
witness = circuit.calculateWitness({ "in[0]": "111", "in[1]": "222" });
|
||||
assert(witness[0].equals(snarkjs.bigInt(1)));
|
||||
assert(witness[1].equals(snarkjs.bigInt(0)));
|
||||
witness = await circuit.calculateWitness({ "in": [111,222] }, true);
|
||||
assert(witness[0].equals(bigInt(1)));
|
||||
assert(witness[1].equals(bigInt(0)));
|
||||
|
||||
witness = circuit.calculateWitness({ "in[0]": "444", "in[1]": "444" });
|
||||
assert(witness[0].equals(snarkjs.bigInt(1)));
|
||||
assert(witness[1].equals(snarkjs.bigInt(1)));
|
||||
witness = await circuit.calculateWitness({ "in": [444,444] }, true);
|
||||
assert(witness[0].equals(bigInt(1)));
|
||||
assert(witness[1].equals(bigInt(1)));
|
||||
});
|
||||
it("Should create a comparison lessthan", async() => {
|
||||
const cirDef = await compiler(path.join(__dirname, "circuits", "lessthan.circom"));
|
||||
|
||||
const circuit = new snarkjs.Circuit(cirDef);
|
||||
const circuit = await tester(path.join(__dirname, "circuits", "lessthan.circom"));
|
||||
|
||||
let witness;
|
||||
witness = circuit.calculateWitness({ "in[0]": "333", "in[1]": "444" });
|
||||
assert(witness[0].equals(snarkjs.bigInt(1)));
|
||||
assert(witness[1].equals(snarkjs.bigInt(1)));
|
||||
witness = await circuit.calculateWitness({ "in": [333,444] }), true;
|
||||
assert(witness[0].equals(bigInt(1)));
|
||||
assert(witness[1].equals(bigInt(1)));
|
||||
|
||||
witness = circuit.calculateWitness({ "in[0]": "1", "in[1]": "1" });
|
||||
assert(witness[0].equals(snarkjs.bigInt(1)));
|
||||
assert(witness[1].equals(snarkjs.bigInt(0)));
|
||||
witness = await circuit.calculateWitness({ "in":[1,1] }, true);
|
||||
assert(witness[0].equals(bigInt(1)));
|
||||
assert(witness[1].equals(bigInt(0)));
|
||||
|
||||
witness = circuit.calculateWitness({ "in[0]": "661", "in[1]": "660" });
|
||||
assert(witness[0].equals(snarkjs.bigInt(1)));
|
||||
assert(witness[1].equals(snarkjs.bigInt(0)));
|
||||
witness = await circuit.calculateWitness({ "in": [661, 660] }, true);
|
||||
assert(witness[0].equals(bigInt(1)));
|
||||
assert(witness[1].equals(bigInt(0)));
|
||||
|
||||
witness = circuit.calculateWitness({ "in[0]": "0", "in[1]": "1" });
|
||||
assert(witness[0].equals(snarkjs.bigInt(1)));
|
||||
assert(witness[1].equals(snarkjs.bigInt(1)));
|
||||
witness = await circuit.calculateWitness({ "in": [0, 1] }, true);
|
||||
assert(witness[0].equals(bigInt(1)));
|
||||
assert(witness[1].equals(bigInt(1)));
|
||||
|
||||
witness = circuit.calculateWitness({ "in[0]": "0", "in[1]": "444" });
|
||||
assert(witness[0].equals(snarkjs.bigInt(1)));
|
||||
assert(witness[1].equals(snarkjs.bigInt(1)));
|
||||
witness = await circuit.calculateWitness({ "in": [0, 444] }, true);
|
||||
assert(witness[0].equals(bigInt(1)));
|
||||
assert(witness[1].equals(bigInt(1)));
|
||||
|
||||
witness = circuit.calculateWitness({ "in[0]": "1", "in[1]": "0" });
|
||||
assert(witness[0].equals(snarkjs.bigInt(1)));
|
||||
assert(witness[1].equals(snarkjs.bigInt(0)));
|
||||
witness = await circuit.calculateWitness({ "in": [1, 0] }, true);
|
||||
assert(witness[0].equals(bigInt(1)));
|
||||
assert(witness[1].equals(bigInt(0)));
|
||||
|
||||
witness = circuit.calculateWitness({ "in[0]": "555", "in[1]": "0" });
|
||||
assert(witness[0].equals(snarkjs.bigInt(1)));
|
||||
assert(witness[1].equals(snarkjs.bigInt(0)));
|
||||
witness = await circuit.calculateWitness({ "in": [555, 0] }, true);
|
||||
assert(witness[0].equals(bigInt(1)));
|
||||
assert(witness[1].equals(bigInt(0)));
|
||||
|
||||
witness = circuit.calculateWitness({ "in[0]": "0", "in[1]": "0" });
|
||||
assert(witness[0].equals(snarkjs.bigInt(1)));
|
||||
assert(witness[1].equals(snarkjs.bigInt(0)));
|
||||
witness = await circuit.calculateWitness({ "in": [0, 0] }, true);
|
||||
assert(witness[0].equals(bigInt(1)));
|
||||
assert(witness[1].equals(bigInt(0)));
|
||||
});
|
||||
it("Should create a comparison lesseqthan", async() => {
|
||||
const cirDef = await compiler(path.join(__dirname, "circuits", "lesseqthan.circom"));
|
||||
|
||||
const circuit = new snarkjs.Circuit(cirDef);
|
||||
const circuit = await tester(path.join(__dirname, "circuits", "lesseqthan.circom"));
|
||||
|
||||
let witness;
|
||||
witness = circuit.calculateWitness({ "in[0]": "333", "in[1]": "444" });
|
||||
assert(witness[0].equals(snarkjs.bigInt(1)));
|
||||
assert(witness[1].equals(snarkjs.bigInt(1)));
|
||||
witness = await circuit.calculateWitness({ "in": [333,444] }, true);
|
||||
assert(witness[0].equals(bigInt(1)));
|
||||
assert(witness[1].equals(bigInt(1)));
|
||||
|
||||
witness = circuit.calculateWitness({ "in[0]": "1", "in[1]": "1" });
|
||||
assert(witness[0].equals(snarkjs.bigInt(1)));
|
||||
assert(witness[1].equals(snarkjs.bigInt(1)));
|
||||
witness = await circuit.calculateWitness({ "in":[1,1] }, true);
|
||||
assert(witness[0].equals(bigInt(1)));
|
||||
assert(witness[1].equals(bigInt(1)));
|
||||
|
||||
witness = circuit.calculateWitness({ "in[0]": "661", "in[1]": "660" });
|
||||
assert(witness[0].equals(snarkjs.bigInt(1)));
|
||||
assert(witness[1].equals(snarkjs.bigInt(0)));
|
||||
witness = await circuit.calculateWitness({ "in": [661, 660] }, true);
|
||||
assert(witness[0].equals(bigInt(1)));
|
||||
assert(witness[1].equals(bigInt(0)));
|
||||
|
||||
witness = circuit.calculateWitness({ "in[0]": "0", "in[1]": "1" });
|
||||
assert(witness[0].equals(snarkjs.bigInt(1)));
|
||||
assert(witness[1].equals(snarkjs.bigInt(1)));
|
||||
witness = await circuit.calculateWitness({ "in": [0, 1] }, true);
|
||||
assert(witness[0].equals(bigInt(1)));
|
||||
assert(witness[1].equals(bigInt(1)));
|
||||
|
||||
witness = circuit.calculateWitness({ "in[0]": "0", "in[1]": "444" });
|
||||
assert(witness[0].equals(snarkjs.bigInt(1)));
|
||||
assert(witness[1].equals(snarkjs.bigInt(1)));
|
||||
witness = await circuit.calculateWitness({ "in": [0, 444] }, true);
|
||||
assert(witness[0].equals(bigInt(1)));
|
||||
assert(witness[1].equals(bigInt(1)));
|
||||
|
||||
witness = circuit.calculateWitness({ "in[0]": "1", "in[1]": "0" });
|
||||
assert(witness[0].equals(snarkjs.bigInt(1)));
|
||||
assert(witness[1].equals(snarkjs.bigInt(0)));
|
||||
witness = await circuit.calculateWitness({ "in": [1, 0] }, true);
|
||||
assert(witness[0].equals(bigInt(1)));
|
||||
assert(witness[1].equals(bigInt(0)));
|
||||
|
||||
witness = circuit.calculateWitness({ "in[0]": "555", "in[1]": "0" });
|
||||
assert(witness[0].equals(snarkjs.bigInt(1)));
|
||||
assert(witness[1].equals(snarkjs.bigInt(0)));
|
||||
witness = await circuit.calculateWitness({ "in": [555, 0] }, true);
|
||||
assert(witness[0].equals(bigInt(1)));
|
||||
assert(witness[1].equals(bigInt(0)));
|
||||
|
||||
witness = circuit.calculateWitness({ "in[0]": "0", "in[1]": "0" });
|
||||
assert(witness[0].equals(snarkjs.bigInt(1)));
|
||||
assert(witness[1].equals(snarkjs.bigInt(1)));
|
||||
witness = await circuit.calculateWitness({ "in": [0, 0] }, true);
|
||||
assert(witness[0].equals(bigInt(1)));
|
||||
assert(witness[1].equals(bigInt(1)));
|
||||
});
|
||||
it("Should create a comparison greaterthan", async() => {
|
||||
const cirDef = await compiler(path.join(__dirname, "circuits", "greaterthan.circom"));
|
||||
|
||||
const circuit = new snarkjs.Circuit(cirDef);
|
||||
const circuit = await tester(path.join(__dirname, "circuits", "greaterthan.circom"));
|
||||
|
||||
let witness;
|
||||
witness = circuit.calculateWitness({ "in[0]": "333", "in[1]": "444" });
|
||||
assert(witness[0].equals(snarkjs.bigInt(1)));
|
||||
assert(witness[1].equals(snarkjs.bigInt(0)));
|
||||
witness = await circuit.calculateWitness({ "in": [333,444] }, true);
|
||||
assert(witness[0].equals(bigInt(1)));
|
||||
assert(witness[1].equals(bigInt(0)));
|
||||
|
||||
witness = circuit.calculateWitness({ "in[0]": "1", "in[1]": "1" });
|
||||
assert(witness[0].equals(snarkjs.bigInt(1)));
|
||||
assert(witness[1].equals(snarkjs.bigInt(0)));
|
||||
witness = await circuit.calculateWitness({ "in":[1,1] }, true);
|
||||
assert(witness[0].equals(bigInt(1)));
|
||||
assert(witness[1].equals(bigInt(0)));
|
||||
|
||||
witness = circuit.calculateWitness({ "in[0]": "661", "in[1]": "660" });
|
||||
assert(witness[0].equals(snarkjs.bigInt(1)));
|
||||
assert(witness[1].equals(snarkjs.bigInt(1)));
|
||||
witness = await circuit.calculateWitness({ "in": [661, 660] }, true);
|
||||
assert(witness[0].equals(bigInt(1)));
|
||||
assert(witness[1].equals(bigInt(1)));
|
||||
|
||||
witness = circuit.calculateWitness({ "in[0]": "0", "in[1]": "1" });
|
||||
assert(witness[0].equals(snarkjs.bigInt(1)));
|
||||
assert(witness[1].equals(snarkjs.bigInt(0)));
|
||||
witness = await circuit.calculateWitness({ "in": [0, 1] }, true);
|
||||
assert(witness[0].equals(bigInt(1)));
|
||||
assert(witness[1].equals(bigInt(0)));
|
||||
|
||||
witness = circuit.calculateWitness({ "in[0]": "0", "in[1]": "444" });
|
||||
assert(witness[0].equals(snarkjs.bigInt(1)));
|
||||
assert(witness[1].equals(snarkjs.bigInt(0)));
|
||||
witness = await circuit.calculateWitness({ "in": [0, 444] }, true);
|
||||
assert(witness[0].equals(bigInt(1)));
|
||||
assert(witness[1].equals(bigInt(0)));
|
||||
|
||||
witness = circuit.calculateWitness({ "in[0]": "1", "in[1]": "0" });
|
||||
assert(witness[0].equals(snarkjs.bigInt(1)));
|
||||
assert(witness[1].equals(snarkjs.bigInt(1)));
|
||||
witness = await circuit.calculateWitness({ "in": [1, 0] }, true);
|
||||
assert(witness[0].equals(bigInt(1)));
|
||||
assert(witness[1].equals(bigInt(1)));
|
||||
|
||||
witness = circuit.calculateWitness({ "in[0]": "555", "in[1]": "0" });
|
||||
assert(witness[0].equals(snarkjs.bigInt(1)));
|
||||
assert(witness[1].equals(snarkjs.bigInt(1)));
|
||||
witness = await circuit.calculateWitness({ "in": [555, 0] }, true);
|
||||
assert(witness[0].equals(bigInt(1)));
|
||||
assert(witness[1].equals(bigInt(1)));
|
||||
|
||||
witness = circuit.calculateWitness({ "in[0]": "0", "in[1]": "0" });
|
||||
assert(witness[0].equals(snarkjs.bigInt(1)));
|
||||
assert(witness[1].equals(snarkjs.bigInt(0)));
|
||||
witness = await circuit.calculateWitness({ "in": [0, 0] }, true);
|
||||
assert(witness[0].equals(bigInt(1)));
|
||||
assert(witness[1].equals(bigInt(0)));
|
||||
});
|
||||
it("Should create a comparison greatereqthan", async() => {
|
||||
const cirDef = await compiler(path.join(__dirname, "circuits", "greatereqthan.circom"));
|
||||
|
||||
const circuit = new snarkjs.Circuit(cirDef);
|
||||
|
||||
console.log("NConstraints BalancesUpdater: " + circuit.nConstraints);
|
||||
const circuit = await tester(path.join(__dirname, "circuits", "greatereqthan.circom"));
|
||||
|
||||
let witness;
|
||||
witness = circuit.calculateWitness({ "in[0]": "333", "in[1]": "444" });
|
||||
assert(witness[0].equals(snarkjs.bigInt(1)));
|
||||
assert(witness[1].equals(snarkjs.bigInt(0)));
|
||||
witness = await circuit.calculateWitness({ "in": [333,444] }, true);
|
||||
assert(witness[0].equals(bigInt(1)));
|
||||
assert(witness[1].equals(bigInt(0)));
|
||||
|
||||
witness = circuit.calculateWitness({ "in[0]": "1", "in[1]": "1" });
|
||||
assert(witness[0].equals(snarkjs.bigInt(1)));
|
||||
assert(witness[1].equals(snarkjs.bigInt(1)));
|
||||
witness = await circuit.calculateWitness({ "in":[1,1] }, true);
|
||||
assert(witness[0].equals(bigInt(1)));
|
||||
assert(witness[1].equals(bigInt(1)));
|
||||
|
||||
witness = circuit.calculateWitness({ "in[0]": "661", "in[1]": "660" });
|
||||
assert(witness[0].equals(snarkjs.bigInt(1)));
|
||||
assert(witness[1].equals(snarkjs.bigInt(1)));
|
||||
witness = await circuit.calculateWitness({ "in": [661, 660] }, true);
|
||||
assert(witness[0].equals(bigInt(1)));
|
||||
assert(witness[1].equals(bigInt(1)));
|
||||
|
||||
witness = circuit.calculateWitness({ "in[0]": "0", "in[1]": "1" });
|
||||
assert(witness[0].equals(snarkjs.bigInt(1)));
|
||||
assert(witness[1].equals(snarkjs.bigInt(0)));
|
||||
witness = await circuit.calculateWitness({ "in": [0, 1] }, true);
|
||||
assert(witness[0].equals(bigInt(1)));
|
||||
assert(witness[1].equals(bigInt(0)));
|
||||
|
||||
witness = circuit.calculateWitness({ "in[0]": "0", "in[1]": "444" });
|
||||
assert(witness[0].equals(snarkjs.bigInt(1)));
|
||||
assert(witness[1].equals(snarkjs.bigInt(0)));
|
||||
witness = await circuit.calculateWitness({ "in": [0, 444] }, true);
|
||||
assert(witness[0].equals(bigInt(1)));
|
||||
assert(witness[1].equals(bigInt(0)));
|
||||
|
||||
witness = circuit.calculateWitness({ "in[0]": "1", "in[1]": "0" });
|
||||
assert(witness[0].equals(snarkjs.bigInt(1)));
|
||||
assert(witness[1].equals(snarkjs.bigInt(1)));
|
||||
witness = await circuit.calculateWitness({ "in": [1, 0] }, true);
|
||||
assert(witness[0].equals(bigInt(1)));
|
||||
assert(witness[1].equals(bigInt(1)));
|
||||
|
||||
witness = circuit.calculateWitness({ "in[0]": "555", "in[1]": "0" });
|
||||
assert(witness[0].equals(snarkjs.bigInt(1)));
|
||||
assert(witness[1].equals(snarkjs.bigInt(1)));
|
||||
witness = await circuit.calculateWitness({ "in": [555, 0] }, true);
|
||||
assert(witness[0].equals(bigInt(1)));
|
||||
assert(witness[1].equals(bigInt(1)));
|
||||
|
||||
witness = circuit.calculateWitness({ "in[0]": "0", "in[1]": "0" });
|
||||
assert(witness[0].equals(snarkjs.bigInt(1)));
|
||||
assert(witness[1].equals(snarkjs.bigInt(1)));
|
||||
witness = await circuit.calculateWitness({ "in": [0, 0] }, true);
|
||||
assert(witness[0].equals(bigInt(1)));
|
||||
assert(witness[1].equals(bigInt(1)));
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,16 +1,14 @@
|
||||
const chai = require("chai");
|
||||
const path = require("path");
|
||||
const snarkjs = require("snarkjs");
|
||||
const compiler = require("circom");
|
||||
// const crypto = require("crypto");
|
||||
|
||||
const tester = require("circom").tester;
|
||||
const bigInt = require("big-integer");
|
||||
|
||||
const eddsa = require("../src/eddsa.js");
|
||||
const babyJub = require("../src/babyjub.js");
|
||||
|
||||
const assert = chai.assert;
|
||||
|
||||
const bigInt = snarkjs.bigInt;
|
||||
|
||||
function print(circuit, w, s) {
|
||||
console.log(s + ": " + w[circuit.getSignalIdx(s)]);
|
||||
}
|
||||
@@ -36,11 +34,7 @@ describe("EdDSA test", function () {
|
||||
this.timeout(100000);
|
||||
|
||||
before( async () => {
|
||||
const cirDef = await compiler(path.join(__dirname, "circuits", "eddsa_test.circom"));
|
||||
|
||||
circuit = new snarkjs.Circuit(cirDef);
|
||||
|
||||
console.log("NConstrains EdDSA: " + circuit.nConstraints);
|
||||
circuit = await tester(path.join(__dirname, "circuits", "eddsa_test.circom"));
|
||||
});
|
||||
|
||||
it("Sign a single 10 bytes from 0 to 9", async () => {
|
||||
@@ -66,9 +60,8 @@ describe("EdDSA test", function () {
|
||||
const sBits = buffer2bits(pSignature.slice(32, 64));
|
||||
const aBits = buffer2bits(pPubKey);
|
||||
|
||||
const w = circuit.calculateWitness({A: aBits, R8: r8Bits, S: sBits, msg: msgBits});
|
||||
|
||||
assert(circuit.checkWitness(w));
|
||||
const w = await circuit.calculateWitness({A: aBits, R8: r8Bits, S: sBits, msg: msgBits}, true);
|
||||
|
||||
await circuit.checkConstraints(w);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
const chai = require("chai");
|
||||
const snarkjs = require("snarkjs");
|
||||
|
||||
const eddsa = require("../src/eddsa.js");
|
||||
const babyJub = require("../src/babyjub.js");
|
||||
|
||||
const assert = chai.assert;
|
||||
|
||||
const bigInt = snarkjs.bigInt;
|
||||
const bigInt = require("big-integer");
|
||||
const utils = require("../src/utils.js");
|
||||
|
||||
describe("EdDSA js test", function () {
|
||||
|
||||
@@ -14,7 +14,7 @@ describe("EdDSA js test", function () {
|
||||
|
||||
it("Sign (using Mimc7) a single 10 bytes from 0 to 9", () => {
|
||||
const msgBuf = Buffer.from("00010203040506070809", "hex");
|
||||
const msg = bigInt.leBuff2int(msgBuf);
|
||||
const msg = utils.leBuff2int(msgBuf);
|
||||
|
||||
// const prvKey = crypto.randomBytes(32);
|
||||
|
||||
@@ -49,7 +49,7 @@ describe("EdDSA js test", function () {
|
||||
|
||||
it("Sign (using Poseidon) a single 10 bytes from 0 to 9", () => {
|
||||
const msgBuf = Buffer.from("00010203040506070809", "hex");
|
||||
const msg = bigInt.leBuff2int(msgBuf);
|
||||
const msg = utils.leBuff2int(msgBuf);
|
||||
|
||||
const prvKey = Buffer.from("0001020304050607080900010203040506070809000102030405060708090001", "hex");
|
||||
|
||||
|
||||
@@ -1,25 +1,20 @@
|
||||
const chai = require("chai");
|
||||
const path = require("path");
|
||||
const snarkjs = require("snarkjs");
|
||||
const compiler = require("circom");
|
||||
const tester = require("circom").tester;
|
||||
const bigInt = require("big-integer");
|
||||
|
||||
const eddsa = require("../src/eddsa.js");
|
||||
|
||||
const assert = chai.assert;
|
||||
|
||||
const bigInt = snarkjs.bigInt;
|
||||
|
||||
describe("EdDSA MiMC test", function () {
|
||||
let circuit;
|
||||
|
||||
this.timeout(100000);
|
||||
|
||||
before( async () => {
|
||||
const cirDef = await compiler(path.join(__dirname, "circuits", "eddsamimc_test.circom"));
|
||||
|
||||
circuit = new snarkjs.Circuit(cirDef);
|
||||
|
||||
console.log("NConstrains EdDSA MiMC: " + circuit.nConstraints);
|
||||
circuit = await tester(path.join(__dirname, "circuits", "eddsamimc_test.circom"));
|
||||
});
|
||||
|
||||
it("Sign a single number", async () => {
|
||||
@@ -33,16 +28,18 @@ describe("EdDSA MiMC test", function () {
|
||||
|
||||
assert(eddsa.verifyMiMC(msg, signature, pubKey));
|
||||
|
||||
const w = circuit.calculateWitness({
|
||||
const w = await circuit.calculateWitness({
|
||||
enabled: 1,
|
||||
Ax: pubKey[0],
|
||||
Ay: pubKey[1],
|
||||
R8x: signature.R8[0],
|
||||
R8y: signature.R8[1],
|
||||
S: signature.S,
|
||||
M: msg});
|
||||
M: msg}, true);
|
||||
|
||||
|
||||
await circuit.checkConstraints(w);
|
||||
|
||||
assert(circuit.checkWitness(w));
|
||||
});
|
||||
|
||||
it("Detect Invalid signature", async () => {
|
||||
@@ -57,14 +54,14 @@ describe("EdDSA MiMC test", function () {
|
||||
|
||||
assert(eddsa.verifyMiMC(msg, signature, pubKey));
|
||||
try {
|
||||
const w = circuit.calculateWitness({
|
||||
const w = await circuit.calculateWitness({
|
||||
enabled: 1,
|
||||
Ax: pubKey[0],
|
||||
Ay: pubKey[1],
|
||||
R8x: signature.R8[0].add(bigInt(1)),
|
||||
R8y: signature.R8[1],
|
||||
S: signature.S,
|
||||
M: msg});
|
||||
M: msg}, true);
|
||||
assert(false);
|
||||
} catch(err) {
|
||||
assert(/Constraint\sdoesn't\smatch(.*)1\s!=\s0/.test(err.message) );
|
||||
@@ -84,15 +81,16 @@ describe("EdDSA MiMC test", function () {
|
||||
|
||||
assert(eddsa.verifyMiMC(msg, signature, pubKey));
|
||||
|
||||
const w = circuit.calculateWitness({
|
||||
const w = await circuit.calculateWitness({
|
||||
enabled: 0,
|
||||
Ax: pubKey[0],
|
||||
Ay: pubKey[1],
|
||||
R8x: signature.R8[0].add(bigInt(1)),
|
||||
R8y: signature.R8[1],
|
||||
S: signature.S,
|
||||
M: msg});
|
||||
M: msg}, true);
|
||||
|
||||
await circuit.checkConstraints(w);
|
||||
|
||||
assert(circuit.checkWitness(w));
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,25 +1,21 @@
|
||||
const chai = require("chai");
|
||||
const path = require("path");
|
||||
const snarkjs = require("snarkjs");
|
||||
const compiler = require("circom");
|
||||
const bigInt = require("big-integer");
|
||||
const tester = require("circom").tester;
|
||||
|
||||
const eddsa = require("../src/eddsa.js");
|
||||
|
||||
const assert = chai.assert;
|
||||
|
||||
const bigInt = snarkjs.bigInt;
|
||||
|
||||
describe("EdDSA Poseidon test", function () {
|
||||
let circuit;
|
||||
|
||||
this.timeout(100000);
|
||||
|
||||
before( async () => {
|
||||
const cirDef = await compiler(path.join(__dirname, "circuits", "eddsaposeidon_test.circom"));
|
||||
|
||||
circuit = new snarkjs.Circuit(cirDef);
|
||||
circuit = await tester(path.join(__dirname, "circuits", "eddsaposeidon_test.circom"));
|
||||
|
||||
console.log("NConstrains EdDSA Poseidon: " + circuit.nConstraints);
|
||||
});
|
||||
|
||||
it("Sign a single number", async () => {
|
||||
@@ -33,16 +29,21 @@ describe("EdDSA Poseidon test", function () {
|
||||
|
||||
assert(eddsa.verifyPoseidon(msg, signature, pubKey));
|
||||
|
||||
const w = circuit.calculateWitness({
|
||||
const input = {
|
||||
enabled: 1,
|
||||
Ax: pubKey[0],
|
||||
Ay: pubKey[1],
|
||||
R8x: signature.R8[0],
|
||||
R8y: signature.R8[1],
|
||||
S: signature.S,
|
||||
M: msg});
|
||||
M: msg
|
||||
};
|
||||
|
||||
assert(circuit.checkWitness(w));
|
||||
// console.log(JSON.stringify(utils.stringifyBigInts(input)));
|
||||
|
||||
const w = await circuit.calculateWitness(input, true);
|
||||
|
||||
await circuit.checkConstraints(w);
|
||||
});
|
||||
|
||||
it("Detect Invalid signature", async () => {
|
||||
@@ -57,14 +58,14 @@ describe("EdDSA Poseidon test", function () {
|
||||
|
||||
assert(eddsa.verifyPoseidon(msg, signature, pubKey));
|
||||
try {
|
||||
circuit.calculateWitness({
|
||||
await circuit.calculateWitness({
|
||||
enabled: 1,
|
||||
Ax: pubKey[0],
|
||||
Ay: pubKey[1],
|
||||
R8x: signature.R8[0].add(bigInt(1)),
|
||||
R8y: signature.R8[1],
|
||||
S: signature.S,
|
||||
M: msg});
|
||||
M: msg}, true);
|
||||
assert(false);
|
||||
} catch(err) {
|
||||
assert(/Constraint\sdoesn't\smatch(.*)1\s!=\s0/.test(err.message) );
|
||||
@@ -84,15 +85,15 @@ describe("EdDSA Poseidon test", function () {
|
||||
|
||||
assert(eddsa.verifyPoseidon(msg, signature, pubKey));
|
||||
|
||||
const w = circuit.calculateWitness({
|
||||
const w = await circuit.calculateWitness({
|
||||
enabled: 0,
|
||||
Ax: pubKey[0],
|
||||
Ay: pubKey[1],
|
||||
R8x: signature.R8[0].add(bigInt(1)),
|
||||
R8y: signature.R8[1],
|
||||
S: signature.S,
|
||||
M: msg});
|
||||
M: msg}, true);
|
||||
|
||||
assert(circuit.checkWitness(w));
|
||||
await circuit.checkConstraints(w);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,172 +1,114 @@
|
||||
const chai = require("chai");
|
||||
const path = require("path");
|
||||
const snarkjs = require("snarkjs");
|
||||
const compiler = require("circom");
|
||||
const bigInt = require("big-integer");
|
||||
const tester = require("circom").tester;
|
||||
const babyJub = require("../src/babyjub.js");
|
||||
|
||||
const assert = chai.assert;
|
||||
|
||||
const bigInt = snarkjs.bigInt;
|
||||
|
||||
|
||||
const q=bigInt("21888242871839275222246405745257275088548364400416034343698204186575808495617");
|
||||
function addPoint(a,b) {
|
||||
const cta = bigInt("168700");
|
||||
const d = bigInt("168696");
|
||||
|
||||
const res = [];
|
||||
res[0] = bigInt((a[0]*b[1] + b[0]*a[1]) * bigInt(bigInt.one + d*a[0]*b[0]*a[1]*b[1]).inverse(q)).affine(q);
|
||||
res[1] = bigInt((a[1]*b[1] - cta*a[0]*b[0]) * bigInt(bigInt.one - d*a[0]*b[0]*a[1]*b[1]).inverse(q)).affine(q);
|
||||
return res;
|
||||
}
|
||||
|
||||
function print(circuit, w, s) {
|
||||
console.log(s + ": " + w[circuit.getSignalIdx(s)]);
|
||||
}
|
||||
|
||||
describe("Exponentioation test", () => {
|
||||
describe("Exponentioation test", function () {
|
||||
|
||||
this.timeout(100000);
|
||||
|
||||
it("Should generate the Exponentiation table in k=0", async () => {
|
||||
|
||||
const cirDef = await compiler(path.join(__dirname, "circuits", "escalarmulw4table_test.circom"));
|
||||
const circuit = await tester(path.join(__dirname, "circuits", "escalarmulw4table_test.circom"));
|
||||
|
||||
// console.log(JSON.stringify(cirDef, null, 1));
|
||||
const w = await circuit.calculateWitness({in: 1});
|
||||
|
||||
// assert.equal(cirDef.nVars, 2);
|
||||
await circuit.checkConstraints(w);
|
||||
|
||||
const circuit = new snarkjs.Circuit(cirDef);
|
||||
let g = [
|
||||
bigInt("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
|
||||
bigInt("16950150798460657717958625567821834550301663161624707787222815936182638968203")
|
||||
];
|
||||
|
||||
console.log("NConstrains: " + circuit.nConstraints);
|
||||
let dbl= [bigInt("0"), bigInt("1")];
|
||||
|
||||
const w = circuit.calculateWitness({in: 1});
|
||||
|
||||
assert(circuit.checkWitness(w));
|
||||
|
||||
let g = [bigInt("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
|
||||
bigInt("16950150798460657717958625567821834550301663161624707787222815936182638968203")]
|
||||
|
||||
dbl= [bigInt("0"), snarkjs.bigInt("1")];
|
||||
const expectedOut = [];
|
||||
|
||||
for (let i=0; i<16; i++) {
|
||||
const xout1 = w[circuit.getSignalIdx(`main.out[${i}][0]`)];
|
||||
const yout1 = w[circuit.getSignalIdx(`main.out[${i}][1]`)];
|
||||
|
||||
// console.log(xout1.toString());
|
||||
// console.log(yout1.toString());
|
||||
// console.log(dbl[0]);
|
||||
// console.log(dbl[1]);
|
||||
|
||||
assert(xout1.equals(dbl[0]));
|
||||
assert(yout1.equals(dbl[1]));
|
||||
|
||||
dbl = addPoint([xout1, yout1],g);
|
||||
expectedOut.push(dbl);
|
||||
dbl = babyJub.addPoint(dbl,g);
|
||||
}
|
||||
|
||||
await circuit.assertOut(w, {out: expectedOut});
|
||||
|
||||
});
|
||||
|
||||
it("Should generate the Exponentiation table in k=3", async () => {
|
||||
|
||||
const cirDef = await compiler(path.join(__dirname, "circuits", "escalarmulw4table_test3.circom"));
|
||||
const circuit = await tester(path.join(__dirname, "circuits", "escalarmulw4table_test3.circom"));
|
||||
|
||||
// console.log(JSON.stringify(cirDef, null, 1));
|
||||
const w = await circuit.calculateWitness({in: 1});
|
||||
|
||||
// assert.equal(cirDef.nVars, 2);
|
||||
await circuit.checkConstraints(w);
|
||||
|
||||
const circuit = new snarkjs.Circuit(cirDef);
|
||||
|
||||
console.log("NConstrains: " + circuit.nConstraints);
|
||||
|
||||
const w = circuit.calculateWitness({in: 1});
|
||||
|
||||
assert(circuit.checkWitness(w));
|
||||
|
||||
let g = [snarkjs.bigInt("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
|
||||
snarkjs.bigInt("16950150798460657717958625567821834550301663161624707787222815936182638968203")]
|
||||
let g = [
|
||||
bigInt("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
|
||||
bigInt("16950150798460657717958625567821834550301663161624707787222815936182638968203")
|
||||
];
|
||||
|
||||
for (let i=0; i<12;i++) {
|
||||
g = addPoint(g,g);
|
||||
g = babyJub.addPoint(g,g);
|
||||
}
|
||||
|
||||
dbl= [snarkjs.bigInt("0"), snarkjs.bigInt("1")];
|
||||
let dbl= [bigInt("0"), bigInt("1")];
|
||||
|
||||
const expectedOut = [];
|
||||
|
||||
for (let i=0; i<16; i++) {
|
||||
const xout1 = w[circuit.getSignalIdx(`main.out[${i}][0]`)];
|
||||
const yout1 = w[circuit.getSignalIdx(`main.out[${i}][1]`)];
|
||||
expectedOut.push(dbl);
|
||||
|
||||
|
||||
// console.log(xout1.toString());
|
||||
// console.log(yout1.toString());
|
||||
// console.log(dbl[0]);
|
||||
// console.log(dbl[1]);
|
||||
|
||||
assert(xout1.equals(dbl[0]));
|
||||
assert(yout1.equals(dbl[1]));
|
||||
|
||||
dbl = addPoint([xout1, yout1],g);
|
||||
dbl = babyJub.addPoint(dbl,g);
|
||||
}
|
||||
|
||||
await circuit.assertOut(w, {out: expectedOut});
|
||||
|
||||
});
|
||||
|
||||
it("Should exponentiate g^31", async () => {
|
||||
const cirDef = await compiler(path.join(__dirname, "circuits", "escalarmul_test.circom"), {reduceConstraints: true});
|
||||
|
||||
// console.log(JSON.stringify(cirDef, null, 1));
|
||||
const circuit = await tester(path.join(__dirname, "circuits", "escalarmul_test.circom"));
|
||||
|
||||
// assert.equal(cirDef.nVars, 2);
|
||||
const w = await circuit.calculateWitness({"in": 31});
|
||||
|
||||
const circuit = new snarkjs.Circuit(cirDef);
|
||||
await circuit.checkConstraints(w);
|
||||
|
||||
console.log("NConstrains: " + circuit.nConstraints);
|
||||
let g = [
|
||||
bigInt("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
|
||||
bigInt("16950150798460657717958625567821834550301663161624707787222815936182638968203")
|
||||
];
|
||||
|
||||
const w = circuit.calculateWitness({"in": 31});
|
||||
|
||||
assert(circuit.checkWitness(w));
|
||||
|
||||
let g = [snarkjs.bigInt("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
|
||||
snarkjs.bigInt("16950150798460657717958625567821834550301663161624707787222815936182638968203")]
|
||||
|
||||
let c = [0n, 1n];
|
||||
let c = [bigInt(0), bigInt(1)];
|
||||
|
||||
for (let i=0; i<31;i++) {
|
||||
c = addPoint(c,g);
|
||||
c = babyJub.addPoint(c,g);
|
||||
}
|
||||
|
||||
const xout = w[circuit.getSignalIdx(`main.out[0]`)];
|
||||
const yout = w[circuit.getSignalIdx(`main.out[1]`)];
|
||||
await circuit.assertOut(w, {out: c});
|
||||
|
||||
/*
|
||||
console.log(xout.toString());
|
||||
console.log(yout.toString());
|
||||
*/
|
||||
assert(xout.equals(c[0]));
|
||||
assert(yout.equals(c[1]));
|
||||
|
||||
console.log("-------")
|
||||
const w2 = circuit.calculateWitness({"in": (1n<<252n)+1n});
|
||||
|
||||
const xout2 = w2[circuit.getSignalIdx(`main.out[0]`)];
|
||||
const yout2 = w2[circuit.getSignalIdx(`main.out[1]`)];
|
||||
const w2 = await circuit.calculateWitness({"in": bigInt(1).shiftLeft(252).add(bigInt.one)});
|
||||
|
||||
c = [g[0], g[1]];
|
||||
for (let i=0; i<252;i++) {
|
||||
c = addPoint(c,c);
|
||||
c = babyJub.addPoint(c,c);
|
||||
}
|
||||
c = addPoint(c,g);
|
||||
c = babyJub.addPoint(c,g);
|
||||
|
||||
// console.log(xout2.toString());
|
||||
// console.log(yout2.toString());
|
||||
// console.log(c[0].toString());
|
||||
// console.log(c[1].toString());
|
||||
|
||||
assert(xout2.equals(c[0]));
|
||||
assert(yout2.equals(c[1]));
|
||||
await circuit.assertOut(w2, {out: c});
|
||||
|
||||
}).timeout(10000000);
|
||||
|
||||
it("Number of constrains for 256 bits", async () => {
|
||||
const cirDef = await compiler(path.join(__dirname, "circuits", "escalarmul_test_min.circom"));
|
||||
|
||||
const circuit = new snarkjs.Circuit(cirDef);
|
||||
const circuit = await tester(path.join(__dirname, "circuits", "escalarmul_test_min.circom"));
|
||||
|
||||
console.log("NConstrains: " + circuit.nConstraints);
|
||||
}).timeout(10000000);
|
||||
|
||||
});
|
||||
|
||||
@@ -1,12 +1,7 @@
|
||||
const chai = require("chai");
|
||||
const path = require("path");
|
||||
const snarkjs = require("snarkjs");
|
||||
const compiler = require("circom");
|
||||
|
||||
const assert = chai.assert;
|
||||
|
||||
const bigInt = snarkjs.bigInt;
|
||||
|
||||
const bigInt = require("big-integer");
|
||||
const tester = require("circom").tester;
|
||||
|
||||
function print(circuit, w, s) {
|
||||
console.log(s + ": " + w[circuit.getSignalIdx(s)]);
|
||||
@@ -18,41 +13,33 @@ describe("Escalarmul test", function () {
|
||||
this.timeout(100000);
|
||||
|
||||
let g = [
|
||||
snarkjs.bigInt("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
|
||||
snarkjs.bigInt("16950150798460657717958625567821834550301663161624707787222815936182638968203")
|
||||
bigInt("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
|
||||
bigInt("16950150798460657717958625567821834550301663161624707787222815936182638968203")
|
||||
];
|
||||
|
||||
before( async() => {
|
||||
const cirDefEMulAny = await compiler(path.join(__dirname, "circuits", "escalarmulany_test.circom"));
|
||||
circuitEMulAny = new snarkjs.Circuit(cirDefEMulAny);
|
||||
console.log("NConstrains Escalarmul any: " + circuitEMulAny.nConstraints);
|
||||
circuitEMulAny = await tester(path.join(__dirname, "circuits", "escalarmulany_test.circom"));
|
||||
});
|
||||
|
||||
it("Should generate Same escalar mul", async () => {
|
||||
|
||||
const w = circuitEMulAny.calculateWitness({"e": 1, "p": g});
|
||||
const w = await circuitEMulAny.calculateWitness({"e": 1, "p": g});
|
||||
|
||||
assert(circuitEMulAny.checkWitness(w));
|
||||
await circuitEMulAny.checkConstraints(w);
|
||||
|
||||
const xout = w[circuitEMulAny.getSignalIdx("main.out[0]")];
|
||||
const yout = w[circuitEMulAny.getSignalIdx("main.out[1]")];
|
||||
await circuitEMulAny.assertOut(w, {out: g}, true);
|
||||
|
||||
assert(xout.equals(g[0]));
|
||||
assert(yout.equals(g[1]));
|
||||
});
|
||||
|
||||
it("If multiply by order should return 0", async () => {
|
||||
|
||||
const r = bigInt("2736030358979909402780800718157159386076813972158567259200215660948447373041");
|
||||
const w = circuitEMulAny.calculateWitness({"e": r, "p": g});
|
||||
const w = await circuitEMulAny.calculateWitness({"e": r, "p": g});
|
||||
|
||||
assert(circuitEMulAny.checkWitness(w));
|
||||
await circuitEMulAny.checkConstraints(w);
|
||||
|
||||
const xout = w[circuitEMulAny.getSignalIdx("main.out[0]")];
|
||||
const yout = w[circuitEMulAny.getSignalIdx("main.out[1]")];
|
||||
await circuitEMulAny.assertOut(w, {out: [0,1]}, true);
|
||||
|
||||
assert(xout.equals(bigInt.zero));
|
||||
assert(yout.equals(bigInt.one));
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
@@ -1,14 +1,11 @@
|
||||
const chai = require("chai");
|
||||
const path = require("path");
|
||||
const snarkjs = require("snarkjs");
|
||||
const compiler = require("circom");
|
||||
const bigInt = require("big-integer");
|
||||
const tester = require("circom").tester;
|
||||
const babyjub = require("../src/babyjub");
|
||||
|
||||
const assert = chai.assert;
|
||||
|
||||
const bigInt = snarkjs.bigInt;
|
||||
|
||||
|
||||
function print(circuit, w, s) {
|
||||
console.log(s + ": " + w[circuit.getSignalIdx(s)]);
|
||||
}
|
||||
@@ -19,35 +16,27 @@ describe("Escalarmul test", function () {
|
||||
this.timeout(100000);
|
||||
|
||||
before( async() => {
|
||||
const cirDef = await compiler(path.join(__dirname, "circuits", "escalarmulfix_test.circom"));
|
||||
circuit = new snarkjs.Circuit(cirDef);
|
||||
console.log("NConstrains Escalarmul fix: " + circuit.nConstraints);
|
||||
circuit = await tester(path.join(__dirname, "circuits", "escalarmulfix_test.circom"));
|
||||
});
|
||||
|
||||
it("Should generate Same escalar mul", async () => {
|
||||
|
||||
const w = circuit.calculateWitness({"e": 0});
|
||||
const w = await circuit.calculateWitness({"e": 0});
|
||||
|
||||
assert(circuit.checkWitness(w));
|
||||
await circuit.checkConstraints(w);
|
||||
|
||||
const xout = w[circuit.getSignalIdx("main.out[0]")];
|
||||
const yout = w[circuit.getSignalIdx("main.out[1]")];
|
||||
await circuit.assertOut(w, {out: [0,1]}, true);
|
||||
|
||||
assert(xout.equals(0));
|
||||
assert(yout.equals(1));
|
||||
});
|
||||
|
||||
it("Should generate Same escalar mul", async () => {
|
||||
|
||||
const w = circuit.calculateWitness({"e": 1});
|
||||
const w = await circuit.calculateWitness({"e": 1}, true);
|
||||
|
||||
assert(circuit.checkWitness(w));
|
||||
await circuit.checkConstraints(w);
|
||||
|
||||
const xout = w[circuit.getSignalIdx("main.out[0]")];
|
||||
const yout = w[circuit.getSignalIdx("main.out[1]")];
|
||||
await circuit.assertOut(w, {out: babyjub.Base8});
|
||||
|
||||
assert(xout.equals(babyjub.Base8[0]));
|
||||
assert(yout.equals(babyjub.Base8[1]));
|
||||
});
|
||||
|
||||
it("Should generate scalar mul of a specific constant", async () => {
|
||||
@@ -58,17 +47,14 @@ describe("Escalarmul test", function () {
|
||||
bigInt("16950150798460657717958625567821834550301663161624707787222815936182638968203")
|
||||
];
|
||||
|
||||
const w = circuit.calculateWitness({"e": s});
|
||||
const w = await circuit.calculateWitness({"e": s}, true);
|
||||
|
||||
assert(circuit.checkWitness(w));
|
||||
|
||||
const xout = w[circuit.getSignalIdx("main.out[0]")];
|
||||
const yout = w[circuit.getSignalIdx("main.out[1]")];
|
||||
await circuit.checkConstraints(w);
|
||||
|
||||
const expectedRes = babyjub.mulPointEscalar(base8, s);
|
||||
|
||||
assert(xout.equals(expectedRes[0]));
|
||||
assert(yout.equals(expectedRes[1]));
|
||||
await circuit.assertOut(w, {out: expectedRes});
|
||||
|
||||
});
|
||||
|
||||
it("Should generate scalar mul of the firsts 50 elements", async () => {
|
||||
@@ -81,31 +67,23 @@ describe("Escalarmul test", function () {
|
||||
for (let i=0; i<50; i++) {
|
||||
const s = bigInt(i);
|
||||
|
||||
const w = circuit.calculateWitness({"e": s});
|
||||
const w = await circuit.calculateWitness({"e": s}, true);
|
||||
|
||||
assert(circuit.checkWitness(w));
|
||||
|
||||
const xout = w[circuit.getSignalIdx("main.out[0]")];
|
||||
const yout = w[circuit.getSignalIdx("main.out[1]")];
|
||||
await circuit.checkConstraints(w);
|
||||
|
||||
const expectedRes = babyjub.mulPointEscalar(base8, s);
|
||||
|
||||
assert(xout.equals(expectedRes[0]));
|
||||
assert(yout.equals(expectedRes[1]));
|
||||
await circuit.assertOut(w, {out: expectedRes});
|
||||
}
|
||||
});
|
||||
|
||||
it("If multiply by order should return 0", async () => {
|
||||
|
||||
const w = circuit.calculateWitness({"e": babyjub.subOrder });
|
||||
const w = await circuit.calculateWitness({"e": babyjub.subOrder }, true);
|
||||
|
||||
assert(circuit.checkWitness(w));
|
||||
await circuit.checkConstraints(w);
|
||||
|
||||
const xout = w[circuit.getSignalIdx("main.out[0]")];
|
||||
const yout = w[circuit.getSignalIdx("main.out[1]")];
|
||||
|
||||
assert(xout.equals(bigInt.zero));
|
||||
assert(yout.equals(bigInt.one));
|
||||
await circuit.assertOut(w, {out: [0,1]});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
@@ -1,35 +1,25 @@
|
||||
const chai = require("chai");
|
||||
const path = require("path");
|
||||
const snarkjs = require("snarkjs");
|
||||
const compiler = require("circom");
|
||||
const tester = require("circom").tester;
|
||||
|
||||
const mimcjs = require("../src/mimc7.js");
|
||||
|
||||
const assert = chai.assert;
|
||||
|
||||
describe("MiMC Circuit test", function () {
|
||||
let circuit;
|
||||
|
||||
this.timeout(100000);
|
||||
|
||||
before( async () => {
|
||||
const cirDef = await compiler(path.join(__dirname, "circuits", "mimc_test.circom"));
|
||||
|
||||
circuit = new snarkjs.Circuit(cirDef);
|
||||
|
||||
console.log("MiMC constraints: " + circuit.nConstraints);
|
||||
circuit = await tester(path.join(__dirname, "circuits", "mimc_test.circom"));
|
||||
});
|
||||
|
||||
it("Should check constrain", async () => {
|
||||
const w = circuit.calculateWitness({x_in: 1, k: 2});
|
||||
|
||||
const res = w[circuit.getSignalIdx("main.out")];
|
||||
const w = await circuit.calculateWitness({x_in: 1, k: 2}, true);
|
||||
|
||||
const res2 = mimcjs.hash(1,2,91);
|
||||
|
||||
assert.equal(res.toString(), res2.toString());
|
||||
|
||||
assert(circuit.checkWitness(w));
|
||||
await circuit.assertOut(w, {out: res2});
|
||||
|
||||
await circuit.checkConstraints(w);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,11 +1,8 @@
|
||||
const chai = require("chai");
|
||||
const path = require("path");
|
||||
const snarkjs = require("snarkjs");
|
||||
const compiler = require("circom");
|
||||
const tester = require("circom").tester;
|
||||
|
||||
const mimcjs = require("../src/mimcsponge.js");
|
||||
|
||||
const assert = chai.assert;
|
||||
|
||||
describe("MiMC Sponge Circuit test", function () {
|
||||
let circuit;
|
||||
@@ -13,46 +10,28 @@ describe("MiMC Sponge Circuit test", function () {
|
||||
this.timeout(100000);
|
||||
|
||||
it("Should check permutation", async () => {
|
||||
const cirDef = await compiler(path.join(__dirname, "circuits", "mimc_sponge_test.circom"));
|
||||
|
||||
circuit = new snarkjs.Circuit(cirDef);
|
||||
circuit = await tester(path.join(__dirname, "circuits", "mimc_sponge_test.circom"));
|
||||
|
||||
console.log("MiMC Feistel constraints: " + circuit.nConstraints);
|
||||
|
||||
const w = circuit.calculateWitness({xL_in: 1, xR_in: 2, k: 3});
|
||||
|
||||
const xLout = w[circuit.getSignalIdx("main.xL_out")];
|
||||
const xRout = w[circuit.getSignalIdx("main.xR_out")];
|
||||
const w = await circuit.calculateWitness({xL_in: 1, xR_in: 2, k: 3});
|
||||
|
||||
const out2 = mimcjs.hash(1,2,3);
|
||||
|
||||
assert.equal(xLout.toString(), out2.xL.toString());
|
||||
assert.equal(xRout.toString(), out2.xR.toString());
|
||||
await circuit.assertOut(w, {xL_out: out2.xL, xR_out: out2.xR});
|
||||
|
||||
assert(circuit.checkWitness(w));
|
||||
await circuit.checkConstraints(w);
|
||||
|
||||
});
|
||||
|
||||
it("Should check hash", async () => {
|
||||
const cirDef = await compiler(path.join(__dirname, "circuits", "mimc_sponge_hash_test.circom"));
|
||||
circuit = await tester(path.join(__dirname, "circuits", "mimc_sponge_hash_test.circom"));
|
||||
|
||||
circuit = new snarkjs.Circuit(cirDef);
|
||||
|
||||
console.log("MiMC Sponge constraints: " + circuit.nConstraints);
|
||||
|
||||
const w = circuit.calculateWitness({ins: [1, 2], k: 0});
|
||||
|
||||
const o1 = w[circuit.getSignalIdx("main.outs[0]")];
|
||||
const o2 = w[circuit.getSignalIdx("main.outs[1]")];
|
||||
const o3 = w[circuit.getSignalIdx("main.outs[2]")];
|
||||
const w = await circuit.calculateWitness({ins: [1, 2], k: 0});
|
||||
|
||||
const out2 = mimcjs.multiHash([1,2], 0, 3);
|
||||
|
||||
assert.equal(o1.toString(), out2[0].toString());
|
||||
assert.equal(o2.toString(), out2[1].toString());
|
||||
assert.equal(o3.toString(), out2[2].toString());
|
||||
|
||||
assert(circuit.checkWitness(w));
|
||||
await circuit.assertOut(w, {outs: out2});
|
||||
|
||||
await circuit.checkConstraints(w);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,13 +1,11 @@
|
||||
const chai = require("chai");
|
||||
const path = require("path");
|
||||
const snarkjs = require("snarkjs");
|
||||
const compiler = require("circom");
|
||||
const bigInt = require("big-integer");
|
||||
const tester = require("circom").tester;
|
||||
const babyJub = require("../src/babyjub.js");
|
||||
|
||||
const assert = chai.assert;
|
||||
|
||||
const bigInt = snarkjs.bigInt;
|
||||
|
||||
describe("Montgomery test", function () {
|
||||
let circuitE2M;
|
||||
let circuitM2E;
|
||||
@@ -15,43 +13,37 @@ describe("Montgomery test", function () {
|
||||
let circuitMDouble;
|
||||
|
||||
let g = [
|
||||
snarkjs.bigInt("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
|
||||
snarkjs.bigInt("16950150798460657717958625567821834550301663161624707787222815936182638968203")];
|
||||
bigInt("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
|
||||
bigInt("16950150798460657717958625567821834550301663161624707787222815936182638968203")
|
||||
];
|
||||
|
||||
let mg, mg2, g2, g3, mg3;
|
||||
|
||||
this.timeout(100000);
|
||||
before( async() => {
|
||||
const cirDefE2M = await compiler(path.join(__dirname, "circuits", "edwards2montgomery.circom"));
|
||||
circuitE2M = new snarkjs.Circuit(cirDefE2M);
|
||||
console.log("NConstrains Edwards -> Montgomery: " + circuitE2M.nConstraints);
|
||||
|
||||
const cirDefM2E = await compiler(path.join(__dirname, "circuits", "montgomery2edwards.circom"));
|
||||
circuitM2E = new snarkjs.Circuit(cirDefM2E);
|
||||
console.log("NConstrains Montgomery -> Edwards: " + circuitM2E.nConstraints);
|
||||
|
||||
const cirDefMAdd = await compiler(path.join(__dirname, "circuits", "montgomeryadd.circom"));
|
||||
circuitMAdd = new snarkjs.Circuit(cirDefMAdd);
|
||||
console.log("NConstrains Montgomery Add: " + circuitMAdd.nConstraints);
|
||||
|
||||
const cirDefMDouble = await compiler(path.join(__dirname, "circuits", "montgomerydouble.circom"));
|
||||
circuitMDouble = new snarkjs.Circuit(cirDefMDouble);
|
||||
console.log("NConstrains Montgomery Double: " + circuitMDouble.nConstraints);
|
||||
circuitE2M = await tester(path.join(__dirname, "circuits", "edwards2montgomery.circom"));
|
||||
await circuitE2M.loadSymbols();
|
||||
circuitM2E = await tester(path.join(__dirname, "circuits", "montgomery2edwards.circom"));
|
||||
await circuitM2E.loadSymbols();
|
||||
circuitMAdd = await tester(path.join(__dirname, "circuits", "montgomeryadd.circom"));
|
||||
await circuitMAdd.loadSymbols();
|
||||
circuitMDouble = await tester(path.join(__dirname, "circuits", "montgomerydouble.circom"));
|
||||
await circuitMDouble.loadSymbols();
|
||||
});
|
||||
it("Convert Edwards to Montgomery and back again", async () => {
|
||||
let w, xout, yout;
|
||||
|
||||
w = circuitE2M.calculateWitness({ in: g});
|
||||
w = await circuitE2M.calculateWitness({ in: g}, true);
|
||||
|
||||
xout = w[circuitE2M.getSignalIdx("main.out[0]")];
|
||||
yout = w[circuitE2M.getSignalIdx("main.out[1]")];
|
||||
xout = w[circuitE2M.symbols["main.out[0]"].varIdx];
|
||||
yout = w[circuitE2M.symbols["main.out[1]"].varIdx];
|
||||
|
||||
mg = [xout, yout];
|
||||
|
||||
w = circuitM2E.calculateWitness({ in: [xout, yout]});
|
||||
w = await circuitM2E.calculateWitness({ in: [xout, yout]}, true);
|
||||
|
||||
xout = w[circuitM2E.getSignalIdx("main.out[0]")];
|
||||
yout = w[circuitM2E.getSignalIdx("main.out[1]")];
|
||||
xout = w[circuitM2E.symbols["main.out[0]"].varIdx];
|
||||
yout = w[circuitM2E.symbols["main.out[1]"].varIdx];
|
||||
|
||||
assert(xout.equals(g[0]));
|
||||
assert(yout.equals(g[1]));
|
||||
@@ -61,17 +53,17 @@ describe("Montgomery test", function () {
|
||||
|
||||
g2 = babyJub.addPoint(g,g);
|
||||
|
||||
w = circuitMDouble.calculateWitness({ in: mg});
|
||||
w = await circuitMDouble.calculateWitness({ in: mg}, true);
|
||||
|
||||
xout = w[circuitE2M.getSignalIdx("main.out[0]")];
|
||||
yout = w[circuitE2M.getSignalIdx("main.out[1]")];
|
||||
xout = w[circuitE2M.symbols["main.out[0]"].varIdx];
|
||||
yout = w[circuitE2M.symbols["main.out[1]"].varIdx];
|
||||
|
||||
mg2 = [xout, yout];
|
||||
|
||||
w = circuitM2E.calculateWitness({ in: mg2});
|
||||
w = await circuitM2E.calculateWitness({ in: mg2}, true);
|
||||
|
||||
xout = w[circuitM2E.getSignalIdx("main.out[0]")];
|
||||
yout = w[circuitM2E.getSignalIdx("main.out[1]")];
|
||||
xout = w[circuitM2E.symbols["main.out[0]"].varIdx];
|
||||
yout = w[circuitM2E.symbols["main.out[1]"].varIdx];
|
||||
|
||||
assert(xout.equals(g2[0]));
|
||||
assert(yout.equals(g2[1]));
|
||||
@@ -81,17 +73,17 @@ describe("Montgomery test", function () {
|
||||
|
||||
g3 = babyJub.addPoint(g,g2);
|
||||
|
||||
w = circuitMAdd.calculateWitness({ in1: mg, in2: mg2});
|
||||
w = await circuitMAdd.calculateWitness({ in1: mg, in2: mg2}, true);
|
||||
|
||||
xout = w[circuitMAdd.getSignalIdx("main.out[0]")];
|
||||
yout = w[circuitMAdd.getSignalIdx("main.out[1]")];
|
||||
xout = w[circuitMAdd.symbols["main.out[0]"].varIdx];
|
||||
yout = w[circuitMAdd.symbols["main.out[1]"].varIdx];
|
||||
|
||||
mg3 = [xout, yout];
|
||||
|
||||
w = circuitM2E.calculateWitness({ in: mg3});
|
||||
w = await circuitM2E.calculateWitness({ in: mg3}, true);
|
||||
|
||||
xout = w[circuitM2E.getSignalIdx("main.out[0]")];
|
||||
yout = w[circuitM2E.getSignalIdx("main.out[1]")];
|
||||
xout = w[circuitM2E.symbols["main.out[0]"].varIdx];
|
||||
yout = w[circuitM2E.symbols["main.out[1]"].varIdx];
|
||||
|
||||
assert(xout.equals(g3[0]));
|
||||
assert(yout.equals(g3[1]));
|
||||
|
||||
@@ -1,25 +1,12 @@
|
||||
const chai = require("chai");
|
||||
const path = require("path");
|
||||
const snarkjs = require("snarkjs");
|
||||
const compiler = require("circom");
|
||||
const bigInt = require("big-integer");
|
||||
const tester = require("circom").tester;
|
||||
|
||||
const assert = chai.assert;
|
||||
|
||||
const bigInt = snarkjs.bigInt;
|
||||
|
||||
|
||||
describe("Mux4 test", () => {
|
||||
describe("Mux4 test", function() {
|
||||
this.timeout(100000);
|
||||
it("Should create a constant multiplexer 4", async () => {
|
||||
|
||||
const cirDef = await compiler(path.join(__dirname, "circuits", "mux4_1.circom"));
|
||||
|
||||
// console.log(JSON.stringify(cirDef, null, 1));
|
||||
|
||||
// assert.equal(cirDef.nVars, 2);
|
||||
|
||||
const circuit = new snarkjs.Circuit(cirDef);
|
||||
|
||||
console.log("NConstrains Mux4: " + circuit.nConstraints);
|
||||
const circuit = await tester(path.join(__dirname, "circuits", "mux4_1.circom"));
|
||||
|
||||
const ct16 = [
|
||||
bigInt("123"),
|
||||
@@ -41,24 +28,17 @@ describe("Mux4 test", () => {
|
||||
];
|
||||
|
||||
for (let i=0; i<16; i++) {
|
||||
const w = circuit.calculateWitness({ "selector": i });
|
||||
const w = await circuit.calculateWitness({ "selector": i }, true);
|
||||
|
||||
assert(circuit.checkWitness(w));
|
||||
await circuit.checkConstraints(w);
|
||||
|
||||
assert(w[0].equals(bigInt(1)));
|
||||
|
||||
// console.log(i + " -> " + w[circuit.getSignalIdx("main.out")].toString());
|
||||
assert(w[circuit.getSignalIdx("main.out")].equals(ct16[i]));
|
||||
await circuit.assertOut(w, {out: ct16[i]});
|
||||
}
|
||||
});
|
||||
|
||||
it("Should create a constant multiplexer 3", async () => {
|
||||
|
||||
const cirDef = await compiler(path.join(__dirname, "circuits", "mux3_1.circom"));
|
||||
|
||||
const circuit = new snarkjs.Circuit(cirDef);
|
||||
|
||||
console.log("NConstrains Mux3: " + circuit.nConstraints);
|
||||
const circuit = await tester(path.join(__dirname, "circuits", "mux3_1.circom"));
|
||||
|
||||
const ct8 = [
|
||||
bigInt("37"),
|
||||
@@ -72,23 +52,18 @@ describe("Mux4 test", () => {
|
||||
];
|
||||
|
||||
for (let i=0; i<8; i++) {
|
||||
const w = circuit.calculateWitness({ "selector": i });
|
||||
const w = await circuit.calculateWitness({ "selector": i }, true);
|
||||
|
||||
assert(w[0].equals(bigInt(1)));
|
||||
await circuit.checkConstraints(w);
|
||||
|
||||
// console.log(i + " -> " + w[circuit.getSignalIdx("main.out")].toString());
|
||||
assert(w[circuit.getSignalIdx("main.out")].equals(ct8[i]));
|
||||
await circuit.assertOut(w, {out: ct8[i]});
|
||||
}
|
||||
});
|
||||
it("Should create a constant multiplexer 2", async () => {
|
||||
|
||||
const cirDef = await compiler(path.join(__dirname, "circuits", "mux2_1.circom"));
|
||||
const circuit = await tester(path.join(__dirname, "circuits", "mux2_1.circom"));
|
||||
|
||||
const circuit = new snarkjs.Circuit(cirDef);
|
||||
|
||||
console.log("NConstrains Mux2: " + circuit.nConstraints);
|
||||
|
||||
const ct8 = [
|
||||
const ct4 = [
|
||||
bigInt("37"),
|
||||
bigInt("47"),
|
||||
bigInt("53"),
|
||||
@@ -96,38 +71,28 @@ describe("Mux4 test", () => {
|
||||
];
|
||||
|
||||
for (let i=0; i<4; i++) {
|
||||
const w = circuit.calculateWitness({ "selector": i });
|
||||
const w = await circuit.calculateWitness({ "selector": i }, true);
|
||||
|
||||
assert(circuit.checkWitness(w));
|
||||
await circuit.checkConstraints(w);
|
||||
|
||||
assert(w[0].equals(bigInt(1)));
|
||||
|
||||
// console.log(i + " -> " + w[circuit.getSignalIdx("main.out")].toString());
|
||||
assert(w[circuit.getSignalIdx("main.out")].equals(ct8[i]));
|
||||
await circuit.assertOut(w, {out: ct4[i]});
|
||||
}
|
||||
});
|
||||
it("Should create a constant multiplexer 1", async () => {
|
||||
|
||||
const cirDef = await compiler(path.join(__dirname, "circuits", "mux1_1.circom"));
|
||||
const circuit = await tester(path.join(__dirname, "circuits", "mux1_1.circom"));
|
||||
|
||||
const circuit = new snarkjs.Circuit(cirDef);
|
||||
|
||||
console.log("NConstrains Mux1: " + circuit.nConstraints);
|
||||
|
||||
const ct8 = [
|
||||
const ct2 = [
|
||||
bigInt("37"),
|
||||
bigInt("47"),
|
||||
];
|
||||
|
||||
for (let i=0; i<2; i++) {
|
||||
const w = circuit.calculateWitness({ "selector": i });
|
||||
const w = await circuit.calculateWitness({ "selector": i }, true);
|
||||
|
||||
assert(circuit.checkWitness(w));
|
||||
await circuit.checkConstraints(w);
|
||||
|
||||
assert(w[0].equals(bigInt(1)));
|
||||
|
||||
// console.log(i + " -> " + w[circuit.getSignalIdx("main.out")].toString());
|
||||
assert(w[circuit.getSignalIdx("main.out")].equals(ct8[i]));
|
||||
await circuit.assertOut(w, {out: ct2[i]});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,11 +1,8 @@
|
||||
const chai = require("chai");
|
||||
const path = require("path");
|
||||
const snarkjs = require("snarkjs");
|
||||
const compiler = require("circom");
|
||||
|
||||
const assert = chai.assert;
|
||||
|
||||
const bigInt = snarkjs.bigInt;
|
||||
const bigInt = require("big-integer");
|
||||
const tester = require("circom").tester;
|
||||
|
||||
const babyJub = require("../src/babyjub.js");
|
||||
|
||||
@@ -22,79 +19,59 @@ describe("Double Pedersen test", function() {
|
||||
let circuit;
|
||||
this.timeout(100000);
|
||||
before( async() => {
|
||||
const cirDef = await compiler(path.join(__dirname, "circuits", "pedersen_test.circom"));
|
||||
|
||||
circuit = new snarkjs.Circuit(cirDef);
|
||||
circuit = await tester(path.join(__dirname, "circuits", "pedersen_test.circom"));
|
||||
|
||||
console.log("NConstrains: " + circuit.nConstraints);
|
||||
});
|
||||
it("Should pedersen at zero", async () => {
|
||||
|
||||
let w, xout, yout;
|
||||
let w;
|
||||
|
||||
w = circuit.calculateWitness({ in: ["0", "0"]});
|
||||
w = await circuit.calculateWitness({ in: ["0", "0"]}, true);
|
||||
|
||||
xout = w[circuit.getSignalIdx("main.out[0]")];
|
||||
yout = w[circuit.getSignalIdx("main.out[1]")];
|
||||
await circuit.assertOut(w, {out: [0,1]});
|
||||
|
||||
assert(xout.equals("0"));
|
||||
assert(yout.equals("1"));
|
||||
});
|
||||
it("Should pedersen at one first generator", async () => {
|
||||
let w, xout, yout;
|
||||
let w;
|
||||
|
||||
w = circuit.calculateWitness({ in: ["1", "0"]});
|
||||
w = await circuit.calculateWitness({ in: ["1", "0"]}, true);
|
||||
|
||||
xout = bigInt(w[circuit.getSignalIdx("main.out[0]")]);
|
||||
yout = bigInt(w[circuit.getSignalIdx("main.out[1]")]);
|
||||
await circuit.assertOut(w, {out: PBASE[0]});
|
||||
|
||||
assert(xout.equals(PBASE[0][0]));
|
||||
assert(yout.equals(PBASE[0][1]));
|
||||
});
|
||||
it("Should pedersen at one second generator", async () => {
|
||||
let w, xout, yout;
|
||||
let w;
|
||||
|
||||
w = circuit.calculateWitness({ in: ["0", "1"]});
|
||||
w = await circuit.calculateWitness({ in: ["0", "1"]}, true);
|
||||
|
||||
xout = w[circuit.getSignalIdx("main.out[0]")];
|
||||
yout = w[circuit.getSignalIdx("main.out[1]")];
|
||||
|
||||
assert(xout.equals(PBASE[1][0]));
|
||||
assert(yout.equals(PBASE[1][1]));
|
||||
await circuit.assertOut(w, {out: PBASE[1]});
|
||||
|
||||
});
|
||||
it("Should pedersen at mixed generators", async () => {
|
||||
let w, xout, yout;
|
||||
w = circuit.calculateWitness({ in: ["3", "7"]});
|
||||
|
||||
xout = w[circuit.getSignalIdx("main.out[0]")];
|
||||
yout = w[circuit.getSignalIdx("main.out[1]")];
|
||||
|
||||
let w;
|
||||
w = await circuit.calculateWitness({ in: ["3", "7"]}, true);
|
||||
|
||||
const r = babyJub.addPoint(
|
||||
babyJub.mulPointEscalar(PBASE[0], 3),
|
||||
babyJub.mulPointEscalar(PBASE[1], 7)
|
||||
);
|
||||
|
||||
assert(xout.equals(r[0]));
|
||||
assert(yout.equals(r[1]));
|
||||
await circuit.assertOut(w, {out: r});
|
||||
|
||||
});
|
||||
it("Should pedersen all ones", async () => {
|
||||
let w, xout, yout;
|
||||
let w;
|
||||
|
||||
const allOnes = bigInt("1").shl(250).sub(bigInt("1"));
|
||||
w = circuit.calculateWitness({ in: [allOnes, allOnes]});
|
||||
const allOnes = bigInt("1").shiftLeft(250).minus(bigInt("1"));
|
||||
w = await circuit.calculateWitness({ in: [allOnes, allOnes]}, true);
|
||||
|
||||
xout = w[circuit.getSignalIdx("main.out[0]")];
|
||||
yout = w[circuit.getSignalIdx("main.out[1]")];
|
||||
|
||||
const r2 = babyJub.addPoint(
|
||||
babyJub.mulPointEscalar(PBASE[0], allOnes),
|
||||
babyJub.mulPointEscalar(PBASE[1], allOnes)
|
||||
);
|
||||
|
||||
assert(xout.equals(r2[0]));
|
||||
assert(yout.equals(r2[1]));
|
||||
await circuit.assertOut(w, {out: r2});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,11 +1,7 @@
|
||||
const chai = require("chai");
|
||||
const path = require("path");
|
||||
const snarkjs = require("snarkjs");
|
||||
const compiler = require("circom");
|
||||
|
||||
const assert = chai.assert;
|
||||
|
||||
const bigInt = snarkjs.bigInt;
|
||||
const bigInt = require("big-integer");
|
||||
const tester = require("circom").tester;
|
||||
|
||||
const babyJub = require("../src/babyjub.js");
|
||||
const pedersen = require("../src/pedersenHash.js");
|
||||
@@ -15,60 +11,39 @@ describe("Pedersen test", function() {
|
||||
let circuit;
|
||||
this.timeout(100000);
|
||||
before( async() => {
|
||||
const cirDef = await compiler(path.join(__dirname, "circuits", "pedersen2_test.circom"));
|
||||
|
||||
circuit = new snarkjs.Circuit(cirDef);
|
||||
|
||||
console.log("NConstrains Pedersen2: " + circuit.nConstraints);
|
||||
circuit = await tester(path.join(__dirname, "circuits", "pedersen2_test.circom"));
|
||||
});
|
||||
it("Should pedersen at zero", async () => {
|
||||
|
||||
let w, xout, yout;
|
||||
let w;
|
||||
|
||||
w = circuit.calculateWitness({ in: 0});
|
||||
|
||||
xout = w[circuit.getSignalIdx("main.out[0]")];
|
||||
yout = w[circuit.getSignalIdx("main.out[1]")];
|
||||
w = await circuit.calculateWitness({ in: 0}, true);
|
||||
|
||||
const b = Buffer.alloc(32);
|
||||
|
||||
const h = pedersen.hash(b);
|
||||
const hP = babyJub.unpackPoint(h);
|
||||
|
||||
/*
|
||||
console.log(`[${xout.toString()}, ${yout.toString()}]`);
|
||||
console.log(`[${hP[0].toString()}, ${hP[1].toString()}]`);
|
||||
*/
|
||||
await circuit.assertOut(w, {out: hP});
|
||||
|
||||
assert(xout.equals(hP[0]));
|
||||
assert(yout.equals(hP[1]));
|
||||
});
|
||||
it("Should pedersen with 253 ones", async () => {
|
||||
|
||||
let w, xout, yout;
|
||||
let w;
|
||||
|
||||
const n = bigInt.one.shl(253).sub(bigInt.one);
|
||||
console.log(n.toString(16));
|
||||
const n = bigInt.one.shiftLeft(253).minus(bigInt.one);
|
||||
|
||||
w = circuit.calculateWitness({ in: n});
|
||||
|
||||
xout = w[circuit.getSignalIdx("main.out[0]")];
|
||||
yout = w[circuit.getSignalIdx("main.out[1]")];
|
||||
w = await circuit.calculateWitness({ in: n}, true);
|
||||
|
||||
const b = Buffer.alloc(32);
|
||||
for (let i=0; i<31; i++) b[i] = 0xFF;
|
||||
b[31] = 0x1F;
|
||||
|
||||
|
||||
const h = pedersen.hash(b);
|
||||
const hP = babyJub.unpackPoint(h);
|
||||
|
||||
/*
|
||||
console.log(`[${xout.toString()}, ${yout.toString()}]`);
|
||||
console.log(`[${hP[0].toString()}, ${hP[1].toString()}]`);
|
||||
*/
|
||||
await circuit.assertOut(w, {out: hP});
|
||||
|
||||
assert(xout.equals(hP[0]));
|
||||
assert(yout.equals(hP[1]));
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,11 +1,5 @@
|
||||
const chai = require("chai");
|
||||
const path = require("path");
|
||||
const snarkjs = require("snarkjs");
|
||||
const compiler = require("circom");
|
||||
|
||||
const assert = chai.assert;
|
||||
|
||||
const bigInt = snarkjs.bigInt;
|
||||
const tester = require("circom").tester;
|
||||
|
||||
const babyJub = require("../src/babyjub.js");
|
||||
|
||||
@@ -14,20 +8,16 @@ describe("Point 2 bits test", function() {
|
||||
let circuit;
|
||||
this.timeout(100000);
|
||||
before( async() => {
|
||||
const cirDef = await compiler(path.join(__dirname, "circuits", "pointbits_loopback.circom"));
|
||||
|
||||
circuit = new snarkjs.Circuit(cirDef);
|
||||
|
||||
console.log("NConstrains Point2Bits loopback: " + circuit.nConstraints);
|
||||
circuit = await tester(path.join(__dirname, "circuits", "pointbits_loopback.circom"));
|
||||
});
|
||||
it("Should do the both convertions for 8Base", async () => {
|
||||
const w = circuit.calculateWitness({ in: babyJub.Base8});
|
||||
const w = await circuit.calculateWitness({ in: babyJub.Base8}, true);
|
||||
|
||||
assert(circuit.checkWitness(w));
|
||||
await circuit.checkConstraints(w);
|
||||
});
|
||||
it("Should do the both convertions for Zero point", async () => {
|
||||
const w = circuit.calculateWitness({ in: [0, 1]});
|
||||
const w = await circuit.calculateWitness({ in: [0, 1]}, true);
|
||||
|
||||
assert(circuit.checkWitness(w));
|
||||
await circuit.checkConstraints(w);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
const chai = require("chai");
|
||||
const path = require("path");
|
||||
const snarkjs = require("snarkjs");
|
||||
const compiler = require("circom");
|
||||
var blake2b = require('blake2b');
|
||||
var blake2b = require("blake2b");
|
||||
const bigInt = require("big-integer");
|
||||
const tester = require("circom").tester;
|
||||
|
||||
const poseidon = require("../src/poseidon.js");
|
||||
|
||||
@@ -11,9 +11,9 @@ const assert = chai.assert;
|
||||
describe("Blake2b version test", function() {
|
||||
it("Should give the expected output for blake2b version", async () => {
|
||||
var output = new Uint8Array(32);
|
||||
var input = Buffer.from('poseidon_constants');
|
||||
h = blake2b(output.length).update(input).digest('hex')
|
||||
assert.equal('e57ba154fb2c47811dc1a2369b27e25a44915b4e4ece4eb8ec74850cb78e01b1', h);
|
||||
var input = Buffer.from("poseidon_constants");
|
||||
const h = blake2b(output.length).update(input).digest("hex");
|
||||
assert.equal("e57ba154fb2c47811dc1a2369b27e25a44915b4e4ece4eb8ec74850cb78e01b1", h);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -24,68 +24,53 @@ describe("Poseidon Circuit test", function () {
|
||||
this.timeout(100000);
|
||||
|
||||
before( async () => {
|
||||
const cirDef6 = await compiler(path.join(__dirname, "circuits", "poseidon6_test.circom"));
|
||||
circuit6 = new snarkjs.Circuit(cirDef6);
|
||||
console.log("Poseidon6 constraints: " + circuit6.nConstraints);
|
||||
const cirDef3 = await compiler(path.join(__dirname, "circuits", "poseidon3_test.circom"));
|
||||
circuit3 = new snarkjs.Circuit(cirDef3);
|
||||
console.log("Poseidon3 constraints: " + circuit3.nConstraints);
|
||||
circuit6 = await tester(path.join(__dirname, "circuits", "poseidon6_test.circom"));
|
||||
circuit3 = await tester(path.join(__dirname, "circuits", "poseidon3_test.circom"));
|
||||
});
|
||||
|
||||
it("Should check constrain of hash([1, 2]) t=6", async () => {
|
||||
const w = circuit6.calculateWitness({inputs: [1, 2]});
|
||||
|
||||
const res = w[circuit6.getSignalIdx("main.out")];
|
||||
const w = await circuit6.calculateWitness({inputs: [1, 2]}, true);
|
||||
|
||||
const hash = poseidon.createHash(6, 8, 57);
|
||||
|
||||
const res2 = hash([1,2]);
|
||||
assert.equal("12242166908188651009877250812424843524687801523336557272219921456462821518061", res2.toString());
|
||||
assert.equal(res.toString(), res2.toString());
|
||||
assert(circuit6.checkWitness(w));
|
||||
await circuit6.assertOut(w, {out : res2});
|
||||
await circuit6.checkConstraints(w);
|
||||
});
|
||||
|
||||
it("Should check constrain of hash([3, 4]) t=6", async () => {
|
||||
const w = circuit6.calculateWitness({inputs: [3, 4]});
|
||||
|
||||
const res = w[circuit6.getSignalIdx("main.out")];
|
||||
const w = await circuit6.calculateWitness({inputs: [3, 4]});
|
||||
|
||||
const hash = poseidon.createHash(6, 8, 57);
|
||||
|
||||
const res2 = hash([3, 4]);
|
||||
|
||||
assert.equal("17185195740979599334254027721507328033796809509313949281114643312710535000993", res2.toString());
|
||||
|
||||
assert.equal(res.toString(), res2.toString());
|
||||
|
||||
assert(circuit6.checkWitness(w));
|
||||
await circuit6.assertOut(w, {out : res2});
|
||||
await circuit6.checkConstraints(w);
|
||||
});
|
||||
|
||||
|
||||
it("Should check constrain of hash([1, 2]) t=3", async () => {
|
||||
const w = circuit3.calculateWitness({inputs: [1, 2]});
|
||||
|
||||
const res = w[circuit3.getSignalIdx("main.out")];
|
||||
const w = await circuit3.calculateWitness({inputs: [1, 2]});
|
||||
|
||||
const hash = poseidon.createHash(3, 8, 57);
|
||||
|
||||
const res2 = hash([1,2]);
|
||||
assert.equal("2104035019328376391822106787753454168168617545136592089411833517434990977743", res2.toString());
|
||||
assert.equal(res.toString(), res2.toString());
|
||||
assert(circuit3.checkWitness(w));
|
||||
await circuit3.assertOut(w, {out : res2});
|
||||
await circuit3.checkConstraints(w);
|
||||
});
|
||||
|
||||
it("Should check constrain of hash([3, 4]) t=3", async () => {
|
||||
const w = circuit3.calculateWitness({inputs: [3, 4]});
|
||||
|
||||
const res = w[circuit3.getSignalIdx("main.out")];
|
||||
const w = await circuit3.calculateWitness({inputs: [3, 4]});
|
||||
|
||||
const hash = poseidon.createHash(3, 8, 57);
|
||||
|
||||
const res2 = hash([3, 4]);
|
||||
assert.equal("12456141564250880945411182508630957604732712316993112736876413121277158512223", res2.toString());
|
||||
|
||||
assert.equal(res.toString(), res2.toString());
|
||||
|
||||
assert(circuit3.checkWitness(w));
|
||||
await circuit3.assertOut(w, {out : res2});
|
||||
await circuit3.checkConstraints(w);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,23 +0,0 @@
|
||||
|
||||
include "../circuits/smt/smtverifier.circom";
|
||||
template SMT(nLevels) {
|
||||
signal input root;
|
||||
signal input mtp[nLevels];
|
||||
signal input hi;
|
||||
signal input hv;
|
||||
|
||||
component smtClaimExists = SMTVerifier(nLevels);
|
||||
smtClaimExists.enabled <== 1;
|
||||
smtClaimExists.fnc <== 0;
|
||||
smtClaimExists.root <== root;
|
||||
for (var i=0; i<nLevels; i++) {
|
||||
smtClaimExists.siblings[i] <== mtp[i];
|
||||
}
|
||||
smtClaimExists.oldKey <== 0;
|
||||
smtClaimExists.oldValue <== 0;
|
||||
smtClaimExists.isOld0 <== 0;
|
||||
|
||||
smtClaimExists.key <== hi;
|
||||
smtClaimExists.value <== hv;
|
||||
}
|
||||
component main = SMT(4);
|
||||
@@ -3,12 +3,12 @@ const path = require("path");
|
||||
const snarkjs = require("snarkjs");
|
||||
const crypto = require("crypto");
|
||||
|
||||
const compiler = require("circom");
|
||||
|
||||
const assert = chai.assert;
|
||||
|
||||
const sha256 = require("./helpers/sha256");
|
||||
|
||||
const tester = require("circom").tester;
|
||||
|
||||
// const printSignal = require("./helpers/printsignal");
|
||||
|
||||
|
||||
@@ -34,7 +34,8 @@ function bitArray2buffer(a) {
|
||||
}
|
||||
|
||||
|
||||
describe("SHA256 test", () => {
|
||||
describe("SHA256 test", function () {
|
||||
this.timeout(100000);
|
||||
|
||||
|
||||
it("Should work bits to array and array to bits", async () => {
|
||||
@@ -45,17 +46,13 @@ describe("SHA256 test", () => {
|
||||
const a = buffer2bitArray(b);
|
||||
const b2 = bitArray2buffer(a);
|
||||
|
||||
assert.equal(b.toString("hex"), b2.toString("hex"));
|
||||
assert.equal(b.toString("hex"), b2.toString("hex"), true);
|
||||
});
|
||||
|
||||
it("Should calculate a hash of 1 compressor", async () => {
|
||||
const cirDef = await compiler(path.join(__dirname, "circuits", "sha256_2_test.circom"));
|
||||
const circuit = new snarkjs.Circuit(cirDef);
|
||||
const cir = await tester(path.join(__dirname, "circuits", "sha256_2_test.circom"));
|
||||
|
||||
console.log("Vars: "+circuit.nVars);
|
||||
console.log("Constraints: "+circuit.nConstraints);
|
||||
|
||||
const witness = circuit.calculateWitness({ "a": "1", "b": "2" });
|
||||
const witness = await cir.calculateWitness({ "a": "1", "b": "2" }, true);
|
||||
|
||||
const b = new Buffer.alloc(54);
|
||||
b[26] = 1;
|
||||
@@ -74,16 +71,7 @@ describe("SHA256 test", () => {
|
||||
}).timeout(1000000);
|
||||
|
||||
it("Should calculate a hash of 2 compressor", async () => {
|
||||
const cirDef = await compiler(path.join(__dirname, "circuits", "sha256_test512.circom"), {reduceConstraints:false} );
|
||||
const circuit = new snarkjs.Circuit(cirDef);
|
||||
|
||||
console.log("Vars: "+circuit.nVars);
|
||||
console.log("Constraints: "+circuit.nConstraints);
|
||||
|
||||
|
||||
// const testStr = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
|
||||
|
||||
// const b = Buffer.from(testStr, 'utf8');
|
||||
const cir = await tester(path.join(__dirname, "circuits", "sha256_test512.circom"));
|
||||
|
||||
const b = new Buffer.alloc(64);
|
||||
for (let i=0; i<64; i++) {
|
||||
@@ -95,7 +83,7 @@ describe("SHA256 test", () => {
|
||||
.digest("hex");
|
||||
|
||||
const arrIn = buffer2bitArray(b);
|
||||
const witness = circuit.calculateWitness({ "in": arrIn }, {logOutput: false});
|
||||
const witness = await cir.calculateWitness({ "in": arrIn }, true);
|
||||
|
||||
const arrOut = witness.slice(1, 257);
|
||||
const hash2 = bitArray2buffer(arrOut).toString("hex");
|
||||
@@ -103,32 +91,25 @@ describe("SHA256 test", () => {
|
||||
assert.equal(hash, hash2);
|
||||
|
||||
}).timeout(1000000);
|
||||
|
||||
it("Should calculate a hash of 2 compressor", async () => {
|
||||
const cirDef = await compiler(path.join(__dirname, "circuits", "sha256_test448.circom"), {reduceConstraints:false} );
|
||||
const circuit = new snarkjs.Circuit(cirDef);
|
||||
|
||||
console.log("Vars: "+circuit.nVars);
|
||||
console.log("Constraints: "+circuit.nConstraints);
|
||||
it ("Should calculate a hash of 2 compressor", async () => {
|
||||
const cir = await tester(path.join(__dirname, "circuits", "sha256_test448.circom"));
|
||||
|
||||
const testStr = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
|
||||
|
||||
const b = Buffer.from(testStr, "utf8");
|
||||
// for (let i=0; i<64; i++) {
|
||||
// b[i] = i+1;
|
||||
// }
|
||||
|
||||
const hash = crypto.createHash("sha256")
|
||||
.update(b)
|
||||
.digest("hex");
|
||||
|
||||
const arrIn = buffer2bitArray(b);
|
||||
const witness = circuit.calculateWitness({ "in": arrIn } , {logOutput: false});
|
||||
|
||||
const witness = await cir.calculateWitness({ "in": arrIn }, true);
|
||||
|
||||
const arrOut = witness.slice(1, 257);
|
||||
const hash2 = bitArray2buffer(arrOut).toString("hex");
|
||||
|
||||
assert.equal(hash, hash2);
|
||||
});
|
||||
|
||||
}).timeout(1000000);
|
||||
});
|
||||
|
||||
59
test/sign.js
59
test/sign.js
@@ -1,11 +1,6 @@
|
||||
const chai = require("chai");
|
||||
const path = require("path");
|
||||
const snarkjs = require("snarkjs");
|
||||
const compiler = require("circom");
|
||||
|
||||
const assert = chai.assert;
|
||||
|
||||
const bigInt = snarkjs.bigInt;
|
||||
const bigInt = require("big-integer");
|
||||
const tester = require("circom").tester;
|
||||
|
||||
function print(circuit, w, s) {
|
||||
console.log(s + ": " + w[circuit.getSignalIdx(s)]);
|
||||
@@ -14,7 +9,7 @@ function print(circuit, w, s) {
|
||||
function getBits(v, n) {
|
||||
const res = [];
|
||||
for (let i=0; i<n; i++) {
|
||||
if (v.shr(i).isOdd()) {
|
||||
if (v.shiftRight(i).isOdd()) {
|
||||
res.push(bigInt.one);
|
||||
} else {
|
||||
res.push(bigInt.zero);
|
||||
@@ -25,64 +20,60 @@ function getBits(v, n) {
|
||||
|
||||
const q = bigInt("21888242871839275222246405745257275088548364400416034343698204186575808495617");
|
||||
|
||||
describe("Sign test", () => {
|
||||
describe("Sign test", function() {
|
||||
let circuit;
|
||||
this.timeout(100000);
|
||||
|
||||
before( async() => {
|
||||
const cirDef = await compiler(path.join(__dirname, "circuits", "sign_test.circom"));
|
||||
|
||||
circuit = new snarkjs.Circuit(cirDef);
|
||||
|
||||
console.log("NConstrains: " + circuit.nConstraints);
|
||||
circuit = await tester(path.join(__dirname, "circuits", "sign_test.circom"));
|
||||
});
|
||||
|
||||
it("Sign of 0", async () => {
|
||||
const inp = getBits(bigInt.zero, 254);
|
||||
const w = circuit.calculateWitness({in: inp});
|
||||
const w = await circuit.calculateWitness({in: inp}, true);
|
||||
|
||||
assert( w[circuit.getSignalIdx("main.sign")].equals(bigInt(0)) );
|
||||
await circuit.assertOut(w, {sign: 0});
|
||||
});
|
||||
|
||||
it("Sign of 3", async () => {
|
||||
const inp = getBits(bigInt(3), 254);
|
||||
const w = circuit.calculateWitness({in: inp});
|
||||
const w = await circuit.calculateWitness({in: inp}, true);
|
||||
|
||||
assert( w[circuit.getSignalIdx("main.sign")].equals(bigInt(0)) );
|
||||
await circuit.assertOut(w, {sign: 0});
|
||||
});
|
||||
|
||||
it("Sign of q/2", async () => {
|
||||
const inp = getBits(q.shr(bigInt.one), 254);
|
||||
const w = circuit.calculateWitness({in: inp});
|
||||
const inp = getBits(q.shiftRight(bigInt.one), 254);
|
||||
const w = await circuit.calculateWitness({in: inp}, true);
|
||||
|
||||
assert( w[circuit.getSignalIdx("main.sign")].equals(bigInt(0)) );
|
||||
await circuit.assertOut(w, {sign: 0});
|
||||
});
|
||||
|
||||
it("Sign of q/2+1", async () => {
|
||||
const inp = getBits(q.shr(bigInt.one).add(bigInt.one), 254);
|
||||
const w = circuit.calculateWitness({in: inp});
|
||||
const inp = getBits(q.shiftRight(bigInt.one).add(bigInt.one), 254);
|
||||
const w = await circuit.calculateWitness({in: inp}, true);
|
||||
|
||||
assert( w[circuit.getSignalIdx("main.sign")].equals(bigInt(1)) );
|
||||
await circuit.assertOut(w, {sign: 1});
|
||||
});
|
||||
|
||||
it("Sign of q-1", async () => {
|
||||
const inp = getBits(q.sub(bigInt.one), 254);
|
||||
const w = circuit.calculateWitness({in: inp});
|
||||
const inp = getBits(q.minus(bigInt.one), 254);
|
||||
const w = await circuit.calculateWitness({in: inp}, true);
|
||||
|
||||
assert( w[circuit.getSignalIdx("main.sign")].equals(bigInt(1)) );
|
||||
await circuit.assertOut(w, {sign: 1});
|
||||
});
|
||||
|
||||
it("Sign of q", async () => {
|
||||
const inp = getBits(q, 254);
|
||||
const w = circuit.calculateWitness({in: inp});
|
||||
const w = await circuit.calculateWitness({in: inp}, true);
|
||||
|
||||
assert( w[circuit.getSignalIdx("main.sign")].equals(bigInt(1)) );
|
||||
await circuit.assertOut(w, {sign: 1});
|
||||
});
|
||||
|
||||
it("Sign of all ones", async () => {
|
||||
const inp = getBits(bigInt(1).shl(254).sub(bigInt(1)), 254);
|
||||
const w = circuit.calculateWitness({in: inp});
|
||||
const inp = getBits(bigInt(1).shiftLeft(254).minus(bigInt(1)), 254);
|
||||
const w = await circuit.calculateWitness({in: inp}, true);
|
||||
|
||||
assert( w[circuit.getSignalIdx("main.sign")].equals(bigInt(1)) );
|
||||
await circuit.assertOut(w, {sign: 1});
|
||||
});
|
||||
|
||||
|
||||
});
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
const chai = require("chai");
|
||||
const snarkjs = require("snarkjs");
|
||||
|
||||
const bigInt = require("big-integer");
|
||||
|
||||
const smt = require("../src/smt.js");
|
||||
|
||||
const assert = chai.assert;
|
||||
|
||||
const bigInt = snarkjs.bigInt;
|
||||
|
||||
|
||||
function stringifyBigInts(o) {
|
||||
if ((typeof(o) == "bigint") || (o instanceof bigInt)) {
|
||||
|
||||
@@ -1,25 +1,23 @@
|
||||
const chai = require("chai");
|
||||
const path = require("path");
|
||||
const snarkjs = require("snarkjs");
|
||||
const compiler = require("circom");
|
||||
const bigInt = require("big-integer");
|
||||
const tester = require("circom").tester;
|
||||
|
||||
const smt = require("../src/smt.js");
|
||||
|
||||
const assert = chai.assert;
|
||||
|
||||
const bigInt = snarkjs.bigInt;
|
||||
|
||||
function print(circuit, w, s) {
|
||||
console.log(s + ": " + w[circuit.getSignalIdx(s)]);
|
||||
}
|
||||
|
||||
async function testInsert(tree, key, value, circuit, log ) {
|
||||
async function testInsert(tree, key, value, circuit ) {
|
||||
|
||||
const res = await tree.insert(key,value);
|
||||
let siblings = res.siblings;
|
||||
while (siblings.length<10) siblings.push(bigInt(0));
|
||||
|
||||
const w = circuit.calculateWitness({
|
||||
const w = await circuit.calculateWitness({
|
||||
fnc: [1,0],
|
||||
oldRoot: res.oldRoot,
|
||||
siblings: siblings,
|
||||
@@ -28,11 +26,12 @@ async function testInsert(tree, key, value, circuit, log ) {
|
||||
isOld0: res.isOld0 ? 1 : 0,
|
||||
newKey: key,
|
||||
newValue: value
|
||||
}, log);
|
||||
}, true);
|
||||
|
||||
await circuit.checkConstraints(w);
|
||||
|
||||
await circuit.assertOut(w, {newRoot: res.newRoot});
|
||||
|
||||
const root1 = w[circuit.getSignalIdx("main.newRoot")];
|
||||
assert(circuit.checkWitness(w));
|
||||
assert(root1.equals(res.newRoot));
|
||||
}
|
||||
|
||||
async function testDelete(tree, key, circuit) {
|
||||
@@ -40,7 +39,7 @@ async function testDelete(tree, key, circuit) {
|
||||
let siblings = res.siblings;
|
||||
while (siblings.length<10) siblings.push(bigInt(0));
|
||||
|
||||
const w = circuit.calculateWitness({
|
||||
const w = await circuit.calculateWitness({
|
||||
fnc: [1,1],
|
||||
oldRoot: res.oldRoot,
|
||||
siblings: siblings,
|
||||
@@ -49,12 +48,11 @@ async function testDelete(tree, key, circuit) {
|
||||
isOld0: res.isOld0 ? 1 : 0,
|
||||
newKey: res.delKey,
|
||||
newValue: res.delValue
|
||||
});
|
||||
}, true);
|
||||
|
||||
const root1 = w[circuit.getSignalIdx("main.newRoot")];
|
||||
await circuit.checkConstraints(w);
|
||||
|
||||
assert(circuit.checkWitness(w));
|
||||
assert(root1.equals(res.newRoot));
|
||||
await circuit.assertOut(w, {newRoot: res.newRoot});
|
||||
}
|
||||
|
||||
async function testUpdate(tree, key, newValue, circuit) {
|
||||
@@ -62,7 +60,7 @@ async function testUpdate(tree, key, newValue, circuit) {
|
||||
let siblings = res.siblings;
|
||||
while (siblings.length<10) siblings.push(bigInt(0));
|
||||
|
||||
const w = circuit.calculateWitness({
|
||||
const w = await circuit.calculateWitness({
|
||||
fnc: [0,1],
|
||||
oldRoot: res.oldRoot,
|
||||
siblings: siblings,
|
||||
@@ -73,25 +71,21 @@ async function testUpdate(tree, key, newValue, circuit) {
|
||||
newValue: res.newValue
|
||||
});
|
||||
|
||||
const root1 = w[circuit.getSignalIdx("main.newRoot")];
|
||||
await circuit.checkConstraints(w);
|
||||
|
||||
assert(circuit.checkWitness(w));
|
||||
assert(root1.equals(res.newRoot));
|
||||
await circuit.assertOut(w, {newRoot: res.newRoot});
|
||||
}
|
||||
|
||||
|
||||
describe("SMT test", function () {
|
||||
describe("SMT Processor test", function () {
|
||||
let circuit;
|
||||
let tree;
|
||||
|
||||
this.timeout(10000000);
|
||||
|
||||
before( async () => {
|
||||
const cirDef = await compiler(path.join(__dirname, "circuits", "smtprocessor10_test.circom"));
|
||||
|
||||
circuit = new snarkjs.Circuit(cirDef);
|
||||
|
||||
console.log("NConstrains SMTProcessor: " + circuit.nConstraints);
|
||||
circuit = await tester(path.join(__dirname, "circuits", "smtprocessor10_test.circom"));
|
||||
await circuit.loadSymbols();
|
||||
|
||||
tree = await smt.newMemEmptyTrie();
|
||||
});
|
||||
@@ -110,8 +104,6 @@ describe("SMT test", function () {
|
||||
await testInsert(tree, key, value, circuit);
|
||||
});
|
||||
|
||||
|
||||
|
||||
it("Should remove an element", async () => {
|
||||
await testDelete(tree, 111, circuit);
|
||||
await testDelete(tree, 333, circuit);
|
||||
@@ -179,7 +171,7 @@ describe("SMT test", function () {
|
||||
it("Should match a NOp with random vals", async () => {
|
||||
let siblings = [];
|
||||
while (siblings.length<10) siblings.push(bigInt(88));
|
||||
const w = circuit.calculateWitness({
|
||||
const w = await circuit.calculateWitness({
|
||||
fnc: [0,0],
|
||||
oldRoot: 11,
|
||||
siblings: siblings,
|
||||
@@ -190,12 +182,12 @@ describe("SMT test", function () {
|
||||
newValue: 77
|
||||
});
|
||||
|
||||
const root1 = w[circuit.getSignalIdx("main.oldRoot")];
|
||||
const root2 = w[circuit.getSignalIdx("main.newRoot")];
|
||||
const root1 = w[circuit.symbols["main.oldRoot"].varIdx];
|
||||
const root2 = w[circuit.symbols["main.newRoot"].varIdx];
|
||||
|
||||
await circuit.checkConstraints(w);
|
||||
|
||||
assert(circuit.checkWitness(w));
|
||||
assert(root1.equals(root2));
|
||||
|
||||
});
|
||||
it("Should update an element", async () => {
|
||||
const tree1 = await smt.newMemEmptyTrie();
|
||||
@@ -213,5 +205,4 @@ describe("SMT test", function () {
|
||||
await testUpdate(tree1, 9, 999, circuit);
|
||||
await testUpdate(tree1, 32, 323232, circuit);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
@@ -1,14 +1,12 @@
|
||||
const chai = require("chai");
|
||||
const path = require("path");
|
||||
const snarkjs = require("snarkjs");
|
||||
const compiler = require("circom");
|
||||
const bigInt = require("big-integer");
|
||||
const tester = require("circom").tester;
|
||||
|
||||
const smt = require("../src/smt.js");
|
||||
|
||||
const assert = chai.assert;
|
||||
|
||||
const bigInt = snarkjs.bigInt;
|
||||
|
||||
function print(circuit, w, s) {
|
||||
console.log(s + ": " + w[circuit.getSignalIdx(s)]);
|
||||
}
|
||||
@@ -21,7 +19,7 @@ async function testInclusion(tree, key, circuit) {
|
||||
let siblings = res.siblings;
|
||||
while (siblings.length<10) siblings.push(bigInt(0));
|
||||
|
||||
const w = circuit.calculateWitness({
|
||||
const w = await circuit.calculateWitness({
|
||||
enabled: 1,
|
||||
fnc: 0,
|
||||
root: tree.root,
|
||||
@@ -31,9 +29,10 @@ async function testInclusion(tree, key, circuit) {
|
||||
isOld0: 0,
|
||||
key: key,
|
||||
value: res.foundValue
|
||||
});
|
||||
}, true);
|
||||
|
||||
await circuit.checkConstraints(w);
|
||||
|
||||
assert(circuit.checkWitness(w));
|
||||
}
|
||||
|
||||
async function testExclusion(tree, key, circuit) {
|
||||
@@ -43,7 +42,7 @@ async function testExclusion(tree, key, circuit) {
|
||||
let siblings = res.siblings;
|
||||
while (siblings.length<10) siblings.push(bigInt(0));
|
||||
|
||||
const w = circuit.calculateWitness({
|
||||
const w = await circuit.calculateWitness({
|
||||
enabled: 1,
|
||||
fnc: 1,
|
||||
root: tree.root,
|
||||
@@ -55,21 +54,18 @@ async function testExclusion(tree, key, circuit) {
|
||||
value: 0
|
||||
});
|
||||
|
||||
assert(circuit.checkWitness(w));
|
||||
await circuit.checkConstraints(w);
|
||||
|
||||
}
|
||||
|
||||
describe("SMT test", function () {
|
||||
describe("SMT Verifier test", function () {
|
||||
let circuit;
|
||||
let tree;
|
||||
|
||||
this.timeout(100000);
|
||||
|
||||
before( async () => {
|
||||
const cirDef = await compiler(path.join(__dirname, "circuits", "smtverifier10_test.circom"));
|
||||
|
||||
circuit = new snarkjs.Circuit(cirDef);
|
||||
|
||||
console.log("NConstrains SMTVerifier: " + circuit.nConstraints);
|
||||
circuit = await tester(path.join(__dirname, "circuits", "smtverifier10_test.circom"));
|
||||
|
||||
tree = await smt.newMemEmptyTrie();
|
||||
await tree.insert(7,77);
|
||||
@@ -97,7 +93,7 @@ describe("SMT test", function () {
|
||||
let siblings = [];
|
||||
for (let i=0; i<10; i++) siblings.push(i);
|
||||
|
||||
const w = circuit.calculateWitness({
|
||||
const w = await circuit.calculateWitness({
|
||||
enabled: 0,
|
||||
fnc: 0,
|
||||
root: 1,
|
||||
@@ -108,7 +104,9 @@ describe("SMT test", function () {
|
||||
key: 44,
|
||||
value: 0
|
||||
});
|
||||
assert(circuit.checkWitness(w));
|
||||
|
||||
|
||||
await circuit.checkConstraints(w);
|
||||
});
|
||||
|
||||
it("Check inclussion Adria case", async () => {
|
||||
|
||||
@@ -1,98 +0,0 @@
|
||||
const path = require("path");
|
||||
const snarkjs = require("snarkjs");
|
||||
const compiler = require("circom");
|
||||
const fs = require("fs")
|
||||
|
||||
const bigInt = snarkjs.bigInt;
|
||||
const smt = require("../src/smt.js");
|
||||
|
||||
const circuitSource = `
|
||||
include "../circuits/smt/smtverifier.circom";
|
||||
template SMT(nLevels) {
|
||||
signal input root;
|
||||
signal input mtp[nLevels];
|
||||
signal input hi;
|
||||
signal input hv;
|
||||
|
||||
component smtClaimExists = SMTVerifier(nLevels);
|
||||
smtClaimExists.enabled <== 1;
|
||||
smtClaimExists.fnc <== 0;
|
||||
smtClaimExists.root <== root;
|
||||
for (var i=0; i<nLevels; i++) {
|
||||
smtClaimExists.siblings[i] <== mtp[i];
|
||||
}
|
||||
smtClaimExists.oldKey <== 0;
|
||||
smtClaimExists.oldValue <== 0;
|
||||
smtClaimExists.isOld0 <== 0;
|
||||
|
||||
smtClaimExists.key <== hi;
|
||||
smtClaimExists.value <== hv;
|
||||
}
|
||||
component main = SMT(4);
|
||||
`;
|
||||
|
||||
describe("smt3test", function () {
|
||||
this.timeout(200000);
|
||||
|
||||
let circuitFileName;
|
||||
|
||||
before( async () => {
|
||||
circuitFileName = path.join(__dirname, ".", "rawsmt3.circom");
|
||||
fs.writeFileSync(circuitFileName,circuitSource);
|
||||
});
|
||||
|
||||
const levels = 4;
|
||||
async function testsmt3(e1, e2) {
|
||||
let tree = await smt.newMemEmptyTrie();
|
||||
|
||||
// insert e1, e2
|
||||
await tree.insert(e1.hi, e1.hv);
|
||||
await tree.insert(e2.hi, e2.hv);
|
||||
|
||||
// generate proof for e1
|
||||
const findInfo = await tree.find(e1.hi);
|
||||
const siblings = findInfo.siblings;
|
||||
while (siblings.length < levels) siblings.push(bigInt(0));
|
||||
|
||||
const input = {
|
||||
root: tree.root,
|
||||
mtp: siblings,
|
||||
hi: e1.hi,
|
||||
hv: e1.hv,
|
||||
};
|
||||
|
||||
const compiledCircuit = await compiler(
|
||||
circuitFileName,
|
||||
{ reduceConstraints: false }
|
||||
);
|
||||
|
||||
const circuit = new snarkjs.Circuit(compiledCircuit);
|
||||
const witness = circuit.calculateWitness(input);
|
||||
circuit.checkWitness(witness);
|
||||
}
|
||||
|
||||
it("TestSmts", async () => {
|
||||
|
||||
const e1 = {
|
||||
hi: bigInt("17124152697573569611556136390143205198134245887034837071647643529178599000839"),
|
||||
hv: bigInt("19650379996168153643111744440707177573540245771926102415571667548153444658179"),
|
||||
};
|
||||
|
||||
const e2ok = {
|
||||
hi: bigInt("16498254692537945203721083102154618658340563351558973077349594629411025251262"),
|
||||
hv: bigInt("19650379996168153643111744440707177573540245771926102415571667548153444658179"),
|
||||
};
|
||||
|
||||
const e2fail = {
|
||||
hi: bigInt("17195092312975762537892237130737365903429674363577646686847513978084990105579"),
|
||||
hv: bigInt("19650379996168153643111744440707177573540245771926102415571667548153444658179"),
|
||||
};
|
||||
|
||||
console.log("test e1, e2ok");
|
||||
await testsmt3(e1, e2ok);
|
||||
|
||||
console.log("test e1, e2fail");
|
||||
await testsmt3(e1, e2fail);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user