Skip to content

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

Open
avi-starkware wants to merge 2 commits into
mainfrom
avi/cairo_native/syscall-trait-align
Open

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

Conversation

@avi-starkware
Copy link
Copy Markdown
Collaborator

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 three PRs replacing the bridge approach in #1597 / #1607:

  1. this PR — align the trait/type surfaces.
  2. extract a shared cairo-native-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. ContractExecutor dispatch enum (replaces 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.

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

@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.570 ± 0.020 11.539 11.611 6.22 ± 0.07
cairo-native (embedded AOT) 1.859 ± 0.021 1.818 1.886 1.00
cairo-native (embedded JIT using LLVM's ORC Engine) 1.920 ± 0.018 1.896 1.950 1.03 ± 0.02

Benchmark for program dict_snapshot

Open benchmarks
Command Mean [ms] Min [ms] Max [ms] Relative
Cairo-vm (Rust, Cairo 1) 547.4 ± 5.5 540.3 558.6 1.00
cairo-native (embedded AOT) 1693.5 ± 23.2 1648.1 1730.6 3.09 ± 0.05
cairo-native (embedded JIT using LLVM's ORC Engine) 1804.8 ± 19.0 1769.8 1835.1 3.30 ± 0.05

Benchmark for program factorial_2M

Open benchmarks
Command Mean [s] Min [s] Max [s] Relative
Cairo-vm (Rust, Cairo 1) 4.970 ± 0.016 4.941 4.988 2.31 ± 0.02
cairo-native (embedded AOT) 2.148 ± 0.020 2.124 2.181 1.00
cairo-native (embedded JIT using LLVM's ORC Engine) 2.198 ± 0.018 2.177 2.232 1.02 ± 0.01

Benchmark for program fib_2M

Open benchmarks
Command Mean [s] Min [s] Max [s] Relative
Cairo-vm (Rust, Cairo 1) 4.862 ± 0.022 4.832 4.901 2.88 ± 0.03
cairo-native (embedded AOT) 1.689 ± 0.016 1.662 1.718 1.00
cairo-native (embedded JIT using LLVM's ORC Engine) 1.759 ± 0.017 1.734 1.782 1.04 ± 0.01

Benchmark for program linear_search

Open benchmarks
Command Mean [ms] Min [ms] Max [ms] Relative
Cairo-vm (Rust, Cairo 1) 589.8 ± 8.6 581.6 604.5 1.00
cairo-native (embedded AOT) 1733.7 ± 12.1 1709.4 1745.5 2.94 ± 0.05
cairo-native (embedded JIT using LLVM's ORC Engine) 1876.3 ± 15.1 1852.2 1897.6 3.18 ± 0.05

Benchmark for program logistic_map

Open benchmarks
Command Mean [ms] Min [ms] Max [ms] Relative
Cairo-vm (Rust, Cairo 1) 500.2 ± 6.4 495.0 515.8 1.00
cairo-native (embedded AOT) 1859.5 ± 11.0 1841.7 1876.2 3.72 ± 0.05
cairo-native (embedded JIT using LLVM's ORC Engine) 2039.4 ± 20.1 2002.4 2061.8 4.08 ± 0.07

@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.930 ± 0.020 1.901 1.964 1.05 ± 0.01
base dict_insert.cairo (AOT) 1.845 ± 0.016 1.822 1.873 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head dict_insert.cairo (JIT) 1.960 ± 0.024 1.925 1.994 1.04 ± 0.02
head dict_insert.cairo (AOT) 1.885 ± 0.016 1.871 1.927 1.00

Base

Command Mean [s] Min [s] Max [s] Relative
base dict_snapshot.cairo (JIT) 1.811 ± 0.014 1.784 1.826 1.05 ± 0.02
base dict_snapshot.cairo (AOT) 1.719 ± 0.030 1.682 1.780 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head dict_snapshot.cairo (JIT) 1.826 ± 0.014 1.807 1.848 1.04 ± 0.02
head dict_snapshot.cairo (AOT) 1.751 ± 0.032 1.699 1.795 1.00

Base

Command Mean [s] Min [s] Max [s] Relative
base factorial_2M.cairo (JIT) 2.283 ± 0.073 2.237 2.486 1.03 ± 0.03
base factorial_2M.cairo (AOT) 2.219 ± 0.016 2.191 2.238 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head factorial_2M.cairo (JIT) 2.248 ± 0.024 2.201 2.289 1.03 ± 0.02
head factorial_2M.cairo (AOT) 2.192 ± 0.032 2.133 2.237 1.00

Base

Command Mean [s] Min [s] Max [s] Relative
base fib_2M.cairo (JIT) 1.763 ± 0.024 1.719 1.789 1.04 ± 0.02
base fib_2M.cairo (AOT) 1.700 ± 0.022 1.672 1.731 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head fib_2M.cairo (JIT) 1.808 ± 0.020 1.782 1.843 1.03 ± 0.02
head fib_2M.cairo (AOT) 1.758 ± 0.028 1.718 1.804 1.00

Base

Command Mean [s] Min [s] Max [s] Relative
base linear_search.cairo (JIT) 1.873 ± 0.021 1.826 1.897 1.07 ± 0.02
base linear_search.cairo (AOT) 1.752 ± 0.027 1.700 1.804 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head linear_search.cairo (JIT) 1.921 ± 0.017 1.903 1.954 1.09 ± 0.02
head linear_search.cairo (AOT) 1.761 ± 0.022 1.724 1.793 1.00

Base

Command Mean [s] Min [s] Max [s] Relative
base logistic_map.cairo (JIT) 2.075 ± 0.017 2.038 2.099 1.11 ± 0.02
base logistic_map.cairo (AOT) 1.874 ± 0.026 1.850 1.922 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head logistic_map.cairo (JIT) 2.061 ± 0.015 2.039 2.081 1.11 ± 0.01
head logistic_map.cairo (AOT) 1.864 ± 0.013 1.845 1.886 1.00

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. 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 this is
  unambiguous.
- 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 (no default impl). The Sierra-level Value lowering for v3 is
  not yet wired in the VM — eval_get_execution_info_v3 currently 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>
@avi-starkware avi-starkware force-pushed the avi/cairo_native/syscall-trait-align branch from 40ef87d to 7698d47 Compare May 14, 2026 12:26
The secp256k1_add / secp256k1_mul / secp256r1_add / secp256r1_mul stub
implementations called `p.to_encoded_point(false)` and then assumed the
result was Coordinates::Uncompressed. The identity element returns
Coordinates::Identity instead, so P + (-P) and k * P (mod ord) panicked
with unreachable!().

Pre-existing bug in sierra-emu, surfaced more visibly by the trait
alignment in this PR (which added is_infinity to the surface).

Add an explicit Coordinates::Identity arm to all four sites, returning
the canonical (0, 0) + is_infinity: true point.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@avi-starkware avi-starkware force-pushed the avi/cairo_native/syscall-trait-align branch from 7698d47 to a37920a Compare May 14, 2026 12:33
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.

1 participant