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