circomlib/test/poseidoncircuit.js
test/poseidoncircuit.jsRaw
1const chai = require("chai");
2const path = require("path");
3const snarkjs = require("snarkjs");
4const compiler = require("circom");
5var blake2b = require('blake2b');
6
7const poseidon = require("../src/poseidon.js");
8
9const assert = chai.assert;
10
11describe("Blake2b version test", function() {
12 it("Should give the expected output for blake2b version", async () => {
13 var output = new Uint8Array(32);
14 var input = Buffer.from('poseidon_constants');
15 h = blake2b(output.length).update(input).digest('hex')
16 assert.equal('e57ba154fb2c47811dc1a2369b27e25a44915b4e4ece4eb8ec74850cb78e01b1', h);
17 });
18});
19
20describe("Poseidon Circuit test", function () {
21 let circuit6;
22 let circuit3;
23
24 this.timeout(100000);
25
26 before( async () => {
27 const cirDef6 = await compiler(path.join(__dirname, "circuits", "poseidon6_test.circom"));
28 circuit6 = new snarkjs.Circuit(cirDef6);
29 console.log("Poseidon6 constraints: " + circuit6.nConstraints);
30 const cirDef3 = await compiler(path.join(__dirname, "circuits", "poseidon3_test.circom"));
31 circuit3 = new snarkjs.Circuit(cirDef3);
32 console.log("Poseidon3 constraints: " + circuit3.nConstraints);
33 });
34
35 it("Should check constrain of hash([1, 2]) t=6", async () => {
36 const w = circuit6.calculateWitness({inputs: [1, 2]});
37
38 const res = w[circuit6.getSignalIdx("main.out")];
39
40 const hash = poseidon.createHash(6, 8, 57);
41
42 const res2 = hash([1,2]);
43 assert.equal("12242166908188651009877250812424843524687801523336557272219921456462821518061", res2.toString());
44 assert.equal(res.toString(), res2.toString());
45 assert(circuit6.checkWitness(w));
46 });
47
48 it("Should check constrain of hash([3, 4]) t=6", async () => {
49 const w = circuit6.calculateWitness({inputs: [3, 4]});
50
51 const res = w[circuit6.getSignalIdx("main.out")];
52
53 const hash = poseidon.createHash(6, 8, 57);
54
55 const res2 = hash([3, 4]);
56 assert.equal("17185195740979599334254027721507328033796809509313949281114643312710535000993", res2.toString());
57
58 assert.equal(res.toString(), res2.toString());
59
60 assert(circuit6.checkWitness(w));
61 });
62
63
64 it("Should check constrain of hash([1, 2]) t=3", async () => {
65 const w = circuit3.calculateWitness({inputs: [1, 2]});
66
67 const res = w[circuit3.getSignalIdx("main.out")];
68
69 const hash = poseidon.createHash(3, 8, 57);
70
71 const res2 = hash([1,2]);
72 assert.equal("2104035019328376391822106787753454168168617545136592089411833517434990977743", res2.toString());
73 assert.equal(res.toString(), res2.toString());
74 assert(circuit3.checkWitness(w));
75 });
76
77 it("Should check constrain of hash([3, 4]) t=3", async () => {
78 const w = circuit3.calculateWitness({inputs: [3, 4]});
79
80 const res = w[circuit3.getSignalIdx("main.out")];
81
82 const hash = poseidon.createHash(3, 8, 57);
83
84 const res2 = hash([3, 4]);
85 assert.equal("12456141564250880945411182508630957604732712316993112736876413121277158512223", res2.toString());
86
87 assert.equal(res.toString(), res2.toString());
88
89 assert(circuit3.checkWitness(w));
90 });
91});