-
Notifications
You must be signed in to change notification settings - Fork 55
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
pinkforest
wants to merge
11
commits into
RustCrypto:master
Choose a base branch
from
pinkforest:add-serde-boxeduint
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
38353e3
Add serde for BoxedUInt
pinkforest fde0c07
Plumb the right type for impl
pinkforest 2a8b746
Right encoding I think ?
pinkforest 6abfd8e
Plumb something maybe right
pinkforest 7f17c5a
Move under encoding and address other things
pinkforest 628bc5e
Re-plumb serde
pinkforest c6b7317
Unroll unrelated debug noise
pinkforest 8c58724
Documentation nit
pinkforest 3016196
Fix loading
pinkforest 77889e4
Address 32bit test variance
pinkforest e457c39
Clear clippy lint
pinkforest File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)?; | ||
|
||
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)] | ||
); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 prioriThere was a problem hiding this comment.
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