mirror of
https://github.com/arnaucube/circomlib.git
synced 2026-02-06 18:56:43 +01:00
Poseidon
This commit is contained in:
3
test/circuits/poseidon_test.circom
Normal file
3
test/circuits/poseidon_test.circom
Normal file
@@ -0,0 +1,3 @@
|
||||
include "../../circuits/poseidon.circom"
|
||||
|
||||
component main = Poseidon(2, 6, 8, 57);
|
||||
39
test/poseidoncircuit.js
Normal file
39
test/poseidoncircuit.js
Normal file
@@ -0,0 +1,39 @@
|
||||
const chai = require("chai");
|
||||
const path = require("path");
|
||||
const snarkjs = require("snarkjs");
|
||||
const compiler = require("circom");
|
||||
|
||||
const poseidon = require("../src/poseidon.js");
|
||||
|
||||
const assert = chai.assert;
|
||||
|
||||
describe("Poseidon Circuit test", function () {
|
||||
let circuit;
|
||||
|
||||
this.timeout(100000);
|
||||
|
||||
before( async () => {
|
||||
const cirDef = await compiler(path.join(__dirname, "circuits", "poseidon_test.circom"));
|
||||
|
||||
circuit = new snarkjs.Circuit(cirDef);
|
||||
|
||||
console.log("Poseidon constraints: " + circuit.nConstraints);
|
||||
});
|
||||
|
||||
it("Should check constrain", async () => {
|
||||
const w = circuit.calculateWitness({inputs: [1, 2]});
|
||||
|
||||
const res = w[circuit.getSignalIdx("main.out")];
|
||||
|
||||
const hash = poseidon.createHash(6, 8, 57);
|
||||
|
||||
const res2 = hash([1,2]);
|
||||
|
||||
console.log(res.toString());
|
||||
|
||||
assert.equal(res.toString(), res2.toString());
|
||||
|
||||
assert(circuit.checkWitness(w));
|
||||
|
||||
});
|
||||
});
|
||||
59
test/poseidoncontract.js
Normal file
59
test/poseidoncontract.js
Normal file
@@ -0,0 +1,59 @@
|
||||
const TestRPC = require("ganache-cli");
|
||||
const Web3 = require("web3");
|
||||
const chai = require("chai");
|
||||
const poseidonGenContract = require("../src/poseidon_gencontract.js");
|
||||
const Poseidon = require("../src/poseidon.js");
|
||||
const bigInt = require("snarkjs").bigInt;
|
||||
|
||||
const assert = chai.assert;
|
||||
const log = (msg) => { if (process.env.MOCHA_VERBOSE) console.log(msg); };
|
||||
|
||||
const SEED = "mimc";
|
||||
|
||||
describe("Poseidon Smart contract test", () => {
|
||||
let testrpc;
|
||||
let web3;
|
||||
let mimc;
|
||||
let accounts;
|
||||
|
||||
before(async () => {
|
||||
testrpc = TestRPC.server({
|
||||
ws: true,
|
||||
gasLimit: 5800000,
|
||||
total_accounts: 10,
|
||||
});
|
||||
|
||||
testrpc.listen(8546, "127.0.0.1");
|
||||
|
||||
web3 = new Web3("ws://127.0.0.1:8546");
|
||||
accounts = await web3.eth.getAccounts();
|
||||
});
|
||||
|
||||
after(async () => testrpc.close());
|
||||
|
||||
it("Should deploy the contract", async () => {
|
||||
const C = new web3.eth.Contract(poseidonGenContract.abi);
|
||||
|
||||
mimc = await C.deploy({
|
||||
data: poseidonGenContract.createCode()
|
||||
}).send({
|
||||
gas: 2500000,
|
||||
from: accounts[0]
|
||||
});
|
||||
});
|
||||
|
||||
it("Shold calculate the mimic correctly", async () => {
|
||||
|
||||
const res = await mimc.methods.poseidon([1,2]).call();
|
||||
|
||||
console.log("Cir: " + bigInt(res.toString(16)).toString(16));
|
||||
|
||||
const hash = Poseidon.createHash(6, 8, 57);
|
||||
|
||||
const res2 = hash([1,2]);
|
||||
console.log("Ref: " + bigInt(res2).toString(16));
|
||||
|
||||
assert.equal(res.toString(), res2.toString());
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user