From 5d78ab3508dcd1f9a4ae730024126ba9b9f53ea2 Mon Sep 17 00:00:00 2001 From: Kris Nuttycombe Date: Mon, 26 Jul 2021 12:05:28 -0600 Subject: [PATCH] Add Eq and Ord implementations for Orchard keys. --- src/keys.rs | 20 +++++++++++--------- src/primitives/redpallas.rs | 15 +++++++++++++++ src/spec.rs | 2 +- 3 files changed, 27 insertions(+), 10 deletions(-) diff --git a/src/keys.rs b/src/keys.rs index 152e3e62..708a546f 100644 --- a/src/keys.rs +++ b/src/keys.rs @@ -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]. /// /// [orchardkeycomponents]: https://zips.z.cash/protocol/nu5.pdf#orchardkeycomponents -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)] pub struct SpendingKey([u8; 32]); impl SpendingKey { @@ -111,7 +111,7 @@ impl From<&SpendingKey> for SpendAuthorizingKey { /// $\mathsf{ak}$ but stored here as a RedPallas verification key. /// /// [orchardkeycomponents]: https://zips.z.cash/protocol/nu5.pdf#orchardkeycomponents -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialOrd, Ord)] pub struct SpendValidatingKey(redpallas::VerificationKey); impl From<&SpendAuthorizingKey> for SpendValidatingKey { @@ -132,6 +132,8 @@ impl PartialEq for SpendValidatingKey { } } +impl Eq for SpendValidatingKey {} + impl SpendValidatingKey { /// Randomizes this spend validating key with the given `randomizer`. pub fn randomize(&self, randomizer: &pallas::Scalar) -> redpallas::VerificationKey { @@ -158,7 +160,7 @@ impl SpendValidatingKey { /// [`Nullifier`]: crate::note::Nullifier /// [`Note`]: crate::note::Note /// [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); impl NullifierDerivingKey { @@ -199,7 +201,7 @@ impl NullifierDerivingKey { /// Defined in [Zcash Protocol Spec § 4.2.3: Orchard Key Components][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); impl From<&SpendingKey> for CommitIvkRandomness { @@ -237,7 +239,7 @@ impl CommitIvkRandomness { /// Defined in [Zcash Protocol Spec § 4.2.3: Orchard Key Components][orchardkeycomponents]. /// /// [orchardkeycomponents]: https://zips.z.cash/protocol/nu5.pdf#orchardkeycomponents -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)] pub struct FullViewingKey { ak: SpendValidatingKey, nk: NullifierDerivingKey, @@ -326,7 +328,7 @@ impl FullViewingKey { /// Defined in [Zcash Protocol Spec § 4.2.3: Orchard Key Components][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]); impl From<&FullViewingKey> for DiversifierKey { @@ -336,7 +338,7 @@ impl From<&FullViewingKey> for DiversifierKey { } /// The index for a particular diversifier. -#[derive(Clone, Copy, Debug)] +#[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct DiversifierIndex([u8; 11]); 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 /// a valid base field element encoding, because we always construct ivk from an integer /// in the correct range. -#[derive(Clone, Debug)] +#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)] struct KeyAgreementPrivateKey(NonZeroPallasScalar); 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]. /// /// [orchardinviewingkeyencoding]: https://zips.z.cash/protocol/nu5.pdf#orchardinviewingkeyencoding -#[derive(Clone, Debug)] +#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)] pub struct IncomingViewingKey { dk: DiversifierKey, ivk: KeyAgreementPrivateKey, diff --git a/src/primitives/redpallas.rs b/src/primitives/redpallas.rs index f367c0b8..38de97f9 100644 --- a/src/primitives/redpallas.rs +++ b/src/primitives/redpallas.rs @@ -1,5 +1,6 @@ //! A minimal RedPallas implementation for use in Zcash. +use std::cmp::{Ord, Ordering, PartialOrd}; use std::convert::{TryFrom, TryInto}; use pasta_curves::pallas; @@ -97,6 +98,20 @@ impl PartialEq for VerificationKey { } } +impl Eq for VerificationKey {} + +impl PartialOrd for VerificationKey { + fn partial_cmp(&self, other: &Self) -> Option { + <[u8; 32]>::from(self).partial_cmp(&<[u8; 32]>::from(other)) + } +} + +impl Ord for VerificationKey { + fn cmp(&self, other: &Self) -> Ordering { + <[u8; 32]>::from(self).cmp(&<[u8; 32]>::from(other)) + } +} + impl VerificationKey { /// Used in the note encryption tests. #[cfg(test)] diff --git a/src/spec.rs b/src/spec.rs index 31cf1c95..0ffd4cb2 100644 --- a/src/spec.rs +++ b/src/spec.rs @@ -93,7 +93,7 @@ impl NonZeroPallasBase { } /// An integer in [1..r_P]. -#[derive(Clone, Copy, Debug)] +#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)] pub(crate) struct NonZeroPallasScalar(pallas::Scalar); impl Default for NonZeroPallasScalar {