Skip to content

Commit

Permalink
ZK-599: Add arity warning to Solidity Poseidon (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
guspiel authored Dec 6, 2024
1 parent 244c5f4 commit e5284ef
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
8 changes: 7 additions & 1 deletion poseidon2-solidity/generate_t8.py
Original file line number Diff line number Diff line change
Expand Up @@ -587,5 +587,11 @@ def partial_round(r):
}}
'''

FUNCTION_COMMENT = """
/*
* Suitable only for 7-tuples. Using `hash` for tuples of other sizes requires adjusting
* the initial state of the hashing function, which is not done in the current implementation.
*/"""

if __name__ == '__main__':
print(generate_code(init, full_round, partial_round, T, ROUNDS_F, ROUNDS_P))
print(generate_code(init, full_round, partial_round, T, ROUNDS_F, ROUNDS_P, FUNCTION_COMMENT))
7 changes: 4 additions & 3 deletions poseidon2-solidity/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@
ARG = ['0x080', '0x0a0', '0x0c0', '0x0e0', '0x100', '0x120', '0x140']


def wrap_into_full_code(assembly_code, T):
def wrap_into_full_code(assembly_code, T, function_comment):
"""Wrap the assembly code into a full Solidity contract."""

return f"""
pragma solidity 0.8.26;
library Poseidon2T{T}Assembly {{
{function_comment}
function hash(uint256[{T - 1}] memory) public pure returns (uint256) {{
assembly {{
Expand Down Expand Up @@ -121,7 +122,7 @@ def store6(val, swap=False): return f'mstore({MEM_SWP[6] if swap else MEM[6]}, {
def store7(val, swap=False): return f'mstore({MEM_SWP[7] if swap else MEM[7]}, {val})'


def generate_code(init, full_round, partial_round, t, full_rounds, partial_rounds):
def generate_code(init, full_round, partial_round, t, full_rounds, partial_rounds, function_comment):
"""Generate the full assembly code for the Poseidon hash function with given parameters and function generators."""

code = init()
Expand All @@ -140,4 +141,4 @@ def generate_code(init, full_round, partial_round, t, full_rounds, partial_round
# We assume that the result is stored in the first memory slot.
code += f'return({MEM[0]}, 0x20)'

return wrap_into_full_code(code.split('\n'), t)
return wrap_into_full_code(code.split('\n'), t, function_comment)

0 comments on commit e5284ef

Please sign in to comment.