Add Serialize, Deserialize for byte wrapper types.
This commit is contained in:
parent
798a3e4631
commit
e58376fc47
|
@ -9,12 +9,15 @@ rand_core = "0.5"
|
|||
thiserror = "1.0"
|
||||
blake2b_simd = "0.5"
|
||||
jubjub = "0.3"
|
||||
serde = { version = "1", optional = true, features = ["derive"] }
|
||||
|
||||
[dev-dependencies]
|
||||
rand = "0.7"
|
||||
rand_chacha = "0.2"
|
||||
proptest = "0.9"
|
||||
lazy_static = "1.4"
|
||||
bincode = "1"
|
||||
|
||||
[features]
|
||||
nightly = []
|
||||
default = ["serde"]
|
||||
|
|
|
@ -9,6 +9,7 @@ use crate::{Error, Randomizer, Scalar, SigType, Signature, SpendAuth};
|
|||
/// [`PublicKey`] type in this library holds other decompressed state
|
||||
/// used in signature verification.
|
||||
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
|
||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||
pub struct PublicKeyBytes<T: SigType> {
|
||||
pub(crate) bytes: [u8; 32],
|
||||
pub(crate) _marker: PhantomData<T>,
|
||||
|
|
|
@ -4,6 +4,7 @@ use crate::SigType;
|
|||
|
||||
/// A RedJubJub signature.
|
||||
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
|
||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||
pub struct Signature<T: SigType> {
|
||||
pub(crate) r_bytes: [u8; 32],
|
||||
pub(crate) s_bytes: [u8; 32],
|
||||
|
|
|
@ -0,0 +1,56 @@
|
|||
use std::convert::TryFrom;
|
||||
|
||||
use proptest::prelude::*;
|
||||
|
||||
use redjubjub_zebra::*;
|
||||
|
||||
proptest! {
|
||||
#[test]
|
||||
fn publickeybytes_serialization(
|
||||
bytes in prop::array::uniform32(any::<u8>()),
|
||||
) {
|
||||
let pk_bytes_from = PublicKeyBytes::<SpendAuth>::from(bytes);
|
||||
let pk_bytes_bincode: PublicKeyBytes::<SpendAuth>
|
||||
= bincode::deserialize(&bytes[..]).unwrap();
|
||||
|
||||
// Check 1: both decoding methods should have the same result.
|
||||
assert_eq!(pk_bytes_from, pk_bytes_bincode);
|
||||
|
||||
// Check 2: bincode encoding should match original bytes.
|
||||
let bytes_bincode = bincode::serialize(&pk_bytes_from).unwrap();
|
||||
assert_eq!(&bytes[..], &bytes_bincode[..]);
|
||||
|
||||
// Check 3: From encoding should match original bytes.
|
||||
let bytes_from: [u8; 32] = pk_bytes_bincode.into();
|
||||
assert_eq!(&bytes[..], &bytes_from[..]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn signature_serialization(
|
||||
lo in prop::array::uniform32(any::<u8>()),
|
||||
hi in prop::array::uniform32(any::<u8>()),
|
||||
) {
|
||||
// array length hack
|
||||
let bytes = {
|
||||
let mut bytes = [0; 64];
|
||||
bytes[0..32].copy_from_slice(&lo[..]);
|
||||
bytes[32..64].copy_from_slice(&hi[..]);
|
||||
bytes
|
||||
};
|
||||
|
||||
let sig_bytes_from = Signature::<SpendAuth>::from(bytes);
|
||||
let sig_bytes_bincode: Signature::<SpendAuth>
|
||||
= bincode::deserialize(&bytes[..]).unwrap();
|
||||
|
||||
// Check 1: both decoding methods should have the same result.
|
||||
assert_eq!(sig_bytes_from, sig_bytes_bincode);
|
||||
|
||||
// Check 2: bincode encoding should match original bytes.
|
||||
let bytes_bincode = bincode::serialize(&sig_bytes_from).unwrap();
|
||||
assert_eq!(&bytes[..], &bytes_bincode[..]);
|
||||
|
||||
// Check 3: From encoding should match original bytes.
|
||||
let bytes_from: [u8; 64] = sig_bytes_bincode.into();
|
||||
assert_eq!(&bytes[..], &bytes_from[..]);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue