diff --git a/circuits/aliascheck.circom b/circuits/aliascheck.circom index c4dfad5..4352a0a 100644 --- a/circuits/aliascheck.circom +++ b/circuits/aliascheck.circom @@ -21,7 +21,6 @@ include "compconstant.circom"; template AliasCheck() { - signal input in[254]; component compConstant = CompConstant(-1); @@ -30,3 +29,14 @@ template AliasCheck() { compConstant.out === 0; } + +template AliasCheckBabyJub() { + signal input in[251]; + + component compConstant = CompConstant(2736030358979909402780800718157159386076813972158567259200215660948447373040); + + for (var i=0; i<251; i++) in[i] ==> compConstant.in[i]; + for (var i=0; i<3; i++) 0 ==> compConstant.in[251+i]; + + compConstant.out === 0; +} diff --git a/test/aliascheck.js b/test/aliascheck.js index 155bdba..1e94692 100644 --- a/test/aliascheck.js +++ b/test/aliascheck.js @@ -56,7 +56,8 @@ describe("Aliascheck test", () => { circuit.calculateWitness({in: inp}); assert(false); } catch(err) { - assert.equal(err.message, "Constraint doesn't match: 1 != 0"); + assert(err.message.indexOf("Constraint doesn't match") >= 0); + assert(err.message.indexOf("1 != 0") >= 0); } }); @@ -67,7 +68,8 @@ describe("Aliascheck test", () => { circuit.calculateWitness({in: inp}); assert(false); } catch(err) { - assert.equal(err.message, "Constraint doesn't match: 1 != 0"); + assert(err.message.indexOf("Constraint doesn't match") >= 0); + assert(err.message.indexOf("1 != 0") >= 0); } }); diff --git a/test/aliascheckbabyjub.js b/test/aliascheckbabyjub.js new file mode 100644 index 0000000..9b82d58 --- /dev/null +++ b/test/aliascheckbabyjub.js @@ -0,0 +1,75 @@ +const chai = require("chai"); +const path = require("path"); +const snarkjs = require("snarkjs"); +const compiler = require("circom"); + +const assert = chai.assert; + +const bigInt = snarkjs.bigInt; + +function print(circuit, w, s) { + console.log(s + ": " + w[circuit.getSignalIdx(s)]); +} + +function getBits(v, n) { + const res = []; + for (let i=0; i { + let circuit; + before( async() => { + const cirDef = await compiler(path.join(__dirname, "circuits", "aliascheckbabyjub_test.circom")); + + circuit = new snarkjs.Circuit(cirDef); + + console.log("NConstrains: " + circuit.nConstraints); + }); + + it("Satisfy the aliastest 0", async () => { + const inp = getBits(bigInt.zero, 251); + circuit.calculateWitness({in: inp}); + }); + + it("Satisfy the aliastest 3", async () => { + const inp = getBits(bigInt(3), 251); + circuit.calculateWitness({in: inp}); + }); + + it("Satisfy the aliastest r-1", async () => { + const inp = getBits(r.sub(bigInt.one), 251); + circuit.calculateWitness({in: inp}); + }); + + it("Nhot not satisfy an input of r", async () => { + const inp = getBits(r, 251); + try { + circuit.calculateWitness({in: inp}); + assert(false); + } catch(err) { + assert(err.message.indexOf("Constraint doesn't match") >= 0); + assert(err.message.indexOf("1 != 0") >= 0); + } + }); + + it("Nhot not satisfy all ones", async () => { + const inp = getBits(bigInt(1).shl(251).sub(bigInt(1)), 251); + try { + circuit.calculateWitness({in: inp}); + assert(false); + } catch(err) { + assert(err.message.indexOf("Constraint doesn't match") >= 0); + assert(err.message.indexOf("1 != 0") >= 0); + } + }); + +}); diff --git a/test/babyjub.js b/test/babyjub.js index f47db49..9f3a0ca 100644 --- a/test/babyjub.js +++ b/test/babyjub.js @@ -100,7 +100,8 @@ describe("Baby Jub test", function () { circuitTest.calculateWitness({x: 1, y: 0}); assert(false, "Should be a valid point"); } catch(err) { - assert.equal(err.message, "Constraint doesn't match: 168700 != 1"); + assert(err.message.indexOf("Constraint doesn't match") >= 0); + assert(err.message.indexOf("168700 != 1") >= 0); } }); @@ -121,5 +122,4 @@ describe("Baby Jub test", function () { const w = circuitPbk.calculateWitness(input); assert(circuitPbk.checkWitness(w)); }); - }); diff --git a/test/circuits/aliascheckbabyjub_test.circom b/test/circuits/aliascheckbabyjub_test.circom new file mode 100644 index 0000000..2a3e326 --- /dev/null +++ b/test/circuits/aliascheckbabyjub_test.circom @@ -0,0 +1,3 @@ +include "../../circuits/aliascheck.circom"; + +component main = AliasCheckBabyJub()