monerochan_core_machine/
lib.rs

1#![allow(
2    clippy::new_without_default,
3    clippy::field_reassign_with_default,
4    clippy::unnecessary_cast,
5    clippy::cast_abs_to_unsigned,
6    clippy::needless_range_loop,
7    clippy::type_complexity,
8    clippy::unnecessary_unwrap,
9    clippy::default_constructed_unit_structs,
10    clippy::box_default,
11    clippy::assign_op_pattern,
12    deprecated,
13    incomplete_features
14)]
15#![warn(unused_extern_crates)]
16
17pub mod air;
18pub mod alu;
19pub mod bytes;
20pub mod control_flow;
21pub mod cpu;
22pub mod global;
23pub mod io;
24pub mod memory;
25pub mod operations;
26pub mod program;
27pub mod riscv;
28pub mod shape;
29#[cfg(feature = "sys")]
30pub mod sys;
31pub mod syscall;
32pub mod utils;
33
34// Re-export the `MONEROCHANReduceProof` struct from monerochan_core_machine.
35//
36// This is done to avoid a circular dependency between monerochan_core_machine and monerochan_core_executor, and
37// enable crates that depend on monerochan_core_machine to import the `MONEROCHANReduceProof` type directly.
38pub mod reduce {
39    pub use monerochan_stark::MONEROCHANReduceProof;
40}
41
42#[cfg(test)]
43pub mod programs {
44    #[allow(dead_code)]
45    #[allow(missing_docs)]
46    pub mod tests {
47        use monerochan_core_executor::{Instruction, Opcode, Program};
48
49        pub use test_artifacts::{
50            FIBONACCI_ELF, PANIC_ELF, SECP256R1_ADD_ELF, SECP256R1_DOUBLE_ELF, SSZ_WITHDRAWALS_ELF,
51            U256XU2048_MUL_ELF,
52        };
53
54        #[must_use]
55        pub fn simple_program() -> Program {
56            let instructions = vec![
57                Instruction::new(Opcode::ADD, 29, 0, 5, false, true),
58                Instruction::new(Opcode::ADD, 30, 0, 37, false, true),
59                Instruction::new(Opcode::ADD, 31, 30, 29, false, false),
60            ];
61            Program::new(instructions, 0, 0)
62        }
63
64        /// Get the fibonacci program.
65        ///
66        /// # Panics
67        ///
68        /// This function will panic if the program fails to load.
69        #[must_use]
70        pub fn fibonacci_program() -> Program {
71            Program::from(FIBONACCI_ELF).unwrap()
72        }
73
74        /// Get the secp256r1 add program.
75        ///
76        /// # Panics
77        ///
78        /// This function will panic if the program fails to load.
79        #[must_use]
80        pub fn secp256r1_add_program() -> Program {
81            Program::from(SECP256R1_ADD_ELF).unwrap()
82        }
83
84        /// Get the secp256r1 double program.
85        ///
86        /// # Panics
87        ///
88        /// This function will panic if the program fails to load.
89        #[must_use]
90        pub fn secp256r1_double_program() -> Program {
91            Program::from(SECP256R1_DOUBLE_ELF).unwrap()
92        }
93
94        /// Get the u256x2048 mul program.
95        ///
96        /// # Panics
97        ///
98        /// This function will panic if the program fails to load.
99        #[must_use]
100        pub fn u256xu2048_mul_program() -> Program {
101            Program::from(U256XU2048_MUL_ELF).unwrap()
102        }
103
104        /// Get the SSZ withdrawals program.
105        ///
106        /// # Panics
107        ///
108        /// This function will panic if the program fails to load.
109        #[must_use]
110        pub fn ssz_withdrawals_program() -> Program {
111            Program::from(SSZ_WITHDRAWALS_ELF).unwrap()
112        }
113
114        /// Get the panic program.
115        ///
116        /// # Panics
117        ///
118        /// This function will panic if the program fails to load.
119        #[must_use]
120        pub fn panic_program() -> Program {
121            Program::from(PANIC_ELF).unwrap()
122        }
123
124        #[must_use]
125        #[allow(clippy::unreadable_literal)]
126        pub fn simple_memory_program() -> Program {
127            let instructions = vec![
128                Instruction::new(Opcode::ADD, 29, 0, 0x12348765, false, true),
129                // SW and LW
130                Instruction::new(Opcode::SW, 29, 0, 0x27654320, false, true),
131                Instruction::new(Opcode::LW, 28, 0, 0x27654320, false, true),
132                // LBU
133                Instruction::new(Opcode::LBU, 27, 0, 0x27654320, false, true),
134                Instruction::new(Opcode::LBU, 26, 0, 0x27654321, false, true),
135                Instruction::new(Opcode::LBU, 25, 0, 0x27654322, false, true),
136                Instruction::new(Opcode::LBU, 24, 0, 0x27654323, false, true),
137                // LB
138                Instruction::new(Opcode::LB, 23, 0, 0x27654320, false, true),
139                Instruction::new(Opcode::LB, 22, 0, 0x27654321, false, true),
140                // LHU
141                Instruction::new(Opcode::LHU, 21, 0, 0x27654320, false, true),
142                Instruction::new(Opcode::LHU, 20, 0, 0x27654322, false, true),
143                // LU
144                Instruction::new(Opcode::LH, 19, 0, 0x27654320, false, true),
145                Instruction::new(Opcode::LH, 18, 0, 0x27654322, false, true),
146                // SB
147                Instruction::new(Opcode::ADD, 17, 0, 0x38276525, false, true),
148                // Save the value 0x12348765 into address 0x43627530
149                Instruction::new(Opcode::SW, 29, 0, 0x43627530, false, true),
150                Instruction::new(Opcode::SB, 17, 0, 0x43627530, false, true),
151                Instruction::new(Opcode::LW, 16, 0, 0x43627530, false, true),
152                Instruction::new(Opcode::SB, 17, 0, 0x43627531, false, true),
153                Instruction::new(Opcode::LW, 15, 0, 0x43627530, false, true),
154                Instruction::new(Opcode::SB, 17, 0, 0x43627532, false, true),
155                Instruction::new(Opcode::LW, 14, 0, 0x43627530, false, true),
156                Instruction::new(Opcode::SB, 17, 0, 0x43627533, false, true),
157                Instruction::new(Opcode::LW, 13, 0, 0x43627530, false, true),
158                // SH
159                // Save the value 0x12348765 into address 0x43627530
160                Instruction::new(Opcode::SW, 29, 0, 0x43627530, false, true),
161                Instruction::new(Opcode::SH, 17, 0, 0x43627530, false, true),
162                Instruction::new(Opcode::LW, 12, 0, 0x43627530, false, true),
163                Instruction::new(Opcode::SH, 17, 0, 0x43627532, false, true),
164                Instruction::new(Opcode::LW, 11, 0, 0x43627530, false, true),
165            ];
166            Program::new(instructions, 0, 0)
167        }
168    }
169}