Skip to main content

stwo_cairo_adapter/
lib.rs

1use std::collections::HashMap;
2use std::ops::Deref;
3
4use builtins::BuiltinSegments;
5use cairo_vm::types::builtin_name::BuiltinName;
6#[cfg(feature = "extract-mem-trace")]
7use cairo_vm::vm::trace::trace_entry::RelocatedTraceEntry;
8use memory::Memory;
9use opcodes::StateTransitions;
10use serde::{Deserialize, Serialize};
11
12#[cfg(feature = "extract-mem-trace")]
13use crate::memory::MemoryEntry;
14
15pub mod adapter;
16pub mod builtins;
17pub mod decode;
18pub mod memory;
19pub mod opcodes;
20pub mod relocator;
21#[cfg(test)]
22pub mod test_utils;
23
24pub const N_REGISTERS: usize = 3;
25
26/// Externally provided inputs for the Stwo prover.
27#[derive(Debug, Serialize, Deserialize, Clone)]
28pub struct ProverInput {
29    pub state_transitions: StateTransitions,
30    pub memory: Memory,
31    pub pc_count: usize,
32    pub public_memory_addresses: Vec<u32>,
33    pub builtin_segments: BuiltinSegments,
34    pub public_segment_context: PublicSegmentContext,
35    #[cfg(feature = "extract-mem-trace")]
36    pub relocated_mem: Vec<MemoryEntry>,
37    #[cfg(feature = "extract-mem-trace")]
38    pub relocated_trace: Vec<RelocatedTraceEntry>,
39}
40
41const N_PUBLIC_SEGMENTS: usize = 11;
42
43/// Represents the pointer arguments of the `main` function.
44#[derive(Debug, Serialize, Deserialize, Clone)]
45pub struct PublicSegmentContext {
46    present: [bool; N_PUBLIC_SEGMENTS],
47}
48impl PublicSegmentContext {
49    pub fn new(vm_format: &[BuiltinName]) -> Self {
50        let mut present = [false; N_PUBLIC_SEGMENTS];
51        for builtin in vm_format {
52            match builtin {
53                BuiltinName::output => present[0] = true,
54                BuiltinName::pedersen => present[1] = true,
55                BuiltinName::range_check => present[2] = true,
56                BuiltinName::ecdsa => present[3] = true,
57                BuiltinName::bitwise => present[4] = true,
58                BuiltinName::ec_op => present[5] = true,
59                BuiltinName::keccak => present[6] = true,
60                BuiltinName::poseidon => present[7] = true,
61                BuiltinName::range_check96 => present[8] = true,
62                BuiltinName::add_mod => present[9] = true,
63                BuiltinName::mul_mod => present[10] = true,
64                BuiltinName::segment_arena => {
65                    // Do nothing.
66                }
67            }
68        }
69        Self { present }
70    }
71
72    pub const fn bootloader_context() -> Self {
73        // Bootloader always uses every builtin.
74        Self {
75            present: [true; N_PUBLIC_SEGMENTS],
76        }
77    }
78}
79impl Deref for PublicSegmentContext {
80    type Target = [bool; N_PUBLIC_SEGMENTS];
81
82    fn deref(&self) -> &Self::Target {
83        &self.present
84    }
85}
86
87/// Sizes of memory address to ID and ID to value tables.
88#[derive(Debug, Serialize, Deserialize)]
89pub struct MemoryTablesSizes {
90    /// Size of memory address to ID table.
91    pub memory_address_to_id: usize,
92    /// Size of memory ID to big value table.
93    pub memory_id_to_big: usize,
94    /// Size of memory ID to small value table.
95    pub memory_id_to_small: usize,
96}
97
98/// Execution resources required to compute trace size.
99#[derive(Debug, Serialize, Deserialize)]
100pub struct ExecutionResources {
101    /// Map opcode to the number of invocations.
102    pub opcodes_instance_counter: HashMap<String, usize>,
103    /// Map builtin to the number of invocations.
104    pub builtin_instance_counter: HashMap<String, usize>,
105    /// Sizes of memory tables.
106    pub memory_tables_sizes: MemoryTablesSizes,
107    /// Number of verify instructions, corresponds to the number of unique pc values.
108    pub verify_instruction: usize,
109}
110
111impl ExecutionResources {
112    /// Create execution resources from prover input.
113    pub fn from_prover_input(input: &ProverInput) -> Self {
114        ExecutionResources {
115            opcodes_instance_counter: input
116                .state_transitions
117                .casm_states_by_opcode
118                .counts()
119                .into_iter()
120                .collect(),
121            builtin_instance_counter: input
122                .builtin_segments
123                .get_counts()
124                .into_iter()
125                .map(|(builtin, count)| (builtin.to_str_with_suffix().to_string(), count))
126                .collect(),
127            memory_tables_sizes: MemoryTablesSizes {
128                memory_address_to_id: input.memory.address_to_id.len(),
129                memory_id_to_big: input.memory.f252_values.len(),
130                memory_id_to_small: input.memory.small_values.len(),
131            },
132            verify_instruction: input.pc_count,
133        }
134    }
135}