diff --git a/circuits/poseidon.circom b/circuits/poseidon.circom index 70635ddb..fd6c4939 100644 --- a/circuits/poseidon.circom +++ b/circuits/poseidon.circom @@ -30,7 +30,7 @@ template Mix(t, M) { for (var i=0; i0) { + ark[i].in[j] <== inputs[j-1]; } else { ark[i].in[j] <== 0; } @@ -91,8 +91,5 @@ template Poseidon(nInputs) { } } - // last round is done only for the first word, so we do it manually to save constraints - component lastSigmaF = Sigma(); - lastSigmaF.in <== mix[nRoundsF + nRoundsP - 2].out[0] + C[t*(nRoundsF + nRoundsP - 1)]; - out <== lastSigmaF.out; + out <== mix[nRoundsF + nRoundsP -1].out[0]; } diff --git a/src/poseidon.js b/src/poseidon.js index bcd54475..af63c409 100644 --- a/src/poseidon.js +++ b/src/poseidon.js @@ -26,7 +26,7 @@ function poseidon(inputs) { const nRoundsF = N_ROUNDS_F; const nRoundsP = N_ROUNDS_P[t - 2]; - let state = [...inputs.map(a => F.e(a)), F.zero]; + let state = [F.zero, ...inputs.map(a => F.e(a))]; for (let r = 0; r < nRoundsF + nRoundsP; r++) { state = state.map((a, i) => F.add(a, C[t - 2][r * t + i])); @@ -36,12 +36,9 @@ function poseidon(inputs) { state[0] = pow5(state[0]); } - // no matrix multiplication in the last round - if (r < nRoundsF + nRoundsP - 1) { - state = state.map((_, i) => - state.reduce((acc, a, j) => F.add(acc, F.mul(M[t - 2][j][i], a)), F.zero) - ); - } + state = state.map((_, i) => + state.reduce((acc, a, j) => F.add(acc, F.mul(M[t - 2][i][j], a)), F.zero) + ); } return F.normalize(state[0]); } diff --git a/src/poseidon_gencontract.js b/src/poseidon_gencontract.js index 3f896abf..1ca16a53 100644 --- a/src/poseidon_gencontract.js +++ b/src/poseidon_gencontract.js @@ -29,7 +29,7 @@ function createCode(nInputs) { function saveM() { for (let i=0; i=nRoundsP+nRoundsF/2)) { for (let j=0; j { + const res2 = poseidon([1,2]); + assert.equal("115cc0f5e7d690413df64c6b9662e9cf2a3617f2743245519e19607a4417189a", res2.toString(16)); + }); + it("Should check constrain reference implementation poseidonperm_x5_254_5", async () => { + const res2 = poseidon([1,2,3,4]); + assert.equal("299c867db6c1fdd79dcefa40e4510b9837e60ebb1ce0663dbaa525df65250465", res2.toString(16)); + }); +});