// -*- mode: rust; -*- // // This file is part of redjubjub. // Copyright (c) 2019-2021 Zcash Foundation // See LICENSE for licensing information. // // Authors: // - Deirdre Connolly // - Henry de Valence use std::convert::{TryFrom, TryInto}; use crate::{Error, Randomizer, SigType, Signature, SpendAuth, VerificationKey}; use rand_core::{CryptoRng, RngCore}; /// A RedJubJub signing key. #[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"))] pub struct SigningKey(reddsa::SigningKey); impl<'a, T: SigType> From<&'a SigningKey> for VerificationKey { fn from(sk: &'a SigningKey) -> VerificationKey { let reddsa_vk = reddsa::VerificationKey::<_>::from(&sk.0); VerificationKey(reddsa_vk) } } impl From> for [u8; 32] { fn from(sk: SigningKey) -> [u8; 32] { sk.0.into() } } impl TryFrom<[u8; 32]> for SigningKey { type Error = Error; fn try_from(bytes: [u8; 32]) -> Result { let reddsa_sk = reddsa::SigningKey::<_>::try_from(bytes)?; Ok(SigningKey(reddsa_sk)) } } #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] struct SerdeHelper([u8; 32]); impl TryFrom for SigningKey { type Error = Error; fn try_from(helper: SerdeHelper) -> Result { helper.0.try_into() } } impl From> for SerdeHelper { fn from(sk: SigningKey) -> Self { Self(sk.into()) } } impl SigningKey { /// Randomize this public key with the given `randomizer`. pub fn randomize(&self, randomizer: &Randomizer) -> SigningKey { let reddsa_sk = self.0.randomize(randomizer); SigningKey(reddsa_sk) } } impl SigningKey { /// Generate a new signing key. pub fn new(rng: R) -> SigningKey { let reddsa_sk = reddsa::SigningKey::new(rng); SigningKey(reddsa_sk) } /// Create a signature of type `T` on `msg` using this `SigningKey`. // Similar to signature::Signer but without boxed errors. pub fn sign(&self, rng: R, msg: &[u8]) -> Signature { let reddsa_sig = self.0.sign(rng, msg); Signature(reddsa_sig) } }