#![allow(non_snake_case)]
#![deny(missing_docs)]
#![cfg_attr(docsrs, feature(doc_cfg))]
#![doc = include_str!("../README.md")]
#![doc = document_features::document_features!()]
extern crate alloc;
use alloc::collections::BTreeMap;
use ed448_goldilocks::{
curve::{edwards::CompressedEdwardsY, ExtendedPoint},
Scalar,
};
use frost_rerandomized::RandomizedCiphersuite;
use rand_core::{CryptoRng, RngCore};
use sha3::{
digest::{ExtendableOutput, Update, XofReader},
Shake256,
};
use frost_core as frost;
#[cfg(test)]
mod tests;
#[cfg(feature = "serde")]
pub use frost_core::serde;
pub use frost_core::{Ciphersuite, Field, FieldError, Group, GroupError};
pub use rand_core;
pub type Error = frost_core::Error<Ed448Shake256>;
#[derive(Clone, Copy)]
pub struct Ed448ScalarField;
impl Field for Ed448ScalarField {
type Scalar = Scalar;
type Serialization = [u8; 57];
fn zero() -> Self::Scalar {
Scalar::zero()
}
fn one() -> Self::Scalar {
Scalar::one()
}
fn invert(scalar: &Self::Scalar) -> Result<Self::Scalar, FieldError> {
if *scalar == <Self as Field>::zero() {
Err(FieldError::InvalidZeroScalar)
} else {
Ok(scalar.invert())
}
}
fn random<R: RngCore + CryptoRng>(rng: &mut R) -> Self::Scalar {
Scalar::random(rng)
}
fn serialize(scalar: &Self::Scalar) -> Self::Serialization {
scalar.to_bytes_rfc_8032()
}
fn deserialize(buf: &Self::Serialization) -> Result<Self::Scalar, FieldError> {
match Scalar::from_canonical_bytes(*buf) {
Some(s) => Ok(s),
None => Err(FieldError::MalformedScalar),
}
}
fn little_endian_serialize(scalar: &Self::Scalar) -> Self::Serialization {
Self::serialize(scalar)
}
}
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct Ed448Group;
impl Group for Ed448Group {
type Field = Ed448ScalarField;
type Element = ExtendedPoint;
type Serialization = [u8; 57];
fn cofactor() -> <Self::Field as Field>::Scalar {
Scalar::one()
}
fn identity() -> Self::Element {
Self::Element::identity()
}
fn generator() -> Self::Element {
Self::Element::generator()
}
fn serialize(element: &Self::Element) -> Result<Self::Serialization, GroupError> {
if *element == Self::identity() {
return Err(GroupError::InvalidIdentityElement);
}
Ok(element.compress().0)
}
fn deserialize(buf: &Self::Serialization) -> Result<Self::Element, GroupError> {
let compressed = CompressedEdwardsY(*buf);
match compressed.decompress() {
Some(point) => {
if point == Self::identity() {
Err(GroupError::InvalidIdentityElement)
} else if point.is_torsion_free() {
if point.compress().0 != compressed.0 {
Err(GroupError::MalformedElement)
} else {
Ok(point)
}
} else {
Err(GroupError::InvalidNonPrimeOrderElement)
}
}
None => Err(GroupError::MalformedElement),
}
}
}
fn hash_to_array(inputs: &[&[u8]]) -> [u8; 114] {
let mut h = Shake256::default();
for i in inputs {
h.update(i);
}
let mut reader = h.finalize_xof();
let mut output = [0u8; 114];
reader.read(&mut output);
output
}
fn hash_to_scalar(inputs: &[&[u8]]) -> Scalar {
let output = hash_to_array(inputs);
Scalar::from_bytes_mod_order_wide(&output)
}
const CONTEXT_STRING: &str = "FROST-ED448-SHAKE256-v1";
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct Ed448Shake256;
impl Ciphersuite for Ed448Shake256 {
const ID: &'static str = CONTEXT_STRING;
type Group = Ed448Group;
type HashOutput = [u8; 114];
type SignatureSerialization = [u8; 114];
fn H1(m: &[u8]) -> <<Self::Group as Group>::Field as Field>::Scalar {
hash_to_scalar(&[CONTEXT_STRING.as_bytes(), b"rho", m])
}
fn H2(m: &[u8]) -> <<Self::Group as Group>::Field as Field>::Scalar {
hash_to_scalar(&[b"SigEd448\0\0", m])
}
fn H3(m: &[u8]) -> <<Self::Group as Group>::Field as Field>::Scalar {
hash_to_scalar(&[CONTEXT_STRING.as_bytes(), b"nonce", m])
}
fn H4(m: &[u8]) -> Self::HashOutput {
hash_to_array(&[CONTEXT_STRING.as_bytes(), b"msg", m])
}
fn H5(m: &[u8]) -> Self::HashOutput {
hash_to_array(&[CONTEXT_STRING.as_bytes(), b"com", m])
}
fn HDKG(m: &[u8]) -> Option<<<Self::Group as Group>::Field as Field>::Scalar> {
Some(hash_to_scalar(&[CONTEXT_STRING.as_bytes(), b"dkg", m]))
}
fn HID(m: &[u8]) -> Option<<<Self::Group as Group>::Field as Field>::Scalar> {
Some(hash_to_scalar(&[CONTEXT_STRING.as_bytes(), b"id", m]))
}
}
impl RandomizedCiphersuite for Ed448Shake256 {
fn hash_randomizer(m: &[u8]) -> Option<<<Self::Group as Group>::Field as Field>::Scalar> {
Some(hash_to_scalar(&[
CONTEXT_STRING.as_bytes(),
b"randomizer",
m,
]))
}
}
type E = Ed448Shake256;
pub type Identifier = frost::Identifier<E>;
pub mod keys {
use super::*;
pub type IdentifierList<'a> = frost::keys::IdentifierList<'a, E>;
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>(
secret: &SigningKey,
max_signers: u16,
min_signers: u16,
identifiers: IdentifierList,
rng: &mut R,
) -> Result<(BTreeMap<Identifier, SecretShare>, PublicKeyPackage), Error> {
frost::keys::split(secret, max_signers, min_signers, identifiers, rng)
}
pub fn reconstruct(secret_shares: &[KeyPackage]) -> Result<SigningKey, Error> {
frost::keys::reconstruct(secret_shares)
}
pub type SecretShare = frost::keys::SecretShare<E>;
pub type SigningShare = frost::keys::SigningShare<E>;
pub type VerifyingShare = frost::keys::VerifyingShare<E>;
pub type KeyPackage = frost::keys::KeyPackage<E>;
pub type PublicKeyPackage = frost::keys::PublicKeyPackage<E>;
pub type VerifiableSecretSharingCommitment = frost::keys::VerifiableSecretSharingCommitment<E>;
pub mod dkg;
pub mod refresh;
pub mod repairable;
}
pub mod round1 {
use crate::keys::SigningShare;
use super::*;
pub type SigningNonces = frost::round1::SigningNonces<E>;
pub type SigningCommitments = frost::round1::SigningCommitments<E>;
pub type NonceCommitment = frost::round1::NonceCommitment<E>;
pub fn commit<RNG>(secret: &SigningShare, rng: &mut RNG) -> (SigningNonces, SigningCommitments)
where
RNG: CryptoRng + RngCore,
{
frost::round1::commit::<E, RNG>(secret, rng)
}
}
pub type SigningPackage = frost::SigningPackage<E>;
pub mod round2 {
use super::*;
pub type SignatureShare = frost::round2::SignatureShare<E>;
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_core::Signature<E>;
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_core::SigningKey<E>;
pub type VerifyingKey = frost_core::VerifyingKey<E>;