Skip to content
Closed
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
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)?;

@pinkforest pinkforest Apr 6, 2024

Copy link
Copy Markdown
Author

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
Copy Markdown
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
Copy Markdown
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)]
);
}
}