From 70e46c982655e2fbb97ad4f32c97ab0ced00685b Mon Sep 17 00:00:00 2001 From: Henry de Valence Date: Wed, 23 Oct 2019 15:55:03 -0700 Subject: [PATCH] Fill in missing Serde impl for MontgomeryPoint. --- Cargo.toml | 2 +- src/montgomery.rs | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 64aabe2..7274867 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -42,7 +42,7 @@ byteorder = { version = "^1.2.3", default-features = false, features = ["i128"] digest = { version = "0.8", default-features = false } clear_on_drop = "=0.2.3" subtle = { version = "2", default-features = false } -serde = { version = "1.0", default-features = false, optional = true } +serde = { version = "1.0", default-features = false, optional = true, features = ["derive"] } packed_simd = { version = "0.3.0", features = ["into_bits"], optional = true } [features] diff --git a/src/montgomery.rs b/src/montgomery.rs index 8b99bb5..a89de22 100644 --- a/src/montgomery.rs +++ b/src/montgomery.rs @@ -64,6 +64,7 @@ use subtle::ConstantTimeEq; /// Holds the \\(u\\)-coordinate of a point on the Montgomery form of /// Curve25519 or its twist. #[derive(Copy, Clone, Debug)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct MontgomeryPoint(pub [u8; 32]); /// Equality of `MontgomeryPoint`s is defined mod p. @@ -312,6 +313,22 @@ mod test { #[cfg(feature = "rand")] use rand_os::OsRng; + #[test] + #[cfg(feature = "serde")] + fn serde_bincode_basepoint_roundtrip() { + use bincode; + + let encoded = bincode::serialize(&constants::X25519_BASEPOINT).unwrap(); + let decoded: MontgomeryPoint = bincode::deserialize(&encoded).unwrap(); + + assert_eq!(encoded.len(), 32); + assert_eq!(decoded, constants::X25519_BASEPOINT); + + let raw_bytes = constants::X25519_BASEPOINT.as_bytes(); + let bp: MontgomeryPoint = bincode::deserialize(raw_bytes).unwrap(); + assert_eq!(bp, constants::X25519_BASEPOINT); + } + /// Test Montgomery -> Edwards on the X/Ed25519 basepoint #[test] fn basepoint_montgomery_to_edwards() {