2023-07-23 14:32:14 -04:00
|
|
|
#![allow(deprecated)]
|
2022-12-24 15:09:09 -05:00
|
|
|
#![cfg_attr(docsrs, feature(doc_auto_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},
|
2023-03-28 04:38:01 -04:00
|
|
|
iter::{Iterator, Sum, Product},
|
2023-03-07 03:10:55 -05:00
|
|
|
hash::{Hash, Hasher},
|
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-09-29 05:33:46 -04:00
|
|
|
use digest::{consts::U64, Digest, HashMarker};
|
2022-04-21 21:36:18 -04:00
|
|
|
|
2022-07-10 16:48:08 -04:00
|
|
|
use subtle::{Choice, CtOption};
|
2022-04-21 21:36:18 -04:00
|
|
|
|
|
|
|
|
pub use curve25519_dalek as dalek;
|
|
|
|
|
|
|
|
|
|
use dalek::{
|
2023-09-12 09:37:48 -04:00
|
|
|
constants::{self, BASEPOINT_ORDER},
|
2022-04-21 21:36:18 -04:00
|
|
|
scalar::Scalar as DScalar,
|
2023-03-20 20:10:00 -04:00
|
|
|
edwards::{EdwardsPoint as DEdwardsPoint, EdwardsBasepointTable, CompressedEdwardsY},
|
|
|
|
|
ristretto::{RistrettoPoint as DRistrettoPoint, RistrettoBasepointTable, CompressedRistretto},
|
2022-04-21 21:36:18 -04:00
|
|
|
};
|
2023-03-20 20:10:00 -04:00
|
|
|
pub use constants::{ED25519_BASEPOINT_TABLE, RISTRETTO_BASEPOINT_TABLE};
|
2022-04-21 21:36:18 -04:00
|
|
|
|
2023-03-07 03:06:46 -05:00
|
|
|
use group::{
|
2023-09-12 08:42:55 -04:00
|
|
|
ff::{Field, PrimeField, FieldBits, PrimeFieldBits},
|
2023-03-07 03:06:46 -05:00
|
|
|
Group, GroupEncoding,
|
|
|
|
|
prime::PrimeGroup,
|
|
|
|
|
};
|
2022-04-21 21:36:18 -04:00
|
|
|
|
2023-03-20 20:10:00 -04:00
|
|
|
mod field;
|
|
|
|
|
pub use field::FieldElement;
|
2022-07-10 15:20:42 -04:00
|
|
|
|
2023-03-28 04:43:10 -04:00
|
|
|
// Use black_box when possible
|
|
|
|
|
#[rustversion::since(1.66)]
|
2025-08-15 20:37:03 -04:00
|
|
|
mod black_box {
|
|
|
|
|
pub(crate) fn black_box<T>(val: T) -> T {
|
|
|
|
|
#[allow(clippy::incompatible_msrv)]
|
|
|
|
|
core::hint::black_box(val)
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-03-28 04:43:10 -04:00
|
|
|
#[rustversion::before(1.66)]
|
2025-08-15 20:37:03 -04:00
|
|
|
mod black_box {
|
|
|
|
|
pub(crate) fn black_box<T>(val: T) -> T {
|
|
|
|
|
val
|
|
|
|
|
}
|
2023-03-10 06:27:44 -05:00
|
|
|
}
|
2025-08-15 20:37:03 -04:00
|
|
|
use black_box::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
|
|
|
}
|
2023-03-28 04:38:01 -04:00
|
|
|
pub(crate) use constant_time;
|
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
|
|
|
}
|
2023-03-28 04:38:01 -04:00
|
|
|
pub(crate) use math_op;
|
2022-04-21 21:36:18 -04:00
|
|
|
|
2022-07-10 16:48:08 -04:00
|
|
|
macro_rules! math {
|
|
|
|
|
($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);
|
|
|
|
|
math_op!($Value, $Factor, Mul, mul, MulAssign, mul_assign, $mul);
|
2022-07-15 01:26:07 -04:00
|
|
|
};
|
2022-07-10 16:48:08 -04:00
|
|
|
}
|
2023-03-28 04:38:01 -04:00
|
|
|
pub(crate) use math;
|
2022-04-21 21:36:18 -04:00
|
|
|
|
2022-07-10 16:48:08 -04:00
|
|
|
macro_rules! math_neg {
|
|
|
|
|
($Value: ident, $Factor: ident, $add: expr, $sub: expr, $mul: expr) => {
|
|
|
|
|
math!($Value, $Factor, $add, $sub, $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-09-29 05:25:29 -04:00
|
|
|
/// Wrapper around the dalek Scalar type.
|
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, Default, Debug, Zeroize)]
|
2022-06-06 04:22:49 -04:00
|
|
|
pub struct Scalar(pub DScalar);
|
|
|
|
|
deref_borrow!(Scalar, DScalar);
|
2022-07-10 16:48:08 -04:00
|
|
|
constant_time!(Scalar, DScalar);
|
|
|
|
|
math_neg!(Scalar, Scalar, DScalar::add, DScalar::sub, DScalar::mul);
|
2023-04-20 01:11:39 -04:00
|
|
|
|
|
|
|
|
macro_rules! from_wrapper {
|
|
|
|
|
($uint: ident) => {
|
|
|
|
|
impl From<$uint> for Scalar {
|
|
|
|
|
fn from(a: $uint) -> Scalar {
|
|
|
|
|
Scalar(DScalar::from(a))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
from_wrapper!(u8);
|
|
|
|
|
from_wrapper!(u16);
|
|
|
|
|
from_wrapper!(u32);
|
|
|
|
|
from_wrapper!(u64);
|
|
|
|
|
from_wrapper!(u128);
|
2022-06-06 04:22:49 -04:00
|
|
|
|
|
|
|
|
impl Scalar {
|
2022-12-15 19:23:42 -05:00
|
|
|
pub fn pow(&self, other: Scalar) -> Scalar {
|
2023-03-28 04:38:01 -04:00
|
|
|
let mut table = [Scalar::ONE; 16];
|
2022-12-15 19:23:42 -05:00
|
|
|
table[1] = *self;
|
|
|
|
|
for i in 2 .. 16 {
|
|
|
|
|
table[i] = table[i - 1] * self;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-28 04:38:01 -04:00
|
|
|
let mut res = Scalar::ONE;
|
2022-12-15 19:23:42 -05:00
|
|
|
let mut bits = 0;
|
2023-03-10 06:27:44 -05:00
|
|
|
for (i, mut bit) in other.to_le_bits().iter_mut().rev().enumerate() {
|
2022-12-15 19:23:42 -05:00
|
|
|
bits <<= 1;
|
2023-07-08 11:29:05 -04:00
|
|
|
let mut bit = u8_from_bool(&mut bit);
|
2022-12-15 19:23:42 -05:00
|
|
|
bits |= bit;
|
2023-03-10 06:27:44 -05:00
|
|
|
bit.zeroize();
|
2022-12-15 19:23:42 -05:00
|
|
|
|
|
|
|
|
if ((i + 1) % 4) == 0 {
|
|
|
|
|
if i != 3 {
|
|
|
|
|
for _ in 0 .. 4 {
|
|
|
|
|
res *= res;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-10-27 08:51:19 -04:00
|
|
|
|
|
|
|
|
let mut scale_by = Scalar::ONE;
|
|
|
|
|
#[allow(clippy::needless_range_loop)]
|
|
|
|
|
for i in 0 .. 16 {
|
|
|
|
|
#[allow(clippy::cast_possible_truncation)] // Safe since 0 .. 16
|
|
|
|
|
{
|
|
|
|
|
scale_by = <_>::conditional_select(&scale_by, &table[i], bits.ct_eq(&(i as u8)));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
res *= scale_by;
|
2022-12-15 19:23:42 -05:00
|
|
|
bits = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
res
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-29 05:25:29 -04:00
|
|
|
/// Perform wide reduction on a 64-byte array to create a Scalar without bias.
|
2022-06-06 04:22:49 -04:00
|
|
|
pub fn from_bytes_mod_order_wide(bytes: &[u8; 64]) -> Scalar {
|
|
|
|
|
Self(DScalar::from_bytes_mod_order_wide(bytes))
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-29 05:25:29 -04:00
|
|
|
/// Derive a Scalar without bias from a digest via wide reduction.
|
2022-09-29 05:33:46 -04:00
|
|
|
pub fn from_hash<D: Digest<OutputSize = U64> + HashMarker>(hash: D) -> Scalar {
|
2022-06-06 04:22:49 -04:00
|
|
|
let mut output = [0u8; 64];
|
|
|
|
|
output.copy_from_slice(&hash.finalize());
|
2022-08-04 14:40:54 -04:00
|
|
|
let res = Scalar(DScalar::from_bytes_mod_order_wide(&output));
|
|
|
|
|
output.zeroize();
|
|
|
|
|
res
|
2022-06-06 04:22:49 -04:00
|
|
|
}
|
2022-04-21 21:36:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Field for Scalar {
|
2023-07-23 14:32:14 -04:00
|
|
|
const ZERO: Scalar = Scalar(DScalar::ZERO);
|
|
|
|
|
const ONE: Scalar = Scalar(DScalar::ONE);
|
2023-03-28 04:38:01 -04:00
|
|
|
|
2023-09-12 08:42:55 -04:00
|
|
|
fn random(rng: impl RngCore) -> Self {
|
|
|
|
|
Self(<DScalar as Field>::random(rng))
|
2022-04-21 21:36:18 -04:00
|
|
|
}
|
|
|
|
|
|
2022-07-15 01:26:07 -04:00
|
|
|
fn square(&self) -> Self {
|
2023-09-12 08:42:55 -04:00
|
|
|
Self(self.0.square())
|
2022-07-15 01:26:07 -04:00
|
|
|
}
|
|
|
|
|
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-05-03 08:49:46 -04:00
|
|
|
fn invert(&self) -> CtOption<Self> {
|
2023-09-12 08:42:55 -04:00
|
|
|
<DScalar as Field>::invert(&self.0).map(Self)
|
2022-05-03 08:49:46 -04:00
|
|
|
}
|
2023-03-28 04:38:01 -04:00
|
|
|
|
2022-07-15 01:26:07 -04:00
|
|
|
fn sqrt(&self) -> CtOption<Self> {
|
2023-09-12 08:42:55 -04:00
|
|
|
self.0.sqrt().map(Self)
|
2022-07-15 01:26:07 -04:00
|
|
|
}
|
2023-03-28 04:38:01 -04:00
|
|
|
|
|
|
|
|
fn sqrt_ratio(num: &Self, div: &Self) -> (Choice, Self) {
|
2023-09-12 08:42:55 -04:00
|
|
|
let (choice, res) = DScalar::sqrt_ratio(num, div);
|
|
|
|
|
(choice, Self(res))
|
2023-03-28 04:38:01 -04:00
|
|
|
}
|
2022-04-21 21:36:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl PrimeField for Scalar {
|
|
|
|
|
type Repr = [u8; 32];
|
2023-03-28 04:38:01 -04:00
|
|
|
|
2023-09-12 08:42:55 -04:00
|
|
|
const MODULUS: &'static str = <DScalar as PrimeField>::MODULUS;
|
|
|
|
|
|
|
|
|
|
const NUM_BITS: u32 = <DScalar as PrimeField>::NUM_BITS;
|
|
|
|
|
const CAPACITY: u32 = <DScalar as PrimeField>::CAPACITY;
|
|
|
|
|
|
|
|
|
|
const TWO_INV: Scalar = Scalar(<DScalar as PrimeField>::TWO_INV);
|
|
|
|
|
|
|
|
|
|
const MULTIPLICATIVE_GENERATOR: Scalar =
|
|
|
|
|
Scalar(<DScalar as PrimeField>::MULTIPLICATIVE_GENERATOR);
|
|
|
|
|
const S: u32 = <DScalar as PrimeField>::S;
|
|
|
|
|
|
|
|
|
|
const ROOT_OF_UNITY: Scalar = Scalar(<DScalar as PrimeField>::ROOT_OF_UNITY);
|
|
|
|
|
const ROOT_OF_UNITY_INV: Scalar = Scalar(<DScalar as PrimeField>::ROOT_OF_UNITY_INV);
|
|
|
|
|
|
|
|
|
|
const DELTA: Scalar = Scalar(<DScalar as PrimeField>::DELTA);
|
2023-03-28 04:38:01 -04:00
|
|
|
|
2022-04-23 03:49:30 -04:00
|
|
|
fn from_repr(bytes: [u8; 32]) -> CtOption<Self> {
|
2023-09-12 08:42:55 -04:00
|
|
|
<DScalar as PrimeField>::from_repr(bytes).map(Scalar)
|
2022-04-23 03:49:30 -04:00
|
|
|
}
|
2022-07-15 01:26:07 -04:00
|
|
|
fn to_repr(&self) -> [u8; 32] {
|
2023-09-12 08:42:55 -04:00
|
|
|
self.0.to_repr()
|
2022-07-15 01:26:07 -04:00
|
|
|
}
|
2022-04-21 21:36:18 -04:00
|
|
|
|
2022-07-15 01:26:07 -04:00
|
|
|
fn is_odd(&self) -> Choice {
|
2023-09-12 08:42:55 -04:00
|
|
|
self.0.is_odd()
|
2022-07-15 01:26:07 -04:00
|
|
|
}
|
2023-03-28 04:38:01 -04:00
|
|
|
|
|
|
|
|
fn from_u128(num: u128) -> Self {
|
2023-09-12 08:42:55 -04:00
|
|
|
Scalar(DScalar::from_u128(num))
|
2022-07-15 01:26:07 -04:00
|
|
|
}
|
2022-04-21 21:36:18 -04:00
|
|
|
}
|
|
|
|
|
|
2022-06-30 03:17:15 -04:00
|
|
|
impl PrimeFieldBits for Scalar {
|
|
|
|
|
type ReprBits = [u8; 32];
|
|
|
|
|
|
|
|
|
|
fn to_le_bits(&self) -> FieldBits<Self::ReprBits> {
|
|
|
|
|
self.to_repr().into()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn char_le_bits() -> FieldBits<Self::ReprBits> {
|
2023-09-12 09:37:48 -04:00
|
|
|
BASEPOINT_ORDER.to_bytes().into()
|
2022-06-30 03:17:15 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-26 02:05:15 -05:00
|
|
|
impl Sum<Scalar> for Scalar {
|
|
|
|
|
fn sum<I: Iterator<Item = Scalar>>(iter: I) -> Scalar {
|
|
|
|
|
Self(DScalar::sum(iter))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-28 04:38:01 -04:00
|
|
|
impl<'a> Sum<&'a Scalar> for Scalar {
|
|
|
|
|
fn sum<I: Iterator<Item = &'a Scalar>>(iter: I) -> Scalar {
|
|
|
|
|
Self(DScalar::sum(iter))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Product<Scalar> for Scalar {
|
|
|
|
|
fn product<I: Iterator<Item = Scalar>>(iter: I) -> Scalar {
|
|
|
|
|
Self(DScalar::product(iter))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a> Product<&'a Scalar> for Scalar {
|
|
|
|
|
fn product<I: Iterator<Item = &'a Scalar>>(iter: I) -> Scalar {
|
|
|
|
|
Self(DScalar::product(iter))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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,
|
2022-04-21 21:36:18 -04:00
|
|
|
|
2022-06-06 04:22:49 -04:00
|
|
|
$BASEPOINT_POINT: ident,
|
|
|
|
|
$BASEPOINT_TABLE: 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
|
|
|
|
2023-03-20 20:10:00 -04:00
|
|
|
/// The basepoint for this curve.
|
2022-06-06 04:22:49 -04:00
|
|
|
pub const $BASEPOINT_POINT: $Point = $Point(constants::$BASEPOINT_POINT);
|
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 {
|
|
|
|
|
$BASEPOINT_POINT
|
|
|
|
|
}
|
|
|
|
|
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
|
|
|
impl Mul<Scalar> for &$Table {
|
|
|
|
|
type Output = $Point;
|
2022-07-15 01:26:07 -04:00
|
|
|
fn mul(self, b: Scalar) -> $Point {
|
2023-03-20 20:10:00 -04:00
|
|
|
$Point(&b.0 * self)
|
2022-07-15 01:26:07 -04:00
|
|
|
}
|
2022-06-06 04:22:49 -04:00
|
|
|
}
|
2023-03-07 03:10:55 -05:00
|
|
|
|
|
|
|
|
// Support being used as a key in a table
|
|
|
|
|
// While it is expensive as a key, due to the field operations required, there's frequently
|
|
|
|
|
// use cases for public key -> value lookups
|
2023-03-07 05:31:02 -05:00
|
|
|
#[allow(unknown_lints, renamed_and_removed_lints)]
|
2023-03-07 04:47:06 -05:00
|
|
|
#[allow(clippy::derived_hash_with_manual_eq, clippy::derive_hash_xor_eq)]
|
2023-03-07 03:10:55 -05:00
|
|
|
impl Hash for $Point {
|
|
|
|
|
fn hash<H: Hasher>(&self, state: &mut H) {
|
|
|
|
|
self.to_bytes().hash(state);
|
|
|
|
|
}
|
|
|
|
|
}
|
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
|
|
|
ED25519_BASEPOINT_POINT,
|
|
|
|
|
ED25519_BASEPOINT_TABLE
|
|
|
|
|
);
|
2022-04-21 21:36:18 -04:00
|
|
|
|
2022-07-31 21:45:53 -05:00
|
|
|
impl EdwardsPoint {
|
|
|
|
|
pub fn mul_by_cofactor(&self) -> EdwardsPoint {
|
|
|
|
|
EdwardsPoint(self.0.mul_by_cofactor())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-06 04:22:49 -04:00
|
|
|
dalek_group!(
|
|
|
|
|
RistrettoPoint,
|
|
|
|
|
DRistrettoPoint,
|
2022-06-28 01:25:26 -04:00
|
|
|
|_| true,
|
2022-06-06 04:22:49 -04:00
|
|
|
RistrettoBasepointTable,
|
2023-03-20 20:10:00 -04:00
|
|
|
CompressedRistretto,
|
2022-06-06 04:22:49 -04:00
|
|
|
RISTRETTO_BASEPOINT_POINT,
|
|
|
|
|
RISTRETTO_BASEPOINT_TABLE
|
|
|
|
|
);
|
2022-12-15 19:23:42 -05: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
|
|
|
}
|