Skip to content
This repository has been archived by the owner on Aug 15, 2024. It is now read-only.

Vec<u32, A> and Vec<u64,A> are MemcopySerializeable #26

Merged
merged 4 commits into from
Dec 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions src/cs/implementations/fast_serialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,65 @@ where
}
}

impl<A: GoodAllocator> MemcopySerializable for Vec<u32, A>
where
Self: 'static,
{
fn write_into_buffer<W: Write>(&self, mut dst: W) -> Result<(), Box<dyn Error>> {
let len_le_bytes = (self.len() as u64).to_le_bytes();
dst.write_all(&len_le_bytes).map_err(Box::new)?;

let num_bytes = self.len() * std::mem::size_of::<u32>();
let src = unsafe { slice::from_raw_parts(self.as_ptr().cast(), num_bytes) };
dst.write_all(src).map_err(Box::new)?;

Ok(())
}

fn read_from_buffer<R: Read>(mut src: R) -> Result<Self, Box<dyn Error>> {
let mut len_le_bytes = [0u8; 8];
src.read_exact(&mut len_le_bytes).map_err(Box::new)?;
let capacity = u64::from_le_bytes(len_le_bytes) as usize;

let num_bytes = capacity * std::mem::size_of::<u32>();
let mut result: Vec<u32, A> = Vec::with_capacity_in(capacity, A::default());
let tmp = unsafe { std::slice::from_raw_parts_mut(result.as_mut_ptr().cast(), num_bytes) };
src.read_exact(tmp).map_err(Box::new)?;
unsafe { result.set_len(capacity) };
Ok(result)
}
}

impl<A: GoodAllocator> MemcopySerializable for Vec<u64, A>
where
Self: 'static,
{
fn write_into_buffer<W: Write>(&self, mut dst: W) -> Result<(), Box<dyn Error>> {
let len_le_bytes = (self.len() as u64).to_le_bytes();
dst.write_all(&len_le_bytes).map_err(Box::new)?;

let num_bytes = self.len() * std::mem::size_of::<u64>();
let src = unsafe { slice::from_raw_parts(self.as_ptr().cast(), num_bytes) };
dst.write_all(src).map_err(Box::new)?;

Ok(())
}

fn read_from_buffer<R: Read>(mut src: R) -> Result<Self, Box<dyn Error>> {
let mut len_le_bytes = [0u8; 8];
src.read_exact(&mut len_le_bytes).map_err(Box::new)?;
let capacity = u64::from_le_bytes(len_le_bytes) as usize;

let num_bytes = capacity * std::mem::size_of::<u64>();
let mut result: Vec<u64, A> = Vec::with_capacity_in(capacity, A::default());
let tmp = unsafe { std::slice::from_raw_parts_mut(result.as_mut_ptr().cast(), num_bytes) };
src.read_exact(tmp).map_err(Box::new)?;
unsafe { result.set_len(capacity) };

Ok(result)
}
}

impl<F: SmallField, const N: usize, A: GoodAllocator> MemcopySerializable for Vec<[F; N], A>
where
Self: 'static,
Expand Down
6 changes: 4 additions & 2 deletions src/cs/implementations/hints/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ impl MemcopySerializable for Vec<Variable> {
let len_le_bytes = (len as u64).to_le_bytes();
dst.write_all(&len_le_bytes).map_err(Box::new)?;

let num_bytes = len * std::mem::size_of::<Variable>();
let ptr: *const u8 = self.as_ptr().cast();
let src = unsafe { std::slice::from_raw_parts(ptr, len) };
let src = unsafe { std::slice::from_raw_parts(ptr, num_bytes) };

dst.write_all(src).map_err(Box::new)?;

Expand Down Expand Up @@ -90,8 +91,9 @@ impl MemcopySerializable for Vec<Witness> {
let len_le_bytes = (len as u64).to_le_bytes();
dst.write_all(&len_le_bytes).map_err(Box::new)?;

let num_bytes = len * std::mem::size_of::<Variable>();
let ptr: *const u8 = self.as_ptr().cast();
let src = unsafe { std::slice::from_raw_parts(ptr, len) };
let src = unsafe { std::slice::from_raw_parts(ptr, num_bytes) };

dst.write_all(src).map_err(Box::new)?;

Expand Down