// -*- 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}, marker::PhantomData, }; use crate::{Error, Randomizer, Scalar, 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 { sk: Scalar, pk: VerificationKey, } impl<'a, T: SigType> From<&'a SigningKey> for VerificationKey { fn from(sk: &'a SigningKey) -> VerificationKey { sk.pk.clone() } } impl From> for [u8; 32] { fn from(sk: SigningKey) -> [u8; 32] { sk.sk.to_bytes() } } impl TryFrom<[u8; 32]> for SigningKey { type Error = Error; fn try_from(bytes: [u8; 32]) -> Result { // XXX-jubjub: this should not use CtOption let maybe_sk = Scalar::from_bytes(&bytes); if maybe_sk.is_some().into() { let sk = maybe_sk.unwrap(); let pk = VerificationKey::from(&sk); Ok(SigningKey { sk, pk }) } else { Err(Error::MalformedSigningKey) } } } #[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 sk = &self.sk + randomizer; let pk = VerificationKey::from(&sk); SigningKey { sk, pk } } } impl SigningKey { /// Generate a new signing key. pub fn new(mut rng: R) -> SigningKey { let sk = { let mut bytes = [0; 64]; rng.fill_bytes(&mut bytes); Scalar::from_bytes_wide(&bytes) }; let pk = VerificationKey::from(&sk); SigningKey { sk, pk } } /// Create a signature of type `T` on `msg` using this `SigningKey`. // Similar to signature::Signer but without boxed errors. pub fn sign(&self, mut rng: R, msg: &[u8]) -> Signature { use crate::HStar; // Choose a byte sequence uniformly at random of length // (\ell_H + 128)/8 bytes. For RedJubjub this is (512 + 128)/8 = 80. let random_bytes = { let mut bytes = [0; 80]; rng.fill_bytes(&mut bytes); bytes }; let nonce = HStar::default() .update(&random_bytes[..]) .update(&self.pk.bytes.bytes[..]) // XXX ugly .update(msg) .finalize(); let r_bytes = jubjub::AffinePoint::from(&T::basepoint() * &nonce).to_bytes(); let c = HStar::default() .update(&r_bytes[..]) .update(&self.pk.bytes.bytes[..]) // XXX ugly .update(msg) .finalize(); let s_bytes = (&nonce + &(&c * &self.sk)).to_bytes(); Signature { r_bytes, s_bytes, _marker: PhantomData, } } }