sp1_primitives/
consts.rs

1/// The maximum size of the memory in bytes.
2pub const MAXIMUM_MEMORY_SIZE: u32 = u32::MAX;
3
4/// The number of bits in a byte.
5pub const BYTE_SIZE: usize = 8;
6
7/// The size of a word in bytes.
8pub const WORD_SIZE: usize = 4;
9
10/// The number of bytes necessary to represent a 64-bit integer.
11pub const LONG_WORD_SIZE: usize = 2 * WORD_SIZE;
12
13/// The Baby Bear prime.
14pub const BABYBEAR_PRIME: u32 = 0x78000001;
15
16pub mod fd {
17    /// The minimum file descriptor.
18    ///
19    /// Any file descriptor must be greater than this value, otherwise the executor will panic.
20    ///
21    /// This is useful for deprecating file descriptors.
22    pub const LOWEST_ALLOWED_FD: u32 = 10;
23
24    /// Creates a file descriptor constant, with respect to the minimum file descriptor.
25    macro_rules! create_fd {
26        ($(
27            #[$attr:meta]
28            pub const $name:ident: u32 = $value:expr;
29        )*) => {
30            $(
31                #[$attr]
32                pub const $name: u32 = $value + $crate::consts::fd::LOWEST_ALLOWED_FD;
33            )*
34        }
35    }
36
37    create_fd! {
38        /// The file descriptor for public values.
39        pub const FD_PUBLIC_VALUES: u32 = 3;
40
41        /// The file descriptor for hints.
42        pub const FD_HINT: u32 = 4;
43
44        /// The file descriptor through which to access `hook_ecrecover`.
45        pub const FD_ECRECOVER_HOOK: u32 = 5;
46
47        /// The file descriptor through which to access `hook_ed_decompress`.
48        pub const FD_EDDECOMPRESS: u32 = 6;
49
50        /// The file descriptor through which to access `hook_rsa_mul_mod`.
51        pub const FD_RSA_MUL_MOD: u32 = 7;
52
53        /// The file descriptor through which to access `hook_bls12_381_sqrt`.
54        pub const FD_BLS12_381_SQRT: u32 = 8;
55
56        /// The file descriptor through which to access `hook_bls12_381_inverse`.
57        pub const FD_BLS12_381_INVERSE: u32 = 9;
58
59        /// The file descriptor through which to access `hook_fp_sqrt`.
60        pub const FD_FP_SQRT: u32 = 10;
61
62        /// The file descriptor through which to access `hook_fp_inverse`.
63        pub const FD_FP_INV: u32 = 11;
64    }
65}
66
67/// Converts a slice of words to a byte vector in little endian.
68pub fn words_to_bytes_le_vec(words: &[u32]) -> Vec<u8> {
69    words.iter().flat_map(|word| word.to_le_bytes().into_iter()).collect::<Vec<_>>()
70}
71
72/// Converts a slice of words to a slice of bytes in little endian.
73pub fn words_to_bytes_le<const B: usize>(words: &[u32]) -> [u8; B] {
74    debug_assert_eq!(words.len() * 4, B);
75    let mut iter = words.iter().flat_map(|word| word.to_le_bytes().into_iter());
76    core::array::from_fn(|_| iter.next().unwrap())
77}
78
79/// Converts a byte array in little endian to a slice of words.
80pub fn bytes_to_words_le<const W: usize>(bytes: &[u8]) -> [u32; W] {
81    debug_assert_eq!(bytes.len(), W * 4);
82    let mut iter = bytes.chunks_exact(4).map(|chunk| u32::from_le_bytes(chunk.try_into().unwrap()));
83    core::array::from_fn(|_| iter.next().unwrap())
84}
85
86/// Converts a byte array in little endian to a vector of words.
87pub fn bytes_to_words_le_vec(bytes: &[u8]) -> Vec<u32> {
88    bytes
89        .chunks_exact(4)
90        .map(|chunk| u32::from_le_bytes(chunk.try_into().unwrap()))
91        .collect::<Vec<_>>()
92}
93
94// Converts a num to a string with commas every 3 digits.
95pub fn num_to_comma_separated<T: ToString>(value: T) -> String {
96    value
97        .to_string()
98        .chars()
99        .rev()
100        .collect::<Vec<_>>()
101        .chunks(3)
102        .map(|chunk| chunk.iter().collect::<String>())
103        .collect::<Vec<_>>()
104        .join(",")
105        .chars()
106        .rev()
107        .collect()
108}