reddsa/src/secret_key.rs

71 lines
2.0 KiB
Rust
Raw Normal View History

use std::{convert::TryFrom, marker::PhantomData};
2019-12-02 21:58:19 -08:00
2019-12-03 15:59:24 -08:00
use crate::{
Binding, Error, PublicKey, PublicKeyBytes, Randomizer, Scalar, SigType, Signature, SpendAuth,
};
2019-12-02 21:58:19 -08:00
2019-12-03 15:39:55 -08:00
use rand_core::{CryptoRng, RngCore};
2019-12-02 21:58:19 -08:00
/// A RedJubJub secret key.
#[derive(Copy, Clone, Debug)]
pub struct SecretKey<T: SigType> {
2019-12-03 14:51:38 -08:00
sk: Scalar,
pk: PublicKey<T>,
2019-12-02 21:58:19 -08:00
}
2019-12-03 15:59:24 -08:00
impl<T: SigType> From<SecretKey<T>> for [u8; 32] {
fn from(sk: SecretKey<T>) -> [u8; 32] {
sk.sk.to_bytes()
2019-12-02 21:58:19 -08:00
}
}
impl<T: SigType> From<[u8; 32]> for SecretKey<T> {
fn from(bytes: [u8; 32]) -> Self {
let sk = {
// XXX-jubjub: would be nice to unconditionally deser
// This incantation ensures deserialization is infallible.
let mut wide = [0; 64];
wide[0..32].copy_from_slice(&bytes);
Scalar::from_bytes_wide(&wide)
};
let pk = PublicKey::from_secret(&sk);
SecretKey { sk, pk }
2019-12-03 14:51:38 -08:00
}
}
impl<T: SigType> SecretKey<T> {
/// Generate a new secret key.
pub fn new<R: RngCore + CryptoRng>(mut rng: R) -> SecretKey<T> {
let sk = {
let mut bytes = [0; 64];
rng.fill_bytes(&mut bytes);
Scalar::from_bytes_wide(&bytes)
};
let pk = PublicKey::from_secret(&sk);
SecretKey { sk, pk }
2019-12-03 15:39:55 -08:00
}
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 SecretKey<Binding> {
/// Create a Zcash `BindingSig` on `msg` using this `SecretKey`.
// Similar to signature::Signer but without boxed errors.
pub fn sign(&self, msg: &[u8]) -> Signature<Binding> {
// could use sign_inner
unimplemented!();
}
}
2019-12-02 22:32:55 -08:00
impl SecretKey<SpendAuth> {
/// Create a Zcash `SpendAuthSig` on `msg` using this `SecretKey`.
2019-12-02 22:32:55 -08:00
// Similar to signature::Signer but without boxed errors.
pub fn sign(&self, msg: &[u8]) -> Signature<SpendAuth> {
// could use sign_inner
2019-12-02 22:20:21 -08:00
unimplemented!();
}
}