mirror of
https://github.com/arnaucube/circomlib.git
synced 2026-02-06 18:56:43 +01:00
Adds a sponge hash function based on MiMC2n-n
This commit is contained in:
3
test/circuits/mimc_sponge_hash_test.circom
Normal file
3
test/circuits/mimc_sponge_hash_test.circom
Normal file
@@ -0,0 +1,3 @@
|
||||
include "../../circuits/mimcsponge.circom"
|
||||
|
||||
component main = MiMCSponge(2, 220, 3);
|
||||
3
test/circuits/mimc_sponge_test.circom
Normal file
3
test/circuits/mimc_sponge_test.circom
Normal file
@@ -0,0 +1,3 @@
|
||||
include "../../circuits/mimcsponge.circom"
|
||||
|
||||
component main = MiMCFeistel(220);
|
||||
58
test/mimcspongecircuit.js
Normal file
58
test/mimcspongecircuit.js
Normal file
@@ -0,0 +1,58 @@
|
||||
const chai = require("chai");
|
||||
const path = require("path");
|
||||
const snarkjs = require("snarkjs");
|
||||
const compiler = require("circom");
|
||||
|
||||
const mimcjs = require("../src/mimcsponge.js");
|
||||
|
||||
const assert = chai.assert;
|
||||
|
||||
describe("MiMC Sponge Circuit test", function () {
|
||||
let circuit;
|
||||
|
||||
this.timeout(100000);
|
||||
|
||||
it("Should check permutation", async () => {
|
||||
const cirDef = await compiler(path.join(__dirname, "circuits", "mimc_sponge_test.circom"));
|
||||
|
||||
circuit = new snarkjs.Circuit(cirDef);
|
||||
|
||||
console.log("MiMC Feistel constraints: " + circuit.nConstraints);
|
||||
|
||||
const w = circuit.calculateWitness({xL_in: 1, xR_in: 2, k: 3});
|
||||
|
||||
const xLout = w[circuit.getSignalIdx("main.xL_out")];
|
||||
const xRout = w[circuit.getSignalIdx("main.xR_out")];
|
||||
|
||||
const out2 = mimcjs.hash(1,2,3);
|
||||
|
||||
assert.equal(xLout.toString(), out2.xL.toString());
|
||||
assert.equal(xRout.toString(), out2.xR.toString());
|
||||
|
||||
assert(circuit.checkWitness(w));
|
||||
|
||||
});
|
||||
|
||||
it("Should check hash", async () => {
|
||||
const cirDef = await compiler(path.join(__dirname, "circuits", "mimc_sponge_hash_test.circom"));
|
||||
|
||||
circuit = new snarkjs.Circuit(cirDef);
|
||||
|
||||
console.log("MiMC Sponge constraints: " + circuit.nConstraints);
|
||||
|
||||
const w = circuit.calculateWitness({ins: [1, 2], k: 0});
|
||||
|
||||
const o1 = w[circuit.getSignalIdx("main.outs[0]")];
|
||||
const o2 = w[circuit.getSignalIdx("main.outs[1]")];
|
||||
const o3 = w[circuit.getSignalIdx("main.outs[2]")];
|
||||
|
||||
const out2 = mimcjs.multiHash([1,2], 0, 3);
|
||||
|
||||
assert.equal(o1.toString(), out2[0].toString());
|
||||
assert.equal(o2.toString(), out2[1].toString());
|
||||
assert.equal(o3.toString(), out2[2].toString());
|
||||
|
||||
assert(circuit.checkWitness(w));
|
||||
|
||||
});
|
||||
});
|
||||
53
test/mimcspongecontract.js
Normal file
53
test/mimcspongecontract.js
Normal file
@@ -0,0 +1,53 @@
|
||||
const TestRPC = require("ganache-cli");
|
||||
const Web3 = require("web3");
|
||||
const chai = require("chai");
|
||||
const mimcGenContract = require("../src/mimcsponge_gencontract.js");
|
||||
const mimcjs = require("../src/mimcsponge.js");
|
||||
|
||||
|
||||
const assert = chai.assert;
|
||||
const log = (msg) => { if (process.env.MOCHA_VERBOSE) console.log(msg); };
|
||||
|
||||
const SEED = "mimcsponge";
|
||||
|
||||
describe("MiMC Sponge 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(mimcGenContract.abi);
|
||||
|
||||
mimc = await C.deploy({
|
||||
data: mimcGenContract.createCode(SEED, 220)
|
||||
}).send({
|
||||
gas: 3500000,
|
||||
from: accounts[0]
|
||||
});
|
||||
});
|
||||
|
||||
it("Shold calculate the mimc correctly", async () => {
|
||||
const res = await mimc.methods.MiMCSponge(1,2,3).call();
|
||||
const res2 = await mimcjs.hash(1,2,3);
|
||||
|
||||
assert.equal(res.xL.toString(), res2.xL.toString());
|
||||
assert.equal(res.xR.toString(), res2.xR.toString());
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user