arnaucube
arnaucube/circomlibPublic Self-hosted repository
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 circuit;
22
23    this.timeout(100000);
24
25    before( async () => {
26        const cirDef = await compiler(path.join(__dirname, "circuits", "poseidon_test.circom"));
27
28        circuit = new snarkjs.Circuit(cirDef);
29
30        console.log("Poseidon constraints: " + circuit.nConstraints);
31    });
32
33    it("Should check constrain of hash([1, 2])", async () => {
34        const w = circuit.calculateWitness({inputs: [1, 2]});
35
36        const res = w[circuit.getSignalIdx("main.out")];
37
38        const hash = poseidon.createHash(6, 8, 57);
39
40        const res2 = hash([1,2]);
41        assert.equal('12242166908188651009877250812424843524687801523336557272219921456462821518061', res2.toString());
42        assert.equal(res.toString(), res2.toString());
43        assert(circuit.checkWitness(w));
44    });
45
46    it("Should check constrain of hash([3, 4])", async () => {
47        const w = circuit.calculateWitness({inputs: [3, 4]});
48
49        const res = w[circuit.getSignalIdx("main.out")];
50
51        const hash = poseidon.createHash(6, 8, 57);
52
53        const res2 = hash([3, 4]);
54        assert.equal('17185195740979599334254027721507328033796809509313949281114643312710535000993', res2.toString());
55
56        assert.equal(res.toString(), res2.toString());
57
58        assert(circuit.checkWitness(w));
59    });
60});