From 77eba52e20c7f95fc77c3ba05088ce9c7539974d Mon Sep 17 00:00:00 2001 From: Bartosz Nowak Date: Fri, 21 Jun 2024 08:43:39 +0200 Subject: [PATCH] program hash of contract --- .../contract_bootloader/contract_bootloader.cairo | 15 +++++++++++++++ src/tasks/aggregate_functions/slr.cairo | 11 +++++++---- src/tasks/computational.cairo | 2 +- tests/cairo_programs/aggregate_functions.cairo | 6 ++++-- tools/make/cairo_format_check.sh | 2 +- tools/make/format_cairo_files.sh | 2 +- 6 files changed, 29 insertions(+), 9 deletions(-) diff --git a/packages/contract_bootloader/contract_bootloader.cairo b/packages/contract_bootloader/contract_bootloader.cairo index 83e57128..1d013b00 100644 --- a/packages/contract_bootloader/contract_bootloader.cairo +++ b/packages/contract_bootloader/contract_bootloader.cairo @@ -17,6 +17,7 @@ from starkware.starknet.core.os.builtins import ( BuiltinInstanceSizes, SelectableBuiltins, ) +from starkware.cairo.common.builtin_poseidon.poseidon import poseidon_hash_many from contract_bootloader.execute_entry_point import execute_entry_point from starkware.starknet.core.os.constants import ENTRY_POINT_TYPE_EXTERNAL from contract_bootloader.execute_syscalls import ExecutionContext, ExecutionInfo @@ -105,3 +106,17 @@ func run_contract_bootloader{ return (retdata_size, retdata); } + +// Computes the hash of a program. +// Arguments: +// * program_data_ptr - the pointer to the program to be hashed. +// Return values: +// * hash - the computed program hash. +func compute_program_hash{poseidon_ptr: PoseidonBuiltin*}( + bytecode_length: felt, bytecode_ptr: felt* +) -> (hash: felt) { + let (hash) = poseidon_hash_many{poseidon_ptr=poseidon_ptr}( + n=bytecode_length, elements=bytecode_ptr + ); + return (hash=hash); +} diff --git a/src/tasks/aggregate_functions/slr.cairo b/src/tasks/aggregate_functions/slr.cairo index 158a5655..16a22f49 100644 --- a/src/tasks/aggregate_functions/slr.cairo +++ b/src/tasks/aggregate_functions/slr.cairo @@ -27,7 +27,7 @@ from src.decoders.header_decoder import HeaderDecoder from src.decoders.transaction_decoder import TransactionDecoder, TransactionType from src.decoders.receipt_decoder import ReceiptDecoder from contract_bootloader.contract_class.compiled_class import CompiledClass -from contract_bootloader.contract_bootloader import run_contract_bootloader +from contract_bootloader.contract_bootloader import run_contract_bootloader, compute_program_hash from src.memorizer import ( AccountMemorizer, StorageMemorizer, @@ -87,7 +87,7 @@ func compute_slr{ ec_op_ptr, keccak_ptr: KeccakBuiltin*, poseidon_ptr: PoseidonBuiltin*, -}(values: Uint256*, values_len: felt, predict: Uint256) -> Uint256 { +}(values: Uint256*, values_len: felt, predict: Uint256) -> (program_hash: felt, result: Uint256) { alloc_locals; let (local task_input_arr: felt*) = alloc(); @@ -129,6 +129,9 @@ func compute_slr{ %} assert compiled_class.bytecode_ptr[compiled_class.bytecode_length] = 0x208b7fff7fff7ffe; + let (program_hash) = compute_program_hash( + bytecode_length=compiled_class.bytecode_length, bytecode_ptr=compiled_class.bytecode_ptr + ); %{ vm_load_program( @@ -144,8 +147,8 @@ func compute_slr{ ); assert retdata_size = 2; - let res: Uint256 = Uint256(low=retdata[0], high=retdata[1]); - return res; + let result: Uint256 = Uint256(low=retdata[0], high=retdata[1]); + return (program_hash=program_hash, result=result); } // Collects the account data points defined in the datalake from the memorizer recursivly diff --git a/src/tasks/computational.cairo b/src/tasks/computational.cairo index 5ef99b2b..89b35aa5 100644 --- a/src/tasks/computational.cairo +++ b/src/tasks/computational.cairo @@ -210,7 +210,7 @@ namespace Task { with fetch_trait { let (data_points, data_points_len) = Datalake.fetch_data_points(tasks[index]); } - let result = compute_slr( + let (program_hash, result) = compute_slr( values=data_points, values_len=data_points_len, predict=tasks[index].ctx_value ); assert [results] = result; diff --git a/tests/cairo_programs/aggregate_functions.cairo b/tests/cairo_programs/aggregate_functions.cairo index 4d235cf6..132e38f8 100644 --- a/tests/cairo_programs/aggregate_functions.cairo +++ b/tests/cairo_programs/aggregate_functions.cairo @@ -241,8 +241,10 @@ func slr_main{ %{ segments.write_arg(ids.array, [1, 0, 3, 0, 2, 0, 5, 0]) %} let values: Uint256* = cast(array, Uint256*); - let output = compute_slr(values=values, values_len=2, predict=Uint256(low=10, high=0)); + let (program_hash, result) = compute_slr( + values=values, values_len=2, predict=Uint256(low=10, high=0) + ); - assert output.low = 21; + assert result.low = 21; return (); } diff --git a/tools/make/cairo_format_check.sh b/tools/make/cairo_format_check.sh index 8c4d3dba..b2a056bf 100755 --- a/tools/make/cairo_format_check.sh +++ b/tools/make/cairo_format_check.sh @@ -37,7 +37,7 @@ exit_status_cairo_files=$? # Find Scarb projects and format them in parallel echo "Finding and formatting Scarb projects..." -find ./src/cairo1 ./src/contracts -mindepth 1 -maxdepth 1 -type d | parallel --halt now,fail=1 format_scarb_project {} +find ./src/contracts -mindepth 1 -maxdepth 1 -type d | parallel --halt now,fail=1 format_scarb_project {} # Capture the exit status of parallel for Scarb projects exit_status_scarb_projects=$? diff --git a/tools/make/format_cairo_files.sh b/tools/make/format_cairo_files.sh index 1eb3abc2..2cb4f3b2 100755 --- a/tools/make/format_cairo_files.sh +++ b/tools/make/format_cairo_files.sh @@ -41,7 +41,7 @@ exit_status_cairo_files=$? # Find Scarb projects and execute format_scarb_project in each echo "Formatting Scarb projects..." -find ./src/cairo1 ./src/contracts -mindepth 1 -maxdepth 1 -type d | parallel --halt now,fail=1 format_scarb_project {} +find ./src/contracts -mindepth 1 -maxdepth 1 -type d | parallel --halt now,fail=1 format_scarb_project {} # Capture the exit status of parallel for Scarb projects exit_status_scarb_projects=$?