Skip to content

Commit

Permalink
Adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
judiciouscoder committed Oct 6, 2021
1 parent a8cdb6c commit 2afc2db
Show file tree
Hide file tree
Showing 78 changed files with 3,526 additions and 0 deletions.
77 changes: 77 additions & 0 deletions test/aliascheck.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
const chai = require("chai");
const path = require("path");

const assert = chai.assert;

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 wasm_tester = require("circom_tester").wasm;

function print(circuit, w, s) {
console.log(s + ": " + w[circuit.getSignalIdx(s)]);
}

function getBits(v, n) {
const res = [];
for (let i=0; i<n; i++) {
if (Scalar.isOdd(Scalar.shr(v,i))) {
res.push(F.one);
} else {
res.push(F.zero);
}
}
return res;
}


describe("Aliascheck test", function () {
this.timeout(100000);

let cir;
before( async() => {

cir = await wasm_tester(path.join(__dirname, "circuits", "aliascheck_test.circom"));
});

it("Satisfy the aliastest 0", async () => {
const inp = getBits(0, 254);
await cir.calculateWitness({in: inp}, true);
});

it("Satisfy the aliastest 3", async () => {
const inp = getBits(3, 254);
await cir.calculateWitness({in: inp}, true);
});

it("Satisfy the aliastest q-1", async () => {
const inp = getBits(F.e(-1), 254);
// console.log(JSON.stringify(utils.stringifyBigInts(inp)));
await cir.calculateWitness({in: inp}, true);
});

it("Should not satisfy an input of q", async () => {
const inp = getBits(q, 254);
try {
await cir.calculateWitness({in: inp}, true);
assert(false);
} catch(err) {
assert(err.message.includes("Assert Failed"));
}
});

it("Should not satisfy all ones", async () => {

const inp = getBits(Scalar.sub(Scalar.shl(1, 254) , 1) , 254);
try {
await cir.calculateWitness({in: inp}, true);
assert(false);
} catch(err) {
assert(err.message.includes("Assert Failed"));
}
});

});
113 changes: 113 additions & 0 deletions test/babyjub.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
const chai = require("chai");
const path = require("path");

const createBlakeHash = require("blake-hash");
const eddsa = require("../src/eddsa.js");
const F = require("../src/babyjub.js").F;

const assert = chai.assert;

const tester = require("circom").tester;
const utils = require("ffjavascript").utils;
const Scalar = require("ffjavascript").Scalar;

describe("Baby Jub test", function () {
let circuitAdd;
let circuitTest;
let circuitPbk;

this.timeout(100000);

before( async() => {
circuitAdd = await tester(path.join(__dirname, "circuits", "babyadd_tester.circom"));

circuitTest = await tester(path.join(__dirname, "circuits", "babycheck_test.circom"));

circuitPbk = await tester(path.join(__dirname, "circuits", "babypbk_test.circom"));
});

it("Should add point (0,1) and (0,1)", async () => {

const input={
x1: 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: F.e(0), yout: F.e(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")
};

const w = await circuitAdd.calculateWitness(input, true);

await circuitAdd.assertOut(w, {
xout: F.e("6890855772600357754907169075114257697580319025794532037257385534741338397365"),
yout: F.e("4338620300185947561074059802482547481416142213883829469920100239455078257889")
});

});

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")
};

const w = await circuitAdd.calculateWitness(input, true);

await circuitAdd.assertOut(w, {
xout: F.e("7916061937171219682591368294088513039687205273691143098332585753343424131937"),
yout: F.e("14035240266687799601661095864649209771790948434046947201833777492504781204499")
});

});

it("Should check (0,1) is a valid point", async() => {
const w = await circuitTest.calculateWitness({x: 0, y:1}, true);

await circuitTest.checkConstraints(w);
});

it("Should check (1,0) is an invalid point", async() => {
try {
await circuitTest.calculateWitness({x: 1, y: 0}, true);
assert(false, "Should be a valid point");
} catch(err) {
assert(/Constraint\sdoesn't\smatch(.*)168700\s!=\s1/.test(err.message) );
}
});

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 = Scalar.shr(utils.leBuff2int(pvk), 3);

const A = eddsa.prv2pub(rawpvk);

const input = {
in : S
};

const w = await circuitPbk.calculateWitness(input, true);

await circuitPbk.assertOut(w, {Ax : A[0], Ay: A[1]});

await circuitPbk.checkConstraints(w);
});

});
170 changes: 170 additions & 0 deletions test/babyjub_js.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
const chai = require("chai");
const babyjub = require("../src/babyjub.js");
const Scalar = require("ffjavascript").Scalar;

const assert = chai.assert;

// const bigInt = require("big-integer");

function buff2hex(buff) {
function i2hex(i) {
return ('0' + i.toString(16)).slice(-2);
}
return Array.from(buff).map(i2hex).join('');
}

describe("Baby Jub js test", function () {

this.timeout(100000);

it("Should add point (0,1) and (0,1)", () => {

const p1 = [
babyjub.F.e(0),
babyjub.F.e(1)];
const p2 = [
babyjub.F.e(0),
babyjub.F.e(1)
];

const out = babyjub.addPoint(p1, p2);
assert(babyjub.F.eq(out[0], babyjub.F.zero));
assert(babyjub.F.eq(out[1], babyjub.F.one));
});

it("Should base be 8*generator", () => {
let res;
res = babyjub.addPoint(babyjub.Generator, babyjub.Generator);
res = babyjub.addPoint(res, res);
res = babyjub.addPoint(res, res);

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 = [
babyjub.F.e("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
babyjub.F.e("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
];
const p2 = [
babyjub.F.e("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
babyjub.F.e("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
];

const out = babyjub.addPoint(p1, p2);
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 = [
babyjub.F.e("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
babyjub.F.e("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
];
const p2 = [
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")));

});

it("should mulPointEscalar 0", () => {
const p = [
babyjub.F.e("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
babyjub.F.e("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
];

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());
assert.equal(r2[1].toString(), r[1].toString());
assert.equal(r[0].toString(), "19372461775513343691590086534037741906533799473648040012278229434133483800898");
assert.equal(r[1].toString(), "9458658722007214007257525444427903161243386465067105737478306991484593958249");
});

it("should mulPointEscalar 1", () => {
const p = [
babyjub.F.e("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
babyjub.F.e("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
];

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 = [
babyjub.F.e("6890855772600357754907169075114257697580319025794532037257385534741338397365"),
babyjub.F.e("4338620300185947561074059802482547481416142213883829469920100239455078257889"),
];

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 = [
babyjub.F.e("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
babyjub.F.e("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
];
assert(babyjub.inCurve(p));
});

it("should inCurve 2", () => {
const p = [
babyjub.F.e("6890855772600357754907169075114257697580319025794532037257385534741338397365"),
babyjub.F.e("4338620300185947561074059802482547481416142213883829469920100239455078257889"),
];
assert(babyjub.inCurve(p));
});

it("should inSubgroup 1", () => {
const p = [
babyjub.F.e("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
babyjub.F.e("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
];
assert(babyjub.inSubgroup(p));
});

it("should inSubgroup 2", () => {
const p = [
babyjub.F.e("6890855772600357754907169075114257697580319025794532037257385534741338397365"),
babyjub.F.e("4338620300185947561074059802482547481416142213883829469920100239455078257889"),
];
assert(babyjub.inSubgroup(p));
});

it("should packPoint - unpackPoint 1", () => {
const p = [
babyjub.F.e("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
babyjub.F.e("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
];
const buf = babyjub.packPoint(p);
assert.equal(buff2hex(buf), "53b81ed5bffe9545b54016234682e7b2f699bd42a5e9eae27ff4051bc698ce85");
const p2 = babyjub.unpackPoint(buf);
assert.equal(p2[0].toString(), "17777552123799933955779906779655732241715742912184938656739573121738514868268");
assert.equal(p2[1].toString(), "2626589144620713026669568689430873010625803728049924121243784502389097019475");
});

it("should packPoint - unpackPoint 2", () => {
const p = [
babyjub.F.e("6890855772600357754907169075114257697580319025794532037257385534741338397365"),
babyjub.F.e("4338620300185947561074059802482547481416142213883829469920100239455078257889"),
];
const buf = babyjub.packPoint(p);
assert.equal(buff2hex(buf), "e114eb17eddf794f063a68fecac515e3620e131976108555735c8b0773929709");
const p2 = babyjub.unpackPoint(buf);
assert.equal(p2[0].toString(), "6890855772600357754907169075114257697580319025794532037257385534741338397365");
assert.equal(p2[1].toString(), "4338620300185947561074059802482547481416142213883829469920100239455078257889");
});
});
Loading

0 comments on commit 2afc2db

Please sign in to comment.