2023-07-23 14:32:14 -04:00
|
|
|
#![allow(deprecated)]
|
2025-11-04 10:20:17 -05:00
|
|
|
#![cfg_attr(docsrs, feature(doc_cfg))]
|
2023-03-28 04:38:01 -04:00
|
|
|
#![no_std] // Prevents writing new code, in what should be a simple wrapper, which requires std
|
2023-03-20 20:10:00 -04:00
|
|
|
#![doc = include_str!("../README.md")]
|
2023-08-01 02:49:31 -04:00
|
|
|
#![allow(clippy::redundant_closure_call)]
|
|
|
|
|
|
2022-04-21 21:36:18 -04:00
|
|
|
use core::{
|
|
|
|
|
borrow::Borrow,
|
2023-07-08 11:29:05 -04:00
|
|
|
ops::{Deref, Add, AddAssign, Sub, SubAssign, Neg, Mul, MulAssign},
|
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
|
|
|
iter::{Iterator, Sum},
|
2022-04-21 21:36:18 -04:00
|
|
|
};
|
|
|
|
|
|
Utilize zeroize (#76)
* Apply Zeroize to nonces used in Bulletproofs
Also makes bit decomposition constant time for a given amount of
outputs.
* Fix nonce reuse for single-signer CLSAG
* Attach Zeroize to most structures in Monero, and ZOnDrop to anything with private data
* Zeroize private keys and nonces
* Merge prepare_outputs and prepare_transactions
* Ensure CLSAG is constant time
* Pass by borrow where needed, bug fixes
The past few commitments have been one in-progress chunk which I've
broken up as best read.
* Add Zeroize to FROST structs
Still needs to zeroize internally, yet next step. Not quite as
aggressive as Monero, partially due to the limitations of HashMaps,
partially due to less concern about metadata, yet does still delete a
few smaller items of metadata (group key, context string...).
* Remove Zeroize from most Monero multisig structs
These structs largely didn't have private data, just fields with private
data, yet those fields implemented ZeroizeOnDrop making them already
covered. While there is still traces of the transaction left in RAM,
fully purging that was never the intent.
* Use Zeroize within dleq
bitvec doesn't offer Zeroize, so a manual zeroing has been implemented.
* Use Zeroize for random_nonce
It isn't perfect, due to the inability to zeroize the digest, and due to
kp256 requiring a few transformations. It does the best it can though.
Does move the per-curve random_nonce to a provided one, which is allowed
as of https://github.com/cfrg/draft-irtf-cfrg-frost/pull/231.
* Use Zeroize on FROST keygen/signing
* Zeroize constant time multiexp.
* Correct when FROST keygen zeroizes
* Move the FROST keys Arc into FrostKeys
Reduces amount of instances in memory.
* Manually implement Debug for FrostCore to not leak the secret share
* Misc bug fixes
* clippy + multiexp test bug fixes
* Correct FROST key gen share summation
It leaked our own share for ourself.
* Fix cross-group DLEq tests
2022-08-03 03:25:18 -05:00
|
|
|
use zeroize::Zeroize;
|
2022-07-10 16:48:08 -04:00
|
|
|
use subtle::{ConstantTimeEq, ConditionallySelectable};
|
|
|
|
|
|
2022-04-21 21:36:18 -04:00
|
|
|
use rand_core::RngCore;
|
|
|
|
|
|
2022-07-10 16:48:08 -04:00
|
|
|
use subtle::{Choice, CtOption};
|
2022-04-21 21:36:18 -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
|
|
|
use curve25519_dalek::{
|
|
|
|
|
edwards::{EdwardsPoint as DEdwardsPoint, CompressedEdwardsY},
|
2022-04-21 21:36:18 -04:00
|
|
|
};
|
2025-09-05 17:31:09 -04:00
|
|
|
pub use curve25519_dalek::{Scalar, ristretto::RistrettoPoint};
|
2022-04-21 21:36:18 -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
|
|
|
use ::ciphersuite::group::{Group, GroupEncoding, prime::PrimeGroup};
|
2022-04-21 21:36:18 -04:00
|
|
|
|
2025-08-20 04:50:37 -04:00
|
|
|
mod ciphersuite;
|
|
|
|
|
pub use crate::ciphersuite::{Ed25519, Ristretto};
|
|
|
|
|
|
2025-08-29 06:14:25 -04:00
|
|
|
use core::hint::black_box;
|
2023-03-10 06:27:44 -05:00
|
|
|
|
|
|
|
|
fn u8_from_bool(bit_ref: &mut bool) -> u8 {
|
|
|
|
|
let bit_ref = black_box(bit_ref);
|
|
|
|
|
|
|
|
|
|
let mut bit = black_box(*bit_ref);
|
2023-12-16 20:54:24 -05:00
|
|
|
#[allow(clippy::cast_lossless)]
|
2023-03-10 06:27:44 -05:00
|
|
|
let res = black_box(bit as u8);
|
|
|
|
|
bit.zeroize();
|
|
|
|
|
debug_assert!((res | 1) == 1);
|
|
|
|
|
|
|
|
|
|
bit_ref.zeroize();
|
2023-03-07 04:22:57 -05:00
|
|
|
res
|
2022-07-08 15:30:56 -04:00
|
|
|
}
|
|
|
|
|
|
2023-03-10 06:27:44 -05:00
|
|
|
// Convert a boolean to a Choice in a *presumably* constant time manner
|
|
|
|
|
fn choice(mut value: bool) -> Choice {
|
|
|
|
|
Choice::from(u8_from_bool(&mut value))
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-06 04:22:49 -04:00
|
|
|
macro_rules! deref_borrow {
|
|
|
|
|
($Source: ident, $Target: ident) => {
|
|
|
|
|
impl Deref for $Source {
|
|
|
|
|
type Target = $Target;
|
2022-04-21 21:36:18 -04:00
|
|
|
|
2022-06-06 04:22:49 -04:00
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
|
&self.0
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-04-21 21:36:18 -04:00
|
|
|
|
2022-06-06 04:22:49 -04:00
|
|
|
impl Borrow<$Target> for $Source {
|
|
|
|
|
fn borrow(&self) -> &$Target {
|
|
|
|
|
&self.0
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-04-21 21:36:18 -04:00
|
|
|
|
2022-06-06 04:22:49 -04:00
|
|
|
impl Borrow<$Target> for &$Source {
|
|
|
|
|
fn borrow(&self) -> &$Target {
|
|
|
|
|
&self.0
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-07-15 01:26:07 -04:00
|
|
|
};
|
2022-04-21 21:36:18 -04:00
|
|
|
}
|
|
|
|
|
|
2022-07-10 16:48:08 -04:00
|
|
|
macro_rules! constant_time {
|
|
|
|
|
($Value: ident, $Inner: ident) => {
|
|
|
|
|
impl ConstantTimeEq for $Value {
|
2022-07-15 01:26:07 -04:00
|
|
|
fn ct_eq(&self, other: &Self) -> Choice {
|
|
|
|
|
self.0.ct_eq(&other.0)
|
|
|
|
|
}
|
2022-06-06 04:22:49 -04:00
|
|
|
}
|
2022-04-21 21:36:18 -04:00
|
|
|
|
2022-07-10 16:48:08 -04:00
|
|
|
impl ConditionallySelectable for $Value {
|
|
|
|
|
fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self {
|
|
|
|
|
$Value($Inner::conditional_select(&a.0, &b.0, choice))
|
|
|
|
|
}
|
2022-06-06 04:22:49 -04:00
|
|
|
}
|
2022-07-15 01:26:07 -04:00
|
|
|
};
|
2022-07-10 16:48:08 -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
|
|
|
|
|
|
|
|
macro_rules! math_op_without_wrapping {
|
|
|
|
|
(
|
|
|
|
|
$Value: ident,
|
|
|
|
|
$Other: ident,
|
|
|
|
|
$Op: ident,
|
|
|
|
|
$op_fn: ident,
|
|
|
|
|
$Assign: ident,
|
|
|
|
|
$assign_fn: ident,
|
|
|
|
|
$function: expr
|
|
|
|
|
) => {
|
|
|
|
|
impl $Op<$Other> for $Value {
|
|
|
|
|
type Output = $Value;
|
|
|
|
|
fn $op_fn(self, other: $Other) -> Self::Output {
|
|
|
|
|
Self($function(self.0, other))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
impl $Assign<$Other> for $Value {
|
|
|
|
|
fn $assign_fn(&mut self, other: $Other) {
|
|
|
|
|
self.0 = $function(self.0, other);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
impl<'a> $Op<&'a $Other> for $Value {
|
|
|
|
|
type Output = $Value;
|
|
|
|
|
fn $op_fn(self, other: &'a $Other) -> Self::Output {
|
|
|
|
|
Self($function(self.0, other))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
impl<'a> $Assign<&'a $Other> for $Value {
|
|
|
|
|
fn $assign_fn(&mut self, other: &'a $Other) {
|
|
|
|
|
self.0 = $function(self.0, other);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
2022-04-21 21:36:18 -04:00
|
|
|
|
2022-07-10 16:48:08 -04:00
|
|
|
macro_rules! math_op {
|
|
|
|
|
(
|
|
|
|
|
$Value: ident,
|
|
|
|
|
$Other: ident,
|
|
|
|
|
$Op: ident,
|
|
|
|
|
$op_fn: ident,
|
|
|
|
|
$Assign: ident,
|
|
|
|
|
$assign_fn: ident,
|
|
|
|
|
$function: expr
|
|
|
|
|
) => {
|
|
|
|
|
impl $Op<$Other> for $Value {
|
|
|
|
|
type Output = $Value;
|
|
|
|
|
fn $op_fn(self, other: $Other) -> Self::Output {
|
|
|
|
|
Self($function(self.0, other.0))
|
|
|
|
|
}
|
2022-06-06 04:22:49 -04:00
|
|
|
}
|
2022-07-10 16:48:08 -04:00
|
|
|
impl $Assign<$Other> for $Value {
|
|
|
|
|
fn $assign_fn(&mut self, other: $Other) {
|
|
|
|
|
self.0 = $function(self.0, other.0);
|
|
|
|
|
}
|
2022-06-06 04:22:49 -04:00
|
|
|
}
|
2022-07-10 16:48:08 -04:00
|
|
|
impl<'a> $Op<&'a $Other> for $Value {
|
|
|
|
|
type Output = $Value;
|
|
|
|
|
fn $op_fn(self, other: &'a $Other) -> Self::Output {
|
|
|
|
|
Self($function(self.0, other.0))
|
|
|
|
|
}
|
2022-06-06 04:22:49 -04:00
|
|
|
}
|
2022-07-10 16:48:08 -04:00
|
|
|
impl<'a> $Assign<&'a $Other> for $Value {
|
|
|
|
|
fn $assign_fn(&mut self, other: &'a $Other) {
|
|
|
|
|
self.0 = $function(self.0, other.0);
|
|
|
|
|
}
|
2022-06-06 04:22:49 -04:00
|
|
|
}
|
2022-07-15 01:26:07 -04:00
|
|
|
};
|
2022-07-10 16:48:08 -04:00
|
|
|
}
|
2022-04-21 21:36:18 -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
|
|
|
macro_rules! math_neg {
|
2022-07-10 16:48:08 -04:00
|
|
|
($Value: ident, $Factor: ident, $add: expr, $sub: expr, $mul: expr) => {
|
|
|
|
|
math_op!($Value, $Value, Add, add, AddAssign, add_assign, $add);
|
|
|
|
|
math_op!($Value, $Value, Sub, sub, SubAssign, sub_assign, $sub);
|
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
|
|
|
math_op_without_wrapping!($Value, $Factor, Mul, mul, MulAssign, mul_assign, $mul);
|
2022-04-21 21:36:18 -04:00
|
|
|
|
2022-07-10 16:48:08 -04:00
|
|
|
impl Neg for $Value {
|
2022-06-06 04:22:49 -04:00
|
|
|
type Output = Self;
|
2022-07-15 01:26:07 -04:00
|
|
|
fn neg(self) -> Self::Output {
|
|
|
|
|
Self(-self.0)
|
|
|
|
|
}
|
2022-06-06 04:22:49 -04:00
|
|
|
}
|
2022-07-15 01:26:07 -04:00
|
|
|
};
|
2022-04-21 21:36:18 -04:00
|
|
|
}
|
|
|
|
|
|
2022-06-06 04:22:49 -04:00
|
|
|
macro_rules! dalek_group {
|
|
|
|
|
(
|
|
|
|
|
$Point: ident,
|
|
|
|
|
$DPoint: ident,
|
2022-06-28 01:25:26 -04:00
|
|
|
$torsion_free: expr,
|
2022-04-21 21:36:18 -04:00
|
|
|
|
2022-06-06 04:22:49 -04:00
|
|
|
$Table: ident,
|
2022-04-21 21:36:18 -04:00
|
|
|
|
2022-06-06 04:22:49 -04:00
|
|
|
$DCompressed: ident,
|
|
|
|
|
) => {
|
2025-08-19 15:25:40 -04:00
|
|
|
/// Wrapper around the dalek Point type.
|
|
|
|
|
///
|
|
|
|
|
/// All operations will be restricted to a prime-order subgroup (equivalent to the group itself
|
|
|
|
|
/// in the case of Ristretto). The exposure of the internal element does allow bypassing this
|
|
|
|
|
/// however, which may lead to undefined/computationally-unsafe behavior, and is entirely at
|
|
|
|
|
/// the user's risk.
|
Utilize zeroize (#76)
* Apply Zeroize to nonces used in Bulletproofs
Also makes bit decomposition constant time for a given amount of
outputs.
* Fix nonce reuse for single-signer CLSAG
* Attach Zeroize to most structures in Monero, and ZOnDrop to anything with private data
* Zeroize private keys and nonces
* Merge prepare_outputs and prepare_transactions
* Ensure CLSAG is constant time
* Pass by borrow where needed, bug fixes
The past few commitments have been one in-progress chunk which I've
broken up as best read.
* Add Zeroize to FROST structs
Still needs to zeroize internally, yet next step. Not quite as
aggressive as Monero, partially due to the limitations of HashMaps,
partially due to less concern about metadata, yet does still delete a
few smaller items of metadata (group key, context string...).
* Remove Zeroize from most Monero multisig structs
These structs largely didn't have private data, just fields with private
data, yet those fields implemented ZeroizeOnDrop making them already
covered. While there is still traces of the transaction left in RAM,
fully purging that was never the intent.
* Use Zeroize within dleq
bitvec doesn't offer Zeroize, so a manual zeroing has been implemented.
* Use Zeroize for random_nonce
It isn't perfect, due to the inability to zeroize the digest, and due to
kp256 requiring a few transformations. It does the best it can though.
Does move the per-curve random_nonce to a provided one, which is allowed
as of https://github.com/cfrg/draft-irtf-cfrg-frost/pull/231.
* Use Zeroize on FROST keygen/signing
* Zeroize constant time multiexp.
* Correct when FROST keygen zeroizes
* Move the FROST keys Arc into FrostKeys
Reduces amount of instances in memory.
* Manually implement Debug for FrostCore to not leak the secret share
* Misc bug fixes
* clippy + multiexp test bug fixes
* Correct FROST key gen share summation
It leaked our own share for ourself.
* Fix cross-group DLEq tests
2022-08-03 03:25:18 -05:00
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, Debug, Zeroize)]
|
2022-06-06 04:22:49 -04:00
|
|
|
pub struct $Point(pub $DPoint);
|
|
|
|
|
deref_borrow!($Point, $DPoint);
|
2022-07-10 16:48:08 -04:00
|
|
|
constant_time!($Point, $DPoint);
|
|
|
|
|
math_neg!($Point, Scalar, $DPoint::add, $DPoint::sub, $DPoint::mul);
|
2022-04-21 21:36:18 -04:00
|
|
|
|
2022-06-06 04:22:49 -04:00
|
|
|
impl Sum<$Point> for $Point {
|
2022-07-15 01:26:07 -04:00
|
|
|
fn sum<I: Iterator<Item = $Point>>(iter: I) -> $Point {
|
|
|
|
|
Self($DPoint::sum(iter))
|
|
|
|
|
}
|
2022-06-06 04:22:49 -04:00
|
|
|
}
|
|
|
|
|
impl<'a> Sum<&'a $Point> for $Point {
|
2022-07-15 01:26:07 -04:00
|
|
|
fn sum<I: Iterator<Item = &'a $Point>>(iter: I) -> $Point {
|
|
|
|
|
Self($DPoint::sum(iter))
|
|
|
|
|
}
|
2022-06-06 04:22:49 -04:00
|
|
|
}
|
2022-04-21 21:36:18 -04:00
|
|
|
|
2022-06-06 04:22:49 -04:00
|
|
|
impl Group for $Point {
|
|
|
|
|
type Scalar = Scalar;
|
2022-08-29 13:02:20 -04:00
|
|
|
fn random(mut rng: impl RngCore) -> Self {
|
|
|
|
|
loop {
|
2023-07-23 14:32:14 -04:00
|
|
|
let mut bytes = [0; 32];
|
2023-02-23 04:27:31 -05:00
|
|
|
rng.fill_bytes(&mut bytes);
|
2023-09-12 08:42:55 -04:00
|
|
|
let Some(point) = Option::<$Point>::from($Point::from_bytes(&bytes)) else {
|
2023-07-23 14:32:14 -04:00
|
|
|
continue;
|
|
|
|
|
};
|
2023-02-23 04:27:31 -05:00
|
|
|
// Ban identity, per the trait specification
|
|
|
|
|
if !bool::from(point.is_identity()) {
|
|
|
|
|
return point;
|
2022-08-29 13:02:20 -04:00
|
|
|
}
|
|
|
|
|
}
|
2022-07-15 01:26:07 -04:00
|
|
|
}
|
|
|
|
|
fn identity() -> Self {
|
|
|
|
|
Self($DPoint::identity())
|
|
|
|
|
}
|
|
|
|
|
fn generator() -> Self {
|
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
|
|
|
Self(<$DPoint as Group>::generator())
|
2022-07-15 01:26:07 -04:00
|
|
|
}
|
|
|
|
|
fn is_identity(&self) -> Choice {
|
|
|
|
|
self.0.ct_eq(&$DPoint::identity())
|
|
|
|
|
}
|
|
|
|
|
fn double(&self) -> Self {
|
2023-09-12 08:42:55 -04:00
|
|
|
Self(self.0.double())
|
2022-07-15 01:26:07 -04:00
|
|
|
}
|
2022-06-06 04:22:49 -04:00
|
|
|
}
|
2022-04-21 21:36:18 -04:00
|
|
|
|
2022-06-28 01:25:26 -04:00
|
|
|
impl GroupEncoding for $Point {
|
|
|
|
|
type Repr = [u8; 32];
|
|
|
|
|
|
|
|
|
|
fn from_bytes(bytes: &Self::Repr) -> CtOption<Self> {
|
2022-07-08 15:30:56 -04:00
|
|
|
let decompressed = $DCompressed(*bytes).decompress();
|
|
|
|
|
// TODO: Same note on unwrap_or as above
|
|
|
|
|
let point = decompressed.unwrap_or($DPoint::identity());
|
2023-03-28 04:38:01 -04:00
|
|
|
CtOption::new(
|
|
|
|
|
$Point(point),
|
|
|
|
|
choice(black_box(decompressed).is_some()) & choice($torsion_free(point)),
|
|
|
|
|
)
|
2022-06-28 01:25:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn from_bytes_unchecked(bytes: &Self::Repr) -> CtOption<Self> {
|
|
|
|
|
$Point::from_bytes(bytes)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn to_bytes(&self) -> Self::Repr {
|
2023-09-12 08:42:55 -04:00
|
|
|
self.0.to_bytes()
|
2022-06-28 01:25:26 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl PrimeGroup for $Point {}
|
2022-06-06 04:22:49 -04:00
|
|
|
};
|
2022-04-21 21:36:18 -04:00
|
|
|
}
|
|
|
|
|
|
2022-06-06 04:22:49 -04:00
|
|
|
dalek_group!(
|
|
|
|
|
EdwardsPoint,
|
|
|
|
|
DEdwardsPoint,
|
2022-06-28 01:25:26 -04:00
|
|
|
|point: DEdwardsPoint| point.is_torsion_free(),
|
2022-06-06 04:22:49 -04:00
|
|
|
EdwardsBasepointTable,
|
2023-03-20 20:10:00 -04:00
|
|
|
CompressedEdwardsY,
|
2022-06-06 04:22:49 -04:00
|
|
|
);
|
2022-04-21 21:36:18 -04:00
|
|
|
|
2022-12-15 20:33:58 -05:00
|
|
|
#[test]
|
2022-12-24 15:09:09 -05:00
|
|
|
fn test_ed25519_group() {
|
2023-02-24 06:03:56 -05:00
|
|
|
ff_group_tests::group::test_prime_group_bits::<_, EdwardsPoint>(&mut rand_core::OsRng);
|
2022-12-15 19:23:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2022-12-24 15:09:09 -05:00
|
|
|
fn test_ristretto_group() {
|
2023-02-24 06:03:56 -05:00
|
|
|
ff_group_tests::group::test_prime_group_bits::<_, RistrettoPoint>(&mut rand_core::OsRng);
|
2022-12-15 19:23:42 -05:00
|
|
|
}
|
2025-08-29 06:14:25 -04:00
|
|
|
|
|
|
|
|
type ThirtyTwoArray = [u8; 32];
|
|
|
|
|
prime_field::odd_prime_field_with_specific_repr!(
|
|
|
|
|
FieldElement,
|
|
|
|
|
"0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffed",
|
|
|
|
|
"02",
|
|
|
|
|
false,
|
|
|
|
|
crate::ThirtyTwoArray
|
|
|
|
|
);
|