diff --git a/zebra-chain/src/addresses/sapling.rs b/zebra-chain/src/addresses/sapling.rs index 28fc77bb1..08f8054fb 100644 --- a/zebra-chain/src/addresses/sapling.rs +++ b/zebra-chain/src/addresses/sapling.rs @@ -144,7 +144,7 @@ mod tests { let authorizing_key = sapling::AuthorizingKey::from(spend_authorizing_key); let nullifier_deriving_key = sapling::NullifierDerivingKey::from(proof_authorizing_key); let incoming_viewing_key = - sapling::IncomingViewingKey::from_keys(authorizing_key, nullifier_deriving_key); + sapling::IncomingViewingKey::from((authorizing_key, nullifier_deriving_key)); let diversifier = sapling::Diversifier::new(&mut OsRng); let transmission_key = sapling::TransmissionKey::from(incoming_viewing_key, diversifier); diff --git a/zebra-chain/src/keys/sapling.rs b/zebra-chain/src/keys/sapling.rs index b587041df..7dcf81ed3 100644 --- a/zebra-chain/src/keys/sapling.rs +++ b/zebra-chain/src/keys/sapling.rs @@ -514,6 +514,25 @@ impl From<[u8; 32]> for IncomingViewingKey { } } +impl From<(AuthorizingKey, NullifierDerivingKey)> for IncomingViewingKey { + /// For this invocation of Blake2s-256 as _CRH^ivk_. + /// + /// https://zips.z.cash/protocol/protocol.pdf#saplingkeycomponents + /// https://zips.z.cash/protocol/protocol.pdf#concreteprfs + /// https://zips.z.cash/protocol/protocol.pdf#jubjub + // TODO: return None if ivk = 0 + // + // "If ivk = 0, discard this key and start over with a new + // [spending key]." - [§4.2.2][ps] + // + // [ps]: https://zips.z.cash/protocol/protocol.pdf#saplingkeycomponents + fn from((ask, nk): (AuthorizingKey, NullifierDerivingKey)) -> Self { + let hash_bytes = crh_ivk(ask.into(), nk.to_bytes()); + + IncomingViewingKey::from(hash_bytes) + } +} + impl From for [u8; 32] { fn from(ivk: IncomingViewingKey) -> [u8; 32] { ivk.scalar.to_bytes() @@ -563,30 +582,6 @@ impl std::str::FromStr for IncomingViewingKey { } } -impl IncomingViewingKey { - /// For this invocation of Blake2s-256 as _CRH^ivk_. - /// - /// https://zips.z.cash/protocol/protocol.pdf#saplingkeycomponents - /// https://zips.z.cash/protocol/protocol.pdf#concreteprfs - /// https://zips.z.cash/protocol/protocol.pdf#jubjub - // TODO: return None if ivk = 0 - // - // "If ivk = 0, discard this key and start over with a new - // [spending key]." - [§4.2.2][ps] - // - // [ps]: https://zips.z.cash/protocol/protocol.pdf#saplingkeycomponents - // - // TODO: won't let me name this `from(arg1, arg2)` when I have From impl'd above? - pub fn from_keys( - authorizing_key: AuthorizingKey, - nullifier_deriving_key: NullifierDerivingKey, - ) -> Self { - let hash_bytes = crh_ivk(authorizing_key.into(), nullifier_deriving_key.to_bytes()); - - IncomingViewingKey::from(hash_bytes) - } -} - /// A _Diversifier_, as described in [protocol specification §4.2.2][ps]. /// /// Combined with an _IncomingViewingKey_, produces a _diversified diff --git a/zebra-chain/src/keys/sapling/tests.rs b/zebra-chain/src/keys/sapling/tests.rs index 3468b8523..640d7ed0b 100644 --- a/zebra-chain/src/keys/sapling/tests.rs +++ b/zebra-chain/src/keys/sapling/tests.rs @@ -1,7 +1,5 @@ #[cfg(test)] use proptest::{array, prelude::*}; -#[cfg(test)] -use proptest_derive::Arbitrary; use super::*; @@ -45,7 +43,7 @@ mod tests { // [spending key]." // https://zips.z.cash/protocol/protocol.pdf#saplingkeycomponents let incoming_viewing_key = - IncomingViewingKey::from_keys(authorizing_key, nullifier_deriving_key); + IncomingViewingKey::from((authorizing_key, nullifier_deriving_key)); let diversifier = Diversifier::new(&mut OsRng); let _transmission_key = TransmissionKey::from(incoming_viewing_key, diversifier); @@ -81,7 +79,7 @@ mod tests { test_vector.nk ); let incoming_viewing_key = - IncomingViewingKey::from_keys(authorizing_key, nullifier_deriving_key); + IncomingViewingKey::from((authorizing_key, nullifier_deriving_key)); assert_eq!(incoming_viewing_key.scalar.to_bytes(), test_vector.ivk); // TODO: replace with _DefaultDiversifier_ with spending