diff --git a/src/public_key.rs b/src/public_key.rs index a303b71..db8490d 100644 --- a/src/public_key.rs +++ b/src/public_key.rs @@ -36,6 +36,10 @@ impl From> for [u8; 32] { /// public key may not be used immediately, it is probably better to use /// [`PublicKeyBytes`], which is a refinement type for `[u8; 32]`. #[derive(Copy, Clone, Debug)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +#[cfg_attr(feature = "serde", serde(try_from = "PublicKeyBytes"))] +#[cfg_attr(feature = "serde", serde(into = "PublicKeyBytes"))] +#[cfg_attr(feature = "serde", serde(bound = "T: SigType"))] pub struct PublicKey { // XXX-jubjub: this should just be Point pub(crate) point: jubjub::ExtendedPoint, diff --git a/tests/bincode.rs b/tests/bincode.rs index e50f3b7..827107e 100644 --- a/tests/bincode.rs +++ b/tests/bincode.rs @@ -25,6 +25,31 @@ proptest! { assert_eq!(&bytes[..], &bytes_from[..]); } + #[test] + fn publickey_serialization( + bytes in prop::array::uniform32(any::()), + ) { + let pk_result_try_from = PublicKey::::try_from(bytes); + let pk_result_bincode: Result, _> + = bincode::deserialize(&bytes[..]); + + // Check 1: both decoding methods should have the same result + match (pk_result_try_from, pk_result_bincode) { + // Both agree on success + (Ok(pk_try_from), Ok(pk_bincode)) => { + // Check 2: bincode encoding should match original bytes + let bytes_bincode = bincode::serialize(&pk_try_from).unwrap(); + assert_eq!(&bytes[..], &bytes_bincode[..]); + // Check 3: From encoding should match original bytes + let bytes_from: [u8; 32] = pk_bincode.into(); + assert_eq!(&bytes[..], &bytes_from[..]); + }, + // Both agree on failure + (Err(_), Err(_)) => {}, + _ => panic!("bincode and try_from do not agree"), + } + } + #[test] fn signature_serialization( lo in prop::array::uniform32(any::()),