openvm_native_compiler/
lib.rs

1#![allow(clippy::type_complexity)]
2#![allow(clippy::needless_range_loop)]
3
4use openvm_instructions::LocalOpcode;
5use openvm_instructions_derive::LocalOpcode;
6use openvm_rv32im_transpiler::BranchEqualOpcode;
7use serde::{Deserialize, Serialize};
8use strum::{EnumCount, EnumIter, FromRepr, IntoEnumIterator};
9
10extern crate alloc;
11extern crate core;
12
13pub mod asm;
14pub mod constraints;
15pub mod conversion;
16pub mod ir;
17
18pub mod prelude {
19    pub use openvm_native_compiler_derive::DslVariable;
20
21    pub use crate::{asm::AsmCompiler, ir::*};
22}
23
24// =================================================================================================
25// Native kernel opcodes
26// =================================================================================================
27
28#[derive(
29    Copy,
30    Clone,
31    Debug,
32    PartialEq,
33    Eq,
34    PartialOrd,
35    Ord,
36    EnumCount,
37    EnumIter,
38    FromRepr,
39    LocalOpcode,
40    Serialize,
41    Deserialize,
42)]
43#[opcode_offset = 0x100]
44#[repr(usize)]
45#[allow(non_camel_case_types)]
46pub enum NativeLoadStoreOpcode {
47    LOADW,
48    STOREW,
49    /// Instruction to write the next hint word into memory.
50    HINT_STOREW,
51}
52
53#[derive(Copy, Clone, Debug, LocalOpcode)]
54#[opcode_offset = 0x108]
55pub struct NativeLoadStore4Opcode(pub NativeLoadStoreOpcode);
56
57impl NativeLoadStore4Opcode {
58    pub fn iter() -> impl Iterator<Item = Self> {
59        NativeLoadStoreOpcode::iter().map(Self)
60    }
61}
62
63pub const BLOCK_LOAD_STORE_SIZE: usize = 4;
64
65#[derive(Copy, Clone, Debug, LocalOpcode)]
66#[opcode_offset = 0x110]
67pub struct NativeBranchEqualOpcode(pub BranchEqualOpcode);
68
69impl NativeBranchEqualOpcode {
70    pub fn iter() -> impl Iterator<Item = Self> {
71        BranchEqualOpcode::iter().map(Self)
72    }
73}
74
75#[derive(
76    Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, EnumCount, EnumIter, FromRepr, LocalOpcode,
77)]
78#[opcode_offset = 0x115]
79#[repr(usize)]
80pub enum NativeJalOpcode {
81    JAL,
82}
83
84#[derive(
85    Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, EnumCount, EnumIter, FromRepr, LocalOpcode,
86)]
87#[opcode_offset = 0x120]
88#[repr(usize)]
89#[allow(non_camel_case_types)]
90pub enum NativeRangeCheckOpcode {
91    RANGE_CHECK,
92}
93
94#[derive(
95    Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, EnumCount, EnumIter, FromRepr, LocalOpcode,
96)]
97#[opcode_offset = 0x125]
98#[repr(usize)]
99pub enum CastfOpcode {
100    CASTF,
101}
102
103#[derive(
104    Copy,
105    Clone,
106    Debug,
107    PartialEq,
108    Eq,
109    PartialOrd,
110    Ord,
111    EnumCount,
112    EnumIter,
113    FromRepr,
114    LocalOpcode,
115    Serialize,
116    Deserialize,
117)]
118#[opcode_offset = 0x130]
119#[repr(usize)]
120pub enum FieldArithmeticOpcode {
121    ADD,
122    SUB,
123    MUL,
124    DIV,
125}
126
127#[derive(
128    Copy,
129    Clone,
130    Debug,
131    PartialEq,
132    Eq,
133    PartialOrd,
134    Ord,
135    EnumCount,
136    EnumIter,
137    FromRepr,
138    LocalOpcode,
139    Serialize,
140    Deserialize,
141)]
142#[opcode_offset = 0x140]
143#[repr(usize)]
144pub enum FieldExtensionOpcode {
145    FE4ADD,
146    FE4SUB,
147    BBE4MUL,
148    BBE4DIV,
149}
150
151#[derive(Copy, Clone, Debug, PartialEq, Eq, FromRepr)]
152#[repr(u16)]
153pub enum NativePhantom {
154    /// Native field element print
155    Print = 0x10,
156    /// Prepare the next input vector for hinting.
157    HintInput,
158    /// Prepare the little-endian bit decomposition of a variable for hinting.
159    HintBits,
160    /// Move data from input stream into hint space
161    HintLoad,
162    /// Prepare the next felt for hinting.
163    HintFelt,
164}
165
166#[derive(
167    Copy,
168    Clone,
169    Debug,
170    PartialEq,
171    Eq,
172    PartialOrd,
173    Ord,
174    EnumCount,
175    EnumIter,
176    FromRepr,
177    LocalOpcode,
178    Serialize,
179    Deserialize,
180)]
181#[opcode_offset = 0x150]
182#[repr(usize)]
183#[allow(non_camel_case_types)]
184pub enum Poseidon2Opcode {
185    PERM_POS2,
186    COMP_POS2,
187}
188
189/// Opcodes for FRI opening proofs.
190#[derive(
191    Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, EnumCount, EnumIter, FromRepr, LocalOpcode,
192)]
193#[opcode_offset = 0x160]
194#[repr(usize)]
195#[allow(non_camel_case_types)]
196pub enum FriOpcode {
197    /// In FRI pcs opening verification, the reduced opening polynomial is computed one evaluation
198    /// per column polynomial, per opening point
199    FRI_REDUCED_OPENING,
200}
201
202/// Opcodes for verify batch.
203#[derive(
204    Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, EnumCount, EnumIter, FromRepr, LocalOpcode,
205)]
206#[opcode_offset = 0x170]
207#[repr(usize)]
208#[allow(non_camel_case_types)]
209pub enum VerifyBatchOpcode {
210    /// In FRI pcs opening verification, the reduced opening polynomial is computed one evaluation
211    /// per column polynomial, per opening point
212    VERIFY_BATCH,
213}