-
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
Serde implementation #73
Conversation
I've been meaning to extract a I would definitely prefer not having to pull in crates like |
The reason why I was trying to avoid using slices is because when using Alternatively, I could just copy the code of The main reason I made this PR is because currently Preferably I would like to have this merged until we have |
This has been bothering me enough I'll go ahead and spike out There are a lot of conflicting concerns that go into I'm a bit surprised to hear that I think we can get away with not copying and pasting code from |
I honestly don't know enough about Serde to argue with you, but just to clarify, compilation isn't the problem, it just throws an error during runtime. I will make a small test to exemplify this. |
@daxpedda are you sure the issue wasn't fixed by RustCrypto/elliptic-curves#536 ? I think the most likely issue, especially if it's occurring at runtime, is the input format decoder doesn't allow borrowing from the input. That's the sort of concern it'd be really nice to solve in a single place like |
[dependencies]
bincode = "1"
# Can't compile without `pem`, this is a bug.
elliptic-curve = { version = "0.12.0-pre.1", features = ["pem"] }
p256 = { version = "0.11.0-pre.0", features = ["serde"] }
serde = "1"
[patch.crates-io]
elliptic-curve = { git = "https://github.com/RustCrypto/traits" } use p256::Scalar;
fn main() {
let scalar = Scalar::ZERO;
let serialized = bincode::serialize(&scalar).unwrap();
let deserialized: Scalar = bincode::deserialize_from(serialized.as_slice()).unwrap();
assert_eq!(scalar, deserialized);
}
Same error on the latest version on crates.io and master. |
The issue there is most likely that the Again, this is why it'd be great to have the deserializer/serializer types in one place so we can test them across multiple Right now all |
<&[u8]>::deserialize(deserializer)?; I think so too, this here just doesn't work. As far as I'm aware there is no way to avoid implementing a
I'm happy to contribute 😃 |
Initial implementation of |
@daxpedda you can use this now: https://crates.io/crates/serdect/0.1.0 |
@@ -20,7 +20,7 @@ macro_rules! impl_uint_aliases { | |||
} | |||
|
|||
fn from_le_bytes(bytes: Self::Repr) -> Self { | |||
Self::from_be_slice(&bytes) |
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.
Bug I found on the way. We need to test Encoding
.
where | ||
D: Deserializer<'de>, | ||
{ | ||
let mut buffer = Self::ZERO.to_le_bytes(); |
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.
Couldn't come up with a cleaner way.
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.
Yeah, it's kind of tough. I think GenericArray::default()
would work if you use Self::from_le_byte_array
and the ArrayEncoding
trait.
Otherwise there's UInt::<Limbs>::Repr::default()
but unfortunately those impls only go up to 32-bytes, so not only would you need a Default
bound to use them but also it would limit the serializable size to U256
unnecessarily.
Implemented
serde::Deserialize
andserde::Serialize
for all types. This might be a bit overkill, implementing it forUInt
might have been enough.As serde currently doesn't support const generics (serde-rs/serde#1937), I used serde-big-array for the
UInt
implementation. The dependency is quiet minimal, this PR also spawned est31/serde-big-array#14 to make sure that the dependency is as minimal as possible.I hope the tests are appropriate enough.