Skip to content

Commit 40ef87d

Browse files
avi-starkwareclaude
andcommitted
align sierra-emu StarknetSyscallHandler trait/types with cairo-native
Sierra-emu adopts cairo-native's syscall surface so the two traits/types match name-for-name. This is a prerequisite for extracting them into a shared crate. - Slice signatures: deploy/library_call/call_contract/emit_event/ send_message_to_l1/keccak/meta_tx_v0/cheatcode now take &[Felt] / &[u64] rather than owned Vec. - Secp256{k1,r1}Point gain an explicit is_infinity flag. Sierra-emu's internal Sierra value still encodes the identity element as (0, 0); from_value recovers the flag, into_value drops it. - sha256_process_block matches cairo-native's mutating signature (&mut [u32; 8], &[u32; 16]) -> SyscallResult<()>. - Add ExecutionInfoV3 / TxV3Info types and a get_execution_info_v3 trait method (defaulted to unimplemented!()). The VM eval site stays todo!() for now; wiring the Sierra-level value lowering is out of scope for this PR. - cairo-native: cheatcode trait method becomes unconditional (was gated behind with-cheatcode). The default impl is unimplemented!(), so this is backwards-compatible for existing impls; only the runtime/codegen side stays feature-gated. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 13ba358 commit 40ef87d

8 files changed

Lines changed: 126 additions & 55 deletions

File tree

debug_utils/sierra-emu/src/starknet.rs

Lines changed: 50 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ use std::{
55

66
pub use self::{
77
block_info::BlockInfo, execution_info::ExecutionInfo, execution_info_v2::ExecutionInfoV2,
8-
resource_bounds::ResourceBounds, secp256k1_point::Secp256k1Point,
9-
secp256r1_point::Secp256r1Point, tx_info::TxInfo, tx_v2_info::TxV2Info, u256::U256,
8+
execution_info_v3::ExecutionInfoV3, resource_bounds::ResourceBounds,
9+
secp256k1_point::Secp256k1Point, secp256r1_point::Secp256r1Point, tx_info::TxInfo,
10+
tx_v2_info::TxV2Info, tx_v3_info::TxV3Info, u256::U256,
1011
};
1112
use k256::elliptic_curve::{
1213
generic_array::GenericArray,
@@ -19,11 +20,13 @@ use starknet_types_core::felt::Felt;
1920
mod block_info;
2021
mod execution_info;
2122
mod execution_info_v2;
23+
mod execution_info_v3;
2224
mod resource_bounds;
2325
mod secp256k1_point;
2426
mod secp256r1_point;
2527
mod tx_info;
2628
mod tx_v2_info;
29+
mod tx_v3_info;
2730
mod u256;
2831

2932
pub type SyscallResult<T> = Result<T, Vec<Felt>>;
@@ -36,11 +39,15 @@ pub trait StarknetSyscallHandler {
3639

3740
fn get_execution_info_v2(&mut self, remaining_gas: &mut u64) -> SyscallResult<ExecutionInfoV2>;
3841

42+
fn get_execution_info_v3(&mut self, _remaining_gas: &mut u64) -> SyscallResult<ExecutionInfoV3> {
43+
unimplemented!()
44+
}
45+
3946
fn deploy(
4047
&mut self,
4148
class_hash: Felt,
4249
contract_address_salt: Felt,
43-
calldata: Vec<Felt>,
50+
calldata: &[Felt],
4451
deploy_from_zero: bool,
4552
remaining_gas: &mut u64,
4653
) -> SyscallResult<(Felt, Vec<Felt>)>;
@@ -51,15 +58,15 @@ pub trait StarknetSyscallHandler {
5158
&mut self,
5259
class_hash: Felt,
5360
function_selector: Felt,
54-
calldata: Vec<Felt>,
61+
calldata: &[Felt],
5562
remaining_gas: &mut u64,
5663
) -> SyscallResult<Vec<Felt>>;
5764

5865
fn call_contract(
5966
&mut self,
6067
address: Felt,
6168
entry_point_selector: Felt,
62-
calldata: Vec<Felt>,
69+
calldata: &[Felt],
6370
remaining_gas: &mut u64,
6471
) -> SyscallResult<Vec<Felt>>;
6572

@@ -80,19 +87,19 @@ pub trait StarknetSyscallHandler {
8087

8188
fn emit_event(
8289
&mut self,
83-
keys: Vec<Felt>,
84-
data: Vec<Felt>,
90+
keys: &[Felt],
91+
data: &[Felt],
8592
remaining_gas: &mut u64,
8693
) -> SyscallResult<()>;
8794

8895
fn send_message_to_l1(
8996
&mut self,
9097
to_address: Felt,
91-
payload: Vec<Felt>,
98+
payload: &[Felt],
9299
remaining_gas: &mut u64,
93100
) -> SyscallResult<()>;
94101

95-
fn keccak(&mut self, input: Vec<u64>, remaining_gas: &mut u64) -> SyscallResult<U256>;
102+
fn keccak(&mut self, input: &[u64], remaining_gas: &mut u64) -> SyscallResult<U256>;
96103

97104
fn secp256k1_new(
98105
&mut self,
@@ -164,17 +171,17 @@ pub trait StarknetSyscallHandler {
164171

165172
fn sha256_process_block(
166173
&mut self,
167-
prev_state: [u32; 8],
168-
current_block: [u32; 16],
174+
prev_state: &mut [u32; 8],
175+
current_block: &[u32; 16],
169176
remaining_gas: &mut u64,
170-
) -> SyscallResult<[u32; 8]>;
177+
) -> SyscallResult<()>;
171178

172179
fn meta_tx_v0(
173180
&mut self,
174181
_address: Felt,
175182
_entry_point_selector: Felt,
176-
_calldata: Vec<Felt>,
177-
_signature: Vec<Felt>,
183+
_calldata: &[Felt],
184+
_signature: &[Felt],
178185
_remaining_gas: &mut u64,
179186
) -> SyscallResult<Vec<Felt>> {
180187
unimplemented!();
@@ -188,7 +195,7 @@ pub trait StarknetSyscallHandler {
188195
unimplemented!()
189196
}
190197

191-
fn cheatcode(&mut self, _selector: Felt, _input: Vec<Felt>) -> Vec<Felt> {
198+
fn cheatcode(&mut self, _selector: Felt, _input: &[Felt]) -> Vec<Felt> {
192199
unimplemented!()
193200
}
194201
}
@@ -292,7 +299,7 @@ impl StarknetSyscallHandler for StubSyscallHandler {
292299
&mut self,
293300
_class_hash: Felt,
294301
_contract_address_salt: Felt,
295-
_calldata: Vec<Felt>,
302+
_calldata: &[Felt],
296303
_deploy_from_zero: bool,
297304
_remaining_gas: &mut u64,
298305
) -> SyscallResult<(Felt, Vec<Felt>)> {
@@ -307,7 +314,7 @@ impl StarknetSyscallHandler for StubSyscallHandler {
307314
&mut self,
308315
_class_hash: Felt,
309316
_function_selector: Felt,
310-
_calldata: Vec<Felt>,
317+
_calldata: &[Felt],
311318
_remaining_gas: &mut u64,
312319
) -> SyscallResult<Vec<Felt>> {
313320
unimplemented!()
@@ -317,7 +324,7 @@ impl StarknetSyscallHandler for StubSyscallHandler {
317324
&mut self,
318325
_address: Felt,
319326
_entry_point_selector: Felt,
320-
_calldata: Vec<Felt>,
327+
_calldata: &[Felt],
321328
_remaining_gas: &mut u64,
322329
) -> SyscallResult<Vec<Felt>> {
323330
unimplemented!()
@@ -349,8 +356,8 @@ impl StarknetSyscallHandler for StubSyscallHandler {
349356

350357
fn emit_event(
351358
&mut self,
352-
keys: Vec<Felt>,
353-
data: Vec<Felt>,
359+
keys: &[Felt],
360+
data: &[Felt],
354361
_remaining_gas: &mut u64,
355362
) -> SyscallResult<()> {
356363
self.events.push(StubEvent {
@@ -363,13 +370,13 @@ impl StarknetSyscallHandler for StubSyscallHandler {
363370
fn send_message_to_l1(
364371
&mut self,
365372
_to_address: Felt,
366-
_payload: Vec<Felt>,
373+
_payload: &[Felt],
367374
_remaining_gas: &mut u64,
368375
) -> SyscallResult<()> {
369376
unimplemented!()
370377
}
371378

372-
fn keccak(&mut self, input: Vec<u64>, gas: &mut u64) -> SyscallResult<U256> {
379+
fn keccak(&mut self, input: &[u64], gas: &mut u64) -> SyscallResult<U256> {
373380
let length = input.len();
374381

375382
if length % 17 != 0 {
@@ -428,7 +435,11 @@ impl StarknetSyscallHandler for StubSyscallHandler {
428435
);
429436

430437
if bool::from(point.is_some()) {
431-
Ok(Some(Secp256k1Point { x, y }))
438+
Ok(Some(Secp256k1Point {
439+
x,
440+
y,
441+
is_infinity: false,
442+
}))
432443
} else {
433444
Ok(None)
434445
}
@@ -509,6 +520,7 @@ impl StarknetSyscallHandler for StubSyscallHandler {
509520
hi: u128::from_be_bytes(y[0..16].try_into().unwrap()),
510521
lo: u128::from_be_bytes(y[16..32].try_into().unwrap()),
511522
},
523+
is_infinity: false,
512524
})
513525
}
514526

@@ -573,6 +585,7 @@ impl StarknetSyscallHandler for StubSyscallHandler {
573585
hi: u128::from_be_bytes(y[0..16].try_into().unwrap()),
574586
lo: u128::from_be_bytes(y[16..32].try_into().unwrap()),
575587
},
588+
is_infinity: false,
576589
})
577590
}
578591

@@ -621,6 +634,7 @@ impl StarknetSyscallHandler for StubSyscallHandler {
621634
hi: u128::from_be_bytes(y[0..16].try_into().unwrap()),
622635
lo: u128::from_be_bytes(y[16..32].try_into().unwrap()),
623636
},
637+
is_infinity: false,
624638
}))
625639
} else {
626640
Ok(None)
@@ -658,7 +672,11 @@ impl StarknetSyscallHandler for StubSyscallHandler {
658672
);
659673

660674
if bool::from(point.is_some()) {
661-
Ok(Some(Secp256r1Point { x, y }))
675+
Ok(Some(Secp256r1Point {
676+
x,
677+
y,
678+
is_infinity: false,
679+
}))
662680
} else {
663681
Ok(None)
664682
}
@@ -739,6 +757,7 @@ impl StarknetSyscallHandler for StubSyscallHandler {
739757
hi: u128::from_be_bytes(y[0..16].try_into().unwrap()),
740758
lo: u128::from_be_bytes(y[16..32].try_into().unwrap()),
741759
},
760+
is_infinity: false,
742761
})
743762
}
744763

@@ -802,6 +821,7 @@ impl StarknetSyscallHandler for StubSyscallHandler {
802821
hi: u128::from_be_bytes(y[0..16].try_into().unwrap()),
803822
lo: u128::from_be_bytes(y[16..32].try_into().unwrap()),
804823
},
824+
is_infinity: false,
805825
})
806826
}
807827

@@ -839,6 +859,7 @@ impl StarknetSyscallHandler for StubSyscallHandler {
839859
hi: u128::from_be_bytes(y[0..16].try_into().unwrap()),
840860
lo: u128::from_be_bytes(y[16..32].try_into().unwrap()),
841861
},
862+
is_infinity: false,
842863
}))
843864
} else {
844865
Ok(None)
@@ -855,16 +876,15 @@ impl StarknetSyscallHandler for StubSyscallHandler {
855876

856877
fn sha256_process_block(
857878
&mut self,
858-
prev_state: [u32; 8],
859-
current_block: [u32; 16],
879+
prev_state: &mut [u32; 8],
880+
current_block: &[u32; 16],
860881
_remaining_gas: &mut u64,
861-
) -> SyscallResult<[u32; 8]> {
862-
let mut state = prev_state;
882+
) -> SyscallResult<()> {
863883
let data_as_bytes = sha2::digest::generic_array::GenericArray::from_exact_iter(
864884
current_block.iter().flat_map(|x| x.to_be_bytes()),
865885
)
866886
.unwrap();
867-
sha2::compress256(&mut state, &[data_as_bytes]);
868-
Ok(state)
887+
sha2::compress256(prev_state, &[data_as_bytes]);
888+
Ok(())
869889
}
870890
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
use super::{BlockInfo, TxV3Info};
2+
use serde::Serialize;
3+
use starknet_types_core::felt::Felt;
4+
5+
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize)]
6+
pub struct ExecutionInfoV3 {
7+
pub block_info: BlockInfo,
8+
pub tx_info: TxV3Info,
9+
pub caller_address: Felt,
10+
pub contract_address: Felt,
11+
pub entry_point_selector: Felt,
12+
}

debug_utils/sierra-emu/src/starknet/secp256k1_point.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
use super::U256;
22
use crate::Value;
33

4-
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
4+
#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
55
pub struct Secp256k1Point {
66
pub x: U256,
77
pub y: U256,
8+
pub is_infinity: bool,
89
}
910

1011
impl Secp256k1Point {
@@ -22,6 +23,10 @@ impl Secp256k1Point {
2223
let y = U256::from_value(v.remove(1));
2324
let x = U256::from_value(v.remove(0));
2425

25-
Self { x, y }
26+
// Sierra encodes the point at infinity as (0, 0); recover the explicit flag here
27+
// so the trait surface matches cairo-native's representation.
28+
let is_infinity = x.lo == 0 && x.hi == 0 && y.lo == 0 && y.hi == 0;
29+
30+
Self { x, y, is_infinity }
2631
}
2732
}

debug_utils/sierra-emu/src/starknet/secp256r1_point.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
use super::U256;
22
use crate::Value;
33

4-
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
4+
#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
55
pub struct Secp256r1Point {
66
pub x: U256,
77
pub y: U256,
8+
pub is_infinity: bool,
89
}
910

1011
impl Secp256r1Point {
@@ -22,6 +23,10 @@ impl Secp256r1Point {
2223
let y = U256::from_value(v.remove(1));
2324
let x = U256::from_value(v.remove(0));
2425

25-
Self { x, y }
26+
// Sierra encodes the point at infinity as (0, 0); recover the explicit flag here
27+
// so the trait surface matches cairo-native's representation.
28+
let is_infinity = x.lo == 0 && x.hi == 0 && y.lo == 0 && y.hi == 0;
29+
30+
Self { x, y, is_infinity }
2631
}
2732
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use super::ResourceBounds;
2+
use serde::Serialize;
3+
use starknet_types_core::felt::Felt;
4+
5+
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize)]
6+
pub struct TxV3Info {
7+
pub version: Felt,
8+
pub account_contract_address: Felt,
9+
pub max_fee: u128,
10+
pub signature: Vec<Felt>,
11+
pub transaction_hash: Felt,
12+
pub chain_id: Felt,
13+
pub nonce: Felt,
14+
pub resource_bounds: Vec<ResourceBounds>,
15+
pub tip: u128,
16+
pub paymaster_data: Vec<Felt>,
17+
pub nonce_data_availability_mode: u32,
18+
pub fee_data_availability_mode: u32,
19+
pub account_deployment_data: Vec<Felt>,
20+
pub proof_facts: Vec<Felt>,
21+
}

0 commit comments

Comments
 (0)