frost/src/public_key.rs

88 lines
2.8 KiB
Rust
Raw Normal View History

use std::{convert::TryFrom, marker::PhantomData};
2019-12-02 21:58:19 -08:00
2019-12-03 13:39:26 -08:00
use crate::{Binding, Error, Randomizer, SigType, Signature, SpendAuth};
2019-12-02 21:58:19 -08:00
2019-12-03 15:59:24 -08:00
/// 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.
2019-12-02 21:58:19 -08:00
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub struct PublicKeyBytes<T: SigType> {
2019-12-03 15:59:24 -08:00
pub(crate) bytes: [u8; 32],
pub(crate) _marker: PhantomData<T>,
}
2019-12-02 21:58:19 -08:00
impl<T: SigType> From<[u8; 32]> for PublicKeyBytes<T> {
fn from(bytes: [u8; 32]) -> PublicKeyBytes<T> {
2019-12-03 13:39:26 -08:00
PublicKeyBytes {
bytes,
_marker: PhantomData,
}
2019-12-02 21:58:19 -08:00
}
}
impl<T: SigType> From<PublicKeyBytes<T>> for [u8; 32] {
fn from(refined: PublicKeyBytes<T>) -> [u8; 32] {
refined.bytes
2019-12-02 21:58:19 -08:00
}
}
/// A RedJubJub public key.
#[derive(Copy, Clone, Debug)]
pub struct PublicKey<T: SigType> {
2019-12-03 14:51:38 -08:00
// XXX-jubjub: this should just be Point
2019-12-03 15:01:54 -08:00
pub(crate) point: jubjub::ExtendedPoint,
2019-12-03 15:59:24 -08:00
pub(crate) bytes: PublicKeyBytes<T>,
2019-12-02 21:58:19 -08:00
}
impl<T: SigType> From<PublicKey<T>> for PublicKeyBytes<T> {
fn from(pk: PublicKey<T>) -> PublicKeyBytes<T> {
2019-12-03 15:59:24 -08:00
pk.bytes
2019-12-02 21:58:19 -08:00
}
}
impl<T: SigType> TryFrom<PublicKeyBytes<T>> for PublicKey<T> {
2019-12-02 21:58:19 -08:00
type Error = Error;
fn try_from(bytes: PublicKeyBytes<T>) -> Result<Self, Self::Error> {
2019-12-03 14:51:38 -08:00
// 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(),
2019-12-03 15:59:24 -08:00
bytes,
2019-12-03 14:51:38 -08:00
})
} else {
Err(Error::MalformedPublicKey)
}
2019-12-02 21:58:19 -08:00
}
}
2019-12-02 22:20:21 -08:00
impl<T: SigType> PublicKey<T> {
2019-12-02 22:32:55 -08:00
/// Randomize this public key with the given `randomizer`.
pub fn randomize(&self, randomizer: Randomizer) -> PublicKey<T> {
2019-12-02 22:32:55 -08:00
unimplemented!();
}
}
impl PublicKey<Binding> {
/// Verify a Zcash `BindingSig` over `msg` made by this public key.
// This is similar to impl signature::Verifier but without boxed errors
pub fn verify(&self, msg: &[u8], signature: &Signature<Binding>) -> Result<(), Error> {
// this lets us specialize the basepoint parameter, could call a verify_inner
unimplemented!();
}
}
2019-12-02 22:32:55 -08:00
impl PublicKey<SpendAuth> {
/// Verify a Zcash `SpendAuthSig` over `msg` made by this public key.
2019-12-02 22:32:55 -08:00
// This is similar to impl signature::Verifier but without boxed errors
pub fn verify(&self, msg: &[u8], signature: &Signature<SpendAuth>) -> Result<(), Error> {
// this lets us specialize the basepoint parameter, could call a verify_inner
2019-12-02 22:20:21 -08:00
unimplemented!();
}
}