openvm_rv32im_guest/
lib.rs

1#![no_std]
2extern crate alloc;
3
4/// Library functions for user input/output.
5#[cfg(target_os = "zkvm")]
6mod io;
7
8#[cfg(target_os = "zkvm")]
9pub use io::*;
10use strum_macros::FromRepr;
11
12/// This is custom-0 defined in RISC-V spec document
13pub const SYSTEM_OPCODE: u8 = 0x0b;
14pub const CSR_OPCODE: u8 = 0b1110011;
15pub const RV32_ALU_OPCODE: u8 = 0b0110011;
16pub const RV32M_FUNCT7: u8 = 0x01;
17pub const NATIVE_STOREW_FUNCT3: u8 = 0b111;
18pub const NATIVE_STOREW_FUNCT7: u32 = 2;
19
20pub const TERMINATE_FUNCT3: u8 = 0b000;
21pub const HINT_FUNCT3: u8 = 0b001;
22pub const HINT_STOREW_IMM: u32 = 0;
23pub const HINT_BUFFER_IMM: u32 = 1;
24pub const REVEAL_FUNCT3: u8 = 0b010;
25pub const PHANTOM_FUNCT3: u8 = 0b011;
26pub const CSRRW_FUNCT3: u8 = 0b001;
27
28/// imm options for system phantom instructions
29#[derive(Debug, Copy, Clone, PartialEq, Eq, FromRepr)]
30#[repr(u16)]
31pub enum PhantomImm {
32    HintInput = 0,
33    PrintStr,
34    HintRandom,
35    HintLoadByKey,
36}
37
38/// Encode a 2d-array of field elements into bytes for `hint_load_by_key`
39#[cfg(not(target_os = "zkvm"))]
40pub fn hint_load_by_key_encode<F: p3_field::PrimeField32>(
41    value: &[alloc::vec::Vec<F>],
42) -> alloc::vec::Vec<u8> {
43    let len = value.len();
44    let mut ret = (len as u32).to_le_bytes().to_vec();
45    for v in value {
46        ret.extend((v.len() as u32).to_le_bytes());
47        ret.extend(v.iter().flat_map(|x| x.as_canonical_u32().to_le_bytes()));
48    }
49    ret
50}