From aeb811baeb7359f313fc514a30038ca8664353fe Mon Sep 17 00:00:00 2001 From: saitima Date: Thu, 14 Dec 2023 17:32:00 +0100 Subject: [PATCH 1/4] Vec and Vec are MemcopySerializeable --- src/cs/implementations/fast_serialization.rs | 61 ++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/src/cs/implementations/fast_serialization.rs b/src/cs/implementations/fast_serialization.rs index 732bf37..c9feaed 100644 --- a/src/cs/implementations/fast_serialization.rs +++ b/src/cs/implementations/fast_serialization.rs @@ -207,6 +207,67 @@ where } } +impl MemcopySerializable for Vec +where + Self: 'static, +{ + fn write_into_buffer(&self, mut dst: W) -> Result<(), Box> { + 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::(); + 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(mut src: R) -> Result> { + 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::(); + let mut result: Vec = Vec::with_capacity_in(capacity, A::default()); + let mut 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 MemcopySerializable for Vec +where + Self: 'static, +{ + fn write_into_buffer(&self, mut dst: W) -> Result<(), Box> { + 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::(); + 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(mut src: R) -> Result> { + 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::(); + let mut result: Vec = Vec::with_capacity_in(capacity, A::default()); + let mut 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 MemcopySerializable for Vec<[F; N], A> where Self: 'static, From 7089201f9c2fb192deb73bc98f5c3c12f43ad3c4 Mon Sep 17 00:00:00 2001 From: saitima Date: Thu, 14 Dec 2023 17:37:57 +0100 Subject: [PATCH 2/4] use unnoticed Results --- src/cs/implementations/fast_serialization.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cs/implementations/fast_serialization.rs b/src/cs/implementations/fast_serialization.rs index c9feaed..295446b 100644 --- a/src/cs/implementations/fast_serialization.rs +++ b/src/cs/implementations/fast_serialization.rs @@ -231,7 +231,7 @@ where let mut result: Vec = Vec::with_capacity_in(capacity, A::default()); let mut tmp = unsafe { std::slice::from_raw_parts_mut(result.as_mut_ptr().cast(), num_bytes) }; - src.read_exact(tmp).map_err(Box::new); + src.read_exact(tmp).map_err(Box::new)?; unsafe { result.set_len(capacity) }; Ok(result) } @@ -261,7 +261,7 @@ where let mut result: Vec = Vec::with_capacity_in(capacity, A::default()); let mut tmp = unsafe { std::slice::from_raw_parts_mut(result.as_mut_ptr().cast(), num_bytes) }; - src.read_exact(tmp).map_err(Box::new); + src.read_exact(tmp).map_err(Box::new)?; unsafe { result.set_len(capacity) }; Ok(result) From 8b3901ec5d69bc4a50d6ccd5ff92857cbcde2fb1 Mon Sep 17 00:00:00 2001 From: saitima Date: Thu, 14 Dec 2023 17:40:11 +0100 Subject: [PATCH 3/4] immutable tmps --- src/cs/implementations/fast_serialization.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/cs/implementations/fast_serialization.rs b/src/cs/implementations/fast_serialization.rs index 295446b..ea7301c 100644 --- a/src/cs/implementations/fast_serialization.rs +++ b/src/cs/implementations/fast_serialization.rs @@ -229,8 +229,7 @@ where let num_bytes = capacity * std::mem::size_of::(); let mut result: Vec = Vec::with_capacity_in(capacity, A::default()); - let mut tmp = - unsafe { std::slice::from_raw_parts_mut(result.as_mut_ptr().cast(), num_bytes) }; + 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) @@ -259,8 +258,7 @@ where let num_bytes = capacity * std::mem::size_of::(); let mut result: Vec = Vec::with_capacity_in(capacity, A::default()); - let mut tmp = - unsafe { std::slice::from_raw_parts_mut(result.as_mut_ptr().cast(), num_bytes) }; + 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) }; From d1f46fb0495cf8ef8b8af2e4203b684f8f0264a2 Mon Sep 17 00:00:00 2001 From: saitima Date: Sun, 24 Dec 2023 17:34:41 +0100 Subject: [PATCH 4/4] fix serialization for dense copy hints --- src/cs/implementations/hints/mod.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/cs/implementations/hints/mod.rs b/src/cs/implementations/hints/mod.rs index c26a2f1..65c6039 100644 --- a/src/cs/implementations/hints/mod.rs +++ b/src/cs/implementations/hints/mod.rs @@ -50,8 +50,9 @@ impl MemcopySerializable for Vec { 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::(); 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)?; @@ -90,8 +91,9 @@ impl MemcopySerializable for Vec { 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::(); 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)?;