2019-06-04 14:40:15 +03:00
|
|
|
const chai = require("chai");
|
|
|
|
const path = require("path");
|
2019-12-14 22:32:45 +03:00
|
|
|
var blake2b = require("blake2b");
|
|
|
|
const tester = require("circom").tester;
|
2019-06-04 14:40:15 +03:00
|
|
|
|
|
|
|
const poseidon = require("../src/poseidon.js");
|
|
|
|
|
|
|
|
const assert = chai.assert;
|
|
|
|
|
2019-06-28 11:50:49 +03:00
|
|
|
describe("Blake2b version test", function() {
|
|
|
|
it("Should give the expected output for blake2b version", async () => {
|
|
|
|
var output = new Uint8Array(32);
|
2019-12-14 22:32:45 +03:00
|
|
|
var input = Buffer.from("poseidon_constants");
|
|
|
|
const h = blake2b(output.length).update(input).digest("hex");
|
|
|
|
assert.equal("e57ba154fb2c47811dc1a2369b27e25a44915b4e4ece4eb8ec74850cb78e01b1", h);
|
2019-06-28 11:50:49 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-06-04 14:40:15 +03:00
|
|
|
describe("Poseidon Circuit test", function () {
|
2020-02-25 16:41:51 +03:00
|
|
|
let circuit6;
|
|
|
|
let circuit3;
|
2019-06-04 14:40:15 +03:00
|
|
|
|
|
|
|
this.timeout(100000);
|
|
|
|
|
|
|
|
before( async () => {
|
2020-03-26 21:52:34 +03:00
|
|
|
circuit6 = await tester(path.join(__dirname, "circuits", "poseidon6_test.circom"));
|
|
|
|
circuit3 = await tester(path.join(__dirname, "circuits", "poseidon3_test.circom"));
|
2020-02-25 16:41:51 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it("Should check constrain of hash([1, 2]) t=6", async () => {
|
2020-03-26 21:52:34 +03:00
|
|
|
const w = await circuit6.calculateWitness({inputs: [1, 2]}, true);
|
2019-06-04 14:40:15 +03:00
|
|
|
|
2020-02-25 16:41:51 +03:00
|
|
|
const hash = poseidon.createHash(6, 8, 57);
|
|
|
|
|
|
|
|
const res2 = hash([1,2]);
|
|
|
|
assert.equal("12242166908188651009877250812424843524687801523336557272219921456462821518061", res2.toString());
|
2020-03-26 21:52:34 +03:00
|
|
|
await circuit6.assertOut(w, {out : res2});
|
|
|
|
await circuit6.checkConstraints(w);
|
2019-06-04 14:40:15 +03:00
|
|
|
});
|
|
|
|
|
2020-02-25 16:41:51 +03:00
|
|
|
it("Should check constrain of hash([3, 4]) t=6", async () => {
|
2020-03-26 21:52:34 +03:00
|
|
|
const w = await circuit6.calculateWitness({inputs: [3, 4]});
|
2019-06-04 14:40:15 +03:00
|
|
|
|
|
|
|
const hash = poseidon.createHash(6, 8, 57);
|
|
|
|
|
2020-02-25 16:41:51 +03:00
|
|
|
const res2 = hash([3, 4]);
|
|
|
|
|
2019-12-14 22:32:45 +03:00
|
|
|
assert.equal("17185195740979599334254027721507328033796809509313949281114643312710535000993", res2.toString());
|
2020-03-26 21:52:34 +03:00
|
|
|
await circuit6.assertOut(w, {out : res2});
|
|
|
|
await circuit6.checkConstraints(w);
|
2020-02-25 16:41:51 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it("Should check constrain of hash([1, 2]) t=3", async () => {
|
2020-03-26 21:52:34 +03:00
|
|
|
const w = await circuit3.calculateWitness({inputs: [1, 2]});
|
2020-02-25 16:41:51 +03:00
|
|
|
|
|
|
|
const hash = poseidon.createHash(3, 8, 57);
|
|
|
|
|
2019-06-04 14:40:15 +03:00
|
|
|
const res2 = hash([1,2]);
|
2020-02-25 16:41:51 +03:00
|
|
|
assert.equal("2104035019328376391822106787753454168168617545136592089411833517434990977743", res2.toString());
|
2020-03-26 21:52:34 +03:00
|
|
|
await circuit3.assertOut(w, {out : res2});
|
|
|
|
await circuit3.checkConstraints(w);
|
2019-06-28 11:50:49 +03:00
|
|
|
});
|
2019-06-04 14:40:15 +03:00
|
|
|
|
2020-02-25 16:41:51 +03:00
|
|
|
it("Should check constrain of hash([3, 4]) t=3", async () => {
|
2020-03-26 21:52:34 +03:00
|
|
|
const w = await circuit3.calculateWitness({inputs: [3, 4]});
|
2019-06-28 11:50:49 +03:00
|
|
|
|
2020-02-25 16:41:51 +03:00
|
|
|
const hash = poseidon.createHash(3, 8, 57);
|
2019-06-28 11:50:49 +03:00
|
|
|
|
|
|
|
const res2 = hash([3, 4]);
|
2020-02-25 16:41:51 +03:00
|
|
|
assert.equal("12456141564250880945411182508630957604732712316993112736876413121277158512223", res2.toString());
|
2020-03-26 21:52:34 +03:00
|
|
|
await circuit3.assertOut(w, {out : res2});
|
|
|
|
await circuit3.checkConstraints(w);
|
2019-06-04 14:40:15 +03:00
|
|
|
});
|
|
|
|
});
|