mirror of
https://github.com/serai-dex/serai.git
synced 2025-12-08 20:29:23 +00:00
This helps identify where the various functionalities are used, or rather, not used. The `Ciphersuite` trait present in `patches/ciphersuite`, facilitating the entire FCMP++ tree, only requires the markers _and_ canonical point decoding. I've opened a PR to upstream such a trait into `group` (https://github.com/zkcrypto/group/pull/68). `WrappedGroup` is still justified for as long as `Group::generator` exists. Moving `::generator()` to its own trait, on an independent structure (upstream) would be massively appreciated. @tarcieri also wanted to update from `fn generator()` to `const GENERATOR`, which would encourage further discussion on https://github.com/zkcrypto/group/issues/32 and https://github.com/zkcrypto/group/issues/45, which have been stagnant. The `Id` trait is occasionally used yet really should be first off the chopping block. Finally, `WithPreferredHash` is only actually used around a third of the time, which more than justifies it being a separate trait. --- Updates `dalek_ff_group::Scalar` to directly re-export `curve25519_dalek::Scalar`, as without issue. `dalek_ff_group::RistrettoPoint` also could be replaced with an export of `curve25519_dalek::RistrettoPoint`, yet the coordinator relies on how we implemented `Hash` on it for the hell of it so it isn't worth it at this time. `dalek_ff_group::EdwardsPoint` can't be replaced for an re-export of `curve25519_dalek::SubgroupPoint` as it doesn't implement `zeroize`, `subtle` traits within a released, non-yanked version. Relevance to https://github.com/serai-dex/serai/issues/201 and https://github.com/dalek-cryptography/curve25519-dalek/issues/811#issuecomment-3247732746. Also updates the `Ristretto` ciphersuite to prefer `Blake2b-512` over `SHA2-512`. In order to maintain compliance with FROST's IETF standard, `modular-frost` defines its own ciphersuite for Ristretto which still uses `SHA2-512`.
126 lines
4.4 KiB
Rust
126 lines
4.4 KiB
Rust
use core::{ops::Deref, convert::AsRef};
|
|
use std::io::{self, Read};
|
|
|
|
use rand_core::{RngCore, CryptoRng};
|
|
|
|
use zeroize::{Zeroize, Zeroizing};
|
|
use subtle::ConstantTimeEq;
|
|
|
|
use ciphersuite::group::{
|
|
ff::{Field, PrimeField},
|
|
Group,
|
|
};
|
|
pub use ciphersuite::{digest::Digest, WrappedGroup, GroupIo, Ciphersuite};
|
|
|
|
#[cfg(any(feature = "ristretto", feature = "ed25519"))]
|
|
mod dalek;
|
|
#[cfg(any(feature = "ristretto", feature = "ed25519"))]
|
|
pub use dalek::*;
|
|
|
|
#[cfg(any(feature = "secp256k1", feature = "p256"))]
|
|
mod kp256;
|
|
#[cfg(feature = "secp256k1")]
|
|
pub use kp256::{Secp256k1, IetfSecp256k1Hram};
|
|
#[cfg(feature = "p256")]
|
|
pub use kp256::{P256, IetfP256Hram};
|
|
|
|
#[cfg(feature = "ed448")]
|
|
mod ed448;
|
|
#[cfg(feature = "ed448")]
|
|
pub use ed448::{Ed448, IetfEd448Hram};
|
|
#[cfg(all(test, feature = "ed448"))]
|
|
pub(crate) use ed448::Ietf8032Ed448Hram;
|
|
|
|
/// FROST Ciphersuite.
|
|
///
|
|
/// This excludes the signing algorithm specific H2, making this solely the curve, its associated
|
|
/// hash function, and the functions derived from it.
|
|
pub trait Curve: GroupIo + Ciphersuite {
|
|
/// Context string for this curve.
|
|
const CONTEXT: &[u8];
|
|
|
|
/// Hash the given dst and data to a byte vector. Used to instantiate H4 and H5.
|
|
fn hash(dst: &[u8], data: &[u8]) -> impl AsRef<[u8]> {
|
|
Self::H::digest([Self::CONTEXT, dst, data].concat())
|
|
}
|
|
|
|
/// Field element from hash. Used to instantiate H1 and H3.
|
|
///
|
|
/// The `dst` MUST be prefixed by `Self::CONTEXT` by the implementor.
|
|
#[allow(non_snake_case)]
|
|
fn hash_to_F(dst: &[u8], msg: &[u8]) -> Self::F;
|
|
|
|
/// Hash the message for the binding factor. H4 from the IETF draft.
|
|
fn hash_msg(msg: &[u8]) -> impl AsRef<[u8]> {
|
|
Self::hash(b"msg", msg)
|
|
}
|
|
|
|
/// Hash the commitments for the binding factor. H5 from the IETF draft.
|
|
fn hash_commitments(commitments: &[u8]) -> impl AsRef<[u8]> {
|
|
Self::hash(b"com", commitments)
|
|
}
|
|
|
|
/// Hash the commitments and message to calculate the binding factor. H1 from the IETF draft.
|
|
//
|
|
// This may return 0, which is invalid according to the FROST preprint, as all binding factors
|
|
// are expected to be in the multiplicative subgroup. This isn't a practical issue, as there's a
|
|
// negligible probability of this returning 0.
|
|
//
|
|
// When raised in
|
|
// https://github.com/cfrg/draft-irtf-cfrg-frost/issues/451#issuecomment-1715985505,
|
|
// the negligible probbility was seen as sufficient reason not to edit the spec to be robust in
|
|
// this regard.
|
|
//
|
|
// While that decision may be disagreeable, this library cannot implement a robust scheme while
|
|
// following the specification. Following the specification is preferred to being robust against
|
|
// an impractical probability enabling a complex attack (made infeasible by the impractical
|
|
// probability required).
|
|
//
|
|
// We could still panic on the 0-hash, preferring correctness to liveliness. Finding the 0-hash
|
|
// is as computationally complex as simply calculating the group key's discrete log however,
|
|
// making it not worth having a panic (as this library is expected not to panic).
|
|
fn hash_binding_factor(binding: &[u8]) -> Self::F {
|
|
<Self as Curve>::hash_to_F(b"rho", binding)
|
|
}
|
|
|
|
/// Securely generate a random nonce. H3 from the IETF draft.
|
|
fn random_nonce<R: RngCore + CryptoRng>(
|
|
secret: &Zeroizing<Self::F>,
|
|
rng: &mut R,
|
|
) -> Zeroizing<Self::F> {
|
|
let mut seed = Zeroizing::new(vec![0; 32]);
|
|
rng.fill_bytes(seed.as_mut());
|
|
|
|
let mut repr = secret.to_repr();
|
|
|
|
// Perform rejection sampling until we reach a non-zero nonce
|
|
// While the IETF spec doesn't explicitly require this, generating a zero nonce will produce
|
|
// commitments which will be rejected for being zero (and if they were used, leak the secret
|
|
// share)
|
|
// Rejection sampling here will prevent an honest participant from ever generating 'malicious'
|
|
// values and ensure safety
|
|
let mut res;
|
|
while {
|
|
seed.extend(repr.as_ref());
|
|
res = Zeroizing::new(<Self as Curve>::hash_to_F(b"nonce", seed.deref()));
|
|
res.ct_eq(&Self::F::ZERO).into()
|
|
} {
|
|
seed = Zeroizing::new(vec![0; 32]);
|
|
rng.fill_bytes(&mut seed);
|
|
}
|
|
repr.as_mut().zeroize();
|
|
|
|
res
|
|
}
|
|
|
|
/// Read a point from a reader, rejecting identity.
|
|
#[allow(non_snake_case)]
|
|
fn read_G<R: Read>(reader: &mut R) -> io::Result<Self::G> {
|
|
let res = <Self as GroupIo>::read_G(reader)?;
|
|
if res.is_identity().into() {
|
|
Err(io::Error::other("identity point"))?;
|
|
}
|
|
Ok(res)
|
|
}
|
|
}
|