Add Serialize, Deserialize to PublicKey.

This commit is contained in:
Henry de Valence 2019-12-09 11:22:39 -08:00
parent e58376fc47
commit 2ca445ad23
2 changed files with 29 additions and 0 deletions

View File

@ -36,6 +36,10 @@ impl<T: SigType> From<PublicKeyBytes<T>> 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<T>"))]
#[cfg_attr(feature = "serde", serde(into = "PublicKeyBytes<T>"))]
#[cfg_attr(feature = "serde", serde(bound = "T: SigType"))]
pub struct PublicKey<T: SigType> {
// XXX-jubjub: this should just be Point
pub(crate) point: jubjub::ExtendedPoint,

View File

@ -25,6 +25,31 @@ proptest! {
assert_eq!(&bytes[..], &bytes_from[..]);
}
#[test]
fn publickey_serialization(
bytes in prop::array::uniform32(any::<u8>()),
) {
let pk_result_try_from = PublicKey::<SpendAuth>::try_from(bytes);
let pk_result_bincode: Result<PublicKey::<SpendAuth>, _>
= 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::<u8>()),