Skip to main content

sp1_primitives/
types.rs

1use alloc::{sync::Arc, vec::Vec};
2use serde::{de::DeserializeOwned, Deserialize, Serialize};
3
4#[derive(Debug, Clone, Copy)]
5pub enum RecursionProgramType {
6    Core,
7    Deferred,
8    Compress,
9    Shrink,
10    Wrap,
11}
12
13/// A buffer of serializable/deserializable objects.                                              
14#[derive(Debug, Clone, Serialize, Deserialize)]
15pub struct Buffer {
16    pub data: Vec<u8>,
17    #[serde(skip)]
18    pub ptr: usize,
19}
20
21impl Buffer {
22    pub const fn new() -> Self {
23        Self { data: Vec::new(), ptr: 0 }
24    }
25
26    pub fn from(data: &[u8]) -> Self {
27        Self { data: data.to_vec(), ptr: 0 }
28    }
29
30    /// Set the position ptr to the beginning of the buffer.                                      
31    pub fn head(&mut self) {
32        self.ptr = 0;
33    }
34
35    /// Read the serializable object from the buffer.                                             
36    pub fn read<T: Serialize + DeserializeOwned>(&mut self) -> T {
37        let result: T =
38            bincode::deserialize(&self.data[self.ptr..]).expect("failed to deserialize");
39        let nb_bytes = bincode::serialized_size(&result).expect("failed to get serialized size");
40        self.ptr += nb_bytes as usize;
41        result
42    }
43
44    pub fn read_slice(&mut self, slice: &mut [u8]) {
45        slice.copy_from_slice(&self.data[self.ptr..self.ptr + slice.len()]);
46        self.ptr += slice.len();
47    }
48
49    /// Write the serializable object from the buffer.                                            
50    pub fn write<T: Serialize>(&mut self, data: &T) {
51        let mut tmp = Vec::new();
52        bincode::serialize_into(&mut tmp, data).expect("serialization failed");
53        self.data.extend(tmp);
54    }
55
56    /// Write the slice of bytes to the buffer.                                                   
57    pub fn write_slice(&mut self, slice: &[u8]) {
58        self.data.extend_from_slice(slice);
59    }
60}
61
62impl Default for Buffer {
63    fn default() -> Self {
64        Self::new()
65    }
66}
67
68/// A type that represents an ELF binary, always cheap to clone.
69#[derive(Debug, Clone)]
70pub enum Elf {
71    /// The ELF binary for the program.
72    Static(&'static [u8]),
73    /// The ELF binary for the test program.
74    Dynamic(Arc<[u8]>),
75}
76
77// todo!(n): implement serde for the ELF type.
78
79impl From<Arc<[u8]>> for Elf {
80    fn from(elf: Arc<[u8]>) -> Self {
81        Self::Dynamic(elf)
82    }
83}
84
85impl From<Vec<u8>> for Elf {
86    fn from(elf: Vec<u8>) -> Self {
87        Self::Dynamic(elf.into())
88    }
89}
90
91impl From<&[u8]> for Elf {
92    fn from(elf: &[u8]) -> Self {
93        Self::Dynamic(elf.into())
94    }
95}
96
97impl core::ops::Deref for Elf {
98    type Target = [u8];
99
100    fn deref(&self) -> &Self::Target {
101        match self {
102            Self::Static(elf) => elf,
103            Self::Dynamic(elf) => elf.as_ref(),
104        }
105    }
106}