#![doc = include_str!("redjubjub/README.md")]
#![allow(non_snake_case)]
#![deny(missing_docs)]
use alloc::collections::BTreeMap;
use frost_rerandomized::RandomizedCiphersuite;
use group::GroupEncoding;
#[cfg(feature = "alloc")]
use group::{ff::Field as _, ff::PrimeField, Group as _};
pub mod rerandomized;
#[cfg(feature = "serde")]
pub use frost_rerandomized::frost_core::serde;
pub use frost_rerandomized::frost_core::{
self as frost, Ciphersuite, Field, FieldError, Group, GroupError,
};
pub use rand_core;
use rand_core::{CryptoRng, RngCore};
use crate::{hash::HStar, private::Sealed, sapling};
pub type Error = frost_rerandomized::frost_core::Error<JubjubBlake2b512>;
#[derive(Clone, Copy)]
pub struct JubjubScalarField;
impl Field for JubjubScalarField {
type Scalar = jubjub::Scalar;
type Serialization = [u8; 32];
fn zero() -> Self::Scalar {
Self::Scalar::zero()
}
fn one() -> Self::Scalar {
Self::Scalar::one()
}
fn invert(scalar: &Self::Scalar) -> Result<Self::Scalar, FieldError> {
if *scalar == <Self as Field>::zero() {
Err(FieldError::InvalidZeroScalar)
} else {
Ok(Self::Scalar::invert(scalar).unwrap())
}
}
fn random<R: RngCore + CryptoRng>(rng: &mut R) -> Self::Scalar {
Self::Scalar::random(rng)
}
fn serialize(scalar: &Self::Scalar) -> Self::Serialization {
scalar.to_bytes()
}
fn little_endian_serialize(scalar: &Self::Scalar) -> Self::Serialization {
Self::serialize(scalar)
}
fn deserialize(buf: &Self::Serialization) -> Result<Self::Scalar, FieldError> {
match Self::Scalar::from_repr(*buf).into() {
Some(s) => Ok(s),
None => Err(FieldError::MalformedScalar),
}
}
}
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct JubjubGroup;
impl Group for JubjubGroup {
type Field = JubjubScalarField;
type Element = jubjub::SubgroupPoint;
type Serialization = [u8; 32];
fn cofactor() -> <Self::Field as Field>::Scalar {
Self::Field::one()
}
fn identity() -> Self::Element {
Self::Element::identity()
}
fn generator() -> Self::Element {
let basepoint: jubjub::AffinePoint = sapling::SpendAuth::basepoint().into();
jubjub::SubgroupPoint::from_raw_unchecked(basepoint.get_u(), basepoint.get_v())
}
fn serialize(element: &Self::Element) -> Result<Self::Serialization, GroupError> {
if *element == Self::identity() {
return Err(GroupError::InvalidIdentityElement);
}
Ok(element.to_bytes())
}
fn deserialize(buf: &Self::Serialization) -> Result<Self::Element, GroupError> {
let point = Self::Element::from_bytes(buf);
match Option::<Self::Element>::from(point) {
Some(point) => {
if point == Self::identity() {
Err(GroupError::InvalidIdentityElement)
} else {
Ok(point)
}
}
None => Err(GroupError::MalformedElement),
}
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct JubjubBlake2b512;
impl Ciphersuite for JubjubBlake2b512 {
const ID: &'static str = "FROST(Jubjub, BLAKE2b-512)";
type Group = JubjubGroup;
type HashOutput = [u8; 64];
type SignatureSerialization = [u8; 64];
fn H1(m: &[u8]) -> <<Self::Group as Group>::Field as Field>::Scalar {
HStar::<sapling::SpendAuth>::new(b"FROST_RedJubjubR")
.update(m)
.finalize()
}
fn H2(m: &[u8]) -> <<Self::Group as Group>::Field as Field>::Scalar {
HStar::<sapling::SpendAuth>::default().update(m).finalize()
}
fn H3(m: &[u8]) -> <<Self::Group as Group>::Field as Field>::Scalar {
HStar::<sapling::SpendAuth>::new(b"FROST_RedJubjubN")
.update(m)
.finalize()
}
fn H4(m: &[u8]) -> Self::HashOutput {
let mut state = blake2b_simd::Params::new()
.hash_length(64)
.personal(b"FROST_RedJubjubM")
.to_state();
*state.update(m).finalize().as_array()
}
fn H5(m: &[u8]) -> Self::HashOutput {
let mut state = blake2b_simd::Params::new()
.hash_length(64)
.personal(b"FROST_RedJubjubC")
.to_state();
*state.update(m).finalize().as_array()
}
fn HDKG(m: &[u8]) -> Option<<<Self::Group as Group>::Field as Field>::Scalar> {
Some(
HStar::<sapling::SpendAuth>::new(b"FROST_RedJubjubD")
.update(m)
.finalize(),
)
}
fn HID(m: &[u8]) -> Option<<<Self::Group as Group>::Field as Field>::Scalar> {
Some(
HStar::<sapling::SpendAuth>::new(b"FROST_RedJubjubI")
.update(m)
.finalize(),
)
}
}
impl RandomizedCiphersuite for JubjubBlake2b512 {
fn hash_randomizer(m: &[u8]) -> Option<<<Self::Group as Group>::Field as Field>::Scalar> {
Some(
HStar::<sapling::SpendAuth>::new(b"FROST_RedJubjubA")
.update(m)
.finalize(),
)
}
}
type J = JubjubBlake2b512;
pub type Identifier = frost::Identifier<J>;
pub mod keys {
use alloc::collections::BTreeMap;
use super::*;
pub type IdentifierList<'a> = frost::keys::IdentifierList<'a, J>;
pub fn generate_with_dealer<RNG: RngCore + CryptoRng>(
max_signers: u16,
min_signers: u16,
identifiers: IdentifierList,
mut rng: RNG,
) -> Result<(BTreeMap<Identifier, SecretShare>, PublicKeyPackage), Error> {
frost::keys::generate_with_dealer(max_signers, min_signers, identifiers, &mut rng)
}
pub fn split<R: RngCore + CryptoRng>(
key: &SigningKey,
max_signers: u16,
min_signers: u16,
identifiers: IdentifierList,
rng: &mut R,
) -> Result<(BTreeMap<Identifier, SecretShare>, PublicKeyPackage), Error> {
frost::keys::split(key, max_signers, min_signers, identifiers, rng)
}
pub type SecretShare = frost::keys::SecretShare<J>;
pub type SigningShare = frost::keys::SigningShare<J>;
pub type VerifyingShare = frost::keys::VerifyingShare<J>;
pub type KeyPackage = frost::keys::KeyPackage<J>;
pub type PublicKeyPackage = frost::keys::PublicKeyPackage<J>;
pub type VerifiableSecretSharingCommitment = frost::keys::VerifiableSecretSharingCommitment<J>;
pub mod dkg;
pub mod repairable;
}
pub mod round1 {
use frost_rerandomized::frost_core::keys::SigningShare;
use super::*;
pub type SigningNonces = frost::round1::SigningNonces<J>;
pub type SigningCommitments = frost::round1::SigningCommitments<J>;
pub type NonceCommitment = frost::round1::NonceCommitment<J>;
pub fn commit<RNG>(
secret: &SigningShare<J>,
rng: &mut RNG,
) -> (SigningNonces, SigningCommitments)
where
RNG: CryptoRng + RngCore,
{
frost::round1::commit::<J, RNG>(secret, rng)
}
}
pub type SigningPackage = frost::SigningPackage<J>;
pub mod round2 {
use super::*;
pub type SignatureShare = frost::round2::SignatureShare<J>;
pub fn sign(
signing_package: &SigningPackage,
signer_nonces: &round1::SigningNonces,
key_package: &keys::KeyPackage,
) -> Result<SignatureShare, Error> {
frost::round2::sign(signing_package, signer_nonces, key_package)
}
}
pub type Signature = frost_rerandomized::frost_core::Signature<J>;
pub fn aggregate(
signing_package: &SigningPackage,
signature_shares: &BTreeMap<Identifier, round2::SignatureShare>,
pubkeys: &keys::PublicKeyPackage,
) -> Result<Signature, Error> {
frost::aggregate(signing_package, signature_shares, pubkeys)
}
pub type CheaterDetection = frost::CheaterDetection;
pub fn aggregate_custom(
signing_package: &SigningPackage,
signature_shares: &BTreeMap<Identifier, round2::SignatureShare>,
pubkeys: &keys::PublicKeyPackage,
cheater_detection: CheaterDetection,
) -> Result<Signature, Error> {
frost::aggregate_custom(
signing_package,
signature_shares,
pubkeys,
cheater_detection,
)
}
pub type SigningKey = frost_rerandomized::frost_core::SigningKey<J>;
pub type VerifyingKey = frost_rerandomized::frost_core::VerifyingKey<J>;
#[cfg(test)]
mod test {
use super::*;
use group::cofactor::CofactorGroup;
#[test]
fn basepoint_is_in_prime_subgroup() {
let basepoint: jubjub::AffinePoint = sapling::SpendAuth::basepoint().into();
assert!(Into::<bool>::into(basepoint.is_prime_order()));
assert!(Into::<bool>::into(
basepoint.to_extended().into_subgroup().is_some()
));
}
}