fix tests

This commit is contained in:
poma 2020-08-10 13:42:52 +03:00
parent 436cf45a04
commit 6282474dc0
No known key found for this signature in database
GPG Key ID: BA20CB01FE165657
4 changed files with 28 additions and 23 deletions

@ -1,10 +1,11 @@
const assert = require("assert"); const assert = require("assert");
const Scalar = require("ffjavascript").Scalar; const bn128 = require("snarkjs").bn128;
const ZqField = require("ffjavascript").ZqField; const bigInt = require("snarkjs").bigInt;
const { unstringifyBigInts } = require("ffjavascript").utils; const F = bn128.Fr;
const { unstringifyBigInts } = require("snarkjs");
// Prime 0x30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001 // 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 // 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 // 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 nRoundsF = N_ROUNDS_F;
const nRoundsP = N_ROUNDS_P[t - 2]; 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++) { for (let r = 0; r < nRoundsF + nRoundsP; r++) {
state = state.map((a, i) => F.add(a, C[t - 2][r * t + i])); 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; module.exports = poseidon;

@ -3,7 +3,7 @@
// //
const Contract = require("./evmasm"); const Contract = require("./evmasm");
const { unstringifyBigInts } = require("ffjavascript").utils; const { unstringifyBigInts } = require("snarkjs");
const { C:K, M } = unstringifyBigInts(require("./poseidon_constants.json")); const { C:K, M } = unstringifyBigInts(require("./poseidon_constants.json"));

@ -1,8 +1,6 @@
const poseidon = require("./poseidon"); const poseidon = require("./poseidon");
const bigInt = require("snarkjs").bigInt; const bigInt = require("snarkjs").bigInt;
//const F = new ZqField(Scalar.fromString("21888242871839275222246405745257275088548364400416034343698204186575808495617"));
exports.hash0 = function (left, right) { exports.hash0 = function (left, right) {
return poseidon([left, right]); return poseidon([left, right]);
}; };
@ -10,5 +8,3 @@ exports.hash0 = function (left, right) {
exports.hash1 = function(key, value) { exports.hash1 = function(key, value) {
return poseidon([key, value, bigInt.one]); return poseidon([key, value, bigInt.one]);
}; };
//exports.F = poseidon.F;

@ -1,6 +1,7 @@
const chai = require("chai"); const chai = require("chai");
const path = require("path"); const path = require("path");
const tester = require("circom").tester; const snarkjs = require("snarkjs");
const compiler = require("circom");
const poseidon = require("../src/poseidon.js"); const poseidon = require("../src/poseidon.js");
@ -12,25 +13,30 @@ describe("Poseidon Circuit test", function () {
this.timeout(100000); this.timeout(100000);
before(async () => { before( async () => {
circuit2 = await tester(path.join(__dirname, "circuits", "poseidon2_test.circom")); const cirDef2 = await compiler(path.join(__dirname, "circuits", "poseidon2_test.circom"));
circuit4 = await tester(path.join(__dirname, "circuits", "poseidon4_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 () => { it("Should check constrain of hash([1, 2])", async () => {
const hash = poseidon([1, 2]); const hash = poseidon([1, 2]);
assert.equal("17117985411748610629288516079940078114952304104811071254131751175361957805920", hash.toString()); assert.equal("17117985411748610629288516079940078114952304104811071254131751175361957805920", hash.toString());
const w = await circuit2.calculateWitness({inputs: [1, 2]}, true); const w = await circuit2.calculateWitness({inputs: [1, 2]}, true);
await circuit2.assertOut(w, {out : hash}); const res = w[circuit2.getSignalIdx("main.out")];
await circuit2.checkConstraints(w); assert.equal(res.toString(), hash.toString());
await circuit2.checkWitness(w);
}); });
it("Should check constrain of hash([3, 4])", async () => { it("Should check constrain of hash([3, 4])", async () => {
const hash = poseidon([3, 4]); const hash = poseidon([3, 4]);
assert.equal("21867347236198497199818917118739170715216974132230970409806500217655788551452", hash.toString()); assert.equal("21867347236198497199818917118739170715216974132230970409806500217655788551452", hash.toString());
const w = await circuit2.calculateWitness({inputs: [3, 4]}); const w = await circuit2.calculateWitness({inputs: [3, 4]});
await circuit2.assertOut(w, {out : hash}); const res = w[circuit2.getSignalIdx("main.out")];
await circuit2.checkConstraints(w); 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]); const hash = poseidon([1, 2, 3, 4]);
assert.equal("10501812514110530158422365608831771203648472822841727510887411206067265790462", hash.toString()); assert.equal("10501812514110530158422365608831771203648472822841727510887411206067265790462", hash.toString());
const w = await circuit4.calculateWitness({inputs: [1, 2, 3, 4]}); const w = await circuit4.calculateWitness({inputs: [1, 2, 3, 4]});
await circuit4.assertOut(w, {out : hash}); const res = w[circuit4.getSignalIdx("main.out")];
await circuit4.checkConstraints(w); assert.equal(res.toString(), hash.toString());
await circuit4.checkWitness(w);
}); });
it("Should check constrain of hash([5, 6, 7, 8])", async () => { it("Should check constrain of hash([5, 6, 7, 8])", async () => {
const hash = poseidon([5, 6, 7, 8]); const hash = poseidon([5, 6, 7, 8]);
assert.equal("20761996991478317428195238015626872345373101531750069996451149877836620406299", hash.toString()); assert.equal("20761996991478317428195238015626872345373101531750069996451149877836620406299", hash.toString());
const w = await circuit4.calculateWitness({inputs: [5, 6, 7, 8]}); const w = await circuit4.calculateWitness({inputs: [5, 6, 7, 8]});
await circuit4.assertOut(w, {out : hash}); const res = w[circuit4.getSignalIdx("main.out")];
await circuit4.checkConstraints(w); assert.equal(res.toString(), hash.toString());
await circuit4.checkWitness(w);
}); });
}); });