#![allow(non_snake_case)] #![deny(missing_docs)] #![doc = include_str!("../README.md")] use k256::{ elliptic_curve::{ group::prime::PrimeCurveAffine, hash2curve::{hash_to_field, ExpandMsgXmd}, sec1::{FromEncodedPoint, ToEncodedPoint}, Field as FFField, PrimeField, }, AffinePoint, ProjectivePoint, Scalar, }; use rand_core::{CryptoRng, RngCore}; use sha2::{Digest, Sha256}; use frost_core::frost; #[cfg(test)] mod tests; // Re-exports in our public API pub use frost_core::{Ciphersuite, Field, FieldError, Group, GroupError}; pub use rand_core; /// An error. pub type Error = frost_core::Error; /// An implementation of the FROST(secp256k1, SHA-256) ciphersuite scalar field. #[derive(Clone, Copy)] pub struct Secp256K1ScalarField; impl Field for Secp256K1ScalarField { type Scalar = Scalar; type Serialization = [u8; 32]; fn zero() -> Self::Scalar { Scalar::ZERO } fn one() -> Self::Scalar { Scalar::ONE } fn invert(scalar: &Self::Scalar) -> Result { // [`Scalar`]'s Eq/PartialEq does a constant-time comparison if *scalar == ::zero() { Err(FieldError::InvalidZeroScalar) } else { Ok(scalar.invert().unwrap()) } } fn random(rng: &mut R) -> Self::Scalar { Scalar::random(rng) } fn serialize(scalar: &Self::Scalar) -> Self::Serialization { scalar.to_bytes().into() } fn deserialize(buf: &Self::Serialization) -> Result { let field_bytes: &k256::FieldBytes = buf.into(); match Scalar::from_repr(*field_bytes).into() { Some(s) => Ok(s), None => Err(FieldError::MalformedScalar), } } fn little_endian_serialize(scalar: &Self::Scalar) -> Self::Serialization { let mut array = Self::serialize(scalar); array.reverse(); array } } /// An implementation of the FROST(secp256k1, SHA-256) ciphersuite group. #[derive(Clone, Copy, PartialEq, Eq)] pub struct Secp256K1Group; impl Group for Secp256K1Group { type Field = Secp256K1ScalarField; type Element = ProjectivePoint; /// [SEC 1][1] serialization of a compressed point in secp256k1 takes 33 bytes /// (1-byte prefix and 32 bytes for the coordinate). /// /// Note that, in the SEC 1 spec, the identity is encoded as a single null byte; /// but here we pad with zeroes. This is acceptable as the identity _should_ never /// be serialized in FROST, else we error. /// /// [1]: https://secg.org/sec1-v2.pdf type Serialization = [u8; 33]; fn cofactor() -> ::Scalar { Scalar::ONE } fn identity() -> Self::Element { ProjectivePoint::IDENTITY } fn generator() -> Self::Element { ProjectivePoint::GENERATOR } fn serialize(element: &Self::Element) -> Self::Serialization { let mut fixed_serialized = [0; 33]; let serialized_point = element.to_affine().to_encoded_point(true); let serialized = serialized_point.as_bytes(); // Sanity check; either it takes all bytes or a single byte (identity). assert!(serialized.len() == fixed_serialized.len() || serialized.len() == 1); // Copy to the left of the buffer (i.e. pad the identity with zeroes). // TODO: Note that identity elements shouldn't be serialized in FROST. This will likely become // part of the API and when that happens, we should return an error instead of // doing this padding. { let (left, _right) = fixed_serialized.split_at_mut(serialized.len()); left.copy_from_slice(serialized); } fixed_serialized } fn deserialize(buf: &Self::Serialization) -> Result { let encoded_point = k256::EncodedPoint::from_bytes(buf).map_err(|_| GroupError::MalformedElement)?; match Option::::from(AffinePoint::from_encoded_point(&encoded_point)) { Some(point) => { if point.is_identity().into() { // This is actually impossible since the identity is encoded a a single byte // which will never happen since we receive a 33-byte buffer. // We leave the check for consistency. Err(GroupError::InvalidIdentityElement) } else { Ok(ProjectivePoint::from(point)) } } None => Err(GroupError::MalformedElement), } } } fn hash_to_array(inputs: &[&[u8]]) -> [u8; 32] { let mut h = Sha256::new(); for i in inputs { h.update(i); } let mut output = [0u8; 32]; output.copy_from_slice(h.finalize().as_slice()); output } fn hash_to_scalar(domain: &[u8], msg: &[u8]) -> Scalar { let mut u = [Secp256K1ScalarField::zero()]; hash_to_field::, Scalar>(&[msg], &[domain], &mut u) .expect("should never return error according to error cases described in ExpandMsgXmd"); u[0] } /// Context string from the ciphersuite in the [spec]. /// /// [spec]: https://www.ietf.org/archive/id/draft-irtf-cfrg-frost-11.html#section-6.5-1 const CONTEXT_STRING: &str = "FROST-secp256k1-SHA256-v11"; /// An implementation of the FROST(secp256k1, SHA-256) ciphersuite. #[derive(Clone, Copy, PartialEq, Eq, Debug)] pub struct Secp256K1Sha256; impl Ciphersuite for Secp256K1Sha256 { type Group = Secp256K1Group; type HashOutput = [u8; 32]; type SignatureSerialization = [u8; 65]; /// H1 for FROST(secp256k1, SHA-256) /// /// [spec]: https://www.ietf.org/archive/id/draft-irtf-cfrg-frost-11.html#section-6.5-2.2.2.1 fn H1(m: &[u8]) -> <::Field as Field>::Scalar { hash_to_scalar((CONTEXT_STRING.to_owned() + "rho").as_bytes(), m) } /// H2 for FROST(secp256k1, SHA-256) /// /// [spec]: https://www.ietf.org/archive/id/draft-irtf-cfrg-frost-11.html#section-6.5-2.2.2.2 fn H2(m: &[u8]) -> <::Field as Field>::Scalar { hash_to_scalar((CONTEXT_STRING.to_owned() + "chal").as_bytes(), m) } /// H3 for FROST(secp256k1, SHA-256) /// /// [spec]: https://www.ietf.org/archive/id/draft-irtf-cfrg-frost-11.html#section-6.5-2.2.2.3 fn H3(m: &[u8]) -> <::Field as Field>::Scalar { hash_to_scalar((CONTEXT_STRING.to_owned() + "nonce").as_bytes(), m) } /// H4 for FROST(secp256k1, SHA-256) /// /// [spec]: https://www.ietf.org/archive/id/draft-irtf-cfrg-frost-11.html#section-6.5-2.2.2.4 fn H4(m: &[u8]) -> Self::HashOutput { hash_to_array(&[CONTEXT_STRING.as_bytes(), b"msg", m]) } /// H5 for FROST(secp256k1, SHA-256) /// /// [spec]: https://www.ietf.org/archive/id/draft-irtf-cfrg-frost-11.html#section-6.5-2.2.2.5 fn H5(m: &[u8]) -> Self::HashOutput { hash_to_array(&[CONTEXT_STRING.as_bytes(), b"com", m]) } /// HDKG for FROST(secp256k1, SHA-256) fn HDKG(m: &[u8]) -> Option<<::Field as Field>::Scalar> { Some(hash_to_scalar( (CONTEXT_STRING.to_owned() + "dkg").as_bytes(), m, )) } } type S = Secp256K1Sha256; /// A FROST(secp256k1, SHA-256) participant identifier. pub type Identifier = frost::Identifier; /// FROST(secp256k1, SHA-256) keys, key generation, key shares. pub mod keys { use super::*; use std::collections::HashMap; /// Allows all participants' keys to be generated using a central, trusted /// dealer. pub fn keygen_with_dealer( max_signers: u16, min_signers: u16, mut rng: RNG, ) -> Result<(HashMap, PublicKeyPackage), Error> { frost::keys::keygen_with_dealer(max_signers, min_signers, &mut rng) } /// Secret and public key material generated by a dealer performing /// [`keygen_with_dealer`]. /// /// # Security /// /// To derive a FROST(secp256k1, SHA-256) keypair, the receiver of the [`SecretShare`] *must* call /// .into(), which under the hood also performs validation. pub type SecretShare = frost::keys::SecretShare; /// A FROST(secp256k1, SHA-256) keypair, which can be generated either by a trusted dealer or using /// a DKG. /// /// When using a central dealer, [`SecretShare`]s are distributed to /// participants, who then perform verification, before deriving /// [`KeyPackage`]s, which they store to later use during signing. pub type KeyPackage = frost::keys::KeyPackage; /// Public data that contains all the signers' public keys as well as the /// group public key. /// /// Used for verification purposes before publishing a signature. pub type PublicKeyPackage = frost::keys::PublicKeyPackage; pub mod dkg; } /// FROST(secp256k1, SHA-256) Round 1 functionality and types. pub mod round1 { use frost_core::frost::keys::SigningShare; use super::*; /// Comprised of FROST(secp256k1, SHA-256) hiding and binding nonces. /// /// Note that [`SigningNonces`] must be used *only once* for a signing /// operation; re-using nonces will result in leakage of a signer's long-lived /// signing key. pub type SigningNonces = frost::round1::SigningNonces; /// Published by each participant in the first round of the signing protocol. /// /// This step can be batched if desired by the implementation. Each /// SigningCommitment can be used for exactly *one* signature. pub type SigningCommitments = frost::round1::SigningCommitments; /// Performed once by each participant selected for the signing operation. /// /// Generates the signing nonces and commitments to be used in the signing /// operation. pub fn commit( participant_identifier: frost::Identifier, secret: &SigningShare, rng: &mut RNG, ) -> (SigningNonces, SigningCommitments) where RNG: CryptoRng + RngCore, { frost::round1::commit::(participant_identifier, secret, rng) } } /// Generated by the coordinator of the signing operation and distributed to /// each signing party. pub type SigningPackage = frost::SigningPackage; /// FROST(secp256k1, SHA-256) Round 2 functionality and types, for signature share generation. pub mod round2 { use super::*; /// A FROST(secp256k1, SHA-256) participant's signature share, which the Coordinator will aggregate with all other signer's /// shares into the joint signature. pub type SignatureShare = frost::round2::SignatureShare; /// Generated by the coordinator of the signing operation and distributed to /// each signing party pub type SigningPackage = frost::SigningPackage; /// Performed once by each participant selected for the signing operation. /// /// Receives the message to be signed and a set of signing commitments and a set /// of randomizing commitments to be used in that signing operation, including /// that for this participant. /// /// Assumes the participant has already determined which nonce corresponds with /// the commitment that was assigned by the coordinator in the SigningPackage. pub fn sign( signing_package: &SigningPackage, signer_nonces: &round1::SigningNonces, key_package: &keys::KeyPackage, ) -> Result { frost::round2::sign(signing_package, signer_nonces, key_package) } } /// A Schnorr signature on FROST(secp256k1, SHA-256). pub type Signature = frost_core::Signature; /// Verifies each FROST(secp256k1, SHA-256) participant's signature share, and if all are valid, /// aggregates the shares into a signature to publish. /// /// Resulting signature is compatible with verification of a plain Schnorr /// signature. /// /// This operation is performed by a coordinator that can communicate with all /// the signing participants before publishing the final signature. The /// coordinator can be one of the participants or a semi-trusted third party /// (who is trusted to not perform denial of service attacks, but does not learn /// any secret information). Note that because the coordinator is trusted to /// report misbehaving parties in order to avoid publishing an invalid /// signature, if the coordinator themselves is a signer and misbehaves, they /// can avoid that step. However, at worst, this results in a denial of /// service attack due to publishing an invalid signature. pub fn aggregate( signing_package: &round2::SigningPackage, signature_shares: &[round2::SignatureShare], pubkeys: &keys::PublicKeyPackage, ) -> Result { frost::aggregate(signing_package, signature_shares, pubkeys) } /// A signing key for a Schnorr signature on FROST(secp256k1, SHA-256). pub type SigningKey = frost_core::SigningKey; /// A valid verifying key for Schnorr signatures on FROST(secp256k1, SHA-256). pub type VerifyingKey = frost_core::VerifyingKey;