Adapt tests to circomlibjs async

This commit is contained in:
Jordi Baylina 2021-10-30 14:06:21 +02:00
parent 65748e6b8b
commit b5eb78c822
No known key found for this signature in database
GPG Key ID: 7480C80C1BE43112
18 changed files with 1249 additions and 6441 deletions

11
.vscode/launch.json vendored
View File

@ -13,6 +13,17 @@
],
"program": "${workspaceFolder}/calcoptimizedposeidonconsts/test_poseidon.js",
"cwd": "${workspaceFolder}/calcoptimizedposeidonconsts"
},
{
"type": "pwa-node",
"request": "launch",
"name": "Tests",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
"cwd": "${workspaceFolder}",
"args": ["test/smtverifier.js"]
}
]
}

7124
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -27,7 +27,7 @@
"blake-hash": "^2.0.0",
"chai": "^4.3.4",
"circom_tester": "0.0.5",
"circomlibjs": "^0.0.8",
"mocha": "^9.1.2"
"circomlibjs": "^0.1.0",
"mocha": "^9.1.3"
}
}

View File

@ -2,8 +2,7 @@ const chai = require("chai");
const path = require("path");
const createBlakeHash = require("blake-hash");
const eddsa = require("circomlibjs").eddsa;
const F = require("circomlibjs").babyjub.F;
const buildEddsa = require("circomlibjs").buildEddsa;
const assert = chai.assert;
@ -12,6 +11,8 @@ const utils = require("ffjavascript").utils;
const Scalar = require("ffjavascript").Scalar;
describe("Baby Jub test", function () {
let eddsa;
let F;
let circuitAdd;
let circuitTest;
let circuitPbk;
@ -19,41 +20,48 @@ describe("Baby Jub test", function () {
this.timeout(100000);
before( async() => {
eddsa = await buildEddsa();
F = eddsa.F;
circuitAdd = await wasm_tester(path.join(__dirname, "circuits", "babyadd_tester.circom"));
circuitTest = await wasm_tester(path.join(__dirname, "circuits", "babycheck_test.circom"));
circuitPbk = await wasm_tester(path.join(__dirname, "circuits", "babypbk_test.circom"));
});
after(async () => {
globalThis.curve_bn128.terminate();
});
it("Should add point (0,1) and (0,1)", async () => {
const input={
x1: F.e(0),
y1: F.e(1),
x2: F.e(0),
y2: F.e(1)
x1: 0,
y1: 1,
x2: 0,
y2: 1
};
const w = await circuitAdd.calculateWitness(input, true);
await circuitAdd.assertOut(w, {xout: F.e(0), yout: F.e(1)});
await circuitAdd.assertOut(w, {xout: 0, yout: 1});
});
it("Should add 2 same numbers", async () => {
const input={
x1: F.e("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
y1: F.e("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
x2: F.e("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
y2: F.e("2626589144620713026669568689430873010625803728049924121243784502389097019475")
x1: 17777552123799933955779906779655732241715742912184938656739573121738514868268n,
y1: 2626589144620713026669568689430873010625803728049924121243784502389097019475n,
x2: 17777552123799933955779906779655732241715742912184938656739573121738514868268n,
y2: 2626589144620713026669568689430873010625803728049924121243784502389097019475n
};
const w = await circuitAdd.calculateWitness(input, true);
await circuitAdd.assertOut(w, {
xout: F.e("6890855772600357754907169075114257697580319025794532037257385534741338397365"),
yout: F.e("4338620300185947561074059802482547481416142213883829469920100239455078257889")
xout: 6890855772600357754907169075114257697580319025794532037257385534741338397365n,
yout: 4338620300185947561074059802482547481416142213883829469920100239455078257889n
});
});
@ -61,17 +69,17 @@ describe("Baby Jub test", function () {
it("Should add 2 different numbers", async () => {
const input={
x1: F.e("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
y1: F.e("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
x2: F.e("16540640123574156134436876038791482806971768689494387082833631921987005038935"),
y2: F.e("20819045374670962167435360035096875258406992893633759881276124905556507972311")
x1: 17777552123799933955779906779655732241715742912184938656739573121738514868268n,
y1: 2626589144620713026669568689430873010625803728049924121243784502389097019475n,
x2: 16540640123574156134436876038791482806971768689494387082833631921987005038935n,
y2: 20819045374670962167435360035096875258406992893633759881276124905556507972311n
};
const w = await circuitAdd.calculateWitness(input, true);
await circuitAdd.assertOut(w, {
xout: F.e("7916061937171219682591368294088513039687205273691143098332585753343424131937"),
yout: F.e("14035240266687799601661095864649209771790948434046947201833777492504781204499")
xout: 7916061937171219682591368294088513039687205273691143098332585753343424131937n,
yout: 14035240266687799601661095864649209771790948434046947201833777492504781204499n
});
});
@ -105,7 +113,7 @@ describe("Baby Jub test", function () {
const w = await circuitPbk.calculateWitness(input, true);
await circuitPbk.assertOut(w, {Ax : A[0], Ay: A[1]});
await circuitPbk.assertOut(w, {Ax : F.toObject(A[0]), Ay: F.toObject(A[1])});
await circuitPbk.checkConstraints(w);
});

View File

@ -3,13 +3,10 @@ const path = require("path");
const wasm_tester = require("circom_tester").wasm;
const eddsa = require("circomlibjs").eddsa;
const babyJub = require("circomlibjs").babyjub;
const buildEddsa = require("circomlibjs").buildEddsa;
const buildBabyjub = require("circomlibjs").buildBabyjub;
const F1Field = require("ffjavascript").F1Field;
const Scalar = require("ffjavascript").Scalar;
exports.p = Scalar.fromString("21888242871839275222246405745257275088548364400416034343698204186575808495617");
const Fr = new F1Field(exports.p);
const assert = chai.assert;
@ -17,14 +14,14 @@ function print(circuit, w, s) {
console.log(s + ": " + w[circuit.getSignalIdx(s)]);
}
function buffer2bits(Fr, buff) {
function buffer2bits(buff) {
const res = [];
for (let i=0; i<buff.length; i++) {
for (let j=0; j<8; j++) {
if ((buff[i]>>j)&1) {
res.push(Fr.toString(Fr.one));
res.push(1n);
} else {
res.push(Fr.toString(Fr.zero));
res.push(0n);
}
}
}
@ -34,12 +31,22 @@ function buffer2bits(Fr, buff) {
describe("EdDSA test", function () {
let circuit;
let eddsa;
let babyJub;
let F;
this.timeout(100000);
before( async () => {
eddsa = await buildEddsa();
babyJub = await buildBabyjub();
F = babyJub.F;
circuit = await wasm_tester(path.join(__dirname, "circuits", "eddsa_test.circom"));
});
after(async () => {
globalThis.curve_bn128.terminate();
});
it("Sign a single 10 bytes from 0 to 9", async () => {
const msg = Buffer.from("00010203040506070809", "hex");
@ -52,17 +59,17 @@ describe("EdDSA test", function () {
const pPubKey = babyJub.packPoint(pubKey);
const signature = eddsa.sign(prvKey, msg);
const signature = eddsa.signPedersen(prvKey, msg);
const pSignature = eddsa.packSignature(signature);
const uSignature = eddsa.unpackSignature(pSignature);
assert(eddsa.verify(msg, uSignature, pubKey));
assert(eddsa.verifyPedersen(msg, uSignature, pubKey));
const msgBits = buffer2bits(Fr, msg);
const r8Bits = buffer2bits(Fr, pSignature.slice(0, 32));
const sBits = buffer2bits(Fr, pSignature.slice(32, 64));
const aBits = buffer2bits(Fr, pPubKey);
const msgBits = buffer2bits( msg);
const r8Bits = buffer2bits( pSignature.slice(0, 32));
const sBits = buffer2bits( pSignature.slice(32, 64));
const aBits = buffer2bits( pPubKey);
const w = await circuit.calculateWitness({A: aBits, R8: r8Bits, S: sBits, msg: msgBits}, true);

View File

@ -2,25 +2,32 @@ const chai = require("chai");
const path = require("path");
const wasm_tester = require("circom_tester").wasm;
const eddsa = require("circomlibjs").eddsa;
const F1Field = require("ffjavascript").F1Field;
const Scalar = require("ffjavascript").Scalar;
exports.p = Scalar.fromString("21888242871839275222246405745257275088548364400416034343698204186575808495617");
const Fr = new F1Field(exports.p);
const buildEddsa = require("circomlibjs").buildEddsa;
const buildBabyjub = require("circomlibjs").buildBabyjub;
const assert = chai.assert;
describe("EdDSA MiMC test", function () {
let circuit;
let eddsa;
let babyJub;
let F;
this.timeout(100000);
before( async () => {
eddsa = await buildEddsa();
babyJub = await buildBabyjub();
F = babyJub.F;
circuit = await wasm_tester(path.join(__dirname, "circuits", "eddsamimc_test.circom"));
});
after(async () => {
globalThis.curve_bn128.terminate();
});
it("Sign a single number", async () => {
const msg = Scalar.e(1234);
const msg = F.e(1234);
const prvKey = Buffer.from("0001020304050607080900010203040506070809000102030405060708090001", "hex");
@ -32,12 +39,12 @@ describe("EdDSA MiMC test", function () {
const w = await circuit.calculateWitness({
enabled: 1,
Ax: pubKey[0],
Ay: pubKey[1],
R8x: signature.R8[0],
R8y: signature.R8[1],
Ax: F.toObject(pubKey[0]),
Ay: F.toObject(pubKey[1]),
R8x: F.toObject(signature.R8[0]),
R8y: F.toObject(signature.R8[1]),
S: signature.S,
M: msg}, true);
M: F.toObject(msg)}, true);
await circuit.checkConstraints(w);
@ -45,7 +52,7 @@ describe("EdDSA MiMC test", function () {
});
it("Detect Invalid signature", async () => {
const msg = Scalar.e(1234);
const msg = F.e(1234);
const prvKey = Buffer.from("0001020304050607080900010203040506070809000102030405060708090001", "hex");
@ -58,12 +65,12 @@ describe("EdDSA MiMC test", function () {
try {
const w = await circuit.calculateWitness({
enabled: 1,
Ax: pubKey[0],
Ay: pubKey[1],
R8x: Fr.toString(Fr.add(Fr.e(signature.R8[0]), Fr.e(1))),
R8y: Fr.toString(Fr.e(signature.R8[1])),
Ax: F.toObject(pubKey[0]),
Ay: F.toObject(pubKey[1]),
R8x: F.toObject(F.add(signature.R8[0], F.e(1))),
R8y: F.toObject(signature.R8[1]),
S: signature.S,
M: msg}, true);
M: F.toObject(msg)}, true);
assert(false);
} catch(err) {
assert(err.message.includes("Assert Failed"));
@ -72,7 +79,7 @@ describe("EdDSA MiMC test", function () {
it("Test a dissabled circuit with a bad signature", async () => {
const msg = Scalar.e(1234);
const msg = F.e(1234);
const prvKey = Buffer.from("0001020304050607080900010203040506070809000102030405060708090001", "hex");
@ -85,12 +92,12 @@ describe("EdDSA MiMC test", function () {
const w = await circuit.calculateWitness({
enabled: 0,
Ax: pubKey[0],
Ay: pubKey[1],
R8x: Fr.toString(Fr.add(Fr.e(signature.R8[0]), Fr.e(1))),
R8y: Fr.toString(Fr.e(signature.R8[1])),
Ax: F.toObject(pubKey[0]),
Ay: F.toObject(pubKey[1]),
R8x: F.toObject(F.add(signature.R8[0], F.e(1))),
R8y: F.toObject(signature.R8[1]),
S: signature.S,
M: msg}, true);
M: F.toObject(msg)}, true);
await circuit.checkConstraints(w);

View File

@ -2,25 +2,31 @@ const chai = require("chai");
const path = require("path");
const wasm_tester = require("circom_tester").wasm;
const eddsa = require("circomlibjs").eddsa;
const F1Field = require("ffjavascript").F1Field;
const Scalar = require("ffjavascript").Scalar;
exports.p = Scalar.fromString("21888242871839275222246405745257275088548364400416034343698204186575808495617");
const Fr = new F1Field(exports.p);
const buildEddsa = require("circomlibjs").buildEddsa;
const buildBabyjub = require("circomlibjs").buildBabyjub;
const assert = chai.assert;
describe("EdDSA Poseidon test", function () {
let circuit;
let eddsa;
let babyJub;
let F;
this.timeout(100000);
before( async () => {
eddsa = await buildEddsa();
babyJub = await buildBabyjub();
F = babyJub.F;
circuit = await wasm_tester(path.join(__dirname, "circuits", "eddsaposeidon_test.circom"));
});
after(async () => {
globalThis.curve_bn128.terminate();
});
it("Sign a single number", async () => {
const msg = Scalar.e(1234);
const msg = F.e(1234);
const prvKey = Buffer.from("0001020304050607080900010203040506070809000102030405060708090001", "hex");
@ -32,12 +38,12 @@ describe("EdDSA Poseidon test", function () {
const input = {
enabled: 1,
Ax: pubKey[0],
Ay: pubKey[1],
R8x: signature.R8[0],
R8y: signature.R8[1],
Ax: F.toObject(pubKey[0]),
Ay: F.toObject(pubKey[1]),
R8x: F.toObject(signature.R8[0]),
R8y: F.toObject(signature.R8[1]),
S: signature.S,
M: msg
M: F.toObject(msg)
};
// console.log(JSON.stringify(utils.stringifyBigInts(input)));
@ -48,7 +54,7 @@ describe("EdDSA Poseidon test", function () {
});
it("Detect Invalid signature", async () => {
const msg = Scalar.e(1234);
const msg = F.e(1234);
const prvKey = Buffer.from("0001020304050607080900010203040506070809000102030405060708090001", "hex");
@ -61,12 +67,12 @@ describe("EdDSA Poseidon test", function () {
try {
await circuit.calculateWitness({
enabled: 1,
Ax: pubKey[0],
Ay: pubKey[1],
R8x: Fr.toString(Fr.add(Fr.e(signature.R8[0]), Fr.e(1))),
R8y: signature.R8[1],
Ax: F.toObject(pubKey[0]),
Ay: F.toObject(pubKey[1]),
R8x: F.toObject(F.add(signature.R8[0], F.e(1))),
R8y: F.toObject(signature.R8[1]),
S: signature.S,
M: msg}, true);
M: F.toObject(msg)}, true);
assert(false);
} catch(err) {
assert(err.message.includes("Assert Failed"));
@ -75,7 +81,7 @@ describe("EdDSA Poseidon test", function () {
it("Test a dissabled circuit with a bad signature", async () => {
const msg = Scalar.e(1234);
const msg = F.e(1234);
const prvKey = Buffer.from("0001020304050607080900010203040506070809000102030405060708090001", "hex");
@ -88,12 +94,12 @@ describe("EdDSA Poseidon test", function () {
const w = await circuit.calculateWitness({
enabled: 0,
Ax: pubKey[0],
Ay: pubKey[1],
R8x: Fr.toString(Fr.add(Fr.e(signature.R8[0]), Fr.e(1))),
R8y: signature.R8[1],
Ax: F.toObject(pubKey[0]),
Ay: F.toObject(pubKey[1]),
R8x: F.toObject(F.add(signature.R8[0], F.e(1))),
R8y: F.toObject(signature.R8[1]),
S: signature.S,
M: msg}, true);
M: F.toObject(msg)}, true);
await circuit.checkConstraints(w);
});

View File

@ -1,12 +1,9 @@
const chai = require("chai");
const path = require("path");
const wasm_tester = require("circom_tester").wasm;
const babyJub = require("circomlibjs").babyjub;
const buildBabyjub = require("circomlibjs").buildBabyjub;
const F1Field = require("ffjavascript").F1Field;
const Scalar = require("ffjavascript").Scalar;
exports.p = Scalar.fromString("21888242871839275222246405745257275088548364400416034343698204186575808495617");
const Fr = new F1Field(exports.p);
const assert = chai.assert;
@ -15,8 +12,18 @@ function print(circuit, w, s) {
}
describe("Exponentioation test", function () {
let babyJub;
let Fr;
this.timeout(100000);
before( async () => {
babyJub = await buildBabyjub();
Fr = babyJub.F;
});
after(async () => {
globalThis.curve_bn128.terminate();
});
it("Should generate the Exponentiation table in k=0", async () => {
const circuit = await wasm_tester(path.join(__dirname, "circuits", "escalarmulw4table_test.circom"));
@ -36,7 +43,7 @@ describe("Exponentioation test", function () {
for (let i=0; i<16; i++) {
expectedOut.push(dbl);
expectedOut.push([Fr.toObject(dbl[0]), Fr.toObject(dbl[1])]);
dbl = babyJub.addPoint(dbl,g);
}
@ -66,7 +73,7 @@ describe("Exponentioation test", function () {
const expectedOut = [];
for (let i=0; i<16; i++) {
expectedOut.push(dbl);
expectedOut.push([Fr.toObject(dbl[0]), Fr.toObject(dbl[1])]);
dbl = babyJub.addPoint(dbl,g);
}
@ -94,9 +101,9 @@ describe("Exponentioation test", function () {
c = babyJub.addPoint(c,g);
}
await circuit.assertOut(w, {out: c});
await circuit.assertOut(w, {out: [Fr.toObject(c[0]), Fr.toObject(c[1])] });
const w2 = await circuit.calculateWitness({"in": Fr.add(Fr.shl(Fr.e(1), Fr.e(252)),Fr.one)});
const w2 = await circuit.calculateWitness({"in": Scalar.add(Scalar.shl(Scalar.e(1), 252),Scalar.e(1))});
c = [g[0], g[1]];
for (let i=0; i<252;i++) {
@ -104,7 +111,7 @@ describe("Exponentioation test", function () {
}
c = babyJub.addPoint(c,g);
await circuit.assertOut(w2, {out: c});
await circuit.assertOut(w2, {out: [Fr.toObject(c[0]), Fr.toObject(c[1])] });
}).timeout(10000000);

View File

@ -1,11 +1,8 @@
const chai = require("chai");
const path = require("path");
const wasm_tester = require("circom_tester").wasm;
const babyjub = require("circomlibjs").babyjub;
const F1Field = require("ffjavascript").F1Field;
const buildBabyjub = require("circomlibjs").buildBabyjub;
const Scalar = require("ffjavascript").Scalar;
exports.p = Scalar.fromString("21888242871839275222246405745257275088548364400416034343698204186575808495617");
const Fr = new F1Field(exports.p);
const assert = chai.assert;
@ -14,13 +11,21 @@ function print(circuit, w, s) {
}
describe("Escalarmul test", function () {
let babyJub;
let Fr;
let circuit;
this.timeout(100000);
before( async() => {
babyJub = await buildBabyjub();
Fr = babyJub.F;
circuit = await wasm_tester(path.join(__dirname, "circuits", "escalarmulfix_test.circom"));
});
after(async () => {
globalThis.curve_bn128.terminate();
});
it("Should generate Same escalar mul", async () => {
@ -38,13 +43,13 @@ describe("Escalarmul test", function () {
await circuit.checkConstraints(w);
await circuit.assertOut(w, {out: babyjub.Base8});
await circuit.assertOut(w, {out: [Fr.toObject(babyJub.Base8[0]), Fr.toObject(babyJub.Base8[1])]});
});
it("Should generate scalar mul of a specific constant", async () => {
const s = Fr.e("2351960337287830298912035165133676222414898052661454064215017316447594616519");
const s = Scalar.e("2351960337287830298912035165133676222414898052661454064215017316447594616519");
const base8 = [
Fr.e("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
Fr.e("16950150798460657717958625567821834550301663161624707787222815936182638968203")
@ -54,9 +59,9 @@ describe("Escalarmul test", function () {
await circuit.checkConstraints(w);
const expectedRes = babyjub.mulPointEscalar(base8, s);
const expectedRes = babyJub.mulPointEscalar(base8, s);
await circuit.assertOut(w, {out: expectedRes});
await circuit.assertOut(w, {out: [Fr.toObject(expectedRes[0]), Fr.toObject(expectedRes[1])]});
});
@ -68,21 +73,21 @@ describe("Escalarmul test", function () {
];
for (let i=0; i<50; i++) {
const s = Fr.e(i);
const s = Scalar.e(i);
const w = await circuit.calculateWitness({"e": s}, true);
await circuit.checkConstraints(w);
const expectedRes = babyjub.mulPointEscalar(base8, s);
const expectedRes = babyJub.mulPointEscalar(base8, s);
await circuit.assertOut(w, {out: expectedRes});
await circuit.assertOut(w, {out: [Fr.toObject(expectedRes[0]), Fr.toObject(expectedRes[1])]});
}
});
it("If multiply by order should return 0", async () => {
const w = await circuit.calculateWitness({"e": babyjub.subOrder }, true);
const w = await circuit.calculateWitness({"e": babyJub.subOrder }, true);
await circuit.checkConstraints(w);

View File

@ -2,23 +2,28 @@ const chai = require("chai");
const path = require("path");
const wasm_tester = require("circom_tester").wasm;
const mimcjs = require("circomlibjs").mimc7;
const buildMimc7 = require("circomlibjs").buildMimc7;
describe("MiMC Circuit test", function () {
let circuit;
let mimc7;
this.timeout(100000);
before( async () => {
mimc7 = await buildMimc7();
circuit = await wasm_tester(path.join(__dirname, "circuits", "mimc_test.circom"));
});
after(async () => {
globalThis.curve_bn128.terminate();
});
it("Should check constrain", async () => {
const w = await circuit.calculateWitness({x_in: 1, k: 2}, true);
const res2 = mimcjs.hash(1,2,91);
const res2 = mimc7.hash(1,2,91);
await circuit.assertOut(w, {out: res2});
await circuit.assertOut(w, {out: mimc7.F.toObject(res2)});
await circuit.checkConstraints(w);
});

View File

@ -1,23 +1,34 @@
const path = require("path");
const wasm_tester = require("circom_tester").wasm;
const mimcjs = require("circomlibjs").mimcsponge;
const buildMimcSponge = require("circomlibjs").buildMimcSponge;
describe("MiMC Sponge Circuit test", function () {
let circuit;
let mimcSponge;
let F;
this.timeout(100000);
before( async () => {
mimcSponge = await buildMimcSponge();
F = mimcSponge.F;
});
after(async () => {
globalThis.curve_bn128.terminate();
});
it("Should check permutation", async () => {
circuit = await wasm_tester(path.join(__dirname, "circuits", "mimc_sponge_test.circom"));
const w = await circuit.calculateWitness({xL_in: 1, xR_in: 2, k: 3});
const out2 = mimcjs.hash(1,2,3);
const out2 = mimcSponge.hash(1,2,3);
await circuit.assertOut(w, {xL_out: out2.xL, xR_out: out2.xR});
await circuit.assertOut(w, {xL_out: F.toObject(out2.xL), xR_out: F.toObject(out2.xR)});
await circuit.checkConstraints(w);
@ -28,7 +39,9 @@ describe("MiMC Sponge Circuit test", function () {
const w = await circuit.calculateWitness({ins: [1, 2], k: 0});
const out2 = mimcjs.multiHash([1,2], 0, 3);
const out2 = mimcSponge.multiHash([1,2], 0, 3);
for (let i=0; i<out2.length; i++) out2[i] = F.toObject(out2[i]);
await circuit.assertOut(w, {outs: out2});

View File

@ -1,29 +1,34 @@
const chai = require("chai");
const path = require("path");
const babyJub = require("circomlibjs").babyjub;
const wasm_tester = require("circom_tester").wasm;
const F1Field = require("ffjavascript").F1Field;
const Scalar = require("ffjavascript").Scalar;
exports.p = Scalar.fromString("21888242871839275222246405745257275088548364400416034343698204186575808495617");
const Fr = new F1Field(exports.p);
const buildBabyjub = require("circomlibjs").buildBabyjub;
const assert = chai.assert;
describe("Montgomery test", function () {
let babyJub;
let Fr;
let circuitE2M;
let circuitM2E;
let circuitMAdd;
let circuitMDouble;
let g = [
Fr.e("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
Fr.e("16950150798460657717958625567821834550301663161624707787222815936182638968203")
];
let g;
let mg, mg2, g2, g3, mg3;
this.timeout(100000);
before( async() => {
babyJub = await buildBabyjub();
Fr = babyJub.F;
g = [
Fr.e("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
Fr.e("16950150798460657717958625567821834550301663161624707787222815936182638968203")
];
circuitE2M = await wasm_tester(path.join(__dirname, "circuits", "edwards2montgomery.circom"));
await circuitE2M.loadSymbols();
circuitM2E = await wasm_tester(path.join(__dirname, "circuits", "montgomery2edwards.circom"));
@ -33,10 +38,14 @@ describe("Montgomery test", function () {
circuitMDouble = await wasm_tester(path.join(__dirname, "circuits", "montgomerydouble.circom"));
await circuitMDouble.loadSymbols();
});
after(async () => {
globalThis.curve_bn128.terminate();
});
it("Convert Edwards to Montgomery and back again", async () => {
let w, xout, yout;
w = await circuitE2M.calculateWitness({ in: g}, true);
w = await circuitE2M.calculateWitness({ in: [Fr.toObject(g[0]), Fr.toObject(g[1])]}, true);
xout = w[circuitE2M.symbols["main.out[0]"].varIdx];
yout = w[circuitE2M.symbols["main.out[1]"].varIdx];
@ -48,8 +57,8 @@ describe("Montgomery test", function () {
xout = w[circuitM2E.symbols["main.out[0]"].varIdx];
yout = w[circuitM2E.symbols["main.out[1]"].varIdx];
assert(Fr.eq(xout, g[0]));
assert(Fr.eq(yout, g[1]));
assert(Fr.eq(Fr.e(xout), g[0]));
assert(Fr.eq(Fr.e(yout), g[1]));
});
it("Should double a point", async () => {
let w, xout, yout;
@ -69,8 +78,8 @@ describe("Montgomery test", function () {
yout = w[circuitM2E.symbols["main.out[1]"].varIdx];
assert(Fr.eq(xout, g2[0]));
assert(Fr.eq(yout, g2[1]));
assert(Fr.eq(Fr.e(xout), g2[0]));
assert(Fr.eq(Fr.e(yout), g2[1]));
});
it("Should add a point", async () => {
let w, xout, yout;
@ -89,7 +98,7 @@ describe("Montgomery test", function () {
xout = w[circuitM2E.symbols["main.out[0]"].varIdx];
yout = w[circuitM2E.symbols["main.out[1]"].varIdx];
assert(Fr.eq(xout, g3[0]));
assert(Fr.eq(yout, g3[1]));
assert(Fr.eq(Fr.e(xout), g3[0]));
assert(Fr.eq(Fr.e(yout), g3[1]));
});
});

View File

@ -1,32 +1,37 @@
const chai = require("chai");
const path = require("path");
const F1Field = require("ffjavascript").F1Field;
const Scalar = require("ffjavascript").Scalar;
exports.p = Scalar.fromString("21888242871839275222246405745257275088548364400416034343698204186575808495617");
const Fr = new F1Field(exports.p);
const wasm_tester = require("circom_tester").wasm;
const babyJub = require("circomlibjs").babyjub;
const buildBabyjub = require("circomlibjs").buildBabyjub;
const PBASE =
[
[Fr.e("10457101036533406547632367118273992217979173478358440826365724437999023779287"),Fr.e("19824078218392094440610104313265183977899662750282163392862422243483260492317")],
[Fr.e("2671756056509184035029146175565761955751135805354291559563293617232983272177"),Fr.e("2663205510731142763556352975002641716101654201788071096152948830924149045094")],
[Fr.e("5802099305472655231388284418920769829666717045250560929368476121199858275951"),Fr.e("5980429700218124965372158798884772646841287887664001482443826541541529227896")],
[Fr.e("7107336197374528537877327281242680114152313102022415488494307685842428166594"),Fr.e("2857869773864086953506483169737724679646433914307247183624878062391496185654")],
[Fr.e("20265828622013100949498132415626198973119240347465898028410217039057588424236"),Fr.e("1160461593266035632937973507065134938065359936056410650153315956301179689506")]
];
describe("Double Pedersen test", function() {
let babyJub;
let Fr;
let PBASE;
let circuit;
this.timeout(100000);
before( async() => {
babyJub = await buildBabyjub();
Fr = babyJub.F;
PBASE =
[
[Fr.e("10457101036533406547632367118273992217979173478358440826365724437999023779287"),Fr.e("19824078218392094440610104313265183977899662750282163392862422243483260492317")],
[Fr.e("2671756056509184035029146175565761955751135805354291559563293617232983272177"),Fr.e("2663205510731142763556352975002641716101654201788071096152948830924149045094")],
[Fr.e("5802099305472655231388284418920769829666717045250560929368476121199858275951"),Fr.e("5980429700218124965372158798884772646841287887664001482443826541541529227896")],
[Fr.e("7107336197374528537877327281242680114152313102022415488494307685842428166594"),Fr.e("2857869773864086953506483169737724679646433914307247183624878062391496185654")],
[Fr.e("20265828622013100949498132415626198973119240347465898028410217039057588424236"),Fr.e("1160461593266035632937973507065134938065359936056410650153315956301179689506")]
];
circuit = await wasm_tester(path.join(__dirname, "circuits", "pedersen_test.circom"));
});
after(async () => {
globalThis.curve_bn128.terminate();
});
it("Should pedersen at zero", async () => {
let w;
@ -41,7 +46,7 @@ describe("Double Pedersen test", function() {
w = await circuit.calculateWitness({ in: ["1", "0"]}, true);
await circuit.assertOut(w, {out: PBASE[0]});
await circuit.assertOut(w, {out: [Fr.toObject(PBASE[0][0]), Fr.toObject(PBASE[0][1])]});
});
it("Should pedersen at one second generator", async () => {
@ -49,7 +54,7 @@ describe("Double Pedersen test", function() {
w = await circuit.calculateWitness({ in: ["0", "1"]}, true);
await circuit.assertOut(w, {out: PBASE[1]});
await circuit.assertOut(w, {out: [Fr.toObject(PBASE[1][0]), Fr.toObject(PBASE[1][1])]});
});
it("Should pedersen at mixed generators", async () => {
@ -61,13 +66,13 @@ describe("Double Pedersen test", function() {
babyJub.mulPointEscalar(PBASE[1], 7)
);
await circuit.assertOut(w, {out: r});
await circuit.assertOut(w, {out: [Fr.toObject(r[0]), Fr.toObject(r[1])]});
});
it("Should pedersen all ones", async () => {
let w;
const allOnes = Fr.sub(Fr.shl(Fr.e("1"), Fr.e(250)), Fr.e("1"));
const allOnes = Scalar.sub(Scalar.shl(Scalar.e(1), 250), Scalar.e(1));
w = await circuit.calculateWitness({ in: [allOnes, allOnes]}, true);
@ -76,6 +81,6 @@ describe("Double Pedersen test", function() {
babyJub.mulPointEscalar(PBASE[1], allOnes)
);
await circuit.assertOut(w, {out: r2});
await circuit.assertOut(w, {out: [Fr.toObject(r2[0]), Fr.toObject(r2[1])]});
});
});

View File

@ -1,23 +1,29 @@
const path = require("path");
const F1Field = require("ffjavascript").F1Field;
const Scalar = require("ffjavascript").Scalar;
exports.p = Scalar.fromString("21888242871839275222246405745257275088548364400416034343698204186575808495617");
const Fr = new F1Field(exports.p);
const buildPedersenHash = require("circomlibjs").buildPedersenHash;
const buildBabyJub = require("circomlibjs").buildBabyjub;
const wasm_tester = require("circom_tester").wasm;
const babyJub = require("circomlibjs").babyjub;
const pedersen = require("circomlibjs").pedersenHash;
describe("Pedersen test", function() {
let babyJub
let pedersen;
let F;
let circuit;
this.timeout(100000);
before( async() => {
babyJub = await buildBabyJub();
F = babyJub.F;
pedersen = await buildPedersenHash();
circuit = await wasm_tester(path.join(__dirname, "circuits", "pedersen2_test.circom"));
});
after(async () => {
globalThis.curve_bn128.terminate();
});
it("Should pedersen at zero", async () => {
let w;
@ -29,16 +35,16 @@ describe("Pedersen test", function() {
const h = pedersen.hash(b);
const hP = babyJub.unpackPoint(h);
await circuit.assertOut(w, {out: hP});
await circuit.assertOut(w, {out: [F.toObject(hP[0]), F.toObject(hP[1])] });
});
it("Should pedersen with 253 ones", async () => {
let w;
const n = Fr.sub(Fr.shl(Fr.one, Fr.e(253)), Fr.one);
const n = F.e(Scalar.sub(Scalar.shl(Scalar.e(1), 253), Scalar.e(1)));
w = await circuit.calculateWitness({ in: n}, true);
w = await circuit.calculateWitness({ in: F.toObject(n)}, true);
const b = Buffer.alloc(32);
for (let i=0; i<31; i++) b[i] = 0xFF;
@ -47,7 +53,7 @@ describe("Pedersen test", function() {
const h = pedersen.hash(b);
const hP = babyJub.unpackPoint(h);
await circuit.assertOut(w, {out: hP});
await circuit.assertOut(w, {out: [F.toObject(hP[0]), F.toObject(hP[1])] });
});
});

View File

@ -1,17 +1,27 @@
const path = require("path");
const wasm_tester = require("circom_tester").wasm;
const buildBabyJub = require("circomlibjs").buildBabyjub;
const babyJub = require("circomlibjs").babyjub;
describe("Point 2 bits test", function() {
let babyJub;
let F;
let circuit;
this.timeout(100000);
before( async() => {
babyJub = await buildBabyJub();
F = babyJub.F;
circuit = await wasm_tester(path.join(__dirname, "circuits", "pointbits_loopback.circom"));
});
after(async () => {
globalThis.curve_bn128.terminate();
});
it("Should do the both convertions for 8Base", async () => {
const w = await circuit.calculateWitness({ in: babyJub.Base8}, true);
const w = await circuit.calculateWitness({ in: [F.toObject(babyJub.Base8[0]), F.toObject(babyJub.Base8[1])]}, true);
await circuit.checkConstraints(w);
});

View File

@ -2,28 +2,35 @@ const chai = require("chai");
const path = require("path");
const wasm_tester = require("circom_tester").wasm;
const poseidon = require("circomlibjs").poseidon;
const buildPoseidon = require("circomlibjs").buildPoseidon;
const assert = chai.assert;
describe("Poseidon Circuit test", function () {
let poseidon;
let F;
let circuit6;
let circuit3;
this.timeout(1000000);
before( async () => {
poseidon = await buildPoseidon();
F = poseidon.F;
circuit6 = await wasm_tester(path.join(__dirname, "circuits", "poseidon6_test.circom"));
circuit3 = await wasm_tester(path.join(__dirname, "circuits", "poseidon3_test.circom"));
});
after(async () => {
globalThis.curve_bn128.terminate();
});
it("Should check constrain of hash([1, 2]) t=6", async () => {
const w = await circuit6.calculateWitness({inputs: [1, 2, 0,0,0]}, true);
const res2 = poseidon([1,2,0,0,0]);
assert.equal("1018317224307729531995786483840663576608797660851238720571059489595066344487", res2.toString());
await circuit6.assertOut(w, {out : res2});
assert(F.eq(F.e("1018317224307729531995786483840663576608797660851238720571059489595066344487"), F.e(res2)));
await circuit6.assertOut(w, {out : F.toObject(res2)});
await circuit6.checkConstraints(w);
});
@ -32,8 +39,8 @@ describe("Poseidon Circuit test", function () {
const res2 = poseidon([3, 4,5,10,23]);
assert.equal("13034429309846638789535561449942021891039729847501137143363028890275222221409", res2.toString());
await circuit6.assertOut(w, {out : res2});
assert(F.eq(F.e("13034429309846638789535561449942021891039729847501137143363028890275222221409"), F.e(res2)));
await circuit6.assertOut(w, {out : F.toObject(res2)});
await circuit6.checkConstraints(w);
});
@ -43,8 +50,8 @@ describe("Poseidon Circuit test", function () {
const res2 = poseidon([1,2]);
assert.equal("7853200120776062878684798364095072458815029376092732009249414926327459813530", res2.toString());
await circuit3.assertOut(w, {out : res2});
assert(F.eq(F.e("7853200120776062878684798364095072458815029376092732009249414926327459813530"), F.e(res2)));
await circuit3.assertOut(w, {out : F.toObject(res2)});
await circuit3.checkConstraints(w);
});
@ -53,8 +60,8 @@ describe("Poseidon Circuit test", function () {
const res2 = poseidon([3, 4]);
assert.equal("14763215145315200506921711489642608356394854266165572616578112107564877678998", res2.toString());
await circuit3.assertOut(w, {out : res2});
assert(F.eq(F.e("14763215145315200506921711489642608356394854266165572616578112107564877678998"), F.e(res2)));
await circuit3.assertOut(w, {out : F.toObject(res2)});
await circuit3.checkConstraints(w);
});
});

View File

@ -3,10 +3,8 @@ const path = require("path");
const wasm_tester = require("circom_tester").wasm;
const F1Field = require("ffjavascript").F1Field;
const Scalar = require("ffjavascript").Scalar;
exports.p = Scalar.fromString("21888242871839275222246405745257275088548364400416034343698204186575808495617");
const Fr = new F1Field(exports.p);
const smt = require("circomlibjs").smt;
const newMemEmptyTrie = require("circomlibjs").newMemEmptyTrie;
const assert = chai.assert;
@ -14,83 +12,96 @@ function print(circuit, w, s) {
console.log(s + ": " + w[circuit.getSignalIdx(s)]);
}
async function testInsert(tree, key, value, circuit ) {
async function testInsert(tree, _key, _value, circuit ) {
const key = tree.F.e(_key);
const value = tree.F.e(_value)
const res = await tree.insert(key,value);
let siblings = res.siblings;
while (siblings.length<10) siblings.push(Fr.e(0));
for (let i=0; i<siblings.length; i++) siblings[i] = tree.F.toObject(siblings[i]);
while (siblings.length<10) siblings.push(0);
const w = await circuit.calculateWitness({
fnc: [1,0],
oldRoot: res.oldRoot,
oldRoot: tree.F.toObject(res.oldRoot),
siblings: siblings,
oldKey: res.isOld0 ? 0 : res.oldKey,
oldValue: res.isOld0 ? 0 : res.oldValue,
oldKey: res.isOld0 ? 0 : tree.F.toObject(res.oldKey),
oldValue: res.isOld0 ? 0 : tree.F.toObject(res.oldValue),
isOld0: res.isOld0 ? 1 : 0,
newKey: key,
newValue: value
newKey: tree.F.toObject(key),
newValue: tree.F.toObject(value)
}, true);
await circuit.checkConstraints(w);
await circuit.assertOut(w, {newRoot: res.newRoot});
await circuit.assertOut(w, {newRoot: tree.F.toObject(res.newRoot)});
}
async function testDelete(tree, key, circuit) {
async function testDelete(tree, _key, circuit) {
const key = tree.F.e(_key);
const res = await tree.delete(key);
let siblings = res.siblings;
while (siblings.length<10) siblings.push(Fr.e(0));
for (let i=0; i<siblings.length; i++) siblings[i] = tree.F.toObject(siblings[i]);
while (siblings.length<10) siblings.push(0);
const w = await circuit.calculateWitness({
fnc: [1,1],
oldRoot: res.oldRoot,
oldRoot: tree.F.toObject(res.oldRoot),
siblings: siblings,
oldKey: res.isOld0 ? 0 : res.oldKey,
oldValue: res.isOld0 ? 0 : res.oldValue,
oldKey: res.isOld0 ? 0 : tree.F.toObject(res.oldKey),
oldValue: res.isOld0 ? 0 : tree.F.toObject(res.oldValue),
isOld0: res.isOld0 ? 1 : 0,
newKey: res.delKey,
newValue: res.delValue
newKey: tree.F.toObject(res.delKey),
newValue: tree.F.toObject(res.delValue)
}, true);
await circuit.checkConstraints(w);
await circuit.assertOut(w, {newRoot: res.newRoot});
await circuit.assertOut(w, {newRoot: tree.F.toObject(res.newRoot)});
}
async function testUpdate(tree, key, newValue, circuit) {
async function testUpdate(tree, _key, _newValue, circuit) {
const key = tree.F.e(_key);
const newValue = tree.F.e(_newValue);
const res = await tree.update(key, newValue);
let siblings = res.siblings;
while (siblings.length<10) siblings.push(Fr.e(0));
for (let i=0; i<siblings.length; i++) siblings[i] = tree.F.toObject(siblings[i]);
while (siblings.length<10) siblings.push(0);
const w = await circuit.calculateWitness({
fnc: [0,1],
oldRoot: res.oldRoot,
oldRoot: tree.F.toObject(res.oldRoot),
siblings: siblings,
oldKey: res.oldKey,
oldValue: res.oldValue,
oldKey: tree.F.toObject(res.oldKey),
oldValue: tree.F.toObject(res.oldValue),
isOld0: 0,
newKey: res.newKey,
newValue: res.newValue
newKey: tree.F.toObject(res.newKey),
newValue: tree.F.toObject(res.newValue)
});
await circuit.checkConstraints(w);
await circuit.assertOut(w, {newRoot: res.newRoot});
await circuit.assertOut(w, {newRoot: tree.F.toObject(res.newRoot)});
}
describe("SMT Processor test", function () {
let circuit;
let tree;
let Fr;
this.timeout(10000000);
this.timeout(1000000000);
before( async () => {
circuit = await wasm_tester(path.join(__dirname, "circuits", "smtprocessor10_test.circom"));
await circuit.loadSymbols();
tree = await smt.newMemEmptyTrie();
tree = await newMemEmptyTrie();
Fr = tree.F;
});
after(async () => {
globalThis.curve_bn128.terminate();
});
it("Should verify an insert to an empty tree", async () => {
@ -115,12 +126,12 @@ describe("SMT Processor test", function () {
it("Should test convination of adding and removing 3 elements", async () => {
const keys = [Fr.e(8), Fr.e(9), Fr.e(32)];
const values = [Fr.e(88), Fr.e(99), Fr.e(3232)];
const tree1 = await smt.newMemEmptyTrie();
const tree2 = await smt.newMemEmptyTrie();
const tree3 = await smt.newMemEmptyTrie();
const tree4 = await smt.newMemEmptyTrie();
const tree5 = await smt.newMemEmptyTrie();
const tree6 = await smt.newMemEmptyTrie();
const tree1 = await newMemEmptyTrie();
const tree2 = await newMemEmptyTrie();
const tree3 = await newMemEmptyTrie();
const tree4 = await newMemEmptyTrie();
const tree5 = await newMemEmptyTrie();
const tree6 = await newMemEmptyTrie();
await testInsert(tree1,keys[0],values[0], circuit);
await testInsert(tree1,keys[1],values[1], circuit);
@ -173,7 +184,7 @@ describe("SMT Processor test", function () {
it("Should match a NOp with random vals", async () => {
let siblings = [];
while (siblings.length<10) siblings.push(Fr.e(88));
while (siblings.length<10) siblings.push(88);
const w = await circuit.calculateWitness({
fnc: [0,0],
oldRoot: 11,
@ -185,16 +196,16 @@ describe("SMT Processor test", function () {
newValue: 77
});
const root1 = w[circuit.symbols["main.oldRoot"].varIdx];
const root2 = w[circuit.symbols["main.newRoot"].varIdx];
const root1 = Fr.e(w[circuit.symbols["main.oldRoot"].varIdx]);
const root2 = Fr.e(w[circuit.symbols["main.newRoot"].varIdx]);
await circuit.checkConstraints(w);
assert(Fr.eq(root1, root2));
});
it("Should update an element", async () => {
const tree1 = await smt.newMemEmptyTrie();
const tree2 = await smt.newMemEmptyTrie();
const tree1 = await newMemEmptyTrie();
const tree2 = await newMemEmptyTrie();
await testInsert(tree1,8,88, circuit);
await testInsert(tree1,9,99, circuit);

View File

@ -1,12 +1,9 @@
const chai = require("chai");
const path = require("path");
const F1Field = require("ffjavascript").F1Field;
const Scalar = require("ffjavascript").Scalar;
exports.p = Scalar.fromString("21888242871839275222246405745257275088548364400416034343698204186575808495617");
const Fr = new F1Field(exports.p);
const wasm_tester = require("circom_tester").wasm;
const smt = require("circomlibjs").smt;
const newMemEmptyTrie = require("circomlibjs").newMemEmptyTrie;
const assert = chai.assert;
@ -14,46 +11,49 @@ function print(circuit, w, s) {
console.log(s + ": " + w[circuit.getSignalIdx(s)]);
}
async function testInclusion(tree, key, circuit) {
async function testInclusion(tree, _key, circuit) {
const key = tree.F.e(_key);
const res = await tree.find(key);
assert(res.found);
let siblings = res.siblings;
while (siblings.length<10) siblings.push(Fr.e(0));
for (let i=0; i<siblings.length; i++) siblings[i] = tree.F.toObject(siblings[i]);
while (siblings.length<10) siblings.push(0);
const w = await circuit.calculateWitness({
enabled: 1,
fnc: 0,
root: tree.root,
root: tree.F.toObject(tree.root),
siblings: siblings,
oldKey: 0,
oldValue: 0,
isOld0: 0,
key: key,
value: res.foundValue
key: tree.F.toObject(key),
value: tree.F.toObject(res.foundValue)
}, true);
await circuit.checkConstraints(w);
}
async function testExclusion(tree, key, circuit) {
async function testExclusion(tree, _key, circuit) {
const key = tree.F.e(_key);
const res = await tree.find(key);
assert(!res.found);
let siblings = res.siblings;
while (siblings.length<10) siblings.push(Fr.e(0));
for (let i=0; i<siblings.length; i++) siblings[i] = tree.F.toObject(siblings[i]);
while (siblings.length<10) siblings.push(0);
const w = await circuit.calculateWitness({
enabled: 1,
fnc: 1,
root: tree.root,
root: tree.F.toObject(tree.root),
siblings: siblings,
oldKey: res.isOld0 ? 0 : res.notFoundKey,
oldValue: res.isOld0 ? 0 : res.notFoundValue,
oldKey: res.isOld0 ? 0 : tree.F.toObject(res.notFoundKey),
oldValue: res.isOld0 ? 0 : tree.F.toObject(res.notFoundValue),
isOld0: res.isOld0 ? 1 : 0,
key: key,
key: tree.F.toObject(key),
value: 0
});
@ -62,6 +62,7 @@ async function testExclusion(tree, key, circuit) {
}
describe("SMT Verifier test", function () {
let Fr;
let circuit;
let tree;
@ -70,11 +71,15 @@ describe("SMT Verifier test", function () {
before( async () => {
circuit = await wasm_tester(path.join(__dirname, "circuits", "smtverifier10_test.circom"));
tree = await smt.newMemEmptyTrie();
tree = await newMemEmptyTrie();
Fr = tree.F;
await tree.insert(7,77);
await tree.insert(8,88);
await tree.insert(32,3232);
});
after(async () => {
globalThis.curve_bn128.terminate();
});
it("Check inclussion in a tree of 3", async () => {
await testInclusion(tree, 7, circuit);
@ -122,13 +127,13 @@ describe("SMT Verifier test", function () {
const e2fail_hi= Fr.e("17195092312975762537892237130737365903429674363577646686847513978084990105579");
const e2fail_hv= Fr.e("19650379996168153643111744440707177573540245771926102415571667548153444658179");
const tree1 = await smt.newMemEmptyTrie();
const tree1 = await newMemEmptyTrie();
await tree1.insert(e1_hi,e1_hv);
await tree1.insert(e2ok_hi,e2ok_hv);
await testInclusion(tree1, e2ok_hi, circuit);
const tree2 = await smt.newMemEmptyTrie();
const tree2 = await newMemEmptyTrie();
await tree2.insert(e1_hi,e1_hv);
await tree2.insert(e2fail_hi,e2fail_hv);