Skip to content

Commit

Permalink
program hash of contract
Browse files Browse the repository at this point in the history
  • Loading branch information
Okm165 committed Jun 21, 2024
1 parent 78d9b21 commit 77eba52
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 9 deletions.
15 changes: 15 additions & 0 deletions packages/contract_bootloader/contract_bootloader.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
11 changes: 7 additions & 4 deletions src/tasks/aggregate_functions/slr.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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(
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/tasks/computational.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 4 additions & 2 deletions tests/cairo_programs/aggregate_functions.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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 ();
}
2 changes: 1 addition & 1 deletion tools/make/cairo_format_check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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=$?
Expand Down
2 changes: 1 addition & 1 deletion tools/make/format_cairo_files.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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=$?
Expand Down

0 comments on commit 77eba52

Please sign in to comment.