Skip to content

align sierra-emu StarknetSyscallHandler trait/types with cairo-native#1610

Open
avi-starkware wants to merge 1 commit into
mainfrom
avi/cairo_native/syscall-trait-align
Open

align sierra-emu StarknetSyscallHandler trait/types with cairo-native#1610
avi-starkware wants to merge 1 commit into
mainfrom
avi/cairo_native/syscall-trait-align

Conversation

@avi-starkware
Copy link
Copy Markdown
Collaborator

@avi-starkware avi-starkware commented May 10, 2026

Summary

Aligns sierra-emu's StarknetSyscallHandler trait and supporting types with cairo-native's shape, so the two surfaces match name-for-name. This is a prerequisite for extracting them into a shared crate (next PR in the stack), which in turn lets us delete the bridge code that PR #1597 / #1607 introduced.

Changes

sierra-emu adopts cairo-native's shape

  • 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. VM call sites in vm/starknet.rs updated to pass borrows.
  • Secp256{k1,r1}Point. Gain an explicit is_infinity: bool flag. The Sierra-level encoding inside sierra-emu still uses (0, 0) to represent the identity element — from_value recovers the flag from that sentinel; into_value drops it (callers that materialize the point at infinity already produce (0, 0), so round-tripping is preserved).
  • sha256_process_block matches cairo-native: (&mut [u32; 8], &[u32; 16]) -> SyscallResult<()> (mutates in place, returns unit).
  • New ExecutionInfoV3 / TxV3Info types + a get_execution_info_v3 trait method (defaulted to unimplemented!()). The VM-level Sierra value lowering for v3 stays todo!() — wiring it is out of scope for this PR.

cairo-native

  • cheatcode trait method becomes unconditional. The runtime/codegen plumbing for cheatcode (vtable entry, wrap_cheatcode, cairo_native__vtable_cheatcode, the with-cheatcode cargo feature) stays feature-gated as before — only the trait method comes out from behind the cfg. The default impl is unimplemented!().

Trace dump

  • metadata::trace_dump populates is_infinity when constructing the sierra-emu secp points (it was already reading the field from cairo-native's Secp256Point).

Backwards compatibility

  • cairo-native: backwards-compatible. The only public change is unconditionally exposing cheatcode on the trait, with a default unimplemented!() impl — existing impls compile unchanged, callers compile unchanged, the trait stays object-safe. Cargo feature with-cheatcode still controls the runtime/codegen side.
  • sierra-emu: breaking — slice-vs-Vec, the new is_infinity field, sha256 sig change. sierra-emu lives in this repo and isn't published as an external API surface, so the blast radius is contained.

Stack

This is the first of four PRs replacing the bridge approach in #1597 / #1607:

  1. this PR — align the trait/type surfaces.
  2. extract shared cairo-native-syscalls crate #1611 — extract a shared cairo-starknet-syscalls crate; both crates re-export from it; the bridge (added in add SierraEmuSyscallBridge for cairo-native ↔ sierra-emu interop #1607) becomes unnecessary and is deleted.
  3. add ContractExecutor dispatch enum (Aot + Emu) #1612ContractExecutor dispatch enum (supersedes add ContractExecutor dispatch enum (Aot + Emu) #1598 / add ContractExecutor dispatch enum (Aot + Emu) #1608); the Emu arm calls cairo-native's syscall handler directly, no bridge needed.
  4. add run_with_libfunc_profile + AotWithProgram variant for ContractExecutor #1613run_with_libfunc_profile + AotWithProgram (supersedes add run_with_libfunc_profile + AotWithProgram variant for ContractExecutor #1599 / add run_with_libfunc_profile + AotWithProgram variant for ContractExecutor #1609).

Test plan

  • cargo check clean for cairo-native (default features, with-cheatcode, with-trace-dump)
  • cargo check -p sierra-emu clean (lib + bins + tests)
  • cargo check --workspace --all-features clean
  • CI green

This change is Reviewable

@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 10, 2026

✅ Code is now correctly formatted.

@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 10, 2026

Benchmarking results

Benchmark for program dict_insert

Open benchmarks
Command Mean [s] Min [s] Max [s] Relative
Cairo-vm (Rust, Cairo 1) 11.503 ± 0.103 11.389 11.740 5.76 ± 0.09
cairo-native (embedded AOT) 2.002 ± 0.034 1.965 2.072 1.00 ± 0.02
cairo-native (embedded JIT using LLVM's ORC Engine) 1.996 ± 0.028 1.955 2.036 1.00

Benchmark for program dict_snapshot

Open benchmarks
Command Mean [ms] Min [ms] Max [ms] Relative
Cairo-vm (Rust, Cairo 1) 572.6 ± 14.7 563.4 612.4 1.00
cairo-native (embedded AOT) 1718.6 ± 23.7 1671.8 1749.5 3.00 ± 0.09
cairo-native (embedded JIT using LLVM's ORC Engine) 1752.8 ± 49.7 1710.1 1873.3 3.06 ± 0.12

Benchmark for program factorial_2M

Open benchmarks
Command Mean [s] Min [s] Max [s] Relative
Cairo-vm (Rust, Cairo 1) 4.960 ± 0.024 4.939 5.024 2.19 ± 0.03
cairo-native (embedded AOT) 2.264 ± 0.021 2.230 2.290 1.00 ± 0.02
cairo-native (embedded JIT using LLVM's ORC Engine) 2.261 ± 0.031 2.217 2.303 1.00

Benchmark for program fib_2M

Open benchmarks
Command Mean [s] Min [s] Max [s] Relative
Cairo-vm (Rust, Cairo 1) 4.855 ± 0.060 4.813 5.019 2.69 ± 0.05
cairo-native (embedded AOT) 1.803 ± 0.028 1.753 1.840 1.00
cairo-native (embedded JIT using LLVM's ORC Engine) 1.816 ± 0.015 1.793 1.837 1.01 ± 0.02

Benchmark for program linear_search

Open benchmarks
Command Mean [ms] Min [ms] Max [ms] Relative
Cairo-vm (Rust, Cairo 1) 625.0 ± 35.4 598.6 713.0 1.00
cairo-native (embedded AOT) 1754.3 ± 21.4 1718.0 1774.5 2.81 ± 0.16
cairo-native (embedded JIT using LLVM's ORC Engine) 1757.9 ± 16.7 1734.1 1788.5 2.81 ± 0.16

Benchmark for program logistic_map

Open benchmarks
Command Mean [ms] Min [ms] Max [ms] Relative
Cairo-vm (Rust, Cairo 1) 522.5 ± 9.4 511.5 536.4 1.00
cairo-native (embedded AOT) 1915.7 ± 33.9 1874.7 1965.6 3.67 ± 0.09
cairo-native (embedded JIT using LLVM's ORC Engine) 2006.7 ± 20.6 1974.8 2028.1 3.84 ± 0.08

@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 10, 2026

Benchmark results Main vs HEAD.

Base

Command Mean [s] Min [s] Max [s] Relative
base dict_insert.cairo (JIT) 1.879 ± 0.014 1.859 1.901 1.01 ± 0.01
base dict_insert.cairo (AOT) 1.854 ± 0.013 1.837 1.887 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head dict_insert.cairo (JIT) 1.990 ± 0.045 1.891 2.040 1.00
head dict_insert.cairo (AOT) 2.000 ± 0.054 1.925 2.114 1.00 ± 0.04

Base

Command Mean [s] Min [s] Max [s] Relative
base dict_snapshot.cairo (JIT) 1.686 ± 0.008 1.677 1.701 1.02 ± 0.01
base dict_snapshot.cairo (AOT) 1.660 ± 0.010 1.645 1.676 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head dict_snapshot.cairo (JIT) 1.900 ± 0.066 1.802 1.991 1.08 ± 0.06
head dict_snapshot.cairo (AOT) 1.767 ± 0.070 1.660 1.852 1.00

Base

Command Mean [s] Min [s] Max [s] Relative
base factorial_2M.cairo (JIT) 2.181 ± 0.024 2.148 2.234 1.01 ± 0.01
base factorial_2M.cairo (AOT) 2.169 ± 0.012 2.146 2.187 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head factorial_2M.cairo (JIT) 2.218 ± 0.046 2.144 2.281 1.00 ± 0.03
head factorial_2M.cairo (AOT) 2.216 ± 0.034 2.170 2.265 1.00

Base

Command Mean [s] Min [s] Max [s] Relative
base fib_2M.cairo (JIT) 1.691 ± 0.010 1.681 1.708 1.01 ± 0.01
base fib_2M.cairo (AOT) 1.675 ± 0.018 1.642 1.703 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head fib_2M.cairo (JIT) 1.765 ± 0.020 1.740 1.804 1.00
head fib_2M.cairo (AOT) 1.809 ± 0.046 1.715 1.872 1.03 ± 0.03

Base

Command Mean [s] Min [s] Max [s] Relative
base linear_search.cairo (JIT) 1.726 ± 0.018 1.703 1.769 1.02 ± 0.01
base linear_search.cairo (AOT) 1.686 ± 0.011 1.663 1.706 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head linear_search.cairo (JIT) 1.863 ± 0.030 1.819 1.907 1.00
head linear_search.cairo (AOT) 1.896 ± 0.044 1.839 1.969 1.02 ± 0.03

Base

Command Mean [s] Min [s] Max [s] Relative
base logistic_map.cairo (JIT) 1.952 ± 0.020 1.927 1.985 1.06 ± 0.01
base logistic_map.cairo (AOT) 1.836 ± 0.013 1.818 1.856 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head logistic_map.cairo (JIT) 2.152 ± 0.063 2.045 2.231 1.09 ± 0.04
head logistic_map.cairo (AOT) 1.978 ± 0.055 1.883 2.036 1.00

Copy link
Copy Markdown
Collaborator

@orizi orizi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@orizi reviewed 6 files and made 2 comments.
Reviewable status: 6 of 8 files reviewed, 2 unresolved discussions (waiting on avi-starkware, TomerStarkware, and Yoni-Starkware).


debug_utils/sierra-emu/src/starknet.rs line 580 at r1 (raw file):

            Coordinates::Uncompressed { x, y } => (x, y),
            Coordinates::Identity => {
                // k * P can be the identity (e.g. k = ord(P)).

Suggestion:

                // m * P can be the identity (e.g. m = ord(P)).

debug_utils/sierra-emu/src/starknet/secp256r1_point.rs line 8 at r1 (raw file):

    pub x: U256,
    pub y: U256,
    pub is_infinity: bool,

why add is_infinity? what was bad about (0, 0)?

@avi-starkware avi-starkware force-pushed the avi/cairo_native/syscall-trait-align branch from a37920a to 63b478e Compare May 17, 2026 11:57
@avi-starkware
Copy link
Copy Markdown
Collaborator Author

debug_utils/sierra-emu/src/starknet/secp256r1_point.rs line 8 at r1 (raw file):

Previously, orizi wrote…

why add is_infinity? what was bad about (0, 0)?

The is_infinity flag exists on cairo-native's Secp256k1Point (it's part of the published trait surface and the repr(C, align(16)) MLIR ABI). To let cairo-native and sierra-emu share one trait without touching the cairo-native side, the unified struct in cairo-starknet-syscalls keeps the flag.

@avi-starkware avi-starkware force-pushed the avi/cairo_native/syscall-trait-align branch from 63b478e to 26d9249 Compare May 17, 2026 13:58
Copy link
Copy Markdown
Collaborator

@orizi orizi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm:

@orizi reviewed 3 files and all commit messages, made 1 comment, and resolved 2 discussions.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on TomerStarkware and Yoni-Starkware).

@avi-starkware
Copy link
Copy Markdown
Collaborator Author

debug_utils/sierra-emu/src/starknet.rs line 580 at r1 (raw file):

            Coordinates::Uncompressed { x, y } => (x, y),
            Coordinates::Identity => {
                // k * P can be the identity (e.g. k = ord(P)).

Done.

Sierra-emu adopts cairo-native's syscall surface so the two traits/types
match name-for-name. Prerequisite for extracting them into a shared
crate (next PR).

- 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. The Sierra
  Value encoding canonicalizes (0, 0) for the identity element so the
  round-trip via from_value/into_value is lossless: into_value forces
  (x, y) = (0, 0) when is_infinity is set; from_value recovers the flag
  from the sentinel. (0, 0) is not a real curve point, so the aliasing
  is unambiguous.
- Stub secp arithmetic now handles `Coordinates::Identity` from
  to_encoded_point(false), returning the canonical (0, 0) + is_infinity
  point instead of tripping unreachable!(). Pre-existing bug surfaced
  by the trait alignment.
- 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. The Sierra-level Value lowering for v3 isn't wired in the VM
  yet -- eval_get_execution_info_v3 soft-fails with a descriptive felt
  rather than panicking with todo!(), so any contract calling the v3
  syscall sees a graceful syscall error. Full v3 wiring is a separate
  effort.
- 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>
Copy link
Copy Markdown
Collaborator

@TomerStarkware TomerStarkware left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TomerStarkware reviewed 1 file and all commit messages.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on Yoni-Starkware).

Copy link
Copy Markdown
Collaborator

@TomerStarkware TomerStarkware left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on Yoni-Starkware).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants