Impl PartialEq for some Sapling keys
This commit is contained in:
parent
ba3ba6d2d9
commit
9daa1ba3c8
|
|
@ -18,6 +18,7 @@ use std::{
|
||||||
fmt,
|
fmt,
|
||||||
io::{self, Write},
|
io::{self, Write},
|
||||||
ops::Deref,
|
ops::Deref,
|
||||||
|
str::FromStr,
|
||||||
};
|
};
|
||||||
|
|
||||||
use bech32::{self, FromBase32, ToBase32};
|
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;
|
type Err = SerializationError;
|
||||||
|
|
||||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
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
|
/// An _Authorizing Key_, as described in [protocol specification
|
||||||
/// §4.2.2][ps].
|
/// §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
|
/// A _Nullifier Deriving Key_, as described in [protocol
|
||||||
/// specification §4.2.2][ps].
|
/// specification §4.2.2][ps].
|
||||||
///
|
///
|
||||||
|
|
@ -482,8 +507,6 @@ pub struct IncomingViewingKey {
|
||||||
scalar: Scalar,
|
scalar: Scalar,
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: impl a top-level to_bytes or PartialEq between this and [u8; 32]
|
|
||||||
|
|
||||||
// TODO: impl a From that accepts a Network?
|
// TODO: impl a From that accepts a Network?
|
||||||
|
|
||||||
impl Deref for IncomingViewingKey {
|
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 {
|
impl From<[u8; 32]> for IncomingViewingKey {
|
||||||
/// Generate an _IncomingViewingKey_ from existing bytes.
|
/// Generate an _IncomingViewingKey_ from existing bytes.
|
||||||
fn from(mut bytes: [u8; 32]) -> Self {
|
fn from(mut bytes: [u8; 32]) -> Self {
|
||||||
|
|
@ -539,26 +581,7 @@ impl From<IncomingViewingKey> for [u8; 32] {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Debug for IncomingViewingKey {
|
impl FromStr 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 {
|
|
||||||
type Err = SerializationError;
|
type Err = SerializationError;
|
||||||
|
|
||||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
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].
|
/// A _Diversifier_, as described in [protocol specification §4.2.2][ps].
|
||||||
///
|
///
|
||||||
/// Combined with an _IncomingViewingKey_, produces a _diversified
|
/// 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 {
|
impl Diversifier {
|
||||||
/// Generate a new _Diversifier_ that has already been confirmed
|
/// Generate a new _Diversifier_ that has already been confirmed
|
||||||
/// as a preimage to a valid diversified base point when used to
|
/// 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;
|
type Err = SerializationError;
|
||||||
|
|
||||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||||
|
|
|
||||||
|
|
@ -66,24 +66,18 @@ mod tests {
|
||||||
let proof_authorizing_key = ProofAuthorizingKey::from(spending_key);
|
let proof_authorizing_key = ProofAuthorizingKey::from(spending_key);
|
||||||
assert_eq!(proof_authorizing_key.to_bytes(), test_vector.nsk);
|
assert_eq!(proof_authorizing_key.to_bytes(), test_vector.nsk);
|
||||||
let outgoing_viewing_key = OutgoingViewingKey::from(spending_key);
|
let outgoing_viewing_key = OutgoingViewingKey::from(spending_key);
|
||||||
assert_eq!(
|
assert_eq!(outgoing_viewing_key, test_vector.ovk);
|
||||||
Into::<[u8; 32]>::into(outgoing_viewing_key),
|
|
||||||
test_vector.ovk
|
|
||||||
);
|
|
||||||
|
|
||||||
let authorizing_key = AuthorizingKey::from(spend_authorizing_key);
|
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);
|
let nullifier_deriving_key = NullifierDerivingKey::from(proof_authorizing_key);
|
||||||
assert_eq!(
|
assert_eq!(nullifier_deriving_key.to_bytes(), test_vector.nk);
|
||||||
Into::<[u8; 32]>::into(nullifier_deriving_key),
|
|
||||||
test_vector.nk
|
|
||||||
);
|
|
||||||
let incoming_viewing_key =
|
let incoming_viewing_key =
|
||||||
IncomingViewingKey::from((authorizing_key, nullifier_deriving_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);
|
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);
|
let transmission_key = TransmissionKey::from(incoming_viewing_key, diversifier);
|
||||||
assert_eq!(transmission_key.to_bytes(), test_vector.default_pk_d);
|
assert_eq!(transmission_key.to_bytes(), test_vector.default_pk_d);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue