sp1_core_executor/
lib.rs

1//! An implementation of an exucutor for the SP1 RISC-V zkVM.
2
3#![warn(clippy::pedantic)]
4#![allow(clippy::similar_names)]
5#![allow(clippy::cast_possible_wrap)]
6#![allow(clippy::cast_possible_truncation)]
7#![allow(clippy::cast_sign_loss)]
8#![allow(clippy::module_name_repetitions)]
9#![allow(clippy::needless_range_loop)]
10#![allow(clippy::cast_lossless)]
11#![allow(clippy::bool_to_int_with_if)]
12#![allow(clippy::should_panic_without_expect)]
13#![allow(clippy::field_reassign_with_default)]
14#![allow(clippy::manual_assert)]
15#![allow(clippy::unreadable_literal)]
16#![allow(clippy::match_wildcard_for_single_variants)]
17#![allow(clippy::missing_panics_doc)]
18#![allow(clippy::missing_errors_doc)]
19#![allow(clippy::explicit_iter_loop)]
20#![allow(clippy::struct_excessive_bools)]
21#![warn(missing_docs)]
22
23mod air;
24mod context;
25mod cost;
26mod dependencies;
27mod disassembler;
28pub mod estimator;
29pub mod events;
30mod executor;
31mod hook;
32mod instruction;
33mod io;
34mod memory;
35mod opcode;
36#[cfg(feature = "profiling")]
37mod profiler;
38mod program;
39mod record;
40mod reduce;
41mod register;
42mod report;
43mod state;
44pub mod subproof;
45pub mod syscalls;
46mod utils;
47
48pub use air::*;
49pub use context::*;
50pub use cost::*;
51pub use executor::*;
52pub use hook::*;
53pub use instruction::*;
54pub use opcode::*;
55pub use program::*;
56pub use record::*;
57pub use reduce::*;
58pub use register::*;
59pub use report::*;
60pub use state::*;
61pub use utils::*;
62
63/// Used for testing.
64#[cfg(test)]
65pub mod programs {
66    #[allow(dead_code)]
67    #[allow(missing_docs)]
68    pub mod tests {
69        use crate::{Instruction, Opcode, Program};
70
71        pub use test_artifacts::{
72            FIBONACCI_ELF, PANIC_ELF, SECP256R1_ADD_ELF, SECP256R1_DOUBLE_ELF, SSZ_WITHDRAWALS_ELF,
73            U256XU2048_MUL_ELF,
74        };
75
76        #[must_use]
77        pub fn simple_program() -> Program {
78            let instructions = vec![
79                Instruction::new(Opcode::ADD, 29, 0, 5, false, true),
80                Instruction::new(Opcode::ADD, 30, 0, 37, false, true),
81                Instruction::new(Opcode::ADD, 31, 30, 29, false, false),
82            ];
83            Program::new(instructions, 0, 0)
84        }
85
86        /// Get the fibonacci program.
87        ///
88        /// # Panics
89        ///
90        /// This function will panic if the program fails to load.
91        #[must_use]
92        pub fn fibonacci_program() -> Program {
93            Program::from(FIBONACCI_ELF).unwrap()
94        }
95
96        /// Get the secp256r1 add program.
97        ///
98        /// # Panics
99        ///
100        /// This function will panic if the program fails to load.
101        #[must_use]
102        pub fn secp256r1_add_program() -> Program {
103            Program::from(SECP256R1_ADD_ELF).unwrap()
104        }
105
106        /// Get the secp256r1 double program.
107        ///
108        /// # Panics
109        ///
110        /// This function will panic if the program fails to load.
111        #[must_use]
112        pub fn secp256r1_double_program() -> Program {
113            Program::from(SECP256R1_DOUBLE_ELF).unwrap()
114        }
115
116        /// Get the u256x2048 mul program.
117        ///
118        /// # Panics
119        ///
120        /// This function will panic if the program fails to load.
121        #[must_use]
122        pub fn u256xu2048_mul_program() -> Program {
123            Program::from(U256XU2048_MUL_ELF).unwrap()
124        }
125
126        /// Get the SSZ withdrawals program.
127        ///
128        /// # Panics
129        ///
130        /// This function will panic if the program fails to load.
131        #[must_use]
132        pub fn ssz_withdrawals_program() -> Program {
133            Program::from(SSZ_WITHDRAWALS_ELF).unwrap()
134        }
135
136        /// Get the panic program.
137        ///
138        /// # Panics
139        ///
140        /// This function will panic if the program fails to load.
141        #[must_use]
142        pub fn panic_program() -> Program {
143            Program::from(PANIC_ELF).unwrap()
144        }
145
146        #[must_use]
147        #[allow(clippy::unreadable_literal)]
148        pub fn simple_memory_program() -> Program {
149            let instructions = vec![
150                Instruction::new(Opcode::ADD, 29, 0, 0x12348765, false, true),
151                // SW and LW
152                Instruction::new(Opcode::SW, 29, 0, 0x27654320, false, true),
153                Instruction::new(Opcode::LW, 28, 0, 0x27654320, false, true),
154                // LBU
155                Instruction::new(Opcode::LBU, 27, 0, 0x27654320, false, true),
156                Instruction::new(Opcode::LBU, 26, 0, 0x27654321, false, true),
157                Instruction::new(Opcode::LBU, 25, 0, 0x27654322, false, true),
158                Instruction::new(Opcode::LBU, 24, 0, 0x27654323, false, true),
159                // LB
160                Instruction::new(Opcode::LB, 23, 0, 0x27654320, false, true),
161                Instruction::new(Opcode::LB, 22, 0, 0x27654321, false, true),
162                // LHU
163                Instruction::new(Opcode::LHU, 21, 0, 0x27654320, false, true),
164                Instruction::new(Opcode::LHU, 20, 0, 0x27654322, false, true),
165                // LU
166                Instruction::new(Opcode::LH, 19, 0, 0x27654320, false, true),
167                Instruction::new(Opcode::LH, 18, 0, 0x27654322, false, true),
168                // SB
169                Instruction::new(Opcode::ADD, 17, 0, 0x38276525, false, true),
170                // Save the value 0x12348765 into address 0x43627530
171                Instruction::new(Opcode::SW, 29, 0, 0x43627530, false, true),
172                Instruction::new(Opcode::SB, 17, 0, 0x43627530, false, true),
173                Instruction::new(Opcode::LW, 16, 0, 0x43627530, false, true),
174                Instruction::new(Opcode::SB, 17, 0, 0x43627531, false, true),
175                Instruction::new(Opcode::LW, 15, 0, 0x43627530, false, true),
176                Instruction::new(Opcode::SB, 17, 0, 0x43627532, false, true),
177                Instruction::new(Opcode::LW, 14, 0, 0x43627530, false, true),
178                Instruction::new(Opcode::SB, 17, 0, 0x43627533, false, true),
179                Instruction::new(Opcode::LW, 13, 0, 0x43627530, false, true),
180                // SH
181                // Save the value 0x12348765 into address 0x43627530
182                Instruction::new(Opcode::SW, 29, 0, 0x43627530, false, true),
183                Instruction::new(Opcode::SH, 17, 0, 0x43627530, false, true),
184                Instruction::new(Opcode::LW, 12, 0, 0x43627530, false, true),
185                Instruction::new(Opcode::SH, 17, 0, 0x43627532, false, true),
186                Instruction::new(Opcode::LW, 11, 0, 0x43627530, false, true),
187            ];
188            Program::new(instructions, 0, 0)
189        }
190    }
191}