Turn all type aliases into wrapper types with impl Deref

This commit is contained in:
Deirdre Connolly 2020-04-03 01:51:07 -04:00 committed by Deirdre Connolly
parent b9deef2956
commit 8388b13ac9
2 changed files with 88 additions and 27 deletions

View File

@ -71,7 +71,7 @@ impl ZcashDeserialize for SaplingShieldedAddress {
Ok(SaplingShieldedAddress { Ok(SaplingShieldedAddress {
diversifier: sapling::Diversifier(diversifier_bytes), 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)| { .prop_map(|(diversifier_bytes, transmission_key_bytes)| {
return Self { return Self {
diversifier: sapling::Diversifier(diversifier_bytes), diversifier: sapling::Diversifier(diversifier_bytes),
transmission_key: sapling::TransmissionKey::from_bytes(transmission_key_bytes) transmission_key: sapling::TransmissionKey::from_bytes(transmission_key_bytes),
.unwrap(),
}; };
}) })
.boxed() .boxed()
@ -126,7 +125,7 @@ mod tests {
assert_eq!( assert_eq!(
format!("{:?}", zs_addr), 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\" } }"
); );
} }

View File

@ -81,9 +81,9 @@ impl From<[u8; 32]> for SpendingKey {
} }
/// Derived from a _SpendingKey_. /// Derived from a _SpendingKey_.
pub struct SpendAuthorizationKey(Scalar); pub struct SpendAuthorizingKey(Scalar);
impl Deref for SpendAuthorizationKey { impl Deref for SpendAuthorizingKey {
type Target = Scalar; type Target = Scalar;
fn deref(&self) -> &Scalar { fn deref(&self) -> &Scalar {
@ -91,13 +91,21 @@ impl Deref for SpendAuthorizationKey {
} }
} }
impl From<SpendingKey> 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<SpendingKey> for SpendAuthorizingKey {
/// Invokes Blake2b-512 as PRF^expand, t=0, to derive a /// 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#saplingkeycomponents
/// https://zips.z.cash/protocol/protocol.pdf#concreteprfs /// 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); let hash_bytes = prf_expand(spending_key.0, 0);
Self(Scalar::from_bytes_wide(&hash_bytes)) Self(Scalar::from_bytes_wide(&hash_bytes))
@ -163,19 +171,44 @@ impl From<SpendingKey> for OutgoingViewingKey {
} }
/// ///
pub type AuthorizingKey = jubjub::AffinePoint; pub struct AuthorizingKey(pub jubjub::AffinePoint);
// impl fmt::Debug for AuthorizingKey { impl Deref for AuthorizingKey {
// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { type Target = jubjub::AffinePoint;
// f.debug_struct("AuthorizingKey")
// .field("u", &hex::encode(&self.u)) fn deref(&self) -> &jubjub::AffinePoint {
// .field("v", &hex::encode(&self.v)) &self.0
// .finish() }
// } }
// }
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); pub struct IncomingViewingKey(pub Scalar);
@ -188,13 +221,13 @@ impl Deref for IncomingViewingKey {
} }
} }
// impl fmt::Debug for IncomingViewingKey { impl fmt::Debug for IncomingViewingKey {
// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
// f.debug_tuple("IncomingViewingKey") f.debug_tuple("IncomingViewingKey")
// .field(&hex::encode(&self.0)) .field(&hex::encode(self.to_bytes()))
// .finish() .finish()
// } }
// } }
impl IncomingViewingKey { impl IncomingViewingKey {
/// For this invocation of Blake2s-256 as CRH^ivk. /// For this invocation of Blake2s-256 as CRH^ivk.
@ -239,7 +272,36 @@ impl fmt::Debug for Diversifier {
/// _Diversifier_ by the _IncomingViewingKey_ scalar. /// _Diversifier_ by the _IncomingViewingKey_ scalar.
/// ///
/// [ps]: https://zips.z.cash/protocol/protocol.pdf#concretediversifyhash /// [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)] // #[cfg(test)]
// impl Arbitrary for TransmissionKey { // impl Arbitrary for TransmissionKey {