2022-09-29 04:47:55 -04:00
|
|
|
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
|
2023-05-01 04:58:50 -04:00
|
|
|
#![doc = include_str!("../README.md")]
|
2022-06-28 04:02:56 -04:00
|
|
|
#![no_std]
|
2022-05-03 07:20:24 -04:00
|
|
|
|
2023-03-28 04:43:10 -04:00
|
|
|
use zeroize::Zeroize;
|
|
|
|
|
|
Smash the singular `Ciphersuite` trait into multiple
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`.
2025-09-03 12:25:37 -04:00
|
|
|
use digest::{block_api::BlockSizeUser, Digest, Output, HashMarker};
|
2023-03-20 20:10:00 -04:00
|
|
|
|
2022-06-28 04:02:56 -04:00
|
|
|
#[cfg(feature = "merlin")]
|
2022-05-03 07:20:24 -04:00
|
|
|
mod merlin;
|
2022-06-28 04:02:56 -04:00
|
|
|
#[cfg(feature = "merlin")]
|
|
|
|
|
pub use crate::merlin::MerlinTranscript;
|
2022-05-03 07:20:24 -04:00
|
|
|
|
2023-03-20 20:10:00 -04:00
|
|
|
/// Tests for a transcript.
|
2023-03-07 02:25:10 -05:00
|
|
|
#[cfg(any(test, feature = "tests"))]
|
|
|
|
|
pub mod tests;
|
|
|
|
|
|
2023-03-20 20:10:00 -04:00
|
|
|
/// A transcript trait valid over a variety of transcript formats.
|
2023-03-07 05:30:21 -05:00
|
|
|
pub trait Transcript: Send + Clone {
|
|
|
|
|
type Challenge: Send + Sync + Clone + AsRef<[u8]>;
|
2022-06-28 04:02:56 -04:00
|
|
|
|
2022-09-29 05:25:29 -04:00
|
|
|
/// Create a new transcript with the specified name.
|
2022-07-12 01:28:01 -04:00
|
|
|
fn new(name: &'static [u8]) -> Self;
|
|
|
|
|
|
2022-09-29 05:25:29 -04:00
|
|
|
/// Apply a domain separator to the transcript.
|
2022-06-03 01:37:12 -04:00
|
|
|
fn domain_separate(&mut self, label: &'static [u8]);
|
2022-07-09 00:37:39 -04:00
|
|
|
|
2022-09-29 05:25:29 -04:00
|
|
|
/// Append a message to the transcript.
|
2022-11-05 18:43:36 -04:00
|
|
|
fn append_message<M: AsRef<[u8]>>(&mut self, label: &'static [u8], message: M);
|
2022-07-09 00:37:39 -04:00
|
|
|
|
2023-03-02 11:19:26 -05:00
|
|
|
/// Produce a challenge.
|
|
|
|
|
///
|
|
|
|
|
/// Implementors MUST update the transcript as it does so, preventing the same challenge from
|
|
|
|
|
/// being generated multiple times.
|
2022-06-28 04:02:56 -04:00
|
|
|
fn challenge(&mut self, label: &'static [u8]) -> Self::Challenge;
|
2022-07-09 00:37:39 -04:00
|
|
|
|
2023-03-02 11:19:26 -05:00
|
|
|
/// Produce a RNG seed.
|
|
|
|
|
///
|
|
|
|
|
/// Helper function for parties needing to generate random data from an agreed upon state.
|
|
|
|
|
///
|
|
|
|
|
/// Implementors MAY internally call the challenge function for the needed bytes, and accordingly
|
|
|
|
|
/// produce a transcript conflict between two transcripts, one which called challenge(label) and
|
|
|
|
|
/// one which called rng_seed(label) at the same point.
|
2022-05-31 02:12:14 -04:00
|
|
|
fn rng_seed(&mut self, label: &'static [u8]) -> [u8; 32];
|
2022-05-03 07:20:24 -04:00
|
|
|
}
|
|
|
|
|
|
2023-12-17 00:01:41 -05:00
|
|
|
#[derive(Clone, Copy)]
|
2022-06-24 08:42:38 -04:00
|
|
|
enum DigestTranscriptMember {
|
|
|
|
|
Name,
|
|
|
|
|
Domain,
|
|
|
|
|
Label,
|
|
|
|
|
Value,
|
2022-07-15 01:26:07 -04:00
|
|
|
Challenge,
|
2023-03-07 03:55:58 -05:00
|
|
|
Continued,
|
|
|
|
|
Challenged,
|
2022-06-24 08:42:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl DigestTranscriptMember {
|
|
|
|
|
fn as_u8(&self) -> u8 {
|
|
|
|
|
match self {
|
|
|
|
|
DigestTranscriptMember::Name => 0,
|
|
|
|
|
DigestTranscriptMember::Domain => 1,
|
|
|
|
|
DigestTranscriptMember::Label => 2,
|
|
|
|
|
DigestTranscriptMember::Value => 3,
|
2022-07-15 01:26:07 -04:00
|
|
|
DigestTranscriptMember::Challenge => 4,
|
2023-03-07 03:55:58 -05:00
|
|
|
DigestTranscriptMember::Continued => 5,
|
|
|
|
|
DigestTranscriptMember::Challenged => 6,
|
2022-06-24 08:42:38 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-29 05:25:29 -04:00
|
|
|
/// A simple transcript format constructed around the specified hash algorithm.
|
2022-05-03 07:20:24 -04:00
|
|
|
#[derive(Clone, Debug)]
|
Smash the singular `Ciphersuite` trait into multiple
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`.
2025-09-03 12:25:37 -04:00
|
|
|
pub struct DigestTranscript<D: Send + Clone + Digest + HashMarker>(D);
|
2022-05-03 07:20:24 -04:00
|
|
|
|
Smash the singular `Ciphersuite` trait into multiple
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`.
2025-09-03 12:25:37 -04:00
|
|
|
impl<D: Send + Clone + Digest + HashMarker> DigestTranscript<D> {
|
2022-06-24 08:42:38 -04:00
|
|
|
fn append(&mut self, kind: DigestTranscriptMember, value: &[u8]) {
|
2022-09-04 21:23:38 -04:00
|
|
|
self.0.update([kind.as_u8()]);
|
2022-06-24 08:42:38 -04:00
|
|
|
// Assumes messages don't exceed 16 exabytes
|
2022-06-24 18:43:32 -04:00
|
|
|
self.0.update(u64::try_from(value.len()).unwrap().to_le_bytes());
|
|
|
|
|
self.0.update(value);
|
2022-06-24 08:42:38 -04:00
|
|
|
}
|
2022-07-12 01:28:01 -04:00
|
|
|
}
|
|
|
|
|
|
Smash the singular `Ciphersuite` trait into multiple
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`.
2025-09-03 12:25:37 -04:00
|
|
|
impl<D: Send + Clone + Digest + HashMarker> Transcript for DigestTranscript<D> {
|
2022-07-12 01:28:01 -04:00
|
|
|
type Challenge = Output<D>;
|
2022-06-24 08:42:38 -04:00
|
|
|
|
2022-07-12 01:28:01 -04:00
|
|
|
fn new(name: &'static [u8]) -> Self {
|
2022-06-24 18:49:04 -04:00
|
|
|
let mut res = DigestTranscript(D::new());
|
2022-06-24 08:42:38 -04:00
|
|
|
res.append(DigestTranscriptMember::Name, name);
|
|
|
|
|
res
|
2022-05-06 07:33:08 -04:00
|
|
|
}
|
2022-06-28 04:02:56 -04:00
|
|
|
|
2022-11-05 18:43:36 -04:00
|
|
|
fn domain_separate(&mut self, label: &'static [u8]) {
|
2022-06-24 08:42:38 -04:00
|
|
|
self.append(DigestTranscriptMember::Domain, label);
|
2022-05-03 07:20:24 -04:00
|
|
|
}
|
|
|
|
|
|
2022-11-05 18:43:36 -04:00
|
|
|
fn append_message<M: AsRef<[u8]>>(&mut self, label: &'static [u8], message: M) {
|
2022-06-24 08:42:38 -04:00
|
|
|
self.append(DigestTranscriptMember::Label, label);
|
2022-11-05 18:43:36 -04:00
|
|
|
self.append(DigestTranscriptMember::Value, message.as_ref());
|
2022-05-03 07:20:24 -04:00
|
|
|
}
|
|
|
|
|
|
2022-06-28 04:02:56 -04:00
|
|
|
fn challenge(&mut self, label: &'static [u8]) -> Self::Challenge {
|
2022-06-24 08:42:38 -04:00
|
|
|
self.append(DigestTranscriptMember::Challenge, label);
|
2023-03-07 03:55:58 -05:00
|
|
|
let mut cloned = self.0.clone();
|
|
|
|
|
|
|
|
|
|
// Explicitly fork these transcripts to prevent length extension attacks from being possible
|
|
|
|
|
// (at least, without the additional ability to remove a byte from a finalized hash)
|
|
|
|
|
self.0.update([DigestTranscriptMember::Continued.as_u8()]);
|
|
|
|
|
cloned.update([DigestTranscriptMember::Challenged.as_u8()]);
|
|
|
|
|
cloned.finalize()
|
2022-05-03 07:20:24 -04:00
|
|
|
}
|
|
|
|
|
|
2022-05-31 02:12:14 -04:00
|
|
|
fn rng_seed(&mut self, label: &'static [u8]) -> [u8; 32] {
|
2022-05-03 07:20:24 -04:00
|
|
|
let mut seed = [0; 32];
|
2022-06-28 04:02:56 -04:00
|
|
|
seed.copy_from_slice(&self.challenge(label)[.. 32]);
|
2022-05-06 07:33:08 -04:00
|
|
|
seed
|
2022-05-03 07:20:24 -04:00
|
|
|
}
|
|
|
|
|
}
|
2022-06-24 18:58:24 -04:00
|
|
|
|
2023-03-28 04:43:10 -04:00
|
|
|
// Digest doesn't implement Zeroize
|
|
|
|
|
// Implement Zeroize for DigestTranscript by writing twice the block size to the digest in an
|
|
|
|
|
// attempt to overwrite the internal hash state/any leftover bytes
|
Smash the singular `Ciphersuite` trait into multiple
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`.
2025-09-03 12:25:37 -04:00
|
|
|
impl<D: Send + Clone + Digest + HashMarker> Zeroize for DigestTranscript<D>
|
2023-03-28 04:43:10 -04:00
|
|
|
where
|
|
|
|
|
D: BlockSizeUser,
|
|
|
|
|
{
|
|
|
|
|
fn zeroize(&mut self) {
|
|
|
|
|
// Update in 4-byte chunks to reduce call quantity and enable word-level update optimizations
|
|
|
|
|
const WORD_SIZE: usize = 4;
|
|
|
|
|
|
|
|
|
|
// block_size returns the block_size in bytes
|
|
|
|
|
// Use a ceil div in case the block size isn't evenly divisible by our word size
|
2023-10-05 14:27:59 -04:00
|
|
|
let words = D::block_size().div_ceil(WORD_SIZE);
|
2023-03-28 04:43:10 -04:00
|
|
|
for _ in 0 .. (2 * words) {
|
|
|
|
|
self.0.update([255; WORD_SIZE]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Hopefully, the hash state is now overwritten to the point no data is recoverable
|
|
|
|
|
// These writes may be optimized out if they're never read
|
|
|
|
|
// Attempt to get them marked as read
|
|
|
|
|
|
Smash the singular `Ciphersuite` trait into multiple
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`.
2025-09-03 12:25:37 -04:00
|
|
|
fn mark_read<D: Send + Clone + Digest + HashMarker>(transcript: &DigestTranscript<D>) {
|
2023-03-28 04:43:10 -04:00
|
|
|
// Just get a challenge from the state
|
|
|
|
|
let mut challenge = core::hint::black_box(transcript.0.clone().finalize());
|
|
|
|
|
challenge.as_mut().zeroize();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mark_read(self)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-20 20:10:00 -04:00
|
|
|
/// The recommended transcript, guaranteed to be secure against length-extension attacks.
|
2022-06-24 18:58:24 -04:00
|
|
|
#[cfg(feature = "recommended")]
|
|
|
|
|
pub type RecommendedTranscript = DigestTranscript<blake2::Blake2b512>;
|