From e5284ef8bd7079a0e21812f6d9b844ae82aaeb6e Mon Sep 17 00:00:00 2001 From: guspiel Date: Fri, 6 Dec 2024 17:53:29 +0100 Subject: [PATCH] ZK-599: Add arity warning to Solidity Poseidon (#33) --- poseidon2-solidity/generate_t8.py | 8 +++++++- poseidon2-solidity/utils.py | 7 ++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/poseidon2-solidity/generate_t8.py b/poseidon2-solidity/generate_t8.py index 2a93ddeb..93651474 100644 --- a/poseidon2-solidity/generate_t8.py +++ b/poseidon2-solidity/generate_t8.py @@ -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)) diff --git a/poseidon2-solidity/utils.py b/poseidon2-solidity/utils.py index 4621319b..e64a27a0 100644 --- a/poseidon2-solidity/utils.py +++ b/poseidon2-solidity/utils.py @@ -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 {{ @@ -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() @@ -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)