dalek-cryptography / x25519-dalek

X25519 elliptic curve Diffie-Hellman key exchange in pure-Rust, using curve25519-dalek.
BSD 3-Clause "New" or "Revised" License
328 stars 133 forks source link

Support for serde serialize and deserialize #48

Closed DebugSteven closed 4 years ago

DebugSteven commented 4 years ago

Start of adding support for serde serialize and deserialize

DebugSteven commented 4 years ago

This was supposed to be a draft PR. There isn't a way to switch it to be a draft PR after you open one so I'll leave this open until it's complete/closed.

hdevalence commented 4 years ago

@mystor pointed out that the serde-related compilation failure happens because serde's derive by default expects the serde crate to be called serde, but that this can be overridden. I tested adding

#[cfg_attr(feature = "serde", serde(crate = "our_serde"))]

below each

#[cfg_attr(feature = "serde", derive(our_serde::Serialize, our_serde::Deserialize))]

and it resolved the problem.

hdevalence commented 4 years ago

As noted in the PR, X25519 secret keys are unreduced scalars, but the serde implementation for curve25519-dalek Scalars rejects unreduced values on deserialization. I think it would be possible to use https://serde.rs/remote-derive.html to create a private stub struct which serializes the bytes of a scalar without the reduction check.

hdevalence commented 4 years ago

This looks great!