Serialize PublicKey, SecretKey

This commit is contained in:
Henry de Valence 2019-12-03 14:51:38 -08:00
parent 36b3842f3d
commit 06a0a6404d
4 changed files with 56 additions and 12 deletions

View File

@ -3,7 +3,10 @@ use thiserror::Error;
/// An error related to RedJubJub signatures.
#[derive(Error, Debug)]
pub enum Error {
/// This is a stub variant to check that thiserror derive works.
#[error("Stub error-- remove this.")]
StubError,
/// The encoding of a secret key was malformed.
#[error("Malformed secret key encoding.")]
MalformedSecretKey,
/// The encoding of a public key was malformed.
#[error("Malformed public key encoding.")]
MalformedPublicKey,
}

View File

@ -13,6 +13,9 @@ mod signature;
/// An element of the JubJub scalar field used for randomization of public and secret keys.
pub type Randomizer = jubjub::Fr;
/// A better name than Fr.
type Scalar = jubjub::Fr;
pub use error::Error;
pub use public_key::{PublicKey, PublicKeyBytes};
pub use secret_key::{SecretKey, SecretKeyBytes};

View File

@ -29,13 +29,17 @@ impl<T: SigType> From<PublicKeyBytes<T>> for [u8; 32] {
// XXX PartialEq, Eq?
#[derive(Copy, Clone, Debug)]
pub struct PublicKey<T: SigType> {
// fields
// XXX-jubjub: this should just be Point
point: jubjub::ExtendedPoint,
// XXX should this just store a PublicKeyBytes?
bytes: [u8; 32],
_marker: PhantomData<T>,
}
impl<T: SigType> From<PublicKey<T>> for PublicKeyBytes<T> {
fn from(pk: PublicKey<T>) -> PublicKeyBytes<T> {
unimplemented!();
let PublicKey { bytes, _marker, .. } = pk;
PublicKeyBytes { bytes, _marker }
}
}
@ -43,7 +47,18 @@ impl<T: SigType> TryFrom<PublicKeyBytes<T>> for PublicKey<T> {
type Error = Error;
fn try_from(bytes: PublicKeyBytes<T>) -> Result<Self, Self::Error> {
unimplemented!();
// XXX-jubjub: this should not use CtOption
// XXX-jubjub: this takes ownership of bytes, while Fr doesn't.
let maybe_point = jubjub::AffinePoint::from_bytes(bytes.bytes);
if maybe_point.is_some().into() {
Ok(PublicKey {
point: maybe_point.unwrap().into(),
bytes: bytes.bytes,
_marker: PhantomData,
})
} else {
Err(Error::MalformedPublicKey)
}
}
}

View File

@ -1,6 +1,6 @@
use std::{convert::TryFrom, marker::PhantomData};
use crate::{Binding, Error, PublicKey, Randomizer, SigType, Signature, SpendAuth};
use crate::{Binding, Error, PublicKey, Randomizer, Scalar, SigType, Signature, SpendAuth};
/// A refinement type indicating that the inner `[u8; 32]` represents an
/// encoding of a RedJubJub secret key.
@ -29,27 +29,50 @@ impl<T: SigType> From<SecretKeyBytes<T>> for [u8; 32] {
// XXX PartialEq, Eq?
#[derive(Copy, Clone, Debug)]
pub struct SecretKey<T: SigType> {
// fields
sk: Scalar,
_marker: PhantomData<T>,
}
impl<T: SigType> From<SecretKey<T>> for SecretKeyBytes<T> {
fn from(pk: SecretKey<T>) -> SecretKeyBytes<T> {
unimplemented!();
fn from(sk: SecretKey<T>) -> SecretKeyBytes<T> {
SecretKeyBytes {
bytes: sk.sk.to_bytes(),
_marker: PhantomData,
}
}
}
// 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<T: SigType> TryFrom<SecretKeyBytes<T>> for SecretKey<T> {
type Error = Error;
fn try_from(bytes: SecretKeyBytes<T>) -> Result<Self, Self::Error> {
// 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);
if maybe_sk.is_some().into() {
Ok(SecretKey {
sk: maybe_sk.unwrap(),
_marker: PhantomData,
})
} else {
Err(Error::MalformedSecretKey)
}
}
}
impl<'a> From<&'a SecretKey<SpendAuth>> for PublicKey<SpendAuth> {
fn from(sk: &'a SecretKey<SpendAuth>) -> PublicKey<SpendAuth> {
// XXX refactor jubjub API
//let basepoint: jubjub::ExtendedPoint = jubjub::AffinePoint::from_bytes(&crate::constants::SPENDAUTHSIG_BASEPOINT_BYTES).unwrap().into();
unimplemented!();
}
}
impl<'a, T: SigType> From<&'a SecretKey<T>> for PublicKey<T> {
fn from(sk: &'a SecretKey<T>) -> PublicKey<T> {
impl<'a> From<&'a SecretKey<Binding>> for PublicKey<Binding> {
fn from(sk: &'a SecretKey<Binding>) -> PublicKey<Binding> {
unimplemented!();
}
}