Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add serde for BoxedUInt #587

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
3 changes: 3 additions & 0 deletions src/uint/boxed/encoding.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
//! Const-friendly decoding operations for [`BoxedUint`].

#[cfg(feature = "serde")]
mod serde;

use super::BoxedUint;
use crate::{uint::encoding, Limb, Word};
use alloc::boxed::Box;
Expand Down
78 changes: 78 additions & 0 deletions src/uint/boxed/encoding/serde.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
//! Support for serdect on [`BoxedUint`]

use serdect::serde::{de, Deserialize, Deserializer, Serialize, Serializer};

use crate::BoxedUint;

impl<'de> Deserialize<'de> for BoxedUint {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let mut buffer = Self::zero().to_le_bytes();
serdect::array::deserialize_hex_or_bin(buffer.as_mut(), deserializer)?;
Copy link
Author

@pinkforest pinkforest Apr 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't work because it doesn't know the size and zero() doesn't size it for the incoming buf
Having some weird errors from the alloc variant

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to use serdect::slice if the size isn't known a priori

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm I thought I tried (the alloc variant) that and it gave some weird runtime behaviour


let bits_in = buffer.len() * 8;

if bits_in > u32::MAX as usize {
return Err(de::Error::custom(
"Deserialize input overflows BoxedUint u32 bits length",
));
}

Self::from_le_slice(&buffer, bits_in as u32)
.map_err(|_| de::Error::custom("Deserialize error"))
}
}

#[cfg(feature = "serde")]
impl Serialize for BoxedUint {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serdect::array::serialize_hex_lower_or_bin(&self.to_le_bytes(), serializer)
}
}

#[cfg(test)]
mod tests {
use super::BoxedUint;

#[test]
#[cfg(target_pointer_width = "64")]
fn serde() {
let test: BoxedUint = BoxedUint::from_be_hex("7711223344556600", 64).unwrap();

let serialized = bincode::serialize(&test).unwrap();
let deserialized: BoxedUint = bincode::deserialize(&serialized).unwrap();

assert_eq!(test, deserialized);
}

#[test]
#[cfg(target_pointer_width = "64")]
fn serde_owned() {
let test: BoxedUint = BoxedUint::from_be_hex("7711223344556600", 64).unwrap();

let serialized = bincode::serialize(&test).unwrap();
let deserialized: BoxedUint = bincode::deserialize_from(serialized.as_slice()).unwrap();

assert_eq!(test, deserialized);
}

#[test]
#[cfg(target_pointer_width = "32")]
fn from_le_slice_eq() {
let test = hex!("7766554433221100");
let box_uint = BoxedUint::from_le_slice(&bytes, 64).unwrap();

let serialized = bincode::serialize(&box_uint).unwrap();
let deserialized: BoxedUint = bincode::deserialize_from(serialized.as_slice()).unwrap();

assert_eq!(
deserialized.as_limbs(),
&[Limb(0x44556677), Limb(0x00112233)]
);
}
}
Loading