diff --git a/src/lib.rs b/src/lib.rs index 7ca40cf..a016790 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -18,7 +18,7 @@ type Scalar = jubjub::Fr; pub use error::Error; pub use public_key::{PublicKey, PublicKeyBytes}; -pub use secret_key::{SecretKey, SecretKeyBytes}; +pub use secret_key::SecretKey; pub use signature::Signature; /// Abstracts over different RedJubJub parameter choices. diff --git a/src/public_key.rs b/src/public_key.rs index c6d773c..b076238 100644 --- a/src/public_key.rs +++ b/src/public_key.rs @@ -2,12 +2,16 @@ use std::{convert::TryFrom, marker::PhantomData}; use crate::{Binding, Error, Randomizer, SigType, Signature, SpendAuth}; -/// A refinement type indicating that the inner `[u8; 32]` represents an -/// encoding of a RedJubJub public key. +/// A refinement type for `[u8; 32]` indicating that the bytes represent +/// an encoding of a RedJubJub public key. +/// +/// This is useful for representing a compressed public key; the +/// [`PublicKey`] type in this library holds other decompressed state +/// used in signature verification. #[derive(Copy, Clone, PartialEq, Eq, Debug)] pub struct PublicKeyBytes { - bytes: [u8; 32], - _marker: PhantomData, + pub(crate) bytes: [u8; 32], + pub(crate) _marker: PhantomData, } impl From<[u8; 32]> for PublicKeyBytes { @@ -26,20 +30,16 @@ impl From> for [u8; 32] { } /// A RedJubJub public key. -// XXX PartialEq, Eq? #[derive(Copy, Clone, Debug)] pub struct PublicKey { // XXX-jubjub: this should just be Point pub(crate) point: jubjub::ExtendedPoint, - // XXX should this just store a PublicKeyBytes? - pub(crate) bytes: [u8; 32], - pub(crate) _marker: PhantomData, + pub(crate) bytes: PublicKeyBytes, } impl From> for PublicKeyBytes { fn from(pk: PublicKey) -> PublicKeyBytes { - let PublicKey { bytes, _marker, .. } = pk; - PublicKeyBytes { bytes, _marker } + pk.bytes } } @@ -53,8 +53,7 @@ impl TryFrom> for PublicKey { if maybe_point.is_some().into() { Ok(PublicKey { point: maybe_point.unwrap().into(), - bytes: bytes.bytes, - _marker: PhantomData, + bytes, }) } else { Err(Error::MalformedPublicKey) diff --git a/src/secret_key.rs b/src/secret_key.rs index aae0dc6..db8a913 100644 --- a/src/secret_key.rs +++ b/src/secret_key.rs @@ -1,59 +1,31 @@ use std::{convert::TryFrom, marker::PhantomData}; -use crate::{Binding, Error, PublicKey, Randomizer, Scalar, SigType, Signature, SpendAuth}; +use crate::{ + Binding, Error, PublicKey, PublicKeyBytes, Randomizer, Scalar, SigType, Signature, SpendAuth, +}; use rand_core::{CryptoRng, RngCore}; -/// A refinement type indicating that the inner `[u8; 32]` represents an -/// encoding of a RedJubJub secret key. -#[derive(Copy, Clone, PartialEq, Eq, Debug)] -pub struct SecretKeyBytes { - bytes: [u8; 32], - _marker: PhantomData, -} - -impl From<[u8; 32]> for SecretKeyBytes { - fn from(bytes: [u8; 32]) -> SecretKeyBytes { - SecretKeyBytes { - bytes, - _marker: PhantomData, - } - } -} - -impl From> for [u8; 32] { - fn from(refined: SecretKeyBytes) -> [u8; 32] { - refined.bytes - } -} - /// A RedJubJub secret key. -// XXX PartialEq, Eq? #[derive(Copy, Clone, Debug)] pub struct SecretKey { sk: Scalar, _marker: PhantomData, } -impl From> for SecretKeyBytes { - fn from(sk: SecretKey) -> SecretKeyBytes { - SecretKeyBytes { - bytes: sk.sk.to_bytes(), - _marker: PhantomData, - } +impl From> for [u8; 32] { + fn from(sk: SecretKey) -> [u8; 32] { + sk.sk.to_bytes() } } -// XXX could this be a From impl? -// not unless there's an infallible conversion from bytes to scalars, -// which is not currently present in jubjub -impl TryFrom> for SecretKey { +impl TryFrom<[u8; 32]> for SecretKey { type Error = Error; - fn try_from(bytes: SecretKeyBytes) -> Result { + fn try_from(bytes: [u8; 32]) -> Result { // XXX-jubjub: it does not make sense for this to be a CtOption... // XXX-jubjub: this takes a borrow but point deser doesn't - let maybe_sk = Scalar::from_bytes(&bytes.bytes); + let maybe_sk = Scalar::from_bytes(&bytes); if maybe_sk.is_some().into() { Ok(SecretKey { sk: maybe_sk.unwrap(), @@ -65,6 +37,7 @@ impl TryFrom> for SecretKey { } } +/* impl From for SecretKey where R: RngCore + CryptoRng, @@ -79,6 +52,7 @@ where } } } +*/ impl<'a> From<&'a SecretKey> for PublicKey { fn from(sk: &'a SecretKey) -> PublicKey { @@ -107,12 +81,11 @@ fn pk_from_sk_inner( basepoint: jubjub::ExtendedPoint, ) -> PublicKey { let point = &basepoint * &sk.sk; - let bytes = jubjub::AffinePoint::from(&point).to_bytes(); - PublicKey { - point, - bytes, + let bytes = PublicKeyBytes { + bytes: jubjub::AffinePoint::from(&point).to_bytes(), _marker: PhantomData, - } + }; + PublicKey { bytes, point } } impl SecretKey {