Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
poma committed Aug 10, 2020
1 parent 436cf45 commit 6282474
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 23 deletions.
13 changes: 7 additions & 6 deletions src/poseidon.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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]));

Expand All @@ -43,7 +44,7 @@ function poseidon(inputs) {
);
}
}
return F.normalize(state[0]);
return F.affine(state[0]);
}

module.exports = poseidon;
2 changes: 1 addition & 1 deletion src/poseidon_gencontract.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"));

Expand Down
4 changes: 0 additions & 4 deletions src/smt_hashes_poseidon.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
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]);
};

exports.hash1 = function(key, value) {
return poseidon([key, value, bigInt.one]);
};

//exports.F = poseidon.F;
32 changes: 20 additions & 12 deletions test/poseidoncircuit.js
Original file line number Diff line number Diff line change
@@ -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");

Expand All @@ -12,41 +13,48 @@ 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);
});


it("Should check constrain of hash([1, 2, 3, 4])", async () => {
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);
});
});

0 comments on commit 6282474

Please sign in to comment.