Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sha256 support prototype #105

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 108 additions & 1 deletion src/contract_bootloader/execute_syscalls.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ from starkware.cairo.common.registers import get_label_location
from src.utils.chain_info import chain_id_to_layout
from src.memorizers.evm.state_access import EvmStateAccess, EvmStateAccessType, EvmDecoderTarget
from src.utils.chain_info import Layout
from src.contract_bootloader.sha256_utils import finalize_sha256
from starkware.cairo.common.alloc import alloc
from starkware.cairo.common.memcpy import memcpy

struct ExecutionInfo {
selector: felt,
Expand All @@ -28,6 +31,56 @@ struct ExecutionContext {
execution_info: ExecutionInfo*,
}

const SHA256_PROCESS_BLOCK_SELECTOR = 'Sha256ProcessBlock';

// Represents 256 bits of a SHA256 state (8 felts each containing 32 bits).
struct Sha256State {
s0: felt,
s1: felt,
s2: felt,
s3: felt,
s4: felt,
s5: felt,
s6: felt,
s7: felt,
}

// Represents 512 bits of a SHA256 input (16 felts each containing 32 bits).
struct Sha256Input {
s0: felt,
s1: felt,
s2: felt,
s3: felt,
s4: felt,
s5: felt,
s6: felt,
s7: felt,
s8: felt,
s9: felt,
s10: felt,
s11: felt,
s12: felt,
s13: felt,
s14: felt,
s15: felt,
}

struct Sha256ProcessBlock {
input: Sha256Input,
in_state: Sha256State,
out_state: Sha256State,
}

struct Sha256ProcessBlockRequest {
state_ptr: Sha256State*,
input_start: Sha256Input*,
}

struct Sha256ProcessBlockResponse {
state_ptr: Sha256State*,
}


// Executes the system calls in syscall_ptr.
// The signature of the function 'call_execute_syscalls' must match this function's signature.
//
Expand All @@ -53,7 +106,17 @@ func execute_syscalls{
return ();
}

assert [syscall_ptr] = CALL_CONTRACT_SELECTOR;
tempvar selector = [syscall_ptr];

if (selector == SHA256_PROCESS_BLOCK_SELECTOR) {
execute_sha256_process_block();
return execute_syscalls(
execution_context=execution_context,
syscall_ptr_end=syscall_ptr_end
);
}

assert selector = CALL_CONTRACT_SELECTOR;
execute_call_contract(caller_execution_context=execution_context);

return execute_syscalls(execution_context=execution_context, syscall_ptr_end=syscall_ptr_end);
Expand Down Expand Up @@ -126,6 +189,50 @@ func execute_call_contract{
return ();
}

func execute_sha256_process_block{
range_check_ptr,
bitwise_ptr: BitwiseBuiltin*,
poseidon_ptr: PoseidonBuiltin*,
keccak_ptr: KeccakBuiltin*,
syscall_ptr: felt*,
builtin_ptrs: BuiltinPointers*,
pow2_array: felt*,
evm_memorizer: DictAccess*,
evm_decoder_ptr: felt***,
evm_key_hasher_ptr: felt**,
starknet_memorizer: DictAccess*,
starknet_decoder_ptr: felt***,
starknet_key_hasher_ptr: felt**,
}() {
alloc_locals;

let request_header = cast(syscall_ptr, RequestHeader*);
let syscall_ptr = syscall_ptr + RequestHeader.SIZE;

let request = cast(syscall_ptr, Sha256ProcessBlockRequest*);
let syscall_ptr = syscall_ptr + Sha256ProcessBlockRequest.SIZE;

let response_header = cast(syscall_ptr, ResponseHeader*);
let syscall_ptr = syscall_ptr + ResponseHeader.SIZE;

let response = cast(syscall_ptr, Sha256ProcessBlockResponse*);
let syscall_ptr = syscall_ptr + Sha256ProcessBlockResponse.SIZE;

let (block_ptr_start: felt*) = alloc();

memcpy(block_ptr_start, request.input_start, Sha256Input.SIZE);
memcpy(block_ptr_start + Sha256Input.SIZE, request.state_ptr, Sha256State.SIZE);
memcpy(block_ptr_start + Sha256Input.SIZE + Sha256State.SIZE, response.state_ptr, Sha256State.SIZE);

let block_ptr_end = block_ptr_start + Sha256Input.SIZE + Sha256State.SIZE + Sha256State.SIZE;

finalize_sha256(
sha256_ptr_start=block_ptr_start, sha256_ptr_end=block_ptr_end
);

return ();
}

// Returns a failure response with a single felt.
@known_ap_change
func write_failure_response{syscall_ptr: felt*}(remaining_gas: felt, failure_felt: felt) {
Expand Down
Loading