diff --git a/zebra-chain/src/addresses/sapling.rs b/zebra-chain/src/addresses/sapling.rs index b722f2c7..5cfbc4f3 100644 --- a/zebra-chain/src/addresses/sapling.rs +++ b/zebra-chain/src/addresses/sapling.rs @@ -71,7 +71,7 @@ impl ZcashDeserialize for SaplingShieldedAddress { Ok(SaplingShieldedAddress { diversifier: sapling::Diversifier(diversifier_bytes), - transmission_key: sapling::TransmissionKey::from_bytes(transmission_key_bytes).unwrap(), + transmission_key: sapling::TransmissionKey::from_bytes(transmission_key_bytes), }) } } @@ -99,8 +99,7 @@ impl Arbitrary for SaplingShieldedAddress { .prop_map(|(diversifier_bytes, transmission_key_bytes)| { return Self { diversifier: sapling::Diversifier(diversifier_bytes), - transmission_key: sapling::TransmissionKey::from_bytes(transmission_key_bytes) - .unwrap(), + transmission_key: sapling::TransmissionKey::from_bytes(transmission_key_bytes), }; }) .boxed() @@ -126,7 +125,7 @@ mod tests { assert_eq!( format!("{:?}", zs_addr), - "SaplingShieldedAddress { diversifier: Diversifier(\"0000000000000000000000\"), transmission_key: AffinePoint { u: 0x038c2b77a9a63b652905adb192ad7d535beef23468d12c6e6033d7aa9753a98a, v: 0x1935e5d4172dceb9ca1217c8edf23bf786d5e8babe4be50c6efab84d64d207e4 } }" + "SaplingShieldedAddress { diversifier: Diversifier(\"0000000000000000000000\"), transmission_key: TransmissionKey { u: \"8aa95397aad733606e2cd16834f2ee5b537dad92b1ad0529653ba6a9772b8c03\", v: \"e407d2644db8fa6e0ce54bbebae8d586f73bf2edc81712cab9ce2d17d4e53519\" } }" ); } diff --git a/zebra-chain/src/keys/sapling.rs b/zebra-chain/src/keys/sapling.rs index 21121645..c44a1ccf 100644 --- a/zebra-chain/src/keys/sapling.rs +++ b/zebra-chain/src/keys/sapling.rs @@ -81,9 +81,9 @@ impl From<[u8; 32]> for SpendingKey { } /// Derived from a _SpendingKey_. -pub struct SpendAuthorizationKey(Scalar); +pub struct SpendAuthorizingKey(Scalar); -impl Deref for SpendAuthorizationKey { +impl Deref for SpendAuthorizingKey { type Target = Scalar; fn deref(&self) -> &Scalar { @@ -91,13 +91,21 @@ impl Deref for SpendAuthorizationKey { } } -impl From for SpendAuthorizationKey { +impl fmt::Debug for SpendAuthorizingKey { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.debug_tuple("SpendAuthorizingKey") + .field(&hex::encode(self.to_bytes())) + .finish() + } +} + +impl From for SpendAuthorizingKey { /// Invokes Blake2b-512 as PRF^expand, t=0, to derive a - /// SpendAuthorizationKey from a SpendingKey. + /// SpendAuthorizingKey from a SpendingKey. /// /// https://zips.z.cash/protocol/protocol.pdf#saplingkeycomponents /// https://zips.z.cash/protocol/protocol.pdf#concreteprfs - fn from(spending_key: SpendingKey) -> SpendAuthorizationKey { + fn from(spending_key: SpendingKey) -> SpendAuthorizingKey { let hash_bytes = prf_expand(spending_key.0, 0); Self(Scalar::from_bytes_wide(&hash_bytes)) @@ -163,19 +171,44 @@ impl From for OutgoingViewingKey { } /// -pub type AuthorizingKey = jubjub::AffinePoint; +pub struct AuthorizingKey(pub jubjub::AffinePoint); -// impl fmt::Debug for AuthorizingKey { -// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { -// f.debug_struct("AuthorizingKey") -// .field("u", &hex::encode(&self.u)) -// .field("v", &hex::encode(&self.v)) -// .finish() -// } -// } +impl Deref for AuthorizingKey { + type Target = jubjub::AffinePoint; + + fn deref(&self) -> &jubjub::AffinePoint { + &self.0 + } +} + +impl fmt::Debug for AuthorizingKey { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.debug_struct("AuthorizingKey") + .field("u", &hex::encode(self.get_u().to_bytes())) + .field("v", &hex::encode(self.get_v().to_bytes())) + .finish() + } +} /// -pub type NullifierDerivingKey = jubjub::AffinePoint; +pub struct NullifierDerivingKey(pub jubjub::AffinePoint); + +impl Deref for NullifierDerivingKey { + type Target = jubjub::AffinePoint; + + fn deref(&self) -> &jubjub::AffinePoint { + &self.0 + } +} + +impl fmt::Debug for NullifierDerivingKey { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.debug_struct("NullifierDerivingKey") + .field("u", &hex::encode(self.get_u().to_bytes())) + .field("v", &hex::encode(self.get_v().to_bytes())) + .finish() + } +} /// pub struct IncomingViewingKey(pub Scalar); @@ -188,13 +221,13 @@ impl Deref for IncomingViewingKey { } } -// impl fmt::Debug for IncomingViewingKey { -// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { -// f.debug_tuple("IncomingViewingKey") -// .field(&hex::encode(&self.0)) -// .finish() -// } -// } +impl fmt::Debug for IncomingViewingKey { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.debug_tuple("IncomingViewingKey") + .field(&hex::encode(self.to_bytes())) + .finish() + } +} impl IncomingViewingKey { /// For this invocation of Blake2s-256 as CRH^ivk. @@ -239,7 +272,36 @@ impl fmt::Debug for Diversifier { /// _Diversifier_ by the _IncomingViewingKey_ scalar. /// /// [ps]: https://zips.z.cash/protocol/protocol.pdf#concretediversifyhash -pub type TransmissionKey = jubjub::AffinePoint; +#[derive(Copy, Clone, PartialEq)] +pub struct TransmissionKey(pub jubjub::AffinePoint); + +impl Deref for TransmissionKey { + type Target = jubjub::AffinePoint; + + fn deref(&self) -> &jubjub::AffinePoint { + &self.0 + } +} + +impl fmt::Debug for TransmissionKey { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.debug_struct("TransmissionKey") + .field("u", &hex::encode(self.get_u().to_bytes())) + .field("v", &hex::encode(self.get_v().to_bytes())) + .finish() + } +} + +impl TransmissionKey { + /// Attempts to interpret a byte representation of an + /// affine point, failing if the element is not on + /// the curve or non-canonical. + /// + /// https://github.com/zkcrypto/jubjub/blob/master/src/lib.rs#L411 + pub fn from_bytes(b: [u8; 32]) -> Self { + Self(jubjub::AffinePoint::from_bytes(b).unwrap()) + } +} // #[cfg(test)] // impl Arbitrary for TransmissionKey {