Derive Sprout ReceivingKey's from SpendingKey's via SHA256Compress

Test is incomplete, also the type aliases block us from impl'ing Debug or Display.
This commit is contained in:
Deirdre Connolly 2020-03-24 04:49:27 -04:00 committed by Deirdre Connolly
parent bba58807bb
commit ee32de2b86
3 changed files with 40 additions and 2 deletions

1
Cargo.lock generated
View File

@ -2077,6 +2077,7 @@ dependencies = [
"lazy_static",
"proptest",
"proptest-derive",
"rand_core 0.5.1",
"redjubjub",
"ripemd160",
"secp256k1",

View File

@ -14,6 +14,7 @@ chrono = "0.4"
futures = "0.3"
hex = "0.4"
lazy_static = "1.4.0"
rand_core = "0.5.1"
ripemd160 = "0.8.0"
secp256k1 = { version = "0.17.2", features = ["serde"] }
serde = { version = "1", features = ["serde_derive"] }

View File

@ -11,6 +11,8 @@ use std::{
io::{self},
};
use byteorder::{ByteOrder, LittleEndian};
#[cfg(test)]
use proptest::{array, collection::vec, prelude::*};
#[cfg(test)]
@ -24,6 +26,7 @@ use crate::serialization::{SerializationError, ZcashDeserialize, ZcashSerialize}
///
/// All other Sprout key types derive from the SpendingKey value.
/// Actually 252 bits.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct SpendingKey(pub [u8; 32]);
/// Derived from a _SpendingKey_.
@ -31,12 +34,23 @@ pub type ReceivingKey = x25519_dalek::StaticSecret;
impl From<SpendingKey> for ReceivingKey {
fn from(spending_key: SpendingKey) -> ReceivingKey {
ReceivingKey::from(spending_key.0)
let mut state = [0u32; 8];
let mut block = [0u8; 64];
block[0..32].copy_from_slice(&spending_key.0[..]);
block[0] |= 0b11000000;
compress256(&mut state, &block);
let mut derived_bytes = [0u8; 32];
LittleEndian::write_u32_into(&state, &mut derived_bytes);
ReceivingKey::from(derived_bytes)
}
}
/// Derived from a _SpendingKey_.
#[derive(Copy, Clone, Eq, PartialEq)]
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct PayingKey(pub [u8; 32]);
/// Derived from a _ReceivingKey_.
@ -48,6 +62,28 @@ pub struct IncomingViewingKey {
receiving_key: ReceivingKey,
}
#[cfg(test)]
mod tests {
use rand_core::{OsRng, RngCore};
use super::*;
#[test]
fn derive_receiving_key() {
let mut bytes = [0u8; 32];
OsRng.fill_bytes(&mut bytes);
let spending_key = SpendingKey(bytes);
println!("{:?}", spending_key);
let receiving_key = ReceivingKey::from(spending_key);
// println!("{}", receiving_key);
}
}
#[cfg(test)]
proptest! {