Skip to content

Commit 1551e53

Browse files
blockifier: sha512 syscall (#14072)
1 parent f667741 commit 1551e53

56 files changed

Lines changed: 661 additions & 86 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

crates/apollo_compile_to_casm/src/allowed_libfuncs.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,9 @@
199199
"sha256_process_block_syscall": null,
200200
"sha256_state_handle_digest": null,
201201
"sha256_state_handle_init": null,
202+
"sha512_process_block_syscall": null,
203+
"sha512_state_handle_digest": null,
204+
"sha512_state_handle_init": null,
202205
"snapshot_take": null,
203206
"span_from_tuple": null,
204207
"squashed_felt252_dict_entries": {

crates/apollo_compile_to_casm/src/compile_test.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ const SIERRA_COMPILATION_CONFIG: SierraCompilationConfig = SierraCompilationConf
3636

3737
// Libfuncs in allowed_libfuncs.json but not yet in Cairo's audited list.
3838
// Remove entries once they're added to the audited list.
39-
const PENDING_LIBFUNCS: &[&str] = &[];
39+
const PENDING_LIBFUNCS: &[&str] =
40+
&["sha512_process_block_syscall", "sha512_state_handle_digest", "sha512_state_handle_init"];
4041

4142
fn compiler() -> SierraToCasmCompiler {
4243
SierraToCasmCompiler::new(SIERRA_COMPILATION_CONFIG)

crates/apollo_starknet_os_program/src/cairo/starkware/starknet/common/new_syscalls.cairo

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from starkware.cairo.common.cairo_secp.ec import EcPoint
22
from starkware.cairo.common.sha256_state import Sha256Input, Sha256State
3+
from starkware.cairo.common.sha512_state import Sha512Input, Sha512State
34
from starkware.cairo.common.uint256 import Uint256
45

56
// Syscall selectors.
@@ -21,6 +22,7 @@ const SECP256R1_MUL_SELECTOR = 'Secp256r1Mul';
2122
const SECP256R1_NEW_SELECTOR = 'Secp256r1New';
2223
const KECCAK_SELECTOR = 'Keccak';
2324
const SHA256_PROCESS_BLOCK_SELECTOR = 'Sha256ProcessBlock';
25+
const SHA512_PROCESS_BLOCK_SELECTOR = 'Sha512ProcessBlock';
2426
const LIBRARY_CALL_SELECTOR = 'LibraryCall';
2527
const REPLACE_CLASS_SELECTOR = 'ReplaceClass';
2628
const SEND_MESSAGE_TO_L1_SELECTOR = 'SendMessageToL1';
@@ -179,6 +181,11 @@ struct Sha256ProcessBlockRequest {
179181
input_start: Sha256Input*,
180182
}
181183

184+
struct Sha512ProcessBlockRequest {
185+
state_ptr: Sha512State*,
186+
input_start: Sha512Input*,
187+
}
188+
182189
struct SecpAddRequest {
183190
p0: EcPoint*,
184191
p1: EcPoint*,
@@ -271,6 +278,10 @@ struct Sha256ProcessBlockResponse {
271278
state_ptr: Sha256State*,
272279
}
273280

281+
struct Sha512ProcessBlockResponse {
282+
state_ptr: Sha512State*,
283+
}
284+
274285
struct SecpGetXyResponse {
275286
// The x and y coordinates of the given point. Returns (0, 0) for the point at infinity.
276287
x: Uint256,

crates/apollo_starknet_os_program/src/cairo/starkware/starknet/core/os/builtins.cairo

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ from starkware.cairo.common.cairo_builtins import (
1111
)
1212
from starkware.cairo.common.registers import get_fp_and_pc
1313
from starkware.cairo.common.sha256_state import Sha256ProcessBlock
14+
from starkware.cairo.common.sha512_state import Sha512ProcessBlock
1415
from starkware.starknet.builtins.segment_arena.segment_arena import SegmentArenaBuiltin
1516

1617
struct SelectableBuiltins {
@@ -29,6 +30,7 @@ struct SelectableBuiltins {
2930
struct NonSelectableBuiltins {
3031
keccak: KeccakBuiltin*,
3132
sha256: Sha256ProcessBlock*,
33+
sha512: Sha512ProcessBlock*,
3234
}
3335

3436
struct BuiltinPointers {

crates/apollo_starknet_os_program/src/cairo/starkware/starknet/core/os/constants.cairo

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ const ALLOWED_VIRTUAL_OS_PROGRAM_HASHES_0 = (
6969
0x03e98c2d7703b03a7edb73ed7f075f97f1dcbaa8f717cdf6e1a57bf058265473
7070
);
7171
const ALLOWED_VIRTUAL_OS_PROGRAM_HASHES_1 = (
72-
0x05d57d57f104106fe9cb69f2544cb7d5b5f17f2adbe227a6c429937e05ad1b96
72+
0x06ecb73d21c7d98ddd4148f5bcd91cc2747c65364245fbf32a63b05cca1685c2
7373
);
7474
const ALLOWED_VIRTUAL_OS_PROGRAM_HASHES_LEN = 2;
7575

@@ -135,6 +135,7 @@ const SECP256R1_NEW_GAS_COST = 61630;
135135
const KECCAK_GAS_COST = 10000;
136136
const KECCAK_ROUND_COST_GAS_COST = 171707;
137137
const SHA256_PROCESS_BLOCK_GAS_COST = 841295;
138+
const SHA512_PROCESS_BLOCK_GAS_COST = 2413810;
138139

139140
// Cairo 1.0 error codes.
140141
const ERROR_BLOCK_NUMBER_OUT_OF_RANGE = 'Block number out of range';

crates/apollo_starknet_os_program/src/cairo/starkware/starknet/core/os/constants_template.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ const SECP256R1_NEW_GAS_COST = {SECP256R1_NEW_GAS_COST};
117117
const KECCAK_GAS_COST = {KECCAK_GAS_COST};
118118
const KECCAK_ROUND_COST_GAS_COST = {KECCAK_ROUND_COST_GAS_COST};
119119
const SHA256_PROCESS_BLOCK_GAS_COST = {SHA256_PROCESS_BLOCK_GAS_COST};
120+
const SHA512_PROCESS_BLOCK_GAS_COST = {SHA512_PROCESS_BLOCK_GAS_COST};
120121

121122
// Cairo 1.0 error codes.
122123
const ERROR_BLOCK_NUMBER_OUT_OF_RANGE = {ERROR_BLOCK_NUMBER_OUT_OF_RANGE};

crates/apollo_starknet_os_program/src/cairo/starkware/starknet/core/os/execution/execute_syscalls.cairo

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ from starkware.starknet.common.new_syscalls import (
2727
SECP256R1_NEW_SELECTOR,
2828
SEND_MESSAGE_TO_L1_SELECTOR,
2929
SHA256_PROCESS_BLOCK_SELECTOR,
30+
SHA512_PROCESS_BLOCK_SELECTOR,
3031
STORAGE_READ_SELECTOR,
3132
STORAGE_WRITE_SELECTOR,
3233
EmitEventRequest,
@@ -61,6 +62,7 @@ from starkware.starknet.core.os.execution.syscall_impls import (
6162
execute_secp_get_xy,
6263
execute_send_message_to_l1,
6364
execute_sha256_process_block,
65+
execute_sha512_process_block,
6466
execute_storage_read,
6567
execute_storage_write,
6668
reduce_syscall_gas_and_write_response_header,
@@ -222,6 +224,16 @@ func execute_syscalls{
222224
);
223225
}
224226

227+
if (selector == SHA512_PROCESS_BLOCK_SELECTOR) {
228+
execute_sha512_process_block();
229+
%{ OsLoggerExitSyscall %}
230+
return execute_syscalls(
231+
block_context=block_context,
232+
execution_context=execution_context,
233+
syscall_ptr_end=syscall_ptr_end,
234+
);
235+
}
236+
225237
if (selector == SECP256K1_GET_POINT_FROM_X_SELECTOR) {
226238
execute_secp256k1_get_point_from_x();
227239
%{ OsLoggerExitSyscall %}

crates/apollo_starknet_os_program/src/cairo/starkware/starknet/core/os/execution/execute_syscalls__virtual.cairo

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ from starkware.starknet.common.new_syscalls import (
2323
SECP256R1_NEW_SELECTOR,
2424
SEND_MESSAGE_TO_L1_SELECTOR,
2525
SHA256_PROCESS_BLOCK_SELECTOR,
26+
SHA512_PROCESS_BLOCK_SELECTOR,
2627
STORAGE_READ_SELECTOR,
2728
STORAGE_WRITE_SELECTOR,
2829
EmitEventRequest,
@@ -53,6 +54,7 @@ from starkware.starknet.core.os.execution.syscall_impls import (
5354
execute_secp_get_xy,
5455
execute_send_message_to_l1,
5556
execute_sha256_process_block,
57+
execute_sha512_process_block,
5658
execute_storage_read,
5759
execute_storage_write,
5860
reduce_syscall_gas_and_write_response_header,
@@ -179,6 +181,16 @@ func execute_syscalls{
179181
);
180182
}
181183

184+
if (selector == SHA512_PROCESS_BLOCK_SELECTOR) {
185+
execute_sha512_process_block();
186+
%{ OsLoggerExitSyscall %}
187+
return execute_syscalls(
188+
block_context=block_context,
189+
execution_context=execution_context,
190+
syscall_ptr_end=syscall_ptr_end,
191+
);
192+
}
193+
182194
if (selector == SECP256K1_GET_POINT_FROM_X_SELECTOR) {
183195
execute_secp256k1_get_point_from_x();
184196
%{ OsLoggerExitSyscall %}

crates/apollo_starknet_os_program/src/cairo/starkware/starknet/core/os/execution/execute_transactions.cairo

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ from starkware.cairo.common.cairo_builtins import (
88
PoseidonBuiltin,
99
)
1010
from starkware.cairo.common.cairo_sha256.sha256_utils import finalize_sha256
11+
from starkware.cairo.common.cairo_sha512.sha512_utils import finalize_sha512
1112
from starkware.cairo.common.dict_access import DictAccess
1213
from starkware.cairo.common.registers import get_fp_and_pc
1314
from starkware.cairo.common.sha256_state import Sha256ProcessBlock
15+
from starkware.cairo.common.sha512_state import Sha512ProcessBlock
1416
from starkware.starknet.builtins.segment_arena.segment_arena import new_arena
1517
from starkware.starknet.core.os.block_context import BlockContext
1618
from starkware.starknet.core.os.builtins import (
@@ -54,6 +56,7 @@ func execute_transactions{
5456
// Prepare builtin pointers.
5557
let segment_arena_ptr = new_arena();
5658
let (sha256_ptr: Sha256ProcessBlock*) = alloc();
59+
let (sha512_ptr: Sha512ProcessBlock*) = alloc();
5760

5861
let (__fp__, _) = get_fp_and_pc();
5962
local local_builtin_ptrs: BuiltinPointers = BuiltinPointers(
@@ -69,11 +72,14 @@ func execute_transactions{
6972
add_mod=add_mod_ptr,
7073
mul_mod=mul_mod_ptr,
7174
),
72-
non_selectable=NonSelectableBuiltins(keccak=keccak_ptr, sha256=sha256_ptr),
75+
non_selectable=NonSelectableBuiltins(
76+
keccak=keccak_ptr, sha256=sha256_ptr, sha512=sha512_ptr
77+
),
7378
);
7479

7580
let builtin_ptrs = &local_builtin_ptrs;
7681
let sha256_ptr_start = builtin_ptrs.non_selectable.sha256;
82+
let sha512_ptr_start = builtin_ptrs.non_selectable.sha512;
7783

7884
// Execute transactions.
7985
local n_txs;
@@ -108,5 +114,10 @@ func execute_transactions{
108114
sha256_ptr_start=sha256_ptr_start, sha256_ptr_end=builtin_ptrs.non_selectable.sha256
109115
);
110116

117+
// Finalize the sha512 segment.
118+
finalize_sha512(
119+
sha512_ptr_start=sha512_ptr_start, sha512_ptr_end=builtin_ptrs.non_selectable.sha512
120+
);
121+
111122
return ();
112123
}

crates/apollo_starknet_os_program/src/cairo/starkware/starknet/core/os/execution/syscall_impls.cairo

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ from starkware.cairo.common.secp256r1.ec import (
2727
)
2828
from starkware.cairo.common.segments import relocate_segment
2929
from starkware.cairo.common.sha256_state import Sha256Input, Sha256ProcessBlock, Sha256State
30+
from starkware.cairo.common.sha512_state import Sha512Input, Sha512ProcessBlock, Sha512State
3031
from starkware.cairo.common.uint256 import Uint256, assert_uint256_lt, uint256_lt
3132
from starkware.starknet.common.constants import ORIGIN_ADDRESS
3233
from starkware.starknet.common.new_syscalls import (
@@ -68,6 +69,8 @@ from starkware.starknet.common.new_syscalls import (
6869
SendMessageToL1Request,
6970
Sha256ProcessBlockRequest,
7071
Sha256ProcessBlockResponse,
72+
Sha512ProcessBlockRequest,
73+
Sha512ProcessBlockResponse,
7174
StorageReadRequest,
7275
StorageReadResponse,
7376
StorageWriteRequest,
@@ -113,6 +116,7 @@ from starkware.starknet.core.os.constants import (
113116
SECP256R1_NEW_GAS_COST,
114117
SEND_MESSAGE_TO_L1_GAS_COST,
115118
SHA256_PROCESS_BLOCK_GAS_COST,
119+
SHA512_PROCESS_BLOCK_GAS_COST,
116120
SIERRA_ARRAY_LEN_BOUND,
117121
STORAGE_READ_GAS_COST,
118122
STORAGE_WRITE_GAS_COST,
@@ -975,7 +979,9 @@ func execute_keccak{
975979
mul_mod=selectable_builtins.mul_mod,
976980
),
977981
non_selectable=NonSelectableBuiltins(
978-
keccak=keccak_ptr, sha256=non_selectable_builtins.sha256
982+
keccak=keccak_ptr,
983+
sha256=non_selectable_builtins.sha256,
984+
sha512=non_selectable_builtins.sha512,
979985
),
980986
);
981987
return ();
@@ -1022,7 +1028,56 @@ func execute_sha256_process_block{
10221028
tempvar builtin_ptrs = new BuiltinPointers(
10231029
selectable=builtin_ptrs.selectable,
10241030
non_selectable=NonSelectableBuiltins(
1025-
keccak=builtin_ptrs.non_selectable.keccak, sha256=sha256_ptr
1031+
keccak=builtin_ptrs.non_selectable.keccak,
1032+
sha256=sha256_ptr,
1033+
sha512=builtin_ptrs.non_selectable.sha512,
1034+
),
1035+
);
1036+
return ();
1037+
}
1038+
1039+
// Executes the sha512_process_block system call.
1040+
func execute_sha512_process_block{
1041+
range_check_ptr, builtin_ptrs: BuiltinPointers*, syscall_ptr: felt*, outputs: OsCarriedOutputs*
1042+
}() {
1043+
alloc_locals;
1044+
1045+
let request = cast(syscall_ptr + RequestHeader.SIZE, Sha512ProcessBlockRequest*);
1046+
1047+
// Reduce gas.
1048+
let success = reduce_syscall_gas_and_write_response_header(
1049+
total_gas_cost=SHA512_PROCESS_BLOCK_GAS_COST,
1050+
request_struct_size=Sha512ProcessBlockRequest.SIZE,
1051+
);
1052+
if (success == 0) {
1053+
// Not enough gas to execute the syscall.
1054+
return ();
1055+
}
1056+
1057+
local sha512_ptr: Sha512ProcessBlock* = builtin_ptrs.non_selectable.sha512;
1058+
let response = cast(syscall_ptr, Sha512ProcessBlockResponse*);
1059+
let actual_out_state: Sha512State* = &sha512_ptr.out_state;
1060+
1061+
let input: Sha512Input* = &sha512_ptr.input;
1062+
assert [input] = [request.input_start];
1063+
1064+
let state: Sha512State* = &sha512_ptr.in_state;
1065+
assert [state] = [request.state_ptr];
1066+
1067+
// Relocate response.state_ptr (a temporary segment) to actual_out_state in the sha512
1068+
// segment, and copy the state so finalize_sha512 can read it.
1069+
%{ RelocateSha512Segment %}
1070+
1071+
assert [response] = Sha512ProcessBlockResponse(state_ptr=actual_out_state);
1072+
let syscall_ptr = syscall_ptr + Sha512ProcessBlockResponse.SIZE;
1073+
let sha512_ptr = &sha512_ptr[1];
1074+
1075+
tempvar builtin_ptrs = new BuiltinPointers(
1076+
selectable=builtin_ptrs.selectable,
1077+
non_selectable=NonSelectableBuiltins(
1078+
keccak=builtin_ptrs.non_selectable.keccak,
1079+
sha256=builtin_ptrs.non_selectable.sha256,
1080+
sha512=sha512_ptr,
10261081
),
10271082
);
10281083
return ();

0 commit comments

Comments
 (0)