Skip to content

Commit

Permalink
feat: up to poseidon T9 support
Browse files Browse the repository at this point in the history
  • Loading branch information
chancehudson committed Feb 15, 2023
1 parent d8aa875 commit dce1d8f
Show file tree
Hide file tree
Showing 14 changed files with 5,268 additions and 78 deletions.
3 changes: 2 additions & 1 deletion contracts/PoseidonT2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ library PoseidonT2 {
{
// load the inputs from memory
let state1 := addmod(mload(0x80), 0x0c0356530896eec42a97ed937f3135cfc5142b3ae405b8343c1d83ffa604cb81, F)
mstore(0x20, addmod(mload(0xa0), 0x1e28a1d935698ad1142e51182bb54cf4a00ea5aabd6268bd317ea977cc154a30, F))

p := mulmod(state1, state1, F)
state1 := mulmod(mulmod(p, p, F), state1, F)
Expand Down Expand Up @@ -200,7 +201,7 @@ library PoseidonT2 {
p := mulmod(state1, state1, F)
state1 := mulmod(mulmod(p, p, F), state1, F)

mstore(0, addmod(mulmod(state0, M00, F), mulmod(state1, M10, F), F))
mstore(0x0, addmod(mulmod(state0, M00, F), mulmod(state1, M10, F), F))
return(0, 0x20)
}
}
Expand Down
3 changes: 2 additions & 1 deletion contracts/PoseidonT3.sol
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ library PoseidonT3 {
// load the inputs from memory
let state1 := addmod(mload(0x80), 0x00f1445235f2148c5986587169fc1bcd887b08d4d00868df5696fff40956e864, F)
let state2 := addmod(mload(0xa0), 0x08dff3487e8ac99e1f29a058d0fa80b930c728730b7ab36ce879f3890ecf73f5, F)
mstore(0x60, addmod(mload(0xc0), 0x2f27be690fdaee46c3ce28f7532b13c856c35342c84bda6e20966310fadc01d0, F))

p := mulmod(state1, state1, F)
state1 := mulmod(mulmod(p, p, F), state1, F)
Expand Down Expand Up @@ -472,7 +473,7 @@ library PoseidonT3 {
p := mulmod(state2, state2, F)
state2 := mulmod(mulmod(p, p, F), state2, F)

mstore(0, addmod(addmod(mulmod(state0, M00, F), mulmod(state1, M10, F), F), mulmod(state2, M20, F), F))
mstore(0x0, addmod(addmod(mulmod(state0, M00, F), mulmod(state1, M10, F), F), mulmod(state2, M20, F), F))
return(0, 0x20)
}
}
Expand Down
3 changes: 2 additions & 1 deletion contracts/PoseidonT4.sol
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ library PoseidonT4 {
let state1 := addmod(mload(0x80), 0x265ddfe127dd51bd7239347b758f0a1320eb2cc7450acc1dad47f80c8dcf34d6, F)
let state2 := addmod(mload(0xa0), 0x199750ec472f1809e0f66a545e1e51624108ac845015c2aa3dfc36bab497d8aa, F)
let state3 := addmod(mload(0xc0), 0x157ff3fe65ac7208110f06a5f74302b14d743ea25067f0ffd032f787c7f1cdf8, F)
mstore(0xa0, addmod(mload(0xe0), 0x2e49c43c4569dd9c5fd35ac45fca33f10b15c590692f8beefe18f4896ac94902, F))

p := mulmod(state1, state1, F)
state1 := mulmod(mulmod(p, p, F), state1, F)
Expand Down Expand Up @@ -576,7 +577,7 @@ library PoseidonT4 {
p := mulmod(state3, state3, F)
state3 := mulmod(mulmod(p, p, F), state3, F)

mstore(0, addmod(addmod(addmod(mulmod(state0, M00, F), mulmod(state1, M10, F), F), mulmod(state2, M20, F), F), mulmod(state3, M30, F), F))
mstore(0x0, addmod(addmod(addmod(mulmod(state0, M00, F), mulmod(state1, M10, F), F), mulmod(state2, M20, F), F), mulmod(state3, M30, F), F))
return(0, 0x20)
}
}
Expand Down
3 changes: 2 additions & 1 deletion contracts/PoseidonT5.sol
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ library PoseidonT5 {
let state2 := addmod(mload(0xa0), 0x2f83b9df259b2b68bcd748056307c37754907df0c0fb0035f5087c58d5e8c2d4, F)
let state3 := addmod(mload(0xc0), 0x2ca70e2e8d7f39a12447ac83052451b461f15f8b41a75ef31915208f5aba9683, F)
let state4 := addmod(mload(0xe0), 0x1cb5f9319be6a45e91b04d7222271c94994196f12ed22c5d4ec719cb83ecfea9, F)
mstore(0xe0, addmod(mload(0x100), 0x2eb4f99c69f966ebf8a42192de7ff61621c7bb47b93750c2b9ea08d18446c122, F))

p := mulmod(state1, state1, F)
state1 := mulmod(mulmod(p, p, F), state1, F)
Expand Down Expand Up @@ -750,7 +751,7 @@ library PoseidonT5 {
state4 := mulmod(mulmod(p, p, F), state4, F)

mstore(
0,
0x0,
addmod(addmod(addmod(addmod(mulmod(state0, M00, F), mulmod(state1, M10, F), F), mulmod(state2, M20, F), F), mulmod(state3, M30, F), F), mulmod(state4, M40, F), F)
)
return(0, 0x20)
Expand Down
875 changes: 875 additions & 0 deletions contracts/PoseidonT6.sol

Large diffs are not rendered by default.

1,079 changes: 1,079 additions & 0 deletions contracts/PoseidonT7.sol

Large diffs are not rendered by default.

1,332 changes: 1,332 additions & 0 deletions contracts/PoseidonT8.sol

Large diffs are not rendered by default.

1,741 changes: 1,741 additions & 0 deletions contracts/PoseidonT9.sol

Large diffs are not rendered by default.

92 changes: 92 additions & 0 deletions contracts/Test.sol
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,38 @@ interface _PoseidonT5 {
function hash(uint[4] calldata) external pure returns (uint);
}

interface Poseidon6 {
function poseidon(uint[5] calldata) external pure returns (uint);
}

interface _PoseidonT6 {
function hash(uint[5] calldata) external pure returns (uint);
}

interface Poseidon7 {
function poseidon(uint[6] calldata) external pure returns (uint);
}

interface _PoseidonT7 {
function hash(uint[6] calldata) external pure returns (uint);
}

interface Poseidon8 {
function poseidon(uint[7] calldata) external pure returns (uint);
}

interface _PoseidonT8 {
function hash(uint[7] calldata) external pure returns (uint);
}

interface Poseidon9 {
function poseidon(uint[8] calldata) external pure returns (uint);
}

interface _PoseidonT9 {
function hash(uint[8] calldata) external pure returns (uint);
}

contract Test {
// Benchmark poseidon-solidity
function benchmarkA2(address p, uint[1] memory inputs) public view returns (uint) {
Expand Down Expand Up @@ -96,4 +128,64 @@ contract Test {
console.log(g-gasleft());
return r;
}
// Benchmark poseidon-solidity
function benchmarkA6(address p, uint[5] memory inputs) public view returns (uint) {
uint g = gasleft();
uint r = _PoseidonT6(p).hash(inputs);
console.log(g-gasleft());
return r;
}

// Benchmark circomlibjs
function benchmarkB6(address p, uint[5] memory inputs) public view returns (uint) {
uint g = gasleft();
uint r = Poseidon6(p).poseidon(inputs);
console.log(g-gasleft());
return r;
}
// Benchmark poseidon-solidity
function benchmarkA7(address p, uint[6] memory inputs) public view returns (uint) {
uint g = gasleft();
uint r = _PoseidonT7(p).hash(inputs);
console.log(g-gasleft());
return r;
}

// Benchmark circomlibjs
function benchmarkB7(address p, uint[6] memory inputs) public view returns (uint) {
uint g = gasleft();
uint r = Poseidon7(p).poseidon(inputs);
console.log(g-gasleft());
return r;
}
// Benchmark poseidon-solidity
function benchmarkA8(address p, uint[7] memory inputs) public view returns (uint) {
uint g = gasleft();
uint r = _PoseidonT8(p).hash(inputs);
console.log(g-gasleft());
return r;
}

// Benchmark circomlibjs
function benchmarkB8(address p, uint[7] memory inputs) public view returns (uint) {
uint g = gasleft();
uint r = Poseidon8(p).poseidon(inputs);
console.log(g-gasleft());
return r;
}
// Benchmark poseidon-solidity
function benchmarkA9(address p, uint[8] memory inputs) public view returns (uint) {
uint g = gasleft();
uint r = _PoseidonT9(p).hash(inputs);
console.log(g-gasleft());
return r;
}

// Benchmark circomlibjs
function benchmarkB9(address p, uint[8] memory inputs) public view returns (uint) {
uint g = gasleft();
uint r = Poseidon9(p).poseidon(inputs);
console.log(g-gasleft());
return r;
}
}
1 change: 1 addition & 0 deletions hardhat.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module.exports = {
networks: {
hardhat: {
blockGasLimit: 12000000,
allowUnlimitedContractSize: true,
},
local: {
url: 'http://localhost:8545',
Expand Down
2 changes: 1 addition & 1 deletion src/T.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
module.exports = [2, 3, 4, 5]
module.exports = [2, 3, 4, 5, 6, 7, 8, 9]
20 changes: 11 additions & 9 deletions src/build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ import T from './T.js'
const __dirname = path.dirname(url.fileURLToPath(import.meta.url))

for (const t of T) {
// const c = genTContract(t)
const c = prettier.format(genTContract(t), {
parser: 'solidity-parse',
printWidth: 180,
tabWidth: 2,
useTabs: false,
singleQuote: false,
bracketSpacing: false,
})
let c = genTContract(t)
try {
c = prettier.format(genTContract(t), {
parser: 'solidity-parse',
printWidth: 180,
tabWidth: 2,
useTabs: false,
singleQuote: false,
bracketSpacing: false,
})
} catch (_) {}
await fs.writeFile(path.join(__dirname, `../contracts/PoseidonT${t}.sol`), c)
}
Loading

0 comments on commit dce1d8f

Please sign in to comment.