arnaucube
arnaucube/circomlibPublic Self-hosted repository
circomlib/test/poseidoncontract.js
test/poseidoncontract.jsRaw
 1const ganache = require("ganache-cli");
 2const Web3 = require("web3");
 3const chai = require("chai");
 4const poseidonGenContract = require("../src/poseidon_gencontract.js");
 5const Poseidon = require("../src/poseidon.js");
 6const bigInt = require("snarkjs").bigInt;
 7
 8const assert = chai.assert;
 9const log = (msg) => { if (process.env.MOCHA_VERBOSE) console.log(msg); };
10
11describe("Poseidon Smart contract test", function () {
12    let testrpc;
13    let web3;
14    let poseidon6;
15    let poseidon3;
16    let accounts;
17    this.timeout(100000);
18
19    before(async () => {
20        web3 = new Web3(ganache.provider(), null, { transactionConfirmationBlocks: 1 });
21        accounts = await web3.eth.getAccounts();
22    });
23
24    it("Should deploy the contract", async () => {
25        const C = new web3.eth.Contract(poseidonGenContract.abi);
26
27        poseidon6 = await C.deploy({
28            data: poseidonGenContract.createCode(6)
29        }).send({
30            gas: 2500000,
31            from: accounts[0]
32        });
33        poseidon3 = await C.deploy({
34            data: poseidonGenContract.createCode(3)
35        }).send({
36            gas: 2500000,
37            from: accounts[0]
38        });
39    });
40
41    it("Shold calculate the poseidon correctly t=6", async () => {
42
43        const res = await poseidon6.methods.poseidon([1,2]).call();
44
45        // console.log("Cir: " + bigInt(res.toString(16)).toString(16));
46
47        const hash = Poseidon.createHash(6, 8, 57);
48
49        const res2 = hash([1,2]);
50        // console.log("Ref: " + bigInt(res2).toString(16));
51
52        assert.equal(res.toString(), res2.toString());
53    });
54    it("Shold calculate the poseidon correctly t=3", async () => {
55
56        const res = await poseidon3.methods.poseidon([1,2]).call();
57
58        // console.log("Cir: " + bigInt(res.toString(16)).toString(16));
59
60        const hash = Poseidon.createHash(3, 8, 57);
61
62        const res2 = hash([1,2]);
63        // console.log("Ref: " + bigInt(res2).toString(16));
64
65        assert.equal(res.toString(), res2.toString());
66    });
67
68});
69