diff --git a/src/evmasm.js b/src/evmasm.js index 8bf223f..ab7f830 100644 --- a/src/evmasm.js +++ b/src/evmasm.js @@ -154,6 +154,17 @@ class Contract { } push(data) { + if (typeof data === "number") { + let isNeg; + if (data<0) { + isNeg = true; + data = -data; + } + data = data.toString(16); + if (data.length % 2 == 1) data = "0" + data; + data = "0x" + data; + if (isNeg) data = "-"+data; + } const d = Web3Utils.hexToBytes(Web3Utils.toHex(data)); if (d.length == 0 || d.length > 32) { throw new Error("Assertion failed"); diff --git a/src/poseidon.js b/src/poseidon.js index e349fe1..db1c706 100644 --- a/src/poseidon.js +++ b/src/poseidon.js @@ -47,4 +47,3 @@ function poseidon(inputs) { } module.exports = poseidon; -module.exports.F = F; \ No newline at end of file diff --git a/src/poseidon_gencontract.js b/src/poseidon_gencontract.js index f482aec..c714305 100644 --- a/src/poseidon_gencontract.js +++ b/src/poseidon_gencontract.js @@ -2,14 +2,13 @@ // License: LGPL-3.0+ // -const Poseidon = require("./poseidon.js"); - const Contract = require("./evmasm"); +const { unstringifyBigInts } = require("ffjavascript").utils; -const SEED = "poseidon"; -const NROUNDSF = 8; -const NROUNDSP = 57; -const T = 6; +const { C:K, M } = unstringifyBigInts(require("./poseidon_constants.json")); + +const N_ROUNDS_F = 8; +const N_ROUNDS_P = [56, 57, 56, 60, 60, 63, 64, 63]; function toHex256(a) { let S = a.toString(16); @@ -17,38 +16,34 @@ function toHex256(a) { return "0x" + S; } -function createCode(t, nRoundsF, nRoundsP, seed) { - if (typeof seed === "undefined") seed = SEED; - if (typeof nRoundsF === "undefined") nRoundsF = NROUNDSF; - if (typeof nRoundsP === "undefined") nRoundsP = NROUNDSP; - if (typeof t === "undefined") t = T; +function createCode(nInputs) { - const K = Poseidon.getConstants(t, seed, nRoundsP + nRoundsF); - const M = Poseidon.getMatrix(t, seed, nRoundsP + nRoundsF); + if (( nInputs<1) || (nInputs>8)) throw new Error("Invalid number of inputs. Must be 1<=nInputs<=8"); + const t = nInputs + 1; + const nRoundsF = N_ROUNDS_F; + const nRoundsP = N_ROUNDS_P[t - 2]; const C = new Contract(); function saveM() { for (let i=0; i=nRoundsP+nRoundsF/2)) { for (let j=0; j