Add Serialize, Deserialize for SecretKey.
This commit is contained in:
parent
2ca445ad23
commit
87f09b87b5
|
@ -6,6 +6,10 @@ use rand_core::{CryptoRng, RngCore};
|
|||
|
||||
/// A RedJubJub secret key.
|
||||
#[derive(Copy, Clone, Debug)]
|
||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||
#[cfg_attr(feature = "serde", serde(from = "SerdeHelper"))]
|
||||
#[cfg_attr(feature = "serde", serde(into = "SerdeHelper"))]
|
||||
#[cfg_attr(feature = "serde", serde(bound = "T: SigType"))]
|
||||
pub struct SecretKey<T: SigType> {
|
||||
sk: Scalar,
|
||||
pk: PublicKey<T>,
|
||||
|
@ -37,6 +41,21 @@ impl<T: SigType> From<[u8; 32]> for SecretKey<T> {
|
|||
}
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||
struct SerdeHelper([u8; 32]);
|
||||
|
||||
impl<T: SigType> From<SerdeHelper> for SecretKey<T> {
|
||||
fn from(helper: SerdeHelper) -> Self {
|
||||
helper.0.into()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: SigType> From<SecretKey<T>> for SerdeHelper {
|
||||
fn from(sk: SecretKey<T>) -> Self {
|
||||
Self(sk.into())
|
||||
}
|
||||
}
|
||||
|
||||
impl SecretKey<SpendAuth> {
|
||||
/// Randomize this public key with the given `randomizer`.
|
||||
pub fn randomize(&self, randomizer: &Randomizer) -> SecretKey<SpendAuth> {
|
||||
|
|
|
@ -5,6 +5,32 @@ use proptest::prelude::*;
|
|||
use redjubjub_zebra::*;
|
||||
|
||||
proptest! {
|
||||
#[test]
|
||||
fn secretkey_serialization(
|
||||
bytes in prop::array::uniform32(any::<u8>()),
|
||||
) {
|
||||
let sk_from = SecretKey::<SpendAuth>::from(bytes);
|
||||
let sk_bincode: SecretKey::<SpendAuth>
|
||||
= bincode::deserialize(&bytes[..]).unwrap();
|
||||
|
||||
// Check 1: both decoding methods should have the same public key
|
||||
let pk_bytes_from = PublicKeyBytes::from(PublicKey::from(&sk_from));
|
||||
let pk_bytes_bincode = PublicKeyBytes::from(PublicKey::from(&sk_bincode));
|
||||
assert_eq!(pk_bytes_from, pk_bytes_bincode);
|
||||
|
||||
// The below tests fail because we do not require canonically-encoded secret keys.
|
||||
/*
|
||||
|
||||
// Check 2: bincode encoding should match original bytes.
|
||||
let bytes_bincode = bincode::serialize(&sk_from).unwrap();
|
||||
assert_eq!(&bytes[..], &bytes_bincode[..]);
|
||||
|
||||
// Check 3: From encoding should match original bytes.
|
||||
let bytes_from: [u8; 32] = sk_bincode.into();
|
||||
assert_eq!(&bytes[..], &bytes_from[..]);
|
||||
*/
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn publickeybytes_serialization(
|
||||
bytes in prop::array::uniform32(any::<u8>()),
|
||||
|
|
Loading…
Reference in New Issue