Impl PartialEq for some Sapling keys

This commit is contained in:
Deirdre Connolly 2020-04-17 02:38:44 -04:00 committed by Deirdre Connolly
parent ba3ba6d2d9
commit 9daa1ba3c8
2 changed files with 76 additions and 35 deletions

View File

@ -18,6 +18,7 @@ use std::{
fmt,
io::{self, Write},
ops::Deref,
str::FromStr,
};
use bech32::{self, FromBase32, ToBase32};
@ -209,7 +210,7 @@ impl fmt::Display for SpendingKey {
}
}
impl std::str::FromStr for SpendingKey {
impl FromStr for SpendingKey {
type Err = SerializationError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
@ -365,6 +366,18 @@ impl From<SpendingKey> for OutgoingViewingKey {
}
}
impl PartialEq<[u8; 32]> for OutgoingViewingKey {
fn eq(&self, other: &[u8; 32]) -> bool {
self.0 == *other
}
}
impl PartialEq<OutgoingViewingKey> for [u8; 32] {
fn eq(&self, other: &OutgoingViewingKey) -> bool {
*self == other.0
}
}
/// An _Authorizing Key_, as described in [protocol specification
/// §4.2.2][ps].
///
@ -403,6 +416,18 @@ impl From<SpendAuthorizingKey> for AuthorizingKey {
}
}
impl PartialEq<[u8; 32]> for AuthorizingKey {
fn eq(&self, other: &[u8; 32]) -> bool {
Into::<[u8; 32]>::into(self.0) == *other
}
}
impl PartialEq<AuthorizingKey> for [u8; 32] {
fn eq(&self, other: &AuthorizingKey) -> bool {
*self == Into::<[u8; 32]>::into(other.0)
}
}
/// A _Nullifier Deriving Key_, as described in [protocol
/// specification §4.2.2][ps].
///
@ -482,8 +507,6 @@ pub struct IncomingViewingKey {
scalar: Scalar,
}
// TODO: impl a top-level to_bytes or PartialEq between this and [u8; 32]
// TODO: impl a From that accepts a Network?
impl Deref for IncomingViewingKey {
@ -494,6 +517,25 @@ 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.to_bytes()))
.finish()
}
}
impl fmt::Display for IncomingViewingKey {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let hrp = match self.network {
Network::Mainnet => ivk_hrp::MAINNET,
_ => ivk_hrp::TESTNET,
};
bech32::encode_to_fmt(f, hrp, &self.scalar.to_bytes().to_base32()).unwrap()
}
}
impl From<[u8; 32]> for IncomingViewingKey {
/// Generate an _IncomingViewingKey_ from existing bytes.
fn from(mut bytes: [u8; 32]) -> Self {
@ -539,26 +581,7 @@ impl From<IncomingViewingKey> for [u8; 32] {
}
}
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 fmt::Display for IncomingViewingKey {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let hrp = match self.network {
Network::Mainnet => ivk_hrp::MAINNET,
_ => ivk_hrp::TESTNET,
};
bech32::encode_to_fmt(f, hrp, &self.scalar.to_bytes().to_base32()).unwrap()
}
}
impl std::str::FromStr for IncomingViewingKey {
impl FromStr for IncomingViewingKey {
type Err = SerializationError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
@ -582,6 +605,18 @@ impl std::str::FromStr for IncomingViewingKey {
}
}
impl PartialEq<[u8; 32]> for IncomingViewingKey {
fn eq(&self, other: &[u8; 32]) -> bool {
self.scalar.to_bytes() == *other
}
}
impl PartialEq<IncomingViewingKey> for [u8; 32] {
fn eq(&self, other: &IncomingViewingKey) -> bool {
*self == other.scalar.to_bytes()
}
}
/// A _Diversifier_, as described in [protocol specification §4.2.2][ps].
///
/// Combined with an _IncomingViewingKey_, produces a _diversified
@ -629,6 +664,18 @@ impl From<SpendingKey> for Diversifier {
}
}
impl PartialEq<[u8; 11]> for Diversifier {
fn eq(&self, other: &[u8; 11]) -> bool {
self.0 == *other
}
}
impl PartialEq<Diversifier> for [u8; 11] {
fn eq(&self, other: &Diversifier) -> bool {
*self == other.0
}
}
impl Diversifier {
/// Generate a new _Diversifier_ that has already been confirmed
/// as a preimage to a valid diversified base point when used to
@ -764,7 +811,7 @@ impl fmt::Display for FullViewingKey {
}
}
impl std::str::FromStr for FullViewingKey {
impl FromStr for FullViewingKey {
type Err = SerializationError;
fn from_str(s: &str) -> Result<Self, Self::Err> {

View File

@ -66,24 +66,18 @@ mod tests {
let proof_authorizing_key = ProofAuthorizingKey::from(spending_key);
assert_eq!(proof_authorizing_key.to_bytes(), test_vector.nsk);
let outgoing_viewing_key = OutgoingViewingKey::from(spending_key);
assert_eq!(
Into::<[u8; 32]>::into(outgoing_viewing_key),
test_vector.ovk
);
assert_eq!(outgoing_viewing_key, test_vector.ovk);
let authorizing_key = AuthorizingKey::from(spend_authorizing_key);
assert_eq!(Into::<[u8; 32]>::into(authorizing_key), test_vector.ak);
assert_eq!(authorizing_key, test_vector.ak);
let nullifier_deriving_key = NullifierDerivingKey::from(proof_authorizing_key);
assert_eq!(
Into::<[u8; 32]>::into(nullifier_deriving_key),
test_vector.nk
);
assert_eq!(nullifier_deriving_key.to_bytes(), test_vector.nk);
let incoming_viewing_key =
IncomingViewingKey::from((authorizing_key, nullifier_deriving_key));
assert_eq!(incoming_viewing_key.scalar.to_bytes(), test_vector.ivk);
assert_eq!(incoming_viewing_key, test_vector.ivk);
let diversifier = Diversifier::from(spending_key);
assert_eq!(diversifier.0, test_vector.default_d);
assert_eq!(diversifier, test_vector.default_d);
let transmission_key = TransmissionKey::from(incoming_viewing_key, diversifier);
assert_eq!(transmission_key.to_bytes(), test_vector.default_pk_d);