From 528b292da5a0de4695bc3b86764fb6607698e55c Mon Sep 17 00:00:00 2001 From: poma Date: Sun, 9 Aug 2020 14:32:08 +0300 Subject: [PATCH] Update references to Poseidon hash in the project --- circuits/README.md | 4 +-- circuits/eddsaposeidon.circom | 2 +- circuits/smt/smthash_poseidon.circom | 4 +-- src/eddsa.js | 6 ++-- src/smt_hashes_poseidon.js | 10 +++---- test/poseidoncontract.js | 41 +++++++++++++++++++--------- 6 files changed, 40 insertions(+), 27 deletions(-) diff --git a/circuits/README.md b/circuits/README.md index afd4a1f..53cac46 100644 --- a/circuits/README.md +++ b/circuits/README.md @@ -780,7 +780,7 @@ Implementation of Poseidon hash function (LINK) - BENCHMARKS - EXAMPLE -- `Ark(t, C)` +- `Ark(t, C, r)` - DESCRIPTION - SCHEMA @@ -798,7 +798,7 @@ Implementation of Poseidon hash function (LINK) - BENCHMARKS - EXAMPLE -- `Poseidon(nInputs, t, nRoundsF, nRoundsP)` +- `Poseidon(nInputs)` - DESCRIPTION - SCHEMA diff --git a/circuits/eddsaposeidon.circom b/circuits/eddsaposeidon.circom index fb6c782..ff38ff5 100644 --- a/circuits/eddsaposeidon.circom +++ b/circuits/eddsaposeidon.circom @@ -50,7 +50,7 @@ template EdDSAPoseidonVerifier() { // Calculate the h = H(R,A, msg) - component hash = Poseidon(5, 6, 8, 57); + component hash = Poseidon(5); hash.inputs[0] <== R8x; hash.inputs[1] <== R8y; diff --git a/circuits/smt/smthash_poseidon.circom b/circuits/smt/smthash_poseidon.circom index 5a9feb7..263c929 100644 --- a/circuits/smt/smthash_poseidon.circom +++ b/circuits/smt/smthash_poseidon.circom @@ -29,7 +29,7 @@ template SMTHash1() { signal input value; signal output out; - component h = Poseidon(3, 6, 8, 57); // Constant + component h = Poseidon(3); // Constant h.inputs[0] <== key; h.inputs[1] <== value; h.inputs[2] <== 1; @@ -48,7 +48,7 @@ template SMTHash2() { signal input R; signal output out; - component h = Poseidon(2, 6, 8, 57); // Constant + component h = Poseidon(2); // Constant h.inputs[0] <== L; h.inputs[1] <== R; diff --git a/src/eddsa.js b/src/eddsa.js index beae967..d746a47 100644 --- a/src/eddsa.js +++ b/src/eddsa.js @@ -105,8 +105,7 @@ function signPoseidon(prv, msg) { let r = bigInt.leBuff2int(rBuff); r = r.mod(babyJub.subOrder); const R8 = babyJub.mulPointEscalar(babyJub.Base8, r); - const hash = poseidon.createHash(6, 8, 57); - const hm = hash([R8[0], R8[1], A[0], A[1], msg]); + const hm = poseidon([R8[0], R8[1], A[0], A[1], msg]); const S = r.add(hm.mul(s)).mod(babyJub.subOrder); return { R8: R8, @@ -173,8 +172,7 @@ function verifyPoseidon(msg, sig, A) { if (!babyJub.inCurve(A)) return false; if (sig.S>= babyJub.subOrder) return false; - const hash = poseidon.createHash(6, 8, 57); - const hm = hash([sig.R8[0], sig.R8[1], A[0], A[1], msg]); + const hm = poseidon([sig.R8[0], sig.R8[1], A[0], A[1], msg]); const Pleft = babyJub.mulPointEscalar(babyJub.Base8, sig.S); let Pright = babyJub.mulPointEscalar(A, hm.mul(bigInt("8"))); diff --git a/src/smt_hashes_poseidon.js b/src/smt_hashes_poseidon.js index 8ad8770..2b38192 100644 --- a/src/smt_hashes_poseidon.js +++ b/src/smt_hashes_poseidon.js @@ -1,12 +1,12 @@ -const Poseidon = require("./poseidon"); +const poseidon = require("./poseidon"); const bigInt = require("snarkjs").bigInt; -const hash = Poseidon.createHash(6, 8, 57); - exports.hash0 = function (left, right) { - return hash([left, right]); + return poseidon([left, right]); }; exports.hash1 = function(key, value) { - return hash([key, value, bigInt.one]); + return poseidon([key, value, bigInt.one]); }; + +exports.F = poseidon.F; diff --git a/test/poseidoncontract.js b/test/poseidoncontract.js index 4a04d55..12c2c82 100644 --- a/test/poseidoncontract.js +++ b/test/poseidoncontract.js @@ -2,19 +2,18 @@ const ganache = 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 poseidon = require("../src/poseidon.js"); const assert = chai.assert; const log = (msg) => { if (process.env.MOCHA_VERBOSE) console.log(msg); }; -const SEED = "mimc"; - -describe("Poseidon Smart contract test", () => { +describe("Poseidon Smart contract test", function () { let testrpc; let web3; - let mimc; + let poseidon2; + let poseidon4; let accounts; + this.timeout(100000); before(async () => { web3 = new Web3(ganache.provider(), null, { transactionConfirmationBlocks: 1 }); @@ -24,26 +23,42 @@ describe("Poseidon Smart contract test", () => { it("Should deploy the contract", async () => { const C = new web3.eth.Contract(poseidonGenContract.abi); - mimc = await C.deploy({ - data: poseidonGenContract.createCode() + poseidon2 = await C.deploy({ + data: poseidonGenContract.createCode(2) + }).send({ + gas: 2500000, + from: accounts[0] + }); + poseidon4 = await C.deploy({ + data: poseidonGenContract.createCode(4) }).send({ gas: 2500000, from: accounts[0] }); }); - it("Shold calculate the mimic correctly", async () => { + it("Shold calculate the poseidon correctly for 2 inputs", async () => { - const res = await mimc.methods.poseidon([1,2]).call(); + const res = await poseidon2.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]); + const res2 = poseidon([1, 2]); // console.log("Ref: " + bigInt(res2).toString(16)); assert.equal(res.toString(), res2.toString()); }); + it("Shold calculate the poseidon correctly for 4 inputs", async () => { + + const res = await poseidon4.methods.poseidon([1, 2, 3, 4]).call(); + + // console.log("Cir: " + bigInt(res.toString(16)).toString(16)); + + const res2 = poseidon([1, 2, 3, 4]); + // console.log("Ref: " + bigInt(res2).toString(16)); + + assert.equal(res.toString(), res2.toString()); + }); + });