diff --git a/src/poseidon.js b/src/poseidon.js index db1c706..90076d7 100644 --- a/src/poseidon.js +++ b/src/poseidon.js @@ -1,10 +1,11 @@ const assert = require("assert"); -const Scalar = require("ffjavascript").Scalar; -const ZqField = require("ffjavascript").ZqField; -const { unstringifyBigInts } = require("ffjavascript").utils; +const bn128 = require("snarkjs").bn128; +const bigInt = require("snarkjs").bigInt; +const F = bn128.Fr; +const { unstringifyBigInts } = require("snarkjs"); // Prime 0x30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001 -const F = new ZqField(Scalar.fromString("21888242871839275222246405745257275088548364400416034343698204186575808495617")); +// const F = new ZqField(Scalar.fromString("21888242871839275222246405745257275088548364400416034343698204186575808495617")); // Parameters are generated by a reference script https://extgit.iaik.tugraz.at/krypto/hadeshash/-/blob/master/code/generate_parameters_grain.sage // Used like so: sage generate_parameters_grain.sage 1 0 254 2 8 56 0x30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001 @@ -26,7 +27,7 @@ function poseidon(inputs) { const nRoundsF = N_ROUNDS_F; const nRoundsP = N_ROUNDS_P[t - 2]; - let state = [...inputs.map(a => F.e(a)), F.zero]; + let state = [...inputs.map(a => bigInt(a)), F.zero]; for (let r = 0; r < nRoundsF + nRoundsP; r++) { state = state.map((a, i) => F.add(a, C[t - 2][r * t + i])); @@ -43,7 +44,7 @@ function poseidon(inputs) { ); } } - return F.normalize(state[0]); + return F.affine(state[0]); } module.exports = poseidon; diff --git a/src/poseidon_gencontract.js b/src/poseidon_gencontract.js index c714305..4019b94 100644 --- a/src/poseidon_gencontract.js +++ b/src/poseidon_gencontract.js @@ -3,7 +3,7 @@ // const Contract = require("./evmasm"); -const { unstringifyBigInts } = require("ffjavascript").utils; +const { unstringifyBigInts } = require("snarkjs"); const { C:K, M } = unstringifyBigInts(require("./poseidon_constants.json")); diff --git a/src/smt_hashes_poseidon.js b/src/smt_hashes_poseidon.js index 67a080e..2c20266 100644 --- a/src/smt_hashes_poseidon.js +++ b/src/smt_hashes_poseidon.js @@ -1,8 +1,6 @@ const poseidon = require("./poseidon"); const bigInt = require("snarkjs").bigInt; -//const F = new ZqField(Scalar.fromString("21888242871839275222246405745257275088548364400416034343698204186575808495617")); - exports.hash0 = function (left, right) { return poseidon([left, right]); }; @@ -10,5 +8,3 @@ exports.hash0 = function (left, right) { exports.hash1 = function(key, value) { return poseidon([key, value, bigInt.one]); }; - -//exports.F = poseidon.F; diff --git a/test/poseidoncircuit.js b/test/poseidoncircuit.js index 201025d..a046b5f 100644 --- a/test/poseidoncircuit.js +++ b/test/poseidoncircuit.js @@ -1,6 +1,7 @@ const chai = require("chai"); const path = require("path"); -const tester = require("circom").tester; +const snarkjs = require("snarkjs"); +const compiler = require("circom"); const poseidon = require("../src/poseidon.js"); @@ -12,25 +13,30 @@ describe("Poseidon Circuit test", function () { this.timeout(100000); - before(async () => { - circuit2 = await tester(path.join(__dirname, "circuits", "poseidon2_test.circom")); - circuit4 = await tester(path.join(__dirname, "circuits", "poseidon4_test.circom")); + before( async () => { + const cirDef2 = await compiler(path.join(__dirname, "circuits", "poseidon2_test.circom")); + const cirDef4 = await compiler(path.join(__dirname, "circuits", "poseidon4_test.circom")); + + circuit2 = new snarkjs.Circuit(cirDef2); + circuit4 = new snarkjs.Circuit(cirDef4); }); it("Should check constrain of hash([1, 2])", async () => { const hash = poseidon([1, 2]); assert.equal("17117985411748610629288516079940078114952304104811071254131751175361957805920", hash.toString()); const w = await circuit2.calculateWitness({inputs: [1, 2]}, true); - await circuit2.assertOut(w, {out : hash}); - await circuit2.checkConstraints(w); + const res = w[circuit2.getSignalIdx("main.out")]; + assert.equal(res.toString(), hash.toString()); + await circuit2.checkWitness(w); }); it("Should check constrain of hash([3, 4])", async () => { const hash = poseidon([3, 4]); assert.equal("21867347236198497199818917118739170715216974132230970409806500217655788551452", hash.toString()); const w = await circuit2.calculateWitness({inputs: [3, 4]}); - await circuit2.assertOut(w, {out : hash}); - await circuit2.checkConstraints(w); + const res = w[circuit2.getSignalIdx("main.out")]; + assert.equal(res.toString(), hash.toString()); + await circuit2.checkWitness(w); }); @@ -38,15 +44,17 @@ describe("Poseidon Circuit test", function () { const hash = poseidon([1, 2, 3, 4]); assert.equal("10501812514110530158422365608831771203648472822841727510887411206067265790462", hash.toString()); const w = await circuit4.calculateWitness({inputs: [1, 2, 3, 4]}); - await circuit4.assertOut(w, {out : hash}); - await circuit4.checkConstraints(w); + const res = w[circuit4.getSignalIdx("main.out")]; + assert.equal(res.toString(), hash.toString()); + await circuit4.checkWitness(w); }); it("Should check constrain of hash([5, 6, 7, 8])", async () => { const hash = poseidon([5, 6, 7, 8]); assert.equal("20761996991478317428195238015626872345373101531750069996451149877836620406299", hash.toString()); const w = await circuit4.calculateWitness({inputs: [5, 6, 7, 8]}); - await circuit4.assertOut(w, {out : hash}); - await circuit4.checkConstraints(w); + const res = w[circuit4.getSignalIdx("main.out")]; + assert.equal(res.toString(), hash.toString()); + await circuit4.checkWitness(w); }); });