Blake to Blake2b and use of native big num

This commit is contained in:
Jordi Baylina
2020-04-18 22:33:59 +02:00
parent 6df6e9cb1c
commit b2ac4daaa7
42 changed files with 572 additions and 1191 deletions

View File

@@ -3,7 +3,11 @@ const path = require("path");
const assert = chai.assert;
const bigInt = require("big-integer");
const Scalar = require("ffjavascript").Scalar;
const F1Field = require("ffjavascript").F1Field;
const utils = require("ffjavascript").utils;
const q = Scalar.fromString("21888242871839275222246405745257275088548364400416034343698204186575808495617");
const F = new F1Field(q);
const tester = require("circom").tester;
@@ -14,16 +18,15 @@ function print(circuit, w, s) {
function getBits(v, n) {
const res = [];
for (let i=0; i<n; i++) {
if (v.shiftRight(i).isOdd()) {
res.push(bigInt.one);
if (Scalar.isOdd(Scalar.shr(v,i))) {
res.push(F.one);
} else {
res.push(bigInt.zero);
res.push(F.zero);
}
}
return res;
}
const q = bigInt("21888242871839275222246405745257275088548364400416034343698204186575808495617");
describe("Aliascheck test", function () {
this.timeout(100000);
@@ -35,17 +38,18 @@ describe("Aliascheck test", function () {
});
it("Satisfy the aliastest 0", async () => {
const inp = getBits(bigInt.zero, 254);
const inp = getBits(0, 254);
await cir.calculateWitness({in: inp}, true);
});
it("Satisfy the aliastest 3", async () => {
const inp = getBits(bigInt(3), 254);
const inp = getBits(3, 254);
await cir.calculateWitness({in: inp}, true);
});
it("Satisfy the aliastest q-1", async () => {
const inp = getBits(q.minus(bigInt.one), 254);
const inp = getBits(F.minusone, 254);
// console.log(JSON.stringify(utils.stringifyBigInts(inp)));
await cir.calculateWitness({in: inp}, true);
});
@@ -61,7 +65,7 @@ describe("Aliascheck test", function () {
it("Should not satisfy all ones", async () => {
const inp = getBits(bigInt(1).shiftLeft(254).minus(bigInt.one), 254);
const inp = getBits(Scalar.sub(Scalar.shl(1, 254) , 1) , 254);
try {
await cir.calculateWitness({in: inp}, true);
assert(false);

View File

@@ -1,14 +1,15 @@
const chai = require("chai");
const path = require("path");
const createBlakeHash = require("blake-hash");
const blake2b = require("blake2b");
const eddsa = require("../src/eddsa.js");
const F = require("../src/babyjub.js").F;
const assert = chai.assert;
const bigInt = require("big-integer");
const tester = require("circom").tester;
const utils = require("../src/utils.js");
const utils = require("ffjavascript").utils;
const Scalar = require("ffjavascript").Scalar;
describe("Baby Jub test", function () {
let circuitAdd;
@@ -28,31 +29,31 @@ describe("Baby Jub test", function () {
it("Should add point (0,1) and (0,1)", async () => {
const input={
x1: bigInt(0),
y1: bigInt(1),
x2: bigInt(0),
y2: bigInt(1)
x1: F.e(0),
y1: F.e(1),
x2: F.e(0),
y2: F.e(1)
};
const w = await circuitAdd.calculateWitness(input, true);
await circuitAdd.assertOut(w, {xout: bigInt(0), yout: bigInt(1)});
await circuitAdd.assertOut(w, {xout: F.e(0), yout: F.e(1)});
});
it("Should add 2 same numbers", async () => {
const input={
x1: bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
y1: bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
x2: bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
y2: bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475")
x1: F.e("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
y1: F.e("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
x2: F.e("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
y2: F.e("2626589144620713026669568689430873010625803728049924121243784502389097019475")
};
const w = await circuitAdd.calculateWitness(input, true);
await circuitAdd.assertOut(w, {
xout: bigInt("6890855772600357754907169075114257697580319025794532037257385534741338397365"),
yout: bigInt("4338620300185947561074059802482547481416142213883829469920100239455078257889")
xout: F.e("6890855772600357754907169075114257697580319025794532037257385534741338397365"),
yout: F.e("4338620300185947561074059802482547481416142213883829469920100239455078257889")
});
});
@@ -60,17 +61,17 @@ describe("Baby Jub test", function () {
it("Should add 2 different numbers", async () => {
const input={
x1: bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
y1: bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
x2: bigInt("16540640123574156134436876038791482806971768689494387082833631921987005038935"),
y2: bigInt("20819045374670962167435360035096875258406992893633759881276124905556507972311")
x1: F.e("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
y1: F.e("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
x2: F.e("16540640123574156134436876038791482806971768689494387082833631921987005038935"),
y2: F.e("20819045374670962167435360035096875258406992893633759881276124905556507972311")
};
const w = await circuitAdd.calculateWitness(input, true);
await circuitAdd.assertOut(w, {
xout: bigInt("7916061937171219682591368294088513039687205273691143098332585753343424131937"),
yout: bigInt("14035240266687799601661095864649209771790948434046947201833777492504781204499")
xout: F.e("7916061937171219682591368294088513039687205273691143098332585753343424131937"),
yout: F.e("14035240266687799601661095864649209771790948434046947201833777492504781204499")
});
});
@@ -93,8 +94,8 @@ describe("Baby Jub test", function () {
it("Should extract the public key from the private one", async () => {
const rawpvk = Buffer.from("0001020304050607080900010203040506070809000102030405060708090021", "hex");
const pvk = eddsa.pruneBuffer(createBlakeHash("blake512").update(rawpvk).digest().slice(0,32));
const S = utils.leBuff2int(pvk).shiftRight(3);
const pvk = eddsa.pruneBuffer(Buffer.from(blake2b(64).update(rawpvk).digest().slice(0,32)));
const S = Scalar.shr(utils.leBuff2int(pvk), 3);
const A = eddsa.prv2pub(rawpvk);

View File

@@ -1,6 +1,6 @@
const chai = require("chai");
const bigInt = require("big-integer");
const babyjub = require("../src/babyjub.js");
const Scalar = require("ffjavascript").Scalar;
const assert = chai.assert;
@@ -14,16 +14,16 @@ describe("Baby Jub js test", function () {
it("Should add point (0,1) and (0,1)", () => {
const p1 = [
bigInt(0),
bigInt(1)];
babyjub.F.e(0),
babyjub.F.e(1)];
const p2 = [
bigInt(0),
bigInt(1)
babyjub.F.e(0),
babyjub.F.e(1)
];
const out = babyjub.addPoint(p1, p2);
assert(out[0].equals(0));
assert(out[1].equals(1));
assert(babyjub.F.eq(out[0], babyjub.F.zero));
assert(babyjub.F.eq(out[1], babyjub.F.one));
});
it("Should base be 8*generator", () => {
@@ -32,50 +32,50 @@ describe("Baby Jub js test", function () {
res = babyjub.addPoint(res, res);
res = babyjub.addPoint(res, res);
assert(res[0].equals(babyjub.Base8[0]));
assert(res[1].equals(babyjub.Base8[1]));
assert(babyjub.F.eq(res[0], babyjub.Base8[0]));
assert(babyjub.F.eq(res[1], babyjub.Base8[1]));
});
it("Should add 2 same numbers", () => {
const p1 = [
bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
babyjub.F.e("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
babyjub.F.e("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
];
const p2 = [
bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
babyjub.F.e("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
babyjub.F.e("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
];
const out = babyjub.addPoint(p1, p2);
assert(out[0].equals(bigInt("6890855772600357754907169075114257697580319025794532037257385534741338397365")));
assert(out[1].equals(bigInt("4338620300185947561074059802482547481416142213883829469920100239455078257889")));
assert(babyjub.F.eq(out[0], babyjub.F.e("6890855772600357754907169075114257697580319025794532037257385534741338397365")));
assert(babyjub.F.eq(out[1], babyjub.F.e("4338620300185947561074059802482547481416142213883829469920100239455078257889")));
});
it("Should add 2 different numbers", () => {
const p1 = [
bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
babyjub.F.e("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
babyjub.F.e("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
];
const p2 = [
bigInt("16540640123574156134436876038791482806971768689494387082833631921987005038935"),
bigInt("20819045374670962167435360035096875258406992893633759881276124905556507972311"),
babyjub.F.e("16540640123574156134436876038791482806971768689494387082833631921987005038935"),
babyjub.F.e("20819045374670962167435360035096875258406992893633759881276124905556507972311"),
];
const out = babyjub.addPoint(p1, p2);
assert(babyjub.F.eq(out[0], babyjub.F.e("7916061937171219682591368294088513039687205273691143098332585753343424131937")));
assert(babyjub.F.eq(out[1], babyjub.F.e("14035240266687799601661095864649209771790948434046947201833777492504781204499")));
assert(out[0].equals(bigInt("7916061937171219682591368294088513039687205273691143098332585753343424131937")));
assert(out[1].equals(bigInt("14035240266687799601661095864649209771790948434046947201833777492504781204499")));
});
it("should mulPointEscalar 0", () => {
const p = [
bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
babyjub.F.e("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
babyjub.F.e("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
];
const r = babyjub.mulPointEscalar(p, bigInt("3"));
const r = babyjub.mulPointEscalar(p, 3);
let r2 = babyjub.addPoint(p, p);
r2 = babyjub.addPoint(r2, p);
assert.equal(r2[0].toString(), r[0].toString());
@@ -86,62 +86,62 @@ describe("Baby Jub js test", function () {
it("should mulPointEscalar 1", () => {
const p = [
bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
babyjub.F.e("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
babyjub.F.e("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
];
const r = babyjub.mulPointEscalar(p, bigInt("14035240266687799601661095864649209771790948434046947201833777492504781204499"));
const r = babyjub.mulPointEscalar(p, Scalar.fromString("14035240266687799601661095864649209771790948434046947201833777492504781204499"));
assert.equal(r[0].toString(), "17070357974431721403481313912716834497662307308519659060910483826664480189605");
assert.equal(r[1].toString(), "4014745322800118607127020275658861516666525056516280575712425373174125159339");
});
it("should mulPointEscalar 2", () => {
const p = [
bigInt("6890855772600357754907169075114257697580319025794532037257385534741338397365"),
bigInt("4338620300185947561074059802482547481416142213883829469920100239455078257889"),
babyjub.F.e("6890855772600357754907169075114257697580319025794532037257385534741338397365"),
babyjub.F.e("4338620300185947561074059802482547481416142213883829469920100239455078257889"),
];
const r = babyjub.mulPointEscalar(p, bigInt("20819045374670962167435360035096875258406992893633759881276124905556507972311"));
const r = babyjub.mulPointEscalar(p, Scalar.fromString("20819045374670962167435360035096875258406992893633759881276124905556507972311"));
assert.equal(r[0].toString(), "13563888653650925984868671744672725781658357821216877865297235725727006259983");
assert.equal(r[1].toString(), "8442587202676550862664528699803615547505326611544120184665036919364004251662");
});
it("should inCurve 1", () => {
const p = [
bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
babyjub.F.e("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
babyjub.F.e("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
];
assert(babyjub.inCurve(p));
});
it("should inCurve 2", () => {
const p = [
bigInt("6890855772600357754907169075114257697580319025794532037257385534741338397365"),
bigInt("4338620300185947561074059802482547481416142213883829469920100239455078257889"),
babyjub.F.e("6890855772600357754907169075114257697580319025794532037257385534741338397365"),
babyjub.F.e("4338620300185947561074059802482547481416142213883829469920100239455078257889"),
];
assert(babyjub.inCurve(p));
});
it("should inSubgroup 1", () => {
const p = [
bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
babyjub.F.e("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
babyjub.F.e("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
];
assert(babyjub.inSubgroup(p));
});
it("should inSubgroup 2", () => {
const p = [
bigInt("6890855772600357754907169075114257697580319025794532037257385534741338397365"),
bigInt("4338620300185947561074059802482547481416142213883829469920100239455078257889"),
babyjub.F.e("6890855772600357754907169075114257697580319025794532037257385534741338397365"),
babyjub.F.e("4338620300185947561074059802482547481416142213883829469920100239455078257889"),
];
assert(babyjub.inSubgroup(p));
});
it("should packPoint - unpackPoint 1", () => {
const p = [
bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
babyjub.F.e("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
babyjub.F.e("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
];
const buf = babyjub.packPoint(p);
assert.equal(buf.toString("hex"), "53b81ed5bffe9545b54016234682e7b2f699bd42a5e9eae27ff4051bc698ce85");
@@ -152,8 +152,8 @@ describe("Baby Jub js test", function () {
it("should packPoint - unpackPoint 2", () => {
const p = [
bigInt("6890855772600357754907169075114257697580319025794532037257385534741338397365"),
bigInt("4338620300185947561074059802482547481416142213883829469920100239455078257889"),
babyjub.F.e("6890855772600357754907169075114257697580319025794532037257385534741338397365"),
babyjub.F.e("4338620300185947561074059802482547481416142213883829469920100239455078257889"),
];
const buf = babyjub.packPoint(p);
assert.equal(buf.toString("hex"), "e114eb17eddf794f063a68fecac515e3620e131976108555735c8b0773929709");

View File

@@ -1,6 +1,7 @@
const path = require("path");
const bigInt = require("big-integer");
const Fr = require("ffjavascript").bn128.Fr;
const Scalar = require("ffjavascript").Scalar;
const tester = require("circom").tester;
function print(circuit, w, s) {
@@ -8,15 +9,16 @@ function print(circuit, w, s) {
}
async function checkSub(_a,_b, circuit) {
let a=bigInt(_a);
let b=bigInt(_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));
let a=Scalar.e(_a);
let b=Scalar.e(_b);
if (Scalar.lt(a, 0)) a = Scalar.add(a, Scalar.shl(1, 16));
if (Scalar.lt(b, 0)) b = Scalar.add(b, Scalar.shl(1, 16));
const w = await circuit.calculateWitness({a: a, b: b}, true);
let res = a.minus(b);
if (res.lesser(bigInt.zero)) res = res.add(bigInt.one.shiftLeft(16));
await circuit.assertOut(w, {out: bigInt(res)});
let res = Scalar.sub(a, b);
if (Scalar.lt(res, 0)) res = Scalar.add(res, Scalar.shl(1, 16));
await circuit.assertOut(w, {out: res});
}
describe("BinSub test", function () {

View File

@@ -3,7 +3,7 @@ const path = require("path");
const tester = require("circom").tester;
const bigInt = require("big-integer");
const Fr = require("ffjavascript").bn128.Fr;
const assert = chai.assert;
@@ -18,10 +18,10 @@ describe("Binary sum test", function () {
assert.equal(circuit.nVars, 2);
assert.equal(circuit.constraints.length, 1);
const witness = await circuit.calculateWitness({ "in": bigInt("d807aa98", 16)}, true);
const witness = await circuit.calculateWitness({ "in": Fr.e("d807aa98", 16)}, true);
assert(witness[0].equals(bigInt(1)));
assert(witness[1].equals(bigInt("d807aa98", 16)));
assert(Fr.eq(witness[0],Fr.e(1)));
assert(Fr.eq(witness[1],Fr.e("d807aa98", 16)));
});
it("Should create a sum circuit", async () => {
const circuit = await tester(path.join(__dirname, "circuits", "sum_test.circom"));
@@ -31,7 +31,7 @@ describe("Binary sum test", function () {
const witness = await circuit.calculateWitness({ "a": "111", "b": "222" }, true);
assert(witness[0].equals(bigInt(1)));
assert(witness[1].equals(bigInt("333")));
assert(Fr.eq(witness[0],Fr.e(1)));
assert(Fr.eq(witness[1],Fr.e("333")));
});
});

View File

@@ -1,258 +0,0 @@
{
"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"
]
}

View File

@@ -3,7 +3,7 @@ const path = require("path");
const tester = require("circom").tester;
const bigInt = require("big-integer");
const Fr = require("ffjavascript").bn128.Fr;
const assert = chai.assert;
@@ -16,60 +16,61 @@ describe("Comparators test", function () {
let witness;
witness = await circuit.calculateWitness({ "in": 111}, true);
assert(witness[0].equals(bigInt(1)));
assert(witness[1].equals(bigInt(0)));
assert(Fr.eq(witness[0], Fr.e(1)));
assert(Fr.eq(witness[1], Fr.e(0)));
witness = await circuit.calculateWitness({ "in": 0 }, true);
assert(witness[0].equals(bigInt(1)));
assert(witness[1].equals(bigInt(1)));
assert(Fr.eq(witness[0], Fr.e(1)));
assert(Fr.eq(witness[1], Fr.e(1)));
});
it("Should create a isequal circuit", async() => {
const circuit = await tester(path.join(__dirname, "circuits", "isequal.circom"));
let witness;
witness = await circuit.calculateWitness({ "in": [111,222] }, true);
assert(witness[0].equals(bigInt(1)));
assert(witness[1].equals(bigInt(0)));
assert(Fr.eq(witness[0], Fr.e(1)));
assert(Fr.eq(witness[1], Fr.e(0)));
witness = await circuit.calculateWitness({ "in": [444,444] }, true);
assert(witness[0].equals(bigInt(1)));
assert(witness[1].equals(bigInt(1)));
assert(Fr.eq(witness[0], Fr.e(1)));
assert(Fr.eq(witness[1], Fr.e(1)));
});
it("Should create a comparison lessthan", async() => {
const circuit = await tester(path.join(__dirname, "circuits", "lessthan.circom"));
let witness;
witness = await circuit.calculateWitness({ "in": [333,444] }), true;
assert(witness[0].equals(bigInt(1)));
assert(witness[1].equals(bigInt(1)));
assert(Fr.eq(witness[0], Fr.e(1)));
assert(Fr.eq(witness[1], Fr.e(1)));
witness = await circuit.calculateWitness({ "in":[1,1] }, true);
assert(witness[0].equals(bigInt(1)));
assert(witness[1].equals(bigInt(0)));
assert(Fr.eq(witness[0], Fr.e(1)));
assert(Fr.eq(witness[1], Fr.e(0)));
witness = await circuit.calculateWitness({ "in": [661, 660] }, true);
assert(witness[0].equals(bigInt(1)));
assert(witness[1].equals(bigInt(0)));
assert(Fr.eq(witness[0], Fr.e(1)));
assert(Fr.eq(witness[1], Fr.e(0)));
witness = await circuit.calculateWitness({ "in": [0, 1] }, true);
assert(witness[0].equals(bigInt(1)));
assert(witness[1].equals(bigInt(1)));
assert(Fr.eq(witness[0], Fr.e(1)));
assert(Fr.eq(witness[1], Fr.e(1)));
witness = await circuit.calculateWitness({ "in": [0, 444] }, true);
assert(witness[0].equals(bigInt(1)));
assert(witness[1].equals(bigInt(1)));
assert(Fr.eq(witness[0], Fr.e(1)));
assert(Fr.eq(witness[1], Fr.e(1)));
witness = await circuit.calculateWitness({ "in": [1, 0] }, true);
assert(witness[0].equals(bigInt(1)));
assert(witness[1].equals(bigInt(0)));
assert(Fr.eq(witness[0], Fr.e(1)));
assert(Fr.eq(witness[1], Fr.e(0)));
witness = await circuit.calculateWitness({ "in": [555, 0] }, true);
assert(witness[0].equals(bigInt(1)));
assert(witness[1].equals(bigInt(0)));
assert(Fr.eq(witness[0], Fr.e(1)));
assert(Fr.eq(witness[1], Fr.e(0)));
witness = await circuit.calculateWitness({ "in": [0, 0] }, true);
assert(witness[0].equals(bigInt(1)));
assert(witness[1].equals(bigInt(0)));
assert(Fr.eq(witness[0], Fr.e(1)));
assert(Fr.eq(witness[1], Fr.e(0)));
});
it("Should create a comparison lesseqthan", async() => {
@@ -77,36 +78,36 @@ describe("Comparators test", function () {
let witness;
witness = await circuit.calculateWitness({ "in": [333,444] }, true);
assert(witness[0].equals(bigInt(1)));
assert(witness[1].equals(bigInt(1)));
assert(Fr.eq(witness[0], Fr.e(1)));
assert(Fr.eq(witness[1], Fr.e(1)));
witness = await circuit.calculateWitness({ "in":[1,1] }, true);
assert(witness[0].equals(bigInt(1)));
assert(witness[1].equals(bigInt(1)));
assert(Fr.eq(witness[0], Fr.e(1)));
assert(Fr.eq(witness[1], Fr.e(1)));
witness = await circuit.calculateWitness({ "in": [661, 660] }, true);
assert(witness[0].equals(bigInt(1)));
assert(witness[1].equals(bigInt(0)));
assert(Fr.eq(witness[0], Fr.e(1)));
assert(Fr.eq(witness[1], Fr.e(0)));
witness = await circuit.calculateWitness({ "in": [0, 1] }, true);
assert(witness[0].equals(bigInt(1)));
assert(witness[1].equals(bigInt(1)));
assert(Fr.eq(witness[0], Fr.e(1)));
assert(Fr.eq(witness[1], Fr.e(1)));
witness = await circuit.calculateWitness({ "in": [0, 444] }, true);
assert(witness[0].equals(bigInt(1)));
assert(witness[1].equals(bigInt(1)));
assert(Fr.eq(witness[0], Fr.e(1)));
assert(Fr.eq(witness[1], Fr.e(1)));
witness = await circuit.calculateWitness({ "in": [1, 0] }, true);
assert(witness[0].equals(bigInt(1)));
assert(witness[1].equals(bigInt(0)));
assert(Fr.eq(witness[0], Fr.e(1)));
assert(Fr.eq(witness[1], Fr.e(0)));
witness = await circuit.calculateWitness({ "in": [555, 0] }, true);
assert(witness[0].equals(bigInt(1)));
assert(witness[1].equals(bigInt(0)));
assert(Fr.eq(witness[0], Fr.e(1)));
assert(Fr.eq(witness[1], Fr.e(0)));
witness = await circuit.calculateWitness({ "in": [0, 0] }, true);
assert(witness[0].equals(bigInt(1)));
assert(witness[1].equals(bigInt(1)));
assert(Fr.eq(witness[0], Fr.e(1)));
assert(Fr.eq(witness[1], Fr.e(1)));
});
it("Should create a comparison greaterthan", async() => {
@@ -114,71 +115,71 @@ describe("Comparators test", function () {
let witness;
witness = await circuit.calculateWitness({ "in": [333,444] }, true);
assert(witness[0].equals(bigInt(1)));
assert(witness[1].equals(bigInt(0)));
assert(Fr.eq(witness[0], Fr.e(1)));
assert(Fr.eq(witness[1], Fr.e(0)));
witness = await circuit.calculateWitness({ "in":[1,1] }, true);
assert(witness[0].equals(bigInt(1)));
assert(witness[1].equals(bigInt(0)));
assert(Fr.eq(witness[0], Fr.e(1)));
assert(Fr.eq(witness[1], Fr.e(0)));
witness = await circuit.calculateWitness({ "in": [661, 660] }, true);
assert(witness[0].equals(bigInt(1)));
assert(witness[1].equals(bigInt(1)));
assert(Fr.eq(witness[0], Fr.e(1)));
assert(Fr.eq(witness[1], Fr.e(1)));
witness = await circuit.calculateWitness({ "in": [0, 1] }, true);
assert(witness[0].equals(bigInt(1)));
assert(witness[1].equals(bigInt(0)));
assert(Fr.eq(witness[0], Fr.e(1)));
assert(Fr.eq(witness[1], Fr.e(0)));
witness = await circuit.calculateWitness({ "in": [0, 444] }, true);
assert(witness[0].equals(bigInt(1)));
assert(witness[1].equals(bigInt(0)));
assert(Fr.eq(witness[0], Fr.e(1)));
assert(Fr.eq(witness[1], Fr.e(0)));
witness = await circuit.calculateWitness({ "in": [1, 0] }, true);
assert(witness[0].equals(bigInt(1)));
assert(witness[1].equals(bigInt(1)));
assert(Fr.eq(witness[0], Fr.e(1)));
assert(Fr.eq(witness[1], Fr.e(1)));
witness = await circuit.calculateWitness({ "in": [555, 0] }, true);
assert(witness[0].equals(bigInt(1)));
assert(witness[1].equals(bigInt(1)));
assert(Fr.eq(witness[0], Fr.e(1)));
assert(Fr.eq(witness[1], Fr.e(1)));
witness = await circuit.calculateWitness({ "in": [0, 0] }, true);
assert(witness[0].equals(bigInt(1)));
assert(witness[1].equals(bigInt(0)));
assert(Fr.eq(witness[0], Fr.e(1)));
assert(Fr.eq(witness[1], Fr.e(0)));
});
it("Should create a comparison greatereqthan", async() => {
const circuit = await tester(path.join(__dirname, "circuits", "greatereqthan.circom"));
let witness;
witness = await circuit.calculateWitness({ "in": [333,444] }, true);
assert(witness[0].equals(bigInt(1)));
assert(witness[1].equals(bigInt(0)));
assert(Fr.eq(witness[0], Fr.e(1)));
assert(Fr.eq(witness[1], Fr.e(0)));
witness = await circuit.calculateWitness({ "in":[1,1] }, true);
assert(witness[0].equals(bigInt(1)));
assert(witness[1].equals(bigInt(1)));
assert(Fr.eq(witness[0], Fr.e(1)));
assert(Fr.eq(witness[1], Fr.e(1)));
witness = await circuit.calculateWitness({ "in": [661, 660] }, true);
assert(witness[0].equals(bigInt(1)));
assert(witness[1].equals(bigInt(1)));
assert(Fr.eq(witness[0], Fr.e(1)));
assert(Fr.eq(witness[1], Fr.e(1)));
witness = await circuit.calculateWitness({ "in": [0, 1] }, true);
assert(witness[0].equals(bigInt(1)));
assert(witness[1].equals(bigInt(0)));
assert(Fr.eq(witness[0], Fr.e(1)));
assert(Fr.eq(witness[1], Fr.e(0)));
witness = await circuit.calculateWitness({ "in": [0, 444] }, true);
assert(witness[0].equals(bigInt(1)));
assert(witness[1].equals(bigInt(0)));
assert(Fr.eq(witness[0], Fr.e(1)));
assert(Fr.eq(witness[1], Fr.e(0)));
witness = await circuit.calculateWitness({ "in": [1, 0] }, true);
assert(witness[0].equals(bigInt(1)));
assert(witness[1].equals(bigInt(1)));
assert(Fr.eq(witness[0], Fr.e(1)));
assert(Fr.eq(witness[1], Fr.e(1)));
witness = await circuit.calculateWitness({ "in": [555, 0] }, true);
assert(witness[0].equals(bigInt(1)));
assert(witness[1].equals(bigInt(1)));
assert(Fr.eq(witness[0], Fr.e(1)));
assert(Fr.eq(witness[1], Fr.e(1)));
witness = await circuit.calculateWitness({ "in": [0, 0] }, true);
assert(witness[0].equals(bigInt(1)));
assert(witness[1].equals(bigInt(1)));
assert(Fr.eq(witness[0], Fr.e(1)));
assert(Fr.eq(witness[1], Fr.e(1)));
});
});

View File

@@ -2,7 +2,7 @@ const chai = require("chai");
const path = require("path");
const tester = require("circom").tester;
const bigInt = require("big-integer");
const Fr = require("ffjavascript").bn128.Fr;
const eddsa = require("../src/eddsa.js");
const babyJub = require("../src/babyjub.js");
@@ -18,9 +18,9 @@ function buffer2bits(buff) {
for (let i=0; i<buff.length; i++) {
for (let j=0; j<8; j++) {
if ((buff[i]>>j)&1) {
res.push(bigInt.one);
res.push(Fr.one);
} else {
res.push(bigInt.zero);
res.push(Fr.zero);
}
}
}

View File

@@ -5,8 +5,7 @@ const babyJub = require("../src/babyjub.js");
const assert = chai.assert;
const bigInt = require("big-integer");
const utils = require("../src/utils.js");
const utils = require("ffjavascript").utils;
describe("EdDSA js test", function () {
@@ -23,24 +22,24 @@ describe("EdDSA js test", function () {
const pubKey = eddsa.prv2pub(prvKey);
assert.equal(pubKey[0].toString(),
"13277427435165878497778222415993513565335242147425444199013288855685581939618");
"17579234973106307986399040784563986669343100608865726413246909559198451825625");
assert.equal(pubKey[1].toString(),
"13622229784656158136036771217484571176836296686641868549125388198837476602820");
"21581828029826859845363968476425861244058376747493285816141526544272562145486");
const pPubKey = babyJub.packPoint(pubKey);
const signature = eddsa.signMiMC(prvKey, msg);
assert.equal(signature.R8[0].toString(),
"11384336176656855268977457483345535180380036354188103142384839473266348197733");
"12672422877531089818651367820728973438446851190471722610781936061829103362897");
assert.equal(signature.R8[1].toString(),
"15383486972088797283337779941324724402501462225528836549661220478783371668959");
"12052234579439634484237590306927118446073354173341433290934144373261241958718");
assert.equal(signature.S.toString(),
"2523202440825208709475937830811065542425109372212752003460238913256192595070");
"1582013862333331285840015273849085014739146294568319205499642618291614907374");
const pSignature = eddsa.packSignature(signature);
assert.equal(pSignature.toString("hex"), ""+
"dfedb4315d3f2eb4de2d3c510d7a987dcab67089c8ace06308827bf5bcbe02a2"+
"7ed40dab29bf993c928e789d007387998901a24913d44fddb64b1f21fc149405");
"3e417cd811f9c9c545a680b962e45d22ccb62b2284b4fe4bbc9fdb50b252a59a" +
"eefbebe2b895393fa0e9b5b31b19e65a63fee5d7b6261d8d5b6b847c5b637f03");
const uSignature = eddsa.unpackSignature(pSignature);
assert(eddsa.verifyMiMC(msg, uSignature, pubKey));
@@ -56,24 +55,24 @@ describe("EdDSA js test", function () {
const pubKey = eddsa.prv2pub(prvKey);
assert.equal(pubKey[0].toString(),
"13277427435165878497778222415993513565335242147425444199013288855685581939618");
"17579234973106307986399040784563986669343100608865726413246909559198451825625");
assert.equal(pubKey[1].toString(),
"13622229784656158136036771217484571176836296686641868549125388198837476602820");
"21581828029826859845363968476425861244058376747493285816141526544272562145486");
const pPubKey = babyJub.packPoint(pubKey);
const signature = eddsa.signPoseidon(prvKey, msg);
assert.equal(signature.R8[0].toString(),
"11384336176656855268977457483345535180380036354188103142384839473266348197733");
"12672422877531089818651367820728973438446851190471722610781936061829103362897");
assert.equal(signature.R8[1].toString(),
"15383486972088797283337779941324724402501462225528836549661220478783371668959");
"12052234579439634484237590306927118446073354173341433290934144373261241958718");
assert.equal(signature.S.toString(),
"248298168863866362217836334079793350221620631973732197668910946177382043688");
"2318334603430781860679872910160434499077270843466490702990199622594868564504");
const pSignature = eddsa.packSignature(signature);
assert.equal(pSignature.toString("hex"), ""+
"dfedb4315d3f2eb4de2d3c510d7a987dcab67089c8ace06308827bf5bcbe02a2"+
"28506bce274aa1b3f7e7c2fd7e4fe09bff8f9aa37a42def7994e98f322888c00");
"3e417cd811f9c9c545a680b962e45d22ccb62b2284b4fe4bbc9fdb50b252a59a" +
"1852c049fc6286138a0ddb57718049a09374fdf0390686c7ac5637b481212005");
const uSignature = eddsa.unpackSignature(pSignature);
assert(eddsa.verifyPoseidon(msg, uSignature, pubKey));

View File

@@ -1,7 +1,8 @@
const chai = require("chai");
const path = require("path");
const tester = require("circom").tester;
const bigInt = require("big-integer");
const Fr = require("ffjavascript").bn128.Fr;
const eddsa = require("../src/eddsa.js");
@@ -18,7 +19,7 @@ describe("EdDSA MiMC test", function () {
});
it("Sign a single number", async () => {
const msg = bigInt(1234);
const msg = Fr.e(1234);
const prvKey = Buffer.from("0001020304050607080900010203040506070809000102030405060708090001", "hex");
@@ -43,7 +44,7 @@ describe("EdDSA MiMC test", function () {
});
it("Detect Invalid signature", async () => {
const msg = bigInt(1234);
const msg = Fr.e(1234);
const prvKey = Buffer.from("0001020304050607080900010203040506070809000102030405060708090001", "hex");
@@ -58,7 +59,7 @@ describe("EdDSA MiMC test", function () {
enabled: 1,
Ax: pubKey[0],
Ay: pubKey[1],
R8x: signature.R8[0].add(bigInt(1)),
R8x: Fr.add(signature.R8[0], Fr.e(1)),
R8y: signature.R8[1],
S: signature.S,
M: msg}, true);
@@ -70,7 +71,7 @@ describe("EdDSA MiMC test", function () {
it("Test a dissabled circuit with a bad signature", async () => {
const msg = bigInt(1234);
const msg = Fr.e(1234);
const prvKey = Buffer.from("0001020304050607080900010203040506070809000102030405060708090001", "hex");
@@ -85,7 +86,7 @@ describe("EdDSA MiMC test", function () {
enabled: 0,
Ax: pubKey[0],
Ay: pubKey[1],
R8x: signature.R8[0].add(bigInt(1)),
R8x: Fr.add(signature.R8[0], Fr.e(1)),
R8y: signature.R8[1],
S: signature.S,
M: msg}, true);

View File

@@ -1,7 +1,7 @@
const chai = require("chai");
const path = require("path");
const bigInt = require("big-integer");
const tester = require("circom").tester;
const Fr = require("ffjavascript").bn128.Fr;
const eddsa = require("../src/eddsa.js");
@@ -19,7 +19,7 @@ describe("EdDSA Poseidon test", function () {
});
it("Sign a single number", async () => {
const msg = bigInt(1234);
const msg = Fr.e(1234);
const prvKey = Buffer.from("0001020304050607080900010203040506070809000102030405060708090001", "hex");
@@ -47,7 +47,7 @@ describe("EdDSA Poseidon test", function () {
});
it("Detect Invalid signature", async () => {
const msg = bigInt(1234);
const msg = Fr.e(1234);
const prvKey = Buffer.from("0001020304050607080900010203040506070809000102030405060708090001", "hex");
@@ -62,7 +62,7 @@ describe("EdDSA Poseidon test", function () {
enabled: 1,
Ax: pubKey[0],
Ay: pubKey[1],
R8x: signature.R8[0].add(bigInt(1)),
R8x: Fr.add(signature.R8[0], Fr.e(1)),
R8y: signature.R8[1],
S: signature.S,
M: msg}, true);
@@ -74,7 +74,7 @@ describe("EdDSA Poseidon test", function () {
it("Test a dissabled circuit with a bad signature", async () => {
const msg = bigInt(1234);
const msg = Fr.e(1234);
const prvKey = Buffer.from("0001020304050607080900010203040506070809000102030405060708090001", "hex");
@@ -89,7 +89,7 @@ describe("EdDSA Poseidon test", function () {
enabled: 0,
Ax: pubKey[0],
Ay: pubKey[1],
R8x: signature.R8[0].add(bigInt(1)),
R8x: Fr.add(signature.R8[0], Fr.e(1)),
R8y: signature.R8[1],
S: signature.S,
M: msg}, true);

View File

@@ -1,8 +1,9 @@
const chai = require("chai");
const path = require("path");
const bigInt = require("big-integer");
const tester = require("circom").tester;
const babyJub = require("../src/babyjub.js");
const Fr = require("ffjavascript").bn128.Fr;
const assert = chai.assert;
@@ -23,11 +24,11 @@ describe("Exponentioation test", function () {
await circuit.checkConstraints(w);
let g = [
bigInt("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
bigInt("16950150798460657717958625567821834550301663161624707787222815936182638968203")
Fr.e("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
Fr.e("16950150798460657717958625567821834550301663161624707787222815936182638968203")
];
let dbl= [bigInt("0"), bigInt("1")];
let dbl= [Fr.e("0"), Fr.e("1")];
const expectedOut = [];
@@ -50,15 +51,15 @@ describe("Exponentioation test", function () {
await circuit.checkConstraints(w);
let g = [
bigInt("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
bigInt("16950150798460657717958625567821834550301663161624707787222815936182638968203")
Fr.e("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
Fr.e("16950150798460657717958625567821834550301663161624707787222815936182638968203")
];
for (let i=0; i<12;i++) {
g = babyJub.addPoint(g,g);
}
let dbl= [bigInt("0"), bigInt("1")];
let dbl= [Fr.e("0"), Fr.e("1")];
const expectedOut = [];
@@ -81,11 +82,11 @@ describe("Exponentioation test", function () {
await circuit.checkConstraints(w);
let g = [
bigInt("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
bigInt("16950150798460657717958625567821834550301663161624707787222815936182638968203")
Fr.e("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
Fr.e("16950150798460657717958625567821834550301663161624707787222815936182638968203")
];
let c = [bigInt(0), bigInt(1)];
let c = [Fr.e(0), Fr.e(1)];
for (let i=0; i<31;i++) {
c = babyJub.addPoint(c,g);
@@ -93,7 +94,7 @@ describe("Exponentioation test", function () {
await circuit.assertOut(w, {out: c});
const w2 = await circuit.calculateWitness({"in": bigInt(1).shiftLeft(252).add(bigInt.one)});
const w2 = await circuit.calculateWitness({"in": Fr.add(Fr.shl(Fr.e(1), Fr.e(252)),Fr.one)});
c = [g[0], g[1]];
for (let i=0; i<252;i++) {

View File

@@ -1,7 +1,8 @@
const chai = require("chai");
const path = require("path");
const bigInt = require("big-integer");
const tester = require("circom").tester;
const Fr = require("ffjavascript").bn128.Fr;
function print(circuit, w, s) {
console.log(s + ": " + w[circuit.getSignalIdx(s)]);
@@ -13,8 +14,8 @@ describe("Escalarmul test", function () {
this.timeout(100000);
let g = [
bigInt("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
bigInt("16950150798460657717958625567821834550301663161624707787222815936182638968203")
Fr.e("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
Fr.e("16950150798460657717958625567821834550301663161624707787222815936182638968203")
];
before( async() => {
@@ -33,7 +34,7 @@ describe("Escalarmul test", function () {
it("If multiply by order should return 0", async () => {
const r = bigInt("2736030358979909402780800718157159386076813972158567259200215660948447373041");
const r = Fr.e("2736030358979909402780800718157159386076813972158567259200215660948447373041");
const w = await circuitEMulAny.calculateWitness({"e": r, "p": g});
await circuitEMulAny.checkConstraints(w);

View File

@@ -1,8 +1,8 @@
const chai = require("chai");
const path = require("path");
const bigInt = require("big-integer");
const tester = require("circom").tester;
const babyjub = require("../src/babyjub");
const Fr = require("ffjavascript").bn128.Fr;
const assert = chai.assert;
@@ -41,10 +41,10 @@ describe("Escalarmul test", function () {
it("Should generate scalar mul of a specific constant", async () => {
const s = bigInt("2351960337287830298912035165133676222414898052661454064215017316447594616519");
const s = Fr.e("2351960337287830298912035165133676222414898052661454064215017316447594616519");
const base8 = [
bigInt("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
bigInt("16950150798460657717958625567821834550301663161624707787222815936182638968203")
Fr.e("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
Fr.e("16950150798460657717958625567821834550301663161624707787222815936182638968203")
];
const w = await circuit.calculateWitness({"e": s}, true);
@@ -60,12 +60,12 @@ describe("Escalarmul test", function () {
it("Should generate scalar mul of the firsts 50 elements", async () => {
const base8 = [
bigInt("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
bigInt("16950150798460657717958625567821834550301663161624707787222815936182638968203")
Fr.e("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
Fr.e("16950150798460657717958625567821834550301663161624707787222815936182638968203")
];
for (let i=0; i<50; i++) {
const s = bigInt(i);
const s = Fr.e(i);
const w = await circuit.calculateWitness({"e": s}, true);

View File

@@ -1,8 +1,8 @@
const chai = require("chai");
const path = require("path");
const bigInt = require("big-integer");
const tester = require("circom").tester;
const babyJub = require("../src/babyjub.js");
const Fr = require("ffjavascript").bn128.Fr;
const assert = chai.assert;
@@ -13,8 +13,8 @@ describe("Montgomery test", function () {
let circuitMDouble;
let g = [
bigInt("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
bigInt("16950150798460657717958625567821834550301663161624707787222815936182638968203")
Fr.e("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
Fr.e("16950150798460657717958625567821834550301663161624707787222815936182638968203")
];
let mg, mg2, g2, g3, mg3;
@@ -45,8 +45,8 @@ describe("Montgomery test", function () {
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]));
assert(Fr.eq(xout, g[0]));
assert(Fr.eq(yout, g[1]));
});
it("Should double a point", async () => {
let w, xout, yout;
@@ -65,8 +65,9 @@ describe("Montgomery test", function () {
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]));
assert(Fr.eq(xout, g2[0]));
assert(Fr.eq(yout, g2[1]));
});
it("Should add a point", async () => {
let w, xout, yout;
@@ -85,7 +86,7 @@ describe("Montgomery test", function () {
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]));
assert(Fr.eq(xout, g3[0]));
assert(Fr.eq(yout, g3[1]));
});
});

View File

@@ -1,6 +1,6 @@
const path = require("path");
const bigInt = require("big-integer");
const tester = require("circom").tester;
const Fr = require("ffjavascript").bn128.Fr;
describe("Mux4 test", function() {
this.timeout(100000);
@@ -9,22 +9,22 @@ describe("Mux4 test", function() {
const circuit = await tester(path.join(__dirname, "circuits", "mux4_1.circom"));
const ct16 = [
bigInt("123"),
bigInt("456"),
bigInt("789"),
bigInt("012"),
bigInt("111"),
bigInt("222"),
bigInt("333"),
bigInt("4546"),
bigInt("134523"),
bigInt("44356"),
bigInt("15623"),
bigInt("4566"),
bigInt("1223"),
bigInt("4546"),
bigInt("4256"),
bigInt("4456")
Fr.e("123"),
Fr.e("456"),
Fr.e("789"),
Fr.e("012"),
Fr.e("111"),
Fr.e("222"),
Fr.e("333"),
Fr.e("4546"),
Fr.e("134523"),
Fr.e("44356"),
Fr.e("15623"),
Fr.e("4566"),
Fr.e("1223"),
Fr.e("4546"),
Fr.e("4256"),
Fr.e("4456")
];
for (let i=0; i<16; i++) {
@@ -41,14 +41,14 @@ describe("Mux4 test", function() {
const circuit = await tester(path.join(__dirname, "circuits", "mux3_1.circom"));
const ct8 = [
bigInt("37"),
bigInt("47"),
bigInt("53"),
bigInt("71"),
bigInt("89"),
bigInt("107"),
bigInt("163"),
bigInt("191")
Fr.e("37"),
Fr.e("47"),
Fr.e("53"),
Fr.e("71"),
Fr.e("89"),
Fr.e("107"),
Fr.e("163"),
Fr.e("191")
];
for (let i=0; i<8; i++) {
@@ -64,10 +64,10 @@ describe("Mux4 test", function() {
const circuit = await tester(path.join(__dirname, "circuits", "mux2_1.circom"));
const ct4 = [
bigInt("37"),
bigInt("47"),
bigInt("53"),
bigInt("71"),
Fr.e("37"),
Fr.e("47"),
Fr.e("53"),
Fr.e("71"),
];
for (let i=0; i<4; i++) {
@@ -83,8 +83,8 @@ describe("Mux4 test", function() {
const circuit = await tester(path.join(__dirname, "circuits", "mux1_1.circom"));
const ct2 = [
bigInt("37"),
bigInt("47"),
Fr.e("37"),
Fr.e("47"),
];
for (let i=0; i<2; i++) {

View File

@@ -1,18 +1,18 @@
const chai = require("chai");
const path = require("path");
const bigInt = require("big-integer");
const Fr = require("ffjavascript").bn128.Fr;
const tester = require("circom").tester;
const babyJub = require("../src/babyjub.js");
const PBASE =
[
[bigInt("10457101036533406547632367118273992217979173478358440826365724437999023779287"),bigInt("19824078218392094440610104313265183977899662750282163392862422243483260492317")],
[bigInt("2671756056509184035029146175565761955751135805354291559563293617232983272177"),bigInt("2663205510731142763556352975002641716101654201788071096152948830924149045094")],
[bigInt("5802099305472655231388284418920769829666717045250560929368476121199858275951"),bigInt("5980429700218124965372158798884772646841287887664001482443826541541529227896")],
[bigInt("7107336197374528537877327281242680114152313102022415488494307685842428166594"),bigInt("2857869773864086953506483169737724679646433914307247183624878062391496185654")],
[bigInt("20265828622013100949498132415626198973119240347465898028410217039057588424236"),bigInt("1160461593266035632937973507065134938065359936056410650153315956301179689506")]
[Fr.e("7688621503272331394947188562469131124099290577812125474996268020905176040083"),Fr.e("6637287939860384587467947982369268811366630904563077767287326262235485629411")],
[Fr.e("11549681895645637778324638856880330712650895608496649854094912415387988201330"),Fr.e("5771732722784528537721081267383956005090479808901717812009343940574217488577")],
[Fr.e("18790245153471844934157747708238883966079935875787657036767664036124524381945"),Fr.e("18300275459419441151064576487317481499516933849631632883767173501999997278432")],
[Fr.e("16301069151422548986850494139112207641738464387919729729324473657161689764196"),Fr.e("8215273507373494014441104012907835625670941526105528197815397741007626226499")],
[Fr.e("12597665704678284488008395353749282149622295037737374782196049599390683534185"),Fr.e("4072455241781501621593714139281767473040087753548015968773801065193764079468")]
];
describe("Double Pedersen test", function() {
@@ -63,7 +63,7 @@ describe("Double Pedersen test", function() {
it("Should pedersen all ones", async () => {
let w;
const allOnes = bigInt("1").shiftLeft(250).minus(bigInt("1"));
const allOnes = Fr.sub(Fr.shl(Fr.e("1"), Fr.e(250)), Fr.e("1"));
w = await circuit.calculateWitness({ in: [allOnes, allOnes]}, true);

View File

@@ -1,6 +1,6 @@
const path = require("path");
const bigInt = require("big-integer");
const Fr = require("ffjavascript").bn128.Fr;
const tester = require("circom").tester;
const babyJub = require("../src/babyjub.js");
@@ -32,7 +32,7 @@ describe("Pedersen test", function() {
let w;
const n = bigInt.one.shiftLeft(253).minus(bigInt.one);
const n = Fr.sub(Fr.shl(Fr.one, Fr.e(253)), Fr.one);
w = await circuit.calculateWitness({ in: n}, true);

View File

@@ -1,7 +1,6 @@
const chai = require("chai");
const path = require("path");
var blake2b = require("blake2b");
const bigInt = require("big-integer");
const tester = require("circom").tester;
const poseidon = require("../src/poseidon.js");

View File

@@ -3,7 +3,6 @@ const Web3 = require("web3");
const chai = require("chai");
const poseidonGenContract = require("../src/poseidon_gencontract.js");
const Poseidon = require("../src/poseidon.js");
const bigInt = require("snarkjs").bigInt;
const assert = chai.assert;
const log = (msg) => { if (process.env.MOCHA_VERBOSE) console.log(msg); };

View File

@@ -1,7 +1,7 @@
const chai = require("chai");
const path = require("path");
const snarkjs = require("snarkjs");
const crypto = require("crypto");
const Fr = require("ffjavascript").bn128.Fr;
const assert = chai.assert;
@@ -67,7 +67,7 @@ describe("SHA256 test", function () {
assert.equal(hash, hash2);
assert(witness[1].equals(snarkjs.bigInt(r)));
assert(Fr.eq(witness[1], Fr.e(r)));
}).timeout(1000000);
it("Should calculate a hash of 2 compressor", async () => {

View File

@@ -1,5 +1,6 @@
const path = require("path");
const bigInt = require("big-integer");
const Fr = require("ffjavascript").bn128.Fr;
const Scalar = require("ffjavascript").Scalar;
const tester = require("circom").tester;
function print(circuit, w, s) {
@@ -9,16 +10,16 @@ function print(circuit, w, s) {
function getBits(v, n) {
const res = [];
for (let i=0; i<n; i++) {
if (v.shiftRight(i).isOdd()) {
res.push(bigInt.one);
if (Scalar.isOdd(Scalar.shr(v, i))) {
res.push(Fr.one);
} else {
res.push(bigInt.zero);
res.push(Fr.zero);
}
}
return res;
}
const q = bigInt("21888242871839275222246405745257275088548364400416034343698204186575808495617");
const q = Scalar.fromString("21888242871839275222246405745257275088548364400416034343698204186575808495617");
describe("Sign test", function() {
let circuit;
@@ -29,35 +30,35 @@ describe("Sign test", function() {
});
it("Sign of 0", async () => {
const inp = getBits(bigInt.zero, 254);
const inp = getBits(Scalar.e(0), 254);
const w = await circuit.calculateWitness({in: inp}, true);
await circuit.assertOut(w, {sign: 0});
});
it("Sign of 3", async () => {
const inp = getBits(bigInt(3), 254);
const inp = getBits(Scalar.e(3), 254);
const w = await circuit.calculateWitness({in: inp}, true);
await circuit.assertOut(w, {sign: 0});
});
it("Sign of q/2", async () => {
const inp = getBits(q.shiftRight(bigInt.one), 254);
const inp = getBits(Scalar.shr(q, 1), 254);
const w = await circuit.calculateWitness({in: inp}, true);
await circuit.assertOut(w, {sign: 0});
});
it("Sign of q/2+1", async () => {
const inp = getBits(q.shiftRight(bigInt.one).add(bigInt.one), 254);
const inp = getBits(Scalar.add(Scalar.shr(q, 1), 1) , 254);
const w = await circuit.calculateWitness({in: inp}, true);
await circuit.assertOut(w, {sign: 1});
});
it("Sign of q-1", async () => {
const inp = getBits(q.minus(bigInt.one), 254);
const inp = getBits(Scalar.sub(q, 1), 254);
const w = await circuit.calculateWitness({in: inp}, true);
await circuit.assertOut(w, {sign: 1});
@@ -71,7 +72,7 @@ describe("Sign test", function() {
});
it("Sign of all ones", async () => {
const inp = getBits(bigInt(1).shiftLeft(254).minus(bigInt(1)), 254);
const inp = getBits(Scalar.sub(Scalar.shl(1,254),1), 254);
const w = await circuit.calculateWitness({in: inp}, true);
await circuit.assertOut(w, {sign: 1});

View File

@@ -1,28 +1,11 @@
const chai = require("chai");
const bigInt = require("big-integer");
const Fr = require("ffjavascript").bn128.Fr;
const smt = require("../src/smt.js");
const assert = chai.assert;
function stringifyBigInts(o) {
if ((typeof(o) == "bigint") || (o instanceof bigInt)) {
return o.toString(10);
} else if (Array.isArray(o)) {
return o.map(stringifyBigInts);
} else if (typeof o == "object") {
const res = {};
for (let k in o) {
res[k] = stringifyBigInts(o[k]);
}
return res;
} else {
return o;
}
}
describe("SMT Javascript test", function () {
this.timeout(100000);
before( async () => {
@@ -30,22 +13,22 @@ describe("SMT Javascript test", function () {
it("Should insert 2 elements and empty them", async () => {
const tree = await smt.newMemEmptyTrie();
const key1 = bigInt(111);
const value1 = bigInt(222);
const key2 = bigInt(333);
const value2 = bigInt(444);
const key1 = Fr.e(111);
const value1 = Fr.e(222);
const key2 = Fr.e(333);
const value2 = Fr.e(444);
await tree.insert(key1,value1);
await tree.insert(key2,value2);
await tree.delete(key2);
await tree.delete(key1);
assert(tree.root.isZero());
assert(Fr.isZero(tree.root));
});
it("Should insert 3 elements in dferent order and should be the same", async () => {
const keys = [bigInt(8), bigInt(9), bigInt(32)];
const values = [bigInt(88), bigInt(99), bigInt(3232)];
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();
@@ -77,11 +60,11 @@ describe("SMT Javascript test", function () {
await tree6.insert(keys[1],values[1]);
await tree6.insert(keys[0],values[0]);
assert(tree1.root.equals(tree2.root));
assert(tree2.root.equals(tree3.root));
assert(tree3.root.equals(tree4.root));
assert(tree4.root.equals(tree5.root));
assert(tree5.root.equals(tree6.root));
assert(Fr.eq(tree1.root, tree2.root));
assert(Fr.eq(tree2.root, tree3.root));
assert(Fr.eq(tree3.root, tree4.root));
assert(Fr.eq(tree4.root, tree5.root));
assert(Fr.eq(tree5.root, tree6.root));
assert.equal(Object.keys(tree1.db.nodes).length, Object.keys(tree2.db.nodes).length);
assert.equal(Object.keys(tree2.db.nodes).length, Object.keys(tree3.db.nodes).length);
@@ -93,19 +76,19 @@ describe("SMT Javascript test", function () {
await tree1.delete(keys[1]);
await tree2.delete(keys[1]);
await tree2.delete(keys[0]);
assert(tree1.root.equals(tree2.root));
assert(Fr.eq(tree1.root, tree2.root));
await tree3.delete(keys[0]);
await tree3.delete(keys[2]);
await tree4.delete(keys[2]);
await tree4.delete(keys[0]);
assert(tree3.root.equals(tree4.root));
assert(Fr.eq(tree3.root, tree4.root));
await tree5.delete(keys[1]);
await tree5.delete(keys[2]);
await tree6.delete(keys[2]);
await tree6.delete(keys[1]);
assert(tree5.root.equals(tree6.root));
assert(Fr.eq(tree5.root, tree6.root));
await tree1.delete(keys[2]);
await tree2.delete(keys[2]);
@@ -114,12 +97,12 @@ describe("SMT Javascript test", function () {
await tree5.delete(keys[0]);
await tree6.delete(keys[0]);
assert(tree1.root.isZero());
assert(tree2.root.isZero());
assert(tree3.root.isZero());
assert(tree4.root.isZero());
assert(tree5.root.isZero());
assert(tree6.root.isZero());
assert(Fr.isZero(tree1.root));
assert(Fr.isZero(tree2.root));
assert(Fr.isZero(tree3.root));
assert(Fr.isZero(tree4.root));
assert(Fr.isZero(tree5.root));
assert(Fr.isZero(tree6.root));
assert.equal(Object.keys(tree1.db.nodes).length, 0);
assert.equal(Object.keys(tree2.db.nodes).length, 0);
@@ -144,7 +127,7 @@ describe("SMT Javascript test", function () {
const arr = [];
const N = 100;
for (let i=0; i<N; i++) {
arr.push(bigInt(i));
arr.push(Fr.e(i));
}
const insArr = perm(arr);
for (let i=0; i<N; i++) {
@@ -155,7 +138,7 @@ describe("SMT Javascript test", function () {
await tree.delete(delArr[i]);
}
assert(tree.root.isZero());
assert(Fr.isZero(tree.root));
assert.equal(Object.keys(tree.db.nodes).length, 0);
});
@@ -175,7 +158,7 @@ describe("SMT Javascript test", function () {
await tree1.update(9, 999);
await tree1.update(32, 323232);
assert(tree1.root.equals(tree2.root));
assert(Fr.eq(tree1.root, tree2.root));
});
});

View File

@@ -1,7 +1,7 @@
const chai = require("chai");
const path = require("path");
const bigInt = require("big-integer");
const tester = require("circom").tester;
const Fr = require("ffjavascript").bn128.Fr;
const smt = require("../src/smt.js");
@@ -15,7 +15,7 @@ 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));
while (siblings.length<10) siblings.push(Fr.e(0));
const w = await circuit.calculateWitness({
fnc: [1,0],
@@ -37,7 +37,7 @@ async function testInsert(tree, key, value, circuit ) {
async function testDelete(tree, key, circuit) {
const res = await tree.delete(key);
let siblings = res.siblings;
while (siblings.length<10) siblings.push(bigInt(0));
while (siblings.length<10) siblings.push(Fr.e(0));
const w = await circuit.calculateWitness({
fnc: [1,1],
@@ -58,7 +58,7 @@ async function testDelete(tree, key, circuit) {
async function testUpdate(tree, key, newValue, circuit) {
const res = await tree.update(key, newValue);
let siblings = res.siblings;
while (siblings.length<10) siblings.push(bigInt(0));
while (siblings.length<10) siblings.push(Fr.e(0));
const w = await circuit.calculateWitness({
fnc: [0,1],
@@ -91,15 +91,15 @@ describe("SMT Processor test", function () {
});
it("Should verify an insert to an empty tree", async () => {
const key = bigInt(111);
const value = bigInt(222);
const key = Fr.e(111);
const value = Fr.e(222);
await testInsert(tree, key, value, circuit);
});
it("It should add another element", async () => {
const key = bigInt(333);
const value = bigInt(444);
const key = Fr.e(333);
const value = Fr.e(444);
await testInsert(tree, key, value, circuit);
});
@@ -110,8 +110,8 @@ describe("SMT Processor test", function () {
});
it("Should test convination of adding and removing 3 elements", async () => {
const keys = [bigInt(8), bigInt(9), bigInt(32)];
const values = [bigInt(88), bigInt(99), bigInt(3232)];
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();
@@ -170,7 +170,7 @@ describe("SMT Processor test", function () {
it("Should match a NOp with random vals", async () => {
let siblings = [];
while (siblings.length<10) siblings.push(bigInt(88));
while (siblings.length<10) siblings.push(Fr.e(88));
const w = await circuit.calculateWitness({
fnc: [0,0],
oldRoot: 11,
@@ -187,7 +187,7 @@ describe("SMT Processor test", function () {
await circuit.checkConstraints(w);
assert(root1.equals(root2));
assert(Fr.eq(root1, root2));
});
it("Should update an element", async () => {
const tree1 = await smt.newMemEmptyTrie();

View File

@@ -1,6 +1,6 @@
const chai = require("chai");
const path = require("path");
const bigInt = require("big-integer");
const Fr = require("ffjavascript").bn128.Fr;
const tester = require("circom").tester;
const smt = require("../src/smt.js");
@@ -17,7 +17,7 @@ async function testInclusion(tree, key, circuit) {
assert(res.found);
let siblings = res.siblings;
while (siblings.length<10) siblings.push(bigInt(0));
while (siblings.length<10) siblings.push(Fr.e(0));
const w = await circuit.calculateWitness({
enabled: 1,
@@ -40,7 +40,7 @@ async function testExclusion(tree, key, circuit) {
assert(!res.found);
let siblings = res.siblings;
while (siblings.length<10) siblings.push(bigInt(0));
while (siblings.length<10) siblings.push(Fr.e(0));
const w = await circuit.calculateWitness({
enabled: 1,
@@ -110,14 +110,14 @@ describe("SMT Verifier test", function () {
});
it("Check inclussion Adria case", async () => {
const e1_hi= bigInt("17124152697573569611556136390143205198134245887034837071647643529178599000839");
const e1_hv= bigInt("19650379996168153643111744440707177573540245771926102415571667548153444658179");
const e1_hi= Fr.e("17124152697573569611556136390143205198134245887034837071647643529178599000839");
const e1_hv= Fr.e("19650379996168153643111744440707177573540245771926102415571667548153444658179");
const e2ok_hi= bigInt("16498254692537945203721083102154618658340563351558973077349594629411025251262");
const e2ok_hv= bigInt("19650379996168153643111744440707177573540245771926102415571667548153444658179");
const e2ok_hi= Fr.e("16498254692537945203721083102154618658340563351558973077349594629411025251262");
const e2ok_hv= Fr.e("19650379996168153643111744440707177573540245771926102415571667548153444658179");
const e2fail_hi= bigInt("17195092312975762537892237130737365903429674363577646686847513978084990105579");
const e2fail_hv= bigInt("19650379996168153643111744440707177573540245771926102415571667548153444658179");
const e2fail_hi= Fr.e("17195092312975762537892237130737365903429674363577646686847513978084990105579");
const e2fail_hv= Fr.e("19650379996168153643111744440707177573540245771926102415571667548153444658179");
const tree1 = await smt.newMemEmptyTrie();
await tree1.insert(e1_hi,e1_hv);