Skip to content

Commit 2703304

Browse files
starknet_os: os resources test - add secp syscalls
1 parent b7607b6 commit 2703304

6 files changed

Lines changed: 108 additions & 41 deletions

File tree

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ const ALLOWED_VIRTUAL_OS_PROGRAM_HASHES_1 = (
7272
0x06ecb73d21c7d98ddd4148f5bcd91cc2747c65364245fbf32a63b05cca1685c2
7373
);
7474
const ALLOWED_VIRTUAL_OS_PROGRAM_HASHES_2 = (
75-
0x0471dd673f38125ba94e98145e3579d700ad13bd0e177687b67bab6221da4fa0
75+
0x00a1b5d02d393119f2ec91837e6c7dcc0e05ea5bc18c4befbb24cd39b45ee5f3
7676
);
7777
const ALLOWED_VIRTUAL_OS_PROGRAM_HASHES_LEN = 3;
7878

@@ -124,16 +124,16 @@ const META_TX_V0_GAS_COST = 168550;
124124
const META_TX_V0_CALLDATA_FACTOR_GAS_COST = 4850;
125125

126126
// Note the the following costs include `SYSCALL_BASE_GAS_COST` implicitly.
127-
const SECP256K1_ADD_GAS_COST = 43230;
128-
const SECP256K1_GET_POINT_FROM_X_GAS_COST = 41800;
129-
const SECP256K1_GET_XY_GAS_COST = 21670;
130-
const SECP256K1_MUL_GAS_COST = 8143850;
131-
const SECP256K1_NEW_GAS_COST = 48750;
132-
const SECP256R1_ADD_GAS_COST = 63490;
133-
const SECP256R1_GET_POINT_FROM_X_GAS_COST = 54680;
134-
const SECP256R1_GET_XY_GAS_COST = 21870;
135-
const SECP256R1_MUL_GAS_COST = 13511870;
136-
const SECP256R1_NEW_GAS_COST = 61630;
127+
const SECP256K1_ADD_GAS_COST = 42030;
128+
const SECP256K1_GET_POINT_FROM_X_GAS_COST = 40500;
129+
const SECP256K1_GET_XY_GAS_COST = 20470;
130+
const SECP256K1_MUL_GAS_COST = 8142650;
131+
const SECP256K1_NEW_GAS_COST = 46950;
132+
const SECP256R1_ADD_GAS_COST = 62290;
133+
const SECP256R1_GET_POINT_FROM_X_GAS_COST = 53380;
134+
const SECP256R1_GET_XY_GAS_COST = 20670;
135+
const SECP256R1_MUL_GAS_COST = 13510670;
136+
const SECP256R1_NEW_GAS_COST = 59830;
137137

138138
const KECCAK_GAS_COST = 10000;
139139
const KECCAK_ROUND_COST_GAS_COST = 175207;
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"os": "0x1ab0b60c1977187e094075091bce9d7ba684e1e7e6b416a6d1e1645c18a100b",
3-
"virtual_os": "0x471dd673f38125ba94e98145e3579d700ad13bd0e177687b67bab6221da4fa0",
2+
"os": "0x7e8338fb7780e660566659eb112582458266622bd3209b6bb95d2d5f017c2b0",
3+
"virtual_os": "0xa1b5d02d393119f2ec91837e6c7dcc0e05ea5bc18c4befbb24cd39b45ee5f3",
44
"aggregator": "0x700786d51b3854af43d8e12180380bda3029be6c1767e007858de6ca2edac40",
55
"aggregator_with_prefix": "0xe08d300e3f5996e43d6d7cc5a20068e0e58cf1309089f2348317ac580f6c1f"
66
}

crates/blockifier/resources/blockifier_versioned_constants_0_14_4.json

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@
129129
"allowed_virtual_os_program_hashes": [
130130
"0x3e98c2d7703b03a7edb73ed7f075f97f1dcbaa8f717cdf6e1a57bf058265473",
131131
"0x6ecb73d21c7d98ddd4148f5bcd91cc2747c65364245fbf32a63b05cca1685c2",
132-
"0x471dd673f38125ba94e98145e3579d700ad13bd0e177687b67bab6221da4fa0"
132+
"0xa1b5d02d393119f2ec91837e6c7dcc0e05ea5bc18c4befbb24cd39b45ee5f3"
133133
],
134134
"constructor_entry_point_selector": "0x28ffe4ff0f226a9107253e17a904099aa4f63a02a5621de0576e5aa71bc5194",
135135
"default_entry_point_selector": "0x0",
@@ -385,70 +385,70 @@
385385
}
386386
},
387387
"Secp256k1Add": {
388-
"n_steps": 412,
388+
"n_steps": 400,
389389
"n_memory_holes": 0,
390390
"builtin_instance_counter": {
391391
"range_check_builtin": 29
392392
}
393393
},
394394
"Secp256k1GetPointFromX": {
395-
"n_steps": 397,
395+
"n_steps": 384,
396396
"n_memory_holes": 0,
397397
"builtin_instance_counter": {
398398
"range_check_builtin": 30
399399
}
400400
},
401401
"Secp256k1GetXy": {
402-
"n_steps": 209,
402+
"n_steps": 197,
403403
"n_memory_holes": 0,
404404
"builtin_instance_counter": {
405405
"range_check_builtin": 11
406406
}
407407
},
408408
"Secp256k1Mul": {
409-
"n_steps": 76507,
409+
"n_steps": 76495,
410410
"n_memory_holes": 0,
411411
"builtin_instance_counter": {
412412
"range_check_builtin": 7045
413413
}
414414
},
415415
"Secp256k1New": {
416-
"n_steps": 463,
416+
"n_steps": 445,
417417
"n_memory_holes": 0,
418418
"builtin_instance_counter": {
419419
"range_check_builtin": 35
420420
}
421421
},
422422
"Secp256r1Add": {
423-
"n_steps": 595,
423+
"n_steps": 583,
424424
"n_memory_holes": 0,
425425
"builtin_instance_counter": {
426426
"range_check_builtin": 57
427427
}
428428
},
429429
"Secp256r1GetPointFromX": {
430-
"n_steps": 516,
430+
"n_steps": 503,
431431
"n_memory_holes": 0,
432432
"builtin_instance_counter": {
433433
"range_check_builtin": 44
434434
}
435435
},
436436
"Secp256r1GetXy": {
437-
"n_steps": 211,
437+
"n_steps": 199,
438438
"n_memory_holes": 0,
439439
"builtin_instance_counter": {
440440
"range_check_builtin": 11
441441
}
442442
},
443443
"Secp256r1Mul": {
444-
"n_steps": 125346,
444+
"n_steps": 125334,
445445
"n_memory_holes": 0,
446446
"builtin_instance_counter": {
447447
"range_check_builtin": 13961
448448
}
449449
},
450450
"Secp256r1New": {
451-
"n_steps": 582,
451+
"n_steps": 564,
452452
"n_memory_holes": 0,
453453
"builtin_instance_counter": {
454454
"range_check_builtin": 49

crates/blockifier/resources/versioned_constants_diff_regression/0.14.3_0.14.4.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
+ /os_constants/allowed_virtual_os_program_hashes/2: "0x471dd673f38125ba94e98145e3579d700ad13bd0e177687b67bab6221da4fa0"
1+
+ /os_constants/allowed_virtual_os_program_hashes/2: "0xa1b5d02d393119f2ec91837e6c7dcc0e05ea5bc18c4befbb24cd39b45ee5f3"
22
~ /os_resources/execute_syscalls/CallContract/n_steps: 901
33
~ /os_resources/execute_syscalls/EmitEvent/n_steps: 47
44
~ /os_resources/execute_syscalls/GetBlockHash/builtin_instance_counter/range_check_builtin: 3

crates/blockifier_test_utils/resources/feature_contracts/cairo1/os_resources_test_contract.cairo

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,21 @@ mod OsResourcesTestContract {
44
use box::BoxTrait;
55
use core::sha256::{SHA256_INITIAL_STATE, sha256_state_handle_init};
66
use starknet::info::SyscallResultTrait;
7+
use starknet::secp256_trait::Secp256Trait;
8+
use starknet::secp256k1::{
9+
secp256k1_add_syscall,
10+
secp256k1_get_point_from_x_syscall,
11+
secp256k1_get_xy_syscall,
12+
secp256k1_mul_syscall,
13+
secp256k1_new_syscall,
14+
};
15+
use starknet::secp256r1::{
16+
secp256r1_add_syscall,
17+
secp256r1_get_point_from_x_syscall,
18+
secp256r1_get_xy_syscall,
19+
secp256r1_mul_syscall,
20+
secp256r1_new_syscall,
21+
};
722
use starknet::syscalls::{
823
call_contract_syscall, deploy_syscall, emit_event_syscall, get_execution_info_v3_syscall,
924
keccak_syscall, library_call_syscall, replace_class_syscall, send_message_to_l1_syscall,
@@ -14,6 +29,18 @@ mod OsResourcesTestContract {
1429
const STABLE_EXTERNAL_ENTRY_POINT_SELECTOR: felt252 = selector!("external");
1530
const EXECUTE_FUNCTION_SELECTOR: felt252 = selector!("__execute__");
1631

32+
// SECP constants.
33+
const MULT_CONSTANT: u256
34+
= 115792089237316195423570985008687907853269984665640564039457584007913129639935;
35+
const X_FOR_K: u256
36+
= 111793196543967404139194827996419963236210979610743141064269745943111491389389;
37+
const Y_FOR_K: u256
38+
= 64271137072396112709852516195602121116634737731930508083758518861847052748305;
39+
const X_FOR_R: u256
40+
= 36259703446750261746963965979921905598426482711143882545997285073084044643087;
41+
const Y_FOR_R: u256
42+
= 99074502569356486940077471307887399820854676440660107539358273498981469249968;
43+
1744
#[storage]
1845
struct Storage {}
1946

@@ -122,5 +149,43 @@ mod OsResourcesTestContract {
122149
// TODO(Yoni, 1/6/2022): In this case the number of steps depends on the payload size -
123150
// consider counting it.
124151
send_message_to_l1_syscall(100, array![].span()).unwrap_syscall();
152+
153+
// secp256k1 syscalls:
154+
155+
// secp256k1_new syscall.
156+
let p0 = secp256k1_new_syscall(X_FOR_K, Y_FOR_K).unwrap_syscall().unwrap();
157+
158+
//secp256k1_add syscall.
159+
let k_p1 = Secp256Trait::get_generator_point();
160+
secp256k1_add_syscall(p0, k_p1).unwrap_syscall();
161+
162+
// secp256k1_get_point_from_x syscall.
163+
let x:u256 = X_FOR_K;
164+
let _ = secp256k1_get_point_from_x_syscall(:x, y_parity: true).unwrap_syscall();
165+
166+
// secp256k1_get_xy syscall.
167+
secp256k1_get_xy_syscall(k_p1).unwrap_syscall();
168+
169+
// secp256k1_mul syscall.
170+
secp256k1_mul_syscall(k_p1, MULT_CONSTANT).unwrap_syscall();
171+
172+
// secp256r1 syscalls:
173+
174+
// secp256r1_new syscall.
175+
let p0 = secp256r1_new_syscall(X_FOR_R, Y_FOR_R).unwrap_syscall().unwrap();
176+
let r_p1 = Secp256Trait::get_generator_point();
177+
178+
// secp256r1_add syscall.
179+
secp256r1_add_syscall(p0, r_p1).unwrap_syscall();
180+
181+
// secp256r1_get_point_from_x syscall.
182+
let x = X_FOR_R;
183+
let _ = secp256r1_get_point_from_x_syscall(:x, y_parity: true).unwrap_syscall();
184+
185+
// secp256r1_get_xy syscall.
186+
secp256r1_get_xy_syscall(r_p1).unwrap_syscall();
187+
188+
// secp256r1_mul syscall.
189+
secp256r1_mul_syscall(r_p1, MULT_CONSTANT).unwrap_syscall();
125190
}
126191
}

crates/starknet_os_flow_tests/src/os_resources_test.rs

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ use crate::test_manager::{
4949
use crate::tests::NON_TRIVIAL_RESOURCE_BOUNDS;
5050

5151
// TODO(Dori): Delete this, or at least reduce it to a minimal set of unmeasurable syscalls.
52-
const UNMEASURABLE_SYSCALLS: [Selector; 23] = [
52+
const UNMEASURABLE_SYSCALLS: [Selector; 13] = [
5353
Selector::DelegateCall,
5454
Selector::DelegateL1Handler,
5555
Selector::GetBlockNumber,
@@ -61,16 +61,6 @@ const UNMEASURABLE_SYSCALLS: [Selector; 23] = [
6161
Selector::GetTxSignature,
6262
Selector::Sha512ProcessBlock,
6363
Selector::LibraryCallL1Handler,
64-
Selector::Secp256k1Add,
65-
Selector::Secp256k1GetPointFromX,
66-
Selector::Secp256k1GetXy,
67-
Selector::Secp256k1Mul,
68-
Selector::Secp256k1New,
69-
Selector::Secp256r1Add,
70-
Selector::Secp256r1GetPointFromX,
71-
Selector::Secp256r1GetXy,
72-
Selector::Secp256r1Mul,
73-
Selector::Secp256r1New,
7464
Selector::StorageRead,
7565
Selector::StorageWrite,
7666
];
@@ -114,6 +104,9 @@ const FEE_TRANSFER_SYSCALLS: [Selector; 10] = [
114104
Selector::EmitEvent,
115105
];
116106

107+
/// All other syscalls are called only once.
108+
const SYSCALLS_CALLED_TWICE: [Selector; 2] = [Selector::Secp256k1New, Selector::Secp256r1New];
109+
117110
/// See [SYSCALLS_WITH_VIRTUAL_BUILTINS] for why this function is needed.
118111
fn update_resources_for_virtual_builtin_syscall(
119112
selector: Selector,
@@ -344,6 +337,7 @@ async fn test_os_resources_regression() {
344337
// Measure each syscall overhead. If the syscall incurs an inner call, subtract the inner call
345338
// overhead.
346339
let mut syscalls_iter = syscall_traces.iter();
340+
let mut second_visit_syscalls = HashSet::new();
347341
let mut measurements: IndexMap<Selector, VariableResourceParams> = IndexMap::new();
348342
// If the syscall incurs an inner call, subtract the inner call overhead.
349343
let mut maybe_deduct_inner =
@@ -366,11 +360,19 @@ async fn test_os_resources_regression() {
366360
while let Some(syscall_trace) = syscalls_iter.next() {
367361
let selector = syscall_trace.get_selector();
368362

369-
// Ensure we don't visit the same syscall more than once.
370-
assert!(
371-
measurements.get(&selector).is_none(),
372-
"Syscall {selector:?} was visited again, unexpectedly."
373-
);
363+
// Ensure we don't visit the same syscall more than the allowed number of times.
364+
if measurements.get(&selector).is_some() {
365+
assert!(
366+
SYSCALLS_CALLED_TWICE.contains(&selector),
367+
"Syscall {selector:?} was visited again, unexpectedly."
368+
);
369+
assert!(
370+
!second_visit_syscalls.contains(&selector),
371+
"Syscall {selector:?} was visited a third time, unexpectedly."
372+
);
373+
second_visit_syscalls.insert(selector);
374+
continue;
375+
}
374376

375377
// If this syscall incurs an inner call, it should be the next inner call in the
376378
// iterator.

0 commit comments

Comments
 (0)