From 27501702d5489db9e9d40b15e367d569a73d69c3 Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Sat, 6 Mar 2021 00:03:26 +0000 Subject: [PATCH] Use orchard::redpallas types in orchard::keys implementation --- src/keys.rs | 9 +++---- src/primitives/redpallas.rs | 48 +++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 4 deletions(-) diff --git a/src/keys.rs b/src/keys.rs index e66f5c17..80330f42 100644 --- a/src/keys.rs +++ b/src/keys.rs @@ -11,6 +11,7 @@ use subtle::CtOption; use crate::{ address::Address, + primitives::redpallas::{self, SpendAuth}, spec::{ commit_ivk, diversify_hash, extract_p, ka_orchard, prf_expand, prf_expand_vec, to_base, to_scalar, @@ -43,7 +44,7 @@ impl SpendingKey { /// /// [§4.2.3]: https://zips.z.cash/protocol/nu5.pdf#orchardkeycomponents #[derive(Debug)] -pub(crate) struct SpendAuthorizingKey(reddsa::SigningKey); +pub(crate) struct SpendAuthorizingKey(redpallas::SigningKey); impl SpendAuthorizingKey { /// Derives ask from sk. Internal use only, does not enforce all constraints. @@ -70,7 +71,7 @@ impl From<&SpendingKey> for SpendAuthorizingKey { /// TODO: This is its protocol spec name for Sapling, but I'd prefer a different name. #[derive(Debug)] -pub(crate) struct AuthorizingKey(reddsa::VerificationKey); +pub(crate) struct AuthorizingKey(redpallas::VerificationKey); impl From<&SpendAuthorizingKey> for AuthorizingKey { fn from(ask: &SpendAuthorizingKey) -> Self { @@ -121,7 +122,7 @@ impl FullViewingKey { /// [§4.2.3]: https://zips.z.cash/protocol/nu5.pdf#orchardkeycomponents fn derive_dk_ovk(&self) -> (DiversifierKey, OutgoingViewingKey) { let k = self.rivk.to_bytes(); - let b = [self.ak.0.into(), self.nk.0.to_bytes()]; + let b = [(&self.ak.0).into(), self.nk.0.to_bytes()]; let r = prf_expand_vec(&k, &[&[0x82], &b[0][..], &b[1][..]]); ( DiversifierKey(r.as_bytes()[..32].try_into().unwrap()), @@ -205,7 +206,7 @@ pub struct IncomingViewingKey(pallas::Scalar); impl From<&FullViewingKey> for IncomingViewingKey { fn from(fvk: &FullViewingKey) -> Self { - let ak = pallas::Point::from_bytes(&fvk.ak.0.into()).unwrap(); + let ak = pallas::Point::from_bytes(&(&fvk.ak.0).into()).unwrap(); IncomingViewingKey(commit_ivk(&extract_p(&ak), &fvk.nk.0, &fvk.rivk)) } } diff --git a/src/primitives/redpallas.rs b/src/primitives/redpallas.rs index fa4d12b0..0af1677c 100644 --- a/src/primitives/redpallas.rs +++ b/src/primitives/redpallas.rs @@ -1,5 +1,7 @@ //! A minimal RedPallas implementation for use in Zcash. +use std::convert::{TryFrom, TryInto}; + /// A RedPallas signature type. pub trait SigType: reddsa::SigType + private::Sealed {} @@ -15,10 +17,56 @@ impl SigType for Binding {} #[derive(Debug)] pub struct SigningKey(reddsa::SigningKey); +impl From> for [u8; 32] { + fn from(sk: SigningKey) -> [u8; 32] { + sk.0.into() + } +} + +impl From<&SigningKey> for [u8; 32] { + fn from(sk: &SigningKey) -> [u8; 32] { + sk.0.into() + } +} + +impl TryFrom<[u8; 32]> for SigningKey { + type Error = reddsa::Error; + + fn try_from(bytes: [u8; 32]) -> Result { + bytes.try_into().map(SigningKey) + } +} + /// A RedPallas verification key. #[derive(Debug)] pub struct VerificationKey(reddsa::VerificationKey); +impl From> for [u8; 32] { + fn from(vk: VerificationKey) -> [u8; 32] { + vk.0.into() + } +} + +impl From<&VerificationKey> for [u8; 32] { + fn from(vk: &VerificationKey) -> [u8; 32] { + vk.0.into() + } +} + +impl TryFrom<[u8; 32]> for VerificationKey { + type Error = reddsa::Error; + + fn try_from(bytes: [u8; 32]) -> Result { + bytes.try_into().map(VerificationKey) + } +} + +impl<'a, T: SigType> From<&'a SigningKey> for VerificationKey { + fn from(sk: &'a SigningKey) -> VerificationKey { + VerificationKey((&sk.0).into()) + } +} + /// A RedPallas signature. #[derive(Debug)] pub struct Signature(reddsa::Signature);