Skip to main content

sp1_primitives/
consts.rs

1use elf::abi::{PF_NONE, PF_R, PF_W, PF_X};
2
3/// The maximum size of the memory in bytes.
4pub const MAXIMUM_MEMORY_SIZE: u64 = (1u64 << 48) - 1;
5
6/// The number of bits in a byte.
7pub const BYTE_SIZE: usize = 8;
8
9/// The number of bits in a limb.
10pub const LIMB_SIZE: usize = 16;
11
12/// The size of a word in limbs.
13pub const WORD_SIZE: usize = 4;
14
15/// The size of a word in bytes.
16pub const WORD_BYTE_SIZE: usize = 2 * WORD_SIZE;
17
18/// The size of an instruction in bytes.
19pub const INSTRUCTION_WORD_SIZE: usize = 4;
20
21/// The number of bytes necessary to represent a 128-bit integer.
22pub const LONG_WORD_BYTE_SIZE: usize = 2 * WORD_BYTE_SIZE;
23
24/// The log2 page size (in bytes).
25pub const LOG_PAGE_SIZE: usize = 12;
26
27/// The size of a page in bytes.
28pub const PAGE_SIZE: usize = 1 << LOG_PAGE_SIZE;
29
30/// MProtect flags.
31pub const PROT_NONE: u8 = PF_NONE as u8;
32pub const PROT_READ: u8 = PF_R as u8;
33pub const PROT_WRITE: u8 = PF_W as u8;
34pub const PROT_EXEC: u8 = PF_X as u8;
35pub const DEFAULT_PAGE_PROT: u8 = PROT_READ | PROT_WRITE;
36
37/// The type for the ELF note for enabling untrusted programs.
38pub const NOTE_UNTRUSTED_PROGRAM_ENABLED: u32 = 1;
39
40/// The stack top for the 64-bit zkvm.
41pub const STACK_TOP: u64 = 0x78000000;
42
43pub mod fd {
44    /// The minimum file descriptor.
45    ///
46    /// Any file descriptor must be greater than this value, otherwise the executor will panic.
47    ///
48    /// This is useful for deprecating file descriptors.
49    pub const LOWEST_ALLOWED_FD: u32 = 10;
50
51    /// Creates a file descriptor constant, with respect to the minimum file descriptor.
52    macro_rules! create_fd {
53        ($(
54            #[$attr:meta]
55            pub const $name:ident: u32 = $value:expr;
56        )*) => {
57            $(
58                #[$attr]
59                pub const $name: u32 = $value + $crate::consts::fd::LOWEST_ALLOWED_FD;
60            )*
61        }
62    }
63
64    create_fd! {
65        /// The file descriptor for public values.
66        pub const FD_PUBLIC_VALUES: u32 = 3;
67
68        /// The file descriptor for hints.
69        pub const FD_HINT: u32 = 4;
70
71        /// The file descriptor through which to access `hook_ecrecover`.
72        pub const FD_ECRECOVER_HOOK: u32 = 5;
73
74        /// The file descriptor through which to access `hook_ed_decompress`.
75        pub const FD_EDDECOMPRESS: u32 = 6;
76
77        /// The file descriptor through which to access `hook_rsa_mul_mod`.
78        pub const FD_RSA_MUL_MOD: u32 = 7;
79
80        /// The file descriptor through which to access `hook_bls12_381_sqrt`.
81        pub const FD_BLS12_381_SQRT: u32 = 8;
82
83        /// The file descriptor through which to access `hook_bls12_381_inverse`.
84        pub const FD_BLS12_381_INVERSE: u32 = 9;
85
86        /// The file descriptor through which to access `hook_fp_sqrt`.
87        pub const FD_FP_SQRT: u32 = 10;
88
89        /// The file descriptor through which to access `hook_fp_inverse`.
90        pub const FD_FP_INV: u32 = 11;
91    }
92}
93
94/// Converts a slice of words to a byte vector in little endian.
95pub fn words_to_bytes_le_vec<'a>(words: impl IntoIterator<Item = &'a u64>) -> Vec<u8> {
96    words.into_iter().flat_map(|word| word.to_le_bytes().into_iter()).collect::<Vec<_>>()
97}
98
99/// Converts a slice of words to a slice of bytes in little endian.
100pub fn words_to_bytes_le<'a, const B: usize>(words: impl IntoIterator<Item = &'a u64>) -> [u8; B] {
101    let mut iter = words.into_iter().flat_map(|word| word.to_le_bytes().into_iter());
102    core::array::from_fn(|_| iter.next().unwrap())
103}
104
105/// Converts a byte array in little endian to a slice of words.
106pub fn bytes_to_words_le<const W: usize>(bytes: &[u8]) -> [u64; W] {
107    debug_assert_eq!(bytes.len(), W * 8);
108    let mut iter = bytes.chunks_exact(8).map(|chunk| u64::from_le_bytes(chunk.try_into().unwrap()));
109    core::array::from_fn(|_| iter.next().unwrap())
110}
111
112/// Converts a byte array in little endian to a vector of words.
113pub fn bytes_to_words_le_vec(bytes: &[u8]) -> Vec<u64> {
114    bytes
115        .chunks_exact(8)
116        .map(|chunk| u64::from_le_bytes(chunk.try_into().unwrap()))
117        .collect::<Vec<_>>()
118}
119
120// Converts a num to a string with commas every 3 digits.
121pub fn num_to_comma_separated<T: ToString>(value: T) -> String {
122    value
123        .to_string()
124        .chars()
125        .rev()
126        .collect::<Vec<_>>()
127        .chunks(3)
128        .map(|chunk| chunk.iter().collect::<String>())
129        .collect::<Vec<_>>()
130        .join(",")
131        .chars()
132        .rev()
133        .collect()
134}
135
136/// Converts a little endian u32 array into u64 array.
137pub fn u32_to_u64(limbs: &[u32]) -> Vec<u64> {
138    debug_assert!(limbs.len().is_multiple_of(2), "need an even number of u32s");
139    limbs.chunks_exact(2).map(|pair| (pair[0] as u64) | ((pair[1] as u64) << 32)).collect()
140}
141
142/// Converts a little endian u64 array into u32 array.
143pub fn u64_to_u32<'a>(limbs: impl IntoIterator<Item = &'a u64>) -> Vec<u32> {
144    limbs
145        .into_iter()
146        .flat_map(|x| {
147            let lo = *x as u32;
148            let hi = (*x >> 32) as u32;
149            [lo, hi]
150        })
151        .collect()
152}
153
154/// Converts a 32-bit integer to a pair of 16-bit integers.
155pub fn u32_to_u16_limbs(value: u32) -> [u16; 2] {
156    [(value & 0xFFFF) as u16, (value >> 16) as u16]
157}
158
159/// Converts a 64-bit integer to four 16-bit integers.
160pub fn u64_to_u16_limbs(value: u64) -> [u16; 4] {
161    [(value & 0xFFFF) as u16, (value >> 16) as u16, (value >> 32) as u16, (value >> 48) as u16]
162}
163
164// Utility function to split a 64 bit page index into 3 limbs sized 4 bit, 16 bit, and 16 bit (least
165// significant first).
166pub fn split_page_idx(page_idx: u64) -> [u16; 3] {
167    [(page_idx & 0xF) as u16, ((page_idx >> 4) & 0xFFFF) as u16, ((page_idx >> 20) & 0xFFFF) as u16]
168}