Skip to content

add SierraEmuSyscallBridge for cairo-native ↔ sierra-emu interop#1607

Open
avi-starkware wants to merge 1 commit into
mainfrom
avi/cairo_native/sierra-emu-bridge
Open

add SierraEmuSyscallBridge for cairo-native ↔ sierra-emu interop#1607
avi-starkware wants to merge 1 commit into
mainfrom
avi/cairo_native/sierra-emu-bridge

Conversation

@avi-starkware
Copy link
Copy Markdown
Collaborator

Supersedes #1597 — reopened from starkware-libs/cairo_native (instead of avi-starkware/cairo_native) so the stack 1597 → 1598 → 1599 can be expressed via base branches. No code changes.

Summary

Adds an adapter type and feature flag that lets a cairo-native StarknetSyscallHandler drive the sierra-emu virtual machine, removing the need for downstream consumers (e.g. blockifier in starkware-libs/sequencer) to maintain their own copy of the conversion glue between the two crates.

Changes

  • Cargo.toml — new sierra-emu feature flag that enables dep:sierra-emu and the new module. with-trace-dump now depends on sierra-emu instead of pulling in dep:sierra-emu directly.
  • src/sierra_emu_bridge.rsSierraEmuSyscallBridge<'a, H> wraps any &mut H where H: cairo_native::starknet::StarknetSyscallHandler and implements sierra_emu::starknet::StarknetSyscallHandler for it.
  • Conversion helpers (convert_u256, convert_secp_*_point, convert_execution_info{,_v2}, etc.) handle the type-level translation between the two crates' overlapping but distinct syscall surfaces.
  • The Secp256k1 / Secp256r1 conversion treats (0, 0) as the point at infinity when going from sierra-emu (which has no is_infinity flag) to cairo-native — sierra-emu represents the identity element as (0, 0) but cairo-native expects an explicit flag.

Stack

Bottom of stack:

  1. this PRSierraEmuSyscallBridge
  2. ContractExecutor dispatch enum (consumes the bridge)
  3. run_with_libfunc_profile + AotWithProgram variant

Companion sequencer PR that drops the local sierra-emu glue and imports from cairo-native: starkware-libs/sequencer#13880.

Test plan

  • CI passes with default features
  • CI passes with --features sierra-emu
  • CI passes with --features with-trace-dump (which now transitively activates sierra-emu)

Adds an adapter type and feature flag that lets a cairo-native
StarknetSyscallHandler drive the sierra-emu virtual machine, removing
the need for downstream consumers (e.g. blockifier in starknet-libs/sequencer)
to maintain their own copy of the conversion glue between the two crates.

- New `sierra-emu` feature flag in Cargo.toml that enables `dep:sierra-emu`
  and the new `sierra_emu_bridge` module. `with-trace-dump` now depends on
  the new feature instead of pulling in `dep:sierra-emu` directly.
- `SierraEmuSyscallBridge<'a, H>` wraps any `&mut H` where
  `H: cairo_native::starknet::StarknetSyscallHandler` and implements
  `sierra_emu::starknet::StarknetSyscallHandler` for it.
- Conversion helpers (`convert_u256`, `convert_secp_*_point`,
  `convert_execution_info{,_v2}`, etc.) handle the type-level translation
  between the two crates' overlapping but distinct syscall surfaces.
- The Secp256k1 / Secp256r1 conversion treats `(0, 0)` as the point at
  infinity when going from sierra-emu (which has no `is_infinity` flag)
  to cairo-native — sierra-emu represents the identity element as `(0, 0)`
  but cairo-native expects an explicit flag.
@github-actions
Copy link
Copy Markdown

❌ Code is not formatted! Please run cargo format and push the changes.

@github-actions
Copy link
Copy Markdown

Benchmarking results

Benchmark for program dict_insert

Open benchmarks
Command Mean [s] Min [s] Max [s] Relative
Cairo-vm (Rust, Cairo 1) 11.470 ± 0.069 11.394 11.616 6.18 ± 0.13
cairo-native (embedded AOT) 1.857 ± 0.036 1.809 1.924 1.00
cairo-native (embedded JIT using LLVM's ORC Engine) 1.919 ± 0.027 1.874 1.961 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.7 ± 6.0 540.9 558.6 1.00
cairo-native (embedded AOT) 1700.0 ± 19.0 1676.1 1731.2 3.10 ± 0.05
cairo-native (embedded JIT using LLVM's ORC Engine) 1806.9 ± 23.3 1752.1 1834.1 3.30 ± 0.06

Benchmark for program factorial_2M

Open benchmarks
Command Mean [s] Min [s] Max [s] Relative
Cairo-vm (Rust, Cairo 1) 4.950 ± 0.047 4.900 5.045 2.29 ± 0.04
cairo-native (embedded AOT) 2.162 ± 0.029 2.125 2.211 1.00
cairo-native (embedded JIT using LLVM's ORC Engine) 2.213 ± 0.018 2.190 2.243 1.02 ± 0.02

Benchmark for program fib_2M

Open benchmarks
Command Mean [s] Min [s] Max [s] Relative
Cairo-vm (Rust, Cairo 1) 4.867 ± 0.012 4.851 4.890 2.88 ± 0.03
cairo-native (embedded AOT) 1.688 ± 0.017 1.659 1.707 1.00
cairo-native (embedded JIT using LLVM's ORC Engine) 1.761 ± 0.020 1.726 1.786 1.04 ± 0.02

Benchmark for program linear_search

Open benchmarks
Command Mean [ms] Min [ms] Max [ms] Relative
Cairo-vm (Rust, Cairo 1) 585.1 ± 7.2 576.3 600.7 1.00
cairo-native (embedded AOT) 1704.8 ± 13.4 1679.8 1725.4 2.91 ± 0.04
cairo-native (embedded JIT using LLVM's ORC Engine) 1868.9 ± 21.4 1837.1 1899.2 3.19 ± 0.05

Benchmark for program logistic_map

Open benchmarks
Command Mean [ms] Min [ms] Max [ms] Relative
Cairo-vm (Rust, Cairo 1) 499.5 ± 6.2 492.1 511.0 1.00
cairo-native (embedded AOT) 1865.9 ± 22.1 1830.7 1895.7 3.74 ± 0.06
cairo-native (embedded JIT using LLVM's ORC Engine) 2083.0 ± 60.0 2018.6 2202.8 4.17 ± 0.13

@github-actions
Copy link
Copy Markdown

Benchmark results Main vs HEAD.

Base

Command Mean [s] Min [s] Max [s] Relative
base dict_insert.cairo (JIT) 1.921 ± 0.013 1.901 1.939 1.05 ± 0.01
base dict_insert.cairo (AOT) 1.833 ± 0.015 1.802 1.856 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head dict_insert.cairo (JIT) 1.888 ± 0.018 1.867 1.927 1.04 ± 0.02
head dict_insert.cairo (AOT) 1.816 ± 0.024 1.772 1.859 1.00

Base

Command Mean [s] Min [s] Max [s] Relative
base dict_snapshot.cairo (JIT) 1.789 ± 0.014 1.768 1.813 1.06 ± 0.02
base dict_snapshot.cairo (AOT) 1.696 ± 0.025 1.659 1.736 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head dict_snapshot.cairo (JIT) 1.773 ± 0.021 1.749 1.806 1.07 ± 0.02
head dict_snapshot.cairo (AOT) 1.662 ± 0.015 1.642 1.682 1.00

Base

Command Mean [s] Min [s] Max [s] Relative
base factorial_2M.cairo (JIT) 2.202 ± 0.017 2.177 2.224 1.04 ± 0.01
base factorial_2M.cairo (AOT) 2.126 ± 0.018 2.097 2.161 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head factorial_2M.cairo (JIT) 2.166 ± 0.014 2.143 2.183 1.02 ± 0.01
head factorial_2M.cairo (AOT) 2.114 ± 0.012 2.095 2.130 1.00

Base

Command Mean [s] Min [s] Max [s] Relative
base fib_2M.cairo (JIT) 1.724 ± 0.014 1.698 1.743 1.04 ± 0.01
base fib_2M.cairo (AOT) 1.658 ± 0.019 1.630 1.681 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head fib_2M.cairo (JIT) 1.712 ± 0.018 1.688 1.737 1.04 ± 0.01
head fib_2M.cairo (AOT) 1.654 ± 0.014 1.629 1.676 1.00

Base

Command Mean [s] Min [s] Max [s] Relative
base linear_search.cairo (JIT) 1.831 ± 0.014 1.810 1.848 1.08 ± 0.01
base linear_search.cairo (AOT) 1.695 ± 0.012 1.676 1.714 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head linear_search.cairo (JIT) 1.839 ± 0.016 1.823 1.877 1.08 ± 0.02
head linear_search.cairo (AOT) 1.697 ± 0.022 1.666 1.730 1.00

Base

Command Mean [s] Min [s] Max [s] Relative
base logistic_map.cairo (JIT) 1.997 ± 0.011 1.979 2.013 1.10 ± 0.01
base logistic_map.cairo (AOT) 1.814 ± 0.019 1.774 1.838 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head logistic_map.cairo (JIT) 1.998 ± 0.019 1.964 2.025 1.09 ± 0.01
head logistic_map.cairo (AOT) 1.830 ± 0.012 1.816 1.853 1.00

avi-starkware added a commit that referenced this pull request May 14, 2026
Adds a public dispatch enum so a single call site can pick between the
AOT executor and the sierra-emu interpreter at runtime, without forcing
every caller to maintain its own match.

- ContractExecutor { Aot(AotContractExecutor), Emu(EmuContractInfo) },
  with Emu gated on the new sierra-emu cargo feature.
- EmuContractInfo carries Arc<Program> so the program is shared across
  invocations rather than cloned per call.
- ContractExecutor::run dispatches: Aot delegates to
  AotContractExecutor::run; Emu constructs a sierra_emu::VirtualMachine
  and runs it with the caller's syscall handler directly. No adapter is
  needed: the trait is shared via the cairo-native-syscalls crate.
- Cargo: new `sierra-emu` feature (= ["dep:sierra-emu"]). The existing
  `with-trace-dump` feature now activates `sierra-emu` instead of the
  optional dep directly.

Companion to the bridge-free design: with cairo-native and sierra-emu
sharing one trait, the SierraEmuSyscallBridge that PR #1597 / #1607
introduced is no longer necessary.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
avi-starkware added a commit that referenced this pull request May 14, 2026
Adds a public dispatch enum so a single call site can pick between the
AOT executor and the sierra-emu interpreter at runtime, without forcing
every caller to maintain its own match.

- ContractExecutor { Aot(AotContractExecutor), Emu(EmuContractInfo) },
  with Emu gated on the new sierra-emu cargo feature.
- EmuContractInfo carries Arc<Program> so the program is shared across
  invocations rather than cloned per call.
- ContractExecutor::run dispatches: Aot delegates to
  AotContractExecutor::run; Emu constructs a sierra_emu::VirtualMachine
  and runs it with the caller's syscall handler directly. No adapter is
  needed: the trait is shared via the cairo-native-syscalls crate.
- Cargo: new `sierra-emu` feature (= ["dep:sierra-emu"]). The existing
  `with-trace-dump` feature now activates `sierra-emu` instead of the
  optional dep directly.

Companion to the bridge-free design: with cairo-native and sierra-emu
sharing one trait, the SierraEmuSyscallBridge that PR #1597 / #1607
introduced is no longer necessary.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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