Add Eq and Ord implementations for Orchard keys.

This commit is contained in:
Kris Nuttycombe 2021-07-26 12:05:28 -06:00
parent 52f0f158ef
commit 5d78ab3508
3 changed files with 27 additions and 10 deletions

View File

@ -29,7 +29,7 @@ const KDF_ORCHARD_PERSONALIZATION: &[u8; 16] = b"Zcash_OrchardKDF";
/// Defined in [Zcash Protocol Spec § 4.2.3: Orchard Key Components][orchardkeycomponents]. /// Defined in [Zcash Protocol Spec § 4.2.3: Orchard Key Components][orchardkeycomponents].
/// ///
/// [orchardkeycomponents]: https://zips.z.cash/protocol/nu5.pdf#orchardkeycomponents /// [orchardkeycomponents]: https://zips.z.cash/protocol/nu5.pdf#orchardkeycomponents
#[derive(Debug, Clone)] #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct SpendingKey([u8; 32]); pub struct SpendingKey([u8; 32]);
impl SpendingKey { impl SpendingKey {
@ -111,7 +111,7 @@ impl From<&SpendingKey> for SpendAuthorizingKey {
/// $\mathsf{ak}$ but stored here as a RedPallas verification key. /// $\mathsf{ak}$ but stored here as a RedPallas verification key.
/// ///
/// [orchardkeycomponents]: https://zips.z.cash/protocol/nu5.pdf#orchardkeycomponents /// [orchardkeycomponents]: https://zips.z.cash/protocol/nu5.pdf#orchardkeycomponents
#[derive(Debug, Clone)] #[derive(Debug, Clone, PartialOrd, Ord)]
pub struct SpendValidatingKey(redpallas::VerificationKey<SpendAuth>); pub struct SpendValidatingKey(redpallas::VerificationKey<SpendAuth>);
impl From<&SpendAuthorizingKey> for SpendValidatingKey { impl From<&SpendAuthorizingKey> for SpendValidatingKey {
@ -132,6 +132,8 @@ impl PartialEq for SpendValidatingKey {
} }
} }
impl Eq for SpendValidatingKey {}
impl SpendValidatingKey { impl SpendValidatingKey {
/// Randomizes this spend validating key with the given `randomizer`. /// Randomizes this spend validating key with the given `randomizer`.
pub fn randomize(&self, randomizer: &pallas::Scalar) -> redpallas::VerificationKey<SpendAuth> { pub fn randomize(&self, randomizer: &pallas::Scalar) -> redpallas::VerificationKey<SpendAuth> {
@ -158,7 +160,7 @@ impl SpendValidatingKey {
/// [`Nullifier`]: crate::note::Nullifier /// [`Nullifier`]: crate::note::Nullifier
/// [`Note`]: crate::note::Note /// [`Note`]: crate::note::Note
/// [orchardkeycomponents]: https://zips.z.cash/protocol/nu5.pdf#orchardkeycomponents /// [orchardkeycomponents]: https://zips.z.cash/protocol/nu5.pdf#orchardkeycomponents
#[derive(Copy, Debug, Clone)] #[derive(Copy, Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub(crate) struct NullifierDerivingKey(pallas::Base); pub(crate) struct NullifierDerivingKey(pallas::Base);
impl NullifierDerivingKey { impl NullifierDerivingKey {
@ -199,7 +201,7 @@ impl NullifierDerivingKey {
/// Defined in [Zcash Protocol Spec § 4.2.3: Orchard Key Components][orchardkeycomponents]. /// Defined in [Zcash Protocol Spec § 4.2.3: Orchard Key Components][orchardkeycomponents].
/// ///
/// [orchardkeycomponents]: https://zips.z.cash/protocol/nu5.pdf#orchardkeycomponents /// [orchardkeycomponents]: https://zips.z.cash/protocol/nu5.pdf#orchardkeycomponents
#[derive(Copy, Debug, Clone)] #[derive(Copy, Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub(crate) struct CommitIvkRandomness(pallas::Scalar); pub(crate) struct CommitIvkRandomness(pallas::Scalar);
impl From<&SpendingKey> for CommitIvkRandomness { impl From<&SpendingKey> for CommitIvkRandomness {
@ -237,7 +239,7 @@ impl CommitIvkRandomness {
/// Defined in [Zcash Protocol Spec § 4.2.3: Orchard Key Components][orchardkeycomponents]. /// Defined in [Zcash Protocol Spec § 4.2.3: Orchard Key Components][orchardkeycomponents].
/// ///
/// [orchardkeycomponents]: https://zips.z.cash/protocol/nu5.pdf#orchardkeycomponents /// [orchardkeycomponents]: https://zips.z.cash/protocol/nu5.pdf#orchardkeycomponents
#[derive(Debug, Clone)] #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct FullViewingKey { pub struct FullViewingKey {
ak: SpendValidatingKey, ak: SpendValidatingKey,
nk: NullifierDerivingKey, nk: NullifierDerivingKey,
@ -326,7 +328,7 @@ impl FullViewingKey {
/// Defined in [Zcash Protocol Spec § 4.2.3: Orchard Key Components][orchardkeycomponents]. /// Defined in [Zcash Protocol Spec § 4.2.3: Orchard Key Components][orchardkeycomponents].
/// ///
/// [orchardkeycomponents]: https://zips.z.cash/protocol/nu5.pdf#orchardkeycomponents /// [orchardkeycomponents]: https://zips.z.cash/protocol/nu5.pdf#orchardkeycomponents
#[derive(Clone, Debug)] #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct DiversifierKey([u8; 32]); pub struct DiversifierKey([u8; 32]);
impl From<&FullViewingKey> for DiversifierKey { impl From<&FullViewingKey> for DiversifierKey {
@ -336,7 +338,7 @@ impl From<&FullViewingKey> for DiversifierKey {
} }
/// The index for a particular diversifier. /// The index for a particular diversifier.
#[derive(Clone, Copy, Debug)] #[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct DiversifierIndex([u8; 11]); pub struct DiversifierIndex([u8; 11]);
macro_rules! di_from { macro_rules! di_from {
@ -418,7 +420,7 @@ impl Diversifier {
/// decryption of notes). When we actually want to serialize ivk, we're guaranteed to get /// decryption of notes). When we actually want to serialize ivk, we're guaranteed to get
/// a valid base field element encoding, because we always construct ivk from an integer /// a valid base field element encoding, because we always construct ivk from an integer
/// in the correct range. /// in the correct range.
#[derive(Clone, Debug)] #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
struct KeyAgreementPrivateKey(NonZeroPallasScalar); struct KeyAgreementPrivateKey(NonZeroPallasScalar);
impl From<&FullViewingKey> for KeyAgreementPrivateKey { impl From<&FullViewingKey> for KeyAgreementPrivateKey {
@ -455,7 +457,7 @@ impl KeyAgreementPrivateKey {
/// Defined in [Zcash Protocol Spec § 5.6.4.3: Orchard Raw Incoming Viewing Keys][orchardinviewingkeyencoding]. /// Defined in [Zcash Protocol Spec § 5.6.4.3: Orchard Raw Incoming Viewing Keys][orchardinviewingkeyencoding].
/// ///
/// [orchardinviewingkeyencoding]: https://zips.z.cash/protocol/nu5.pdf#orchardinviewingkeyencoding /// [orchardinviewingkeyencoding]: https://zips.z.cash/protocol/nu5.pdf#orchardinviewingkeyencoding
#[derive(Clone, Debug)] #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct IncomingViewingKey { pub struct IncomingViewingKey {
dk: DiversifierKey, dk: DiversifierKey,
ivk: KeyAgreementPrivateKey, ivk: KeyAgreementPrivateKey,

View File

@ -1,5 +1,6 @@
//! A minimal RedPallas implementation for use in Zcash. //! A minimal RedPallas implementation for use in Zcash.
use std::cmp::{Ord, Ordering, PartialOrd};
use std::convert::{TryFrom, TryInto}; use std::convert::{TryFrom, TryInto};
use pasta_curves::pallas; use pasta_curves::pallas;
@ -97,6 +98,20 @@ impl<T: SigType> PartialEq for VerificationKey<T> {
} }
} }
impl<T: SigType> Eq for VerificationKey<T> {}
impl<T: SigType> PartialOrd for VerificationKey<T> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
<[u8; 32]>::from(self).partial_cmp(&<[u8; 32]>::from(other))
}
}
impl<T: SigType> Ord for VerificationKey<T> {
fn cmp(&self, other: &Self) -> Ordering {
<[u8; 32]>::from(self).cmp(&<[u8; 32]>::from(other))
}
}
impl VerificationKey<SpendAuth> { impl VerificationKey<SpendAuth> {
/// Used in the note encryption tests. /// Used in the note encryption tests.
#[cfg(test)] #[cfg(test)]

View File

@ -93,7 +93,7 @@ impl NonZeroPallasBase {
} }
/// An integer in [1..r_P]. /// An integer in [1..r_P].
#[derive(Clone, Copy, Debug)] #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub(crate) struct NonZeroPallasScalar(pallas::Scalar); pub(crate) struct NonZeroPallasScalar(pallas::Scalar);
impl Default for NonZeroPallasScalar { impl Default for NonZeroPallasScalar {