redjubjub/src/signing_key.rs

86 lines
2.6 KiB
Rust
Raw Normal View History

// -*- mode: rust; -*-
//
// This file is part of redjubjub.
// Copyright (c) 2019-2021 Zcash Foundation
// See LICENSE for licensing information.
//
// Authors:
// - Deirdre Connolly <deirdre@zfnd.org>
// - Henry de Valence <hdevalence@hdevalence.ca>
2023-02-01 15:22:37 -08:00
use std::convert::{TryFrom, TryInto};
2019-12-02 21:58:19 -08:00
2021-03-01 06:54:52 -08:00
use crate::{Error, Randomizer, SigType, Signature, SpendAuth, VerificationKey};
2019-12-02 21:58:19 -08:00
2019-12-03 15:39:55 -08:00
use rand_core::{CryptoRng, RngCore};
/// A RedJubJub signing key.
2019-12-02 21:58:19 -08:00
#[derive(Copy, Clone, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(try_from = "SerdeHelper"))]
#[cfg_attr(feature = "serde", serde(into = "SerdeHelper"))]
#[cfg_attr(feature = "serde", serde(bound = "T: SigType"))]
2023-02-01 15:22:37 -08:00
pub struct SigningKey<T: SigType>(reddsa::SigningKey<T::RedDSASigType>);
2019-12-02 21:58:19 -08:00
impl<'a, T: SigType> From<&'a SigningKey<T>> for VerificationKey<T> {
fn from(sk: &'a SigningKey<T>) -> VerificationKey<T> {
2023-02-01 15:22:37 -08:00
let reddsa_vk = reddsa::VerificationKey::<_>::from(&sk.0);
VerificationKey(reddsa_vk)
2019-12-03 22:32:30 -08:00
}
}
impl<T: SigType> From<SigningKey<T>> for [u8; 32] {
fn from(sk: SigningKey<T>) -> [u8; 32] {
2023-02-01 15:22:37 -08:00
sk.0.into()
2019-12-02 21:58:19 -08:00
}
}
impl<T: SigType> TryFrom<[u8; 32]> for SigningKey<T> {
type Error = Error;
fn try_from(bytes: [u8; 32]) -> Result<Self, Self::Error> {
2023-02-01 15:22:37 -08:00
let reddsa_sk = reddsa::SigningKey::<_>::try_from(bytes)?;
Ok(SigningKey(reddsa_sk))
2019-12-03 14:51:38 -08:00
}
}
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
struct SerdeHelper([u8; 32]);
impl<T: SigType> TryFrom<SerdeHelper> for SigningKey<T> {
type Error = Error;
fn try_from(helper: SerdeHelper) -> Result<Self, Self::Error> {
helper.0.try_into()
}
}
impl<T: SigType> From<SigningKey<T>> for SerdeHelper {
fn from(sk: SigningKey<T>) -> Self {
Self(sk.into())
}
}
impl SigningKey<SpendAuth> {
2019-12-04 17:00:55 -08:00
/// Randomize this public key with the given `randomizer`.
pub fn randomize(&self, randomizer: &Randomizer) -> SigningKey<SpendAuth> {
2023-02-01 15:22:37 -08:00
let reddsa_sk = self.0.randomize(randomizer);
SigningKey(reddsa_sk)
2019-12-04 17:00:55 -08:00
}
}
impl<T: SigType> SigningKey<T> {
/// Generate a new signing key.
2023-02-01 15:22:37 -08:00
pub fn new<R: RngCore + CryptoRng>(rng: R) -> SigningKey<T> {
let reddsa_sk = reddsa::SigningKey::new(rng);
SigningKey(reddsa_sk)
2019-12-03 15:39:55 -08:00
}
/// Create a signature of type `T` on `msg` using this `SigningKey`.
// Similar to signature::Signer but without boxed errors.
2023-02-01 15:22:37 -08:00
pub fn sign<R: RngCore + CryptoRng>(&self, rng: R, msg: &[u8]) -> Signature<T> {
let reddsa_sig = self.0.sign(rng, msg);
Signature(reddsa_sig)
2019-12-02 22:20:21 -08:00
}
}