Reorganize data types.

This commit is contained in:
Henry de Valence 2019-12-03 15:59:24 -08:00
parent 52951f7236
commit b44f149381
3 changed files with 27 additions and 55 deletions

View File

@ -18,7 +18,7 @@ type Scalar = jubjub::Fr;
pub use error::Error; pub use error::Error;
pub use public_key::{PublicKey, PublicKeyBytes}; pub use public_key::{PublicKey, PublicKeyBytes};
pub use secret_key::{SecretKey, SecretKeyBytes}; pub use secret_key::SecretKey;
pub use signature::Signature; pub use signature::Signature;
/// Abstracts over different RedJubJub parameter choices. /// Abstracts over different RedJubJub parameter choices.

View File

@ -2,12 +2,16 @@ use std::{convert::TryFrom, marker::PhantomData};
use crate::{Binding, Error, Randomizer, SigType, Signature, SpendAuth}; use crate::{Binding, Error, Randomizer, SigType, Signature, SpendAuth};
/// A refinement type indicating that the inner `[u8; 32]` represents an /// A refinement type for `[u8; 32]` indicating that the bytes represent
/// encoding of a RedJubJub public key. /// 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)] #[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub struct PublicKeyBytes<T: SigType> { pub struct PublicKeyBytes<T: SigType> {
bytes: [u8; 32], pub(crate) bytes: [u8; 32],
_marker: PhantomData<T>, pub(crate) _marker: PhantomData<T>,
} }
impl<T: SigType> From<[u8; 32]> for PublicKeyBytes<T> { impl<T: SigType> From<[u8; 32]> for PublicKeyBytes<T> {
@ -26,20 +30,16 @@ impl<T: SigType> From<PublicKeyBytes<T>> for [u8; 32] {
} }
/// A RedJubJub public key. /// A RedJubJub public key.
// XXX PartialEq, Eq?
#[derive(Copy, Clone, Debug)] #[derive(Copy, Clone, Debug)]
pub struct PublicKey<T: SigType> { pub struct PublicKey<T: SigType> {
// XXX-jubjub: this should just be Point // XXX-jubjub: this should just be Point
pub(crate) point: jubjub::ExtendedPoint, pub(crate) point: jubjub::ExtendedPoint,
// XXX should this just store a PublicKeyBytes? pub(crate) bytes: PublicKeyBytes<T>,
pub(crate) bytes: [u8; 32],
pub(crate) _marker: PhantomData<T>,
} }
impl<T: SigType> From<PublicKey<T>> for PublicKeyBytes<T> { impl<T: SigType> From<PublicKey<T>> for PublicKeyBytes<T> {
fn from(pk: PublicKey<T>) -> PublicKeyBytes<T> { fn from(pk: PublicKey<T>) -> PublicKeyBytes<T> {
let PublicKey { bytes, _marker, .. } = pk; pk.bytes
PublicKeyBytes { bytes, _marker }
} }
} }
@ -53,8 +53,7 @@ impl<T: SigType> TryFrom<PublicKeyBytes<T>> for PublicKey<T> {
if maybe_point.is_some().into() { if maybe_point.is_some().into() {
Ok(PublicKey { Ok(PublicKey {
point: maybe_point.unwrap().into(), point: maybe_point.unwrap().into(),
bytes: bytes.bytes, bytes,
_marker: PhantomData,
}) })
} else { } else {
Err(Error::MalformedPublicKey) Err(Error::MalformedPublicKey)

View File

@ -1,59 +1,31 @@
use std::{convert::TryFrom, marker::PhantomData}; 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}; 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<T: SigType> {
bytes: [u8; 32],
_marker: PhantomData<T>,
}
impl<T: SigType> From<[u8; 32]> for SecretKeyBytes<T> {
fn from(bytes: [u8; 32]) -> SecretKeyBytes<T> {
SecretKeyBytes {
bytes,
_marker: PhantomData,
}
}
}
impl<T: SigType> From<SecretKeyBytes<T>> for [u8; 32] {
fn from(refined: SecretKeyBytes<T>) -> [u8; 32] {
refined.bytes
}
}
/// A RedJubJub secret key. /// A RedJubJub secret key.
// XXX PartialEq, Eq?
#[derive(Copy, Clone, Debug)] #[derive(Copy, Clone, Debug)]
pub struct SecretKey<T: SigType> { pub struct SecretKey<T: SigType> {
sk: Scalar, sk: Scalar,
_marker: PhantomData<T>, _marker: PhantomData<T>,
} }
impl<T: SigType> From<SecretKey<T>> for SecretKeyBytes<T> { impl<T: SigType> From<SecretKey<T>> for [u8; 32] {
fn from(sk: SecretKey<T>) -> SecretKeyBytes<T> { fn from(sk: SecretKey<T>) -> [u8; 32] {
SecretKeyBytes { sk.sk.to_bytes()
bytes: sk.sk.to_bytes(),
_marker: PhantomData,
}
} }
} }
// XXX could this be a From impl? impl<T: SigType> TryFrom<[u8; 32]> for SecretKey<T> {
// not unless there's an infallible conversion from bytes to scalars,
// which is not currently present in jubjub
impl<T: SigType> TryFrom<SecretKeyBytes<T>> for SecretKey<T> {
type Error = Error; type Error = Error;
fn try_from(bytes: SecretKeyBytes<T>) -> Result<Self, Self::Error> { fn try_from(bytes: [u8; 32]) -> Result<Self, Self::Error> {
// XXX-jubjub: it does not make sense for this to be a CtOption... // XXX-jubjub: it does not make sense for this to be a CtOption...
// XXX-jubjub: this takes a borrow but point deser doesn't // 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() { if maybe_sk.is_some().into() {
Ok(SecretKey { Ok(SecretKey {
sk: maybe_sk.unwrap(), sk: maybe_sk.unwrap(),
@ -65,6 +37,7 @@ impl<T: SigType> TryFrom<SecretKeyBytes<T>> for SecretKey<T> {
} }
} }
/*
impl<R, T> From<R> for SecretKey<T> impl<R, T> From<R> for SecretKey<T>
where where
R: RngCore + CryptoRng, R: RngCore + CryptoRng,
@ -79,6 +52,7 @@ where
} }
} }
} }
*/
impl<'a> From<&'a SecretKey<SpendAuth>> for PublicKey<SpendAuth> { impl<'a> From<&'a SecretKey<SpendAuth>> for PublicKey<SpendAuth> {
fn from(sk: &'a SecretKey<SpendAuth>) -> PublicKey<SpendAuth> { fn from(sk: &'a SecretKey<SpendAuth>) -> PublicKey<SpendAuth> {
@ -107,12 +81,11 @@ fn pk_from_sk_inner<T: SigType>(
basepoint: jubjub::ExtendedPoint, basepoint: jubjub::ExtendedPoint,
) -> PublicKey<T> { ) -> PublicKey<T> {
let point = &basepoint * &sk.sk; let point = &basepoint * &sk.sk;
let bytes = jubjub::AffinePoint::from(&point).to_bytes(); let bytes = PublicKeyBytes {
PublicKey { bytes: jubjub::AffinePoint::from(&point).to_bytes(),
point,
bytes,
_marker: PhantomData, _marker: PhantomData,
} };
PublicKey { bytes, point }
} }
impl<T: SigType> SecretKey<T> { impl<T: SigType> SecretKey<T> {