Update references to Poseidon hash in the project

This commit is contained in:
poma 2020-08-09 14:32:08 +03:00
parent 82c2f606cc
commit 528b292da5
No known key found for this signature in database
GPG Key ID: BA20CB01FE165657
6 changed files with 40 additions and 27 deletions

@ -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

@ -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;

@ -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;

@ -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")));

@ -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;

@ -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());
});
});