2023-03-28 04:38:01 -04:00
|
|
|
use core::{
|
2023-07-08 11:29:05 -04:00
|
|
|
ops::{Add, AddAssign, Sub, SubAssign, Neg, Mul, MulAssign},
|
2023-03-28 04:38:01 -04:00
|
|
|
iter::{Sum, Product},
|
|
|
|
|
};
|
2022-07-10 15:20:42 -04:00
|
|
|
|
2023-03-10 06:27:44 -05:00
|
|
|
use zeroize::Zeroize;
|
2022-07-10 15:20:42 -04:00
|
|
|
use rand_core::RngCore;
|
|
|
|
|
|
2022-08-31 03:33:19 -04:00
|
|
|
use subtle::{
|
|
|
|
|
Choice, CtOption, ConstantTimeEq, ConstantTimeLess, ConditionallyNegatable,
|
|
|
|
|
ConditionallySelectable,
|
|
|
|
|
};
|
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
|
|
|
|
2023-04-20 01:11:39 -04:00
|
|
|
use crypto_bigint::{
|
|
|
|
|
Integer, NonZero, Encoding, U256, U512,
|
|
|
|
|
modular::constant_mod::{ResidueParams, Residue},
|
|
|
|
|
impl_modulus,
|
|
|
|
|
};
|
2022-07-10 15:20:42 -04:00
|
|
|
|
2023-03-07 03:06:46 -05:00
|
|
|
use group::ff::{Field, PrimeField, FieldBits, PrimeFieldBits};
|
2022-07-10 15:20:42 -04:00
|
|
|
|
2023-04-20 01:11:39 -04:00
|
|
|
use crate::{u8_from_bool, constant_time, math_op, math};
|
2022-07-10 15:20:42 -04:00
|
|
|
|
2023-04-20 01:11:39 -04:00
|
|
|
// 2 ** 255 - 19
|
2023-02-23 04:05:47 -05:00
|
|
|
// Uses saturating_sub because checked_sub isn't available at compile time
|
|
|
|
|
const MODULUS: U256 = U256::from_u8(1).shl_vartime(255).saturating_sub(&U256::from_u8(19));
|
|
|
|
|
const WIDE_MODULUS: U512 = U256::ZERO.concat(&MODULUS);
|
2022-08-31 03:33:19 -04:00
|
|
|
|
2023-04-20 01:11:39 -04:00
|
|
|
impl_modulus!(
|
|
|
|
|
FieldModulus,
|
|
|
|
|
U256,
|
|
|
|
|
// 2 ** 255 - 19
|
|
|
|
|
"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffed"
|
|
|
|
|
);
|
|
|
|
|
type ResidueType = Residue<FieldModulus, { FieldModulus::LIMBS }>;
|
|
|
|
|
|
2023-03-20 20:10:00 -04:00
|
|
|
/// A constant-time implementation of the Ed25519 field.
|
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)]
|
2023-04-20 01:11:39 -04:00
|
|
|
pub struct FieldElement(ResidueType);
|
|
|
|
|
|
|
|
|
|
// Square root of -1.
|
|
|
|
|
// Formula from RFC-8032 (modp_sqrt_m1/sqrt8k5 z)
|
|
|
|
|
// 2 ** ((MODULUS - 1) // 4) % MODULUS
|
|
|
|
|
const SQRT_M1: FieldElement = FieldElement(
|
|
|
|
|
ResidueType::new(&U256::from_u8(2))
|
|
|
|
|
.pow(&MODULUS.saturating_sub(&U256::ONE).wrapping_div(&U256::from_u8(4))),
|
|
|
|
|
);
|
2022-07-10 15:20:42 -04:00
|
|
|
|
2023-02-23 04:05:47 -05:00
|
|
|
// Constant useful in calculating square roots (RFC-8032 sqrt8k5's exponent used to calculate y)
|
2023-04-20 01:11:39 -04:00
|
|
|
const MOD_3_8: FieldElement = FieldElement(ResidueType::new(
|
|
|
|
|
&MODULUS.saturating_add(&U256::from_u8(3)).wrapping_div(&U256::from_u8(8)),
|
|
|
|
|
));
|
2023-02-23 04:05:47 -05:00
|
|
|
|
|
|
|
|
// Constant useful in sqrt_ratio_i (sqrt(u / v))
|
2023-04-20 01:11:39 -04:00
|
|
|
const MOD_5_8: FieldElement = FieldElement(ResidueType::sub(&MOD_3_8.0, &ResidueType::ONE));
|
2023-02-23 04:05:47 -05:00
|
|
|
|
2023-04-20 01:11:39 -04:00
|
|
|
fn reduce(x: U512) -> ResidueType {
|
|
|
|
|
ResidueType::new(&U256::from_le_slice(
|
|
|
|
|
&x.rem(&NonZero::new(WIDE_MODULUS).unwrap()).to_le_bytes()[.. 32],
|
|
|
|
|
))
|
2022-08-31 03:33:19 -04:00
|
|
|
}
|
|
|
|
|
|
2023-04-20 01:11:39 -04:00
|
|
|
constant_time!(FieldElement, ResidueType);
|
2022-07-10 15:20:42 -04:00
|
|
|
math!(
|
2022-07-10 16:48:08 -04:00
|
|
|
FieldElement,
|
|
|
|
|
FieldElement,
|
2023-04-20 01:11:39 -04:00
|
|
|
|x: ResidueType, y: ResidueType| x.add(&y),
|
|
|
|
|
|x: ResidueType, y: ResidueType| x.sub(&y),
|
|
|
|
|
|x: ResidueType, y: ResidueType| x.mul(&y)
|
2022-07-10 15:20:42 -04:00
|
|
|
);
|
2023-04-20 01:11:39 -04:00
|
|
|
|
|
|
|
|
macro_rules! from_wrapper {
|
|
|
|
|
($uint: ident) => {
|
|
|
|
|
impl From<$uint> for FieldElement {
|
|
|
|
|
fn from(a: $uint) -> FieldElement {
|
|
|
|
|
Self(ResidueType::new(&U256::from(a)))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
from_wrapper!(u8);
|
|
|
|
|
from_wrapper!(u16);
|
|
|
|
|
from_wrapper!(u32);
|
|
|
|
|
from_wrapper!(u64);
|
|
|
|
|
from_wrapper!(u128);
|
2022-07-10 15:20:42 -04:00
|
|
|
|
|
|
|
|
impl Neg for FieldElement {
|
|
|
|
|
type Output = Self;
|
2022-07-15 01:26:07 -04:00
|
|
|
fn neg(self) -> Self::Output {
|
2023-04-20 01:11:39 -04:00
|
|
|
Self(self.0.neg())
|
2022-07-15 01:26:07 -04:00
|
|
|
}
|
2022-07-10 15:20:42 -04:00
|
|
|
}
|
|
|
|
|
|
2022-08-18 22:02:31 +02:00
|
|
|
impl<'a> Neg for &'a FieldElement {
|
|
|
|
|
type Output = FieldElement;
|
|
|
|
|
fn neg(self) -> Self::Output {
|
|
|
|
|
(*self).neg()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-10 15:20:42 -04:00
|
|
|
impl Field for FieldElement {
|
2023-04-20 01:11:39 -04:00
|
|
|
const ZERO: Self = Self(ResidueType::ZERO);
|
|
|
|
|
const ONE: Self = Self(ResidueType::ONE);
|
2023-03-28 04:38:01 -04:00
|
|
|
|
2022-07-10 15:20:42 -04:00
|
|
|
fn random(mut rng: impl RngCore) -> Self {
|
|
|
|
|
let mut bytes = [0; 64];
|
|
|
|
|
rng.fill_bytes(&mut bytes);
|
2022-08-31 03:33:19 -04:00
|
|
|
FieldElement(reduce(U512::from_le_bytes(bytes)))
|
2022-07-10 15:20:42 -04:00
|
|
|
}
|
|
|
|
|
|
2022-07-15 01:26:07 -04:00
|
|
|
fn square(&self) -> Self {
|
2023-04-20 01:11:39 -04:00
|
|
|
FieldElement(self.0.square())
|
2022-07-15 01:26:07 -04:00
|
|
|
}
|
|
|
|
|
fn double(&self) -> Self {
|
2023-04-20 01:11:39 -04:00
|
|
|
FieldElement(self.0.add(&self.0))
|
2022-07-15 01:26:07 -04:00
|
|
|
}
|
2022-07-10 15:20:42 -04:00
|
|
|
|
|
|
|
|
fn invert(&self) -> CtOption<Self> {
|
2023-04-20 01:11:39 -04:00
|
|
|
const NEG_2: FieldElement =
|
|
|
|
|
FieldElement(ResidueType::new(&MODULUS.saturating_sub(&U256::from_u8(2))));
|
2022-08-31 03:33:19 -04:00
|
|
|
CtOption::new(self.pow(NEG_2), !self.is_zero())
|
2022-07-10 15:20:42 -04:00
|
|
|
}
|
|
|
|
|
|
2023-02-23 04:05:47 -05:00
|
|
|
// RFC-8032 sqrt8k5
|
2022-07-10 15:20:42 -04:00
|
|
|
fn sqrt(&self) -> CtOption<Self> {
|
2022-08-31 03:33:19 -04:00
|
|
|
let tv1 = self.pow(MOD_3_8);
|
|
|
|
|
let tv2 = tv1 * SQRT_M1;
|
2022-12-15 19:23:42 -05:00
|
|
|
let candidate = Self::conditional_select(&tv2, &tv1, tv1.square().ct_eq(self));
|
|
|
|
|
CtOption::new(candidate, candidate.square().ct_eq(self))
|
2022-07-10 15:20:42 -04:00
|
|
|
}
|
2023-03-28 04:38:01 -04:00
|
|
|
|
|
|
|
|
fn sqrt_ratio(u: &FieldElement, v: &FieldElement) -> (Choice, FieldElement) {
|
|
|
|
|
let i = SQRT_M1;
|
|
|
|
|
|
|
|
|
|
let u = *u;
|
|
|
|
|
let v = *v;
|
|
|
|
|
|
|
|
|
|
let v3 = v.square() * v;
|
|
|
|
|
let v7 = v3.square() * v;
|
|
|
|
|
let mut r = (u * v3) * (u * v7).pow(MOD_5_8);
|
|
|
|
|
|
|
|
|
|
let check = v * r.square();
|
|
|
|
|
let correct_sign = check.ct_eq(&u);
|
|
|
|
|
let flipped_sign = check.ct_eq(&(-u));
|
|
|
|
|
let flipped_sign_i = check.ct_eq(&((-u) * i));
|
|
|
|
|
|
|
|
|
|
r.conditional_assign(&(r * i), flipped_sign | flipped_sign_i);
|
|
|
|
|
|
|
|
|
|
let r_is_negative = r.is_odd();
|
|
|
|
|
r.conditional_negate(r_is_negative);
|
|
|
|
|
|
|
|
|
|
(correct_sign | flipped_sign, r)
|
|
|
|
|
}
|
2022-07-10 15:20:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl PrimeField for FieldElement {
|
|
|
|
|
type Repr = [u8; 32];
|
2023-03-28 04:38:01 -04:00
|
|
|
|
|
|
|
|
// Big endian representation of the modulus
|
|
|
|
|
const MODULUS: &'static str = "7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffed";
|
|
|
|
|
|
2022-07-10 15:20:42 -04:00
|
|
|
const NUM_BITS: u32 = 255;
|
|
|
|
|
const CAPACITY: u32 = 254;
|
2023-03-28 04:38:01 -04:00
|
|
|
|
2023-04-20 01:11:39 -04:00
|
|
|
const TWO_INV: Self = FieldElement(ResidueType::new(&U256::from_u8(2)).invert().0);
|
2023-03-28 04:38:01 -04:00
|
|
|
|
|
|
|
|
// This was calculated with the method from the ff crate docs
|
|
|
|
|
// SageMath GF(modulus).primitive_element()
|
2023-04-20 01:11:39 -04:00
|
|
|
const MULTIPLICATIVE_GENERATOR: Self = Self(ResidueType::new(&U256::from_u8(2)));
|
2023-03-28 04:38:01 -04:00
|
|
|
// This was set per the specification in the ff crate docs
|
|
|
|
|
// The number of leading zero bits in the little-endian bit representation of (modulus - 1)
|
|
|
|
|
const S: u32 = 2;
|
|
|
|
|
|
|
|
|
|
// This was calculated via the formula from the ff crate docs
|
|
|
|
|
// Self::MULTIPLICATIVE_GENERATOR ** ((modulus - 1) >> Self::S)
|
2023-04-20 01:11:39 -04:00
|
|
|
const ROOT_OF_UNITY: Self = FieldElement(ResidueType::new(&U256::from_be_hex(
|
2023-03-28 04:38:01 -04:00
|
|
|
"2b8324804fc1df0b2b4d00993dfbd7a72f431806ad2fe478c4ee1b274a0ea0b0",
|
2023-04-20 01:11:39 -04:00
|
|
|
)));
|
2023-03-28 04:38:01 -04:00
|
|
|
// Self::ROOT_OF_UNITY.invert()
|
2023-04-20 01:11:39 -04:00
|
|
|
const ROOT_OF_UNITY_INV: Self = FieldElement(Self::ROOT_OF_UNITY.0.invert().0);
|
2023-03-28 04:38:01 -04:00
|
|
|
|
|
|
|
|
// This was calculated via the formula from the ff crate docs
|
|
|
|
|
// Self::MULTIPLICATIVE_GENERATOR ** (2 ** Self::S)
|
2023-04-20 01:11:39 -04:00
|
|
|
const DELTA: Self = FieldElement(ResidueType::new(&U256::from_be_hex(
|
2023-03-28 04:38:01 -04:00
|
|
|
"0000000000000000000000000000000000000000000000000000000000000010",
|
2023-04-20 01:11:39 -04:00
|
|
|
)));
|
2023-03-28 04:38:01 -04:00
|
|
|
|
2022-07-10 15:20:42 -04:00
|
|
|
fn from_repr(bytes: [u8; 32]) -> CtOption<Self> {
|
2023-04-20 01:11:39 -04:00
|
|
|
let res = U256::from_le_bytes(bytes);
|
|
|
|
|
CtOption::new(Self(ResidueType::new(&res)), res.ct_lt(&MODULUS))
|
2022-07-10 15:20:42 -04:00
|
|
|
}
|
2022-07-15 01:26:07 -04:00
|
|
|
fn to_repr(&self) -> [u8; 32] {
|
2023-04-20 01:11:39 -04:00
|
|
|
self.0.retrieve().to_le_bytes()
|
2022-07-15 01:26:07 -04:00
|
|
|
}
|
2022-07-10 15:20:42 -04:00
|
|
|
|
2022-07-15 01:26:07 -04:00
|
|
|
fn is_odd(&self) -> Choice {
|
2023-04-20 01:11:39 -04:00
|
|
|
self.0.retrieve().is_odd()
|
2022-07-15 01:26:07 -04:00
|
|
|
}
|
2023-03-28 04:38:01 -04:00
|
|
|
|
|
|
|
|
fn from_u128(num: u128) -> Self {
|
|
|
|
|
Self::from(num)
|
2022-07-10 15:20:42 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl PrimeFieldBits for FieldElement {
|
|
|
|
|
type ReprBits = [u8; 32];
|
|
|
|
|
|
|
|
|
|
fn to_le_bits(&self) -> FieldBits<Self::ReprBits> {
|
|
|
|
|
self.to_repr().into()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn char_le_bits() -> FieldBits<Self::ReprBits> {
|
2022-12-15 20:33:58 -05:00
|
|
|
MODULUS.to_le_bytes().into()
|
2022-07-10 15:20:42 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl FieldElement {
|
2023-03-20 20:10:00 -04:00
|
|
|
/// Interpret the value as a little-endian integer, square it, and reduce it into a FieldElement.
|
2022-07-10 15:20:42 -04:00
|
|
|
pub fn from_square(value: [u8; 32]) -> FieldElement {
|
|
|
|
|
let value = U256::from_le_bytes(value);
|
2023-04-20 01:11:39 -04:00
|
|
|
FieldElement(reduce(U512::from(value.mul_wide(&value))))
|
2022-07-10 15:20:42 -04:00
|
|
|
}
|
|
|
|
|
|
2023-03-20 20:10:00 -04:00
|
|
|
/// Perform an exponentation.
|
2022-07-10 15:20:42 -04:00
|
|
|
pub fn pow(&self, other: FieldElement) -> FieldElement {
|
2023-03-28 04:38:01 -04:00
|
|
|
let mut table = [FieldElement::ONE; 16];
|
2022-08-31 00:57:23 -04: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 = FieldElement::ONE;
|
2022-08-31 00:57:23 -04: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-08-31 00:57:23 -04:00
|
|
|
bits <<= 1;
|
2023-07-08 11:29:05 -04:00
|
|
|
let mut bit = u8_from_bool(&mut bit);
|
2022-08-31 00:57:23 -04:00
|
|
|
bits |= bit;
|
2023-03-10 06:27:44 -05:00
|
|
|
bit.zeroize();
|
2022-08-31 00:57:23 -04:00
|
|
|
|
|
|
|
|
if ((i + 1) % 4) == 0 {
|
|
|
|
|
if i != 3 {
|
|
|
|
|
for _ in 0 .. 4 {
|
|
|
|
|
res *= res;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
res *= table[usize::from(bits)];
|
|
|
|
|
bits = 0;
|
|
|
|
|
}
|
2022-07-10 15:20:42 -04:00
|
|
|
}
|
|
|
|
|
res
|
|
|
|
|
}
|
2022-08-15 11:37:00 +02:00
|
|
|
|
2023-02-23 04:05:47 -05:00
|
|
|
/// The square root of u/v, as used for Ed25519 point decoding (RFC 8032 5.1.3) and within
|
|
|
|
|
/// Ristretto (5.1 Extracting an Inverse Square Root).
|
|
|
|
|
///
|
|
|
|
|
/// The result is only a valid square root if the Choice is true.
|
|
|
|
|
/// RFC 8032 simply fails if there isn't a square root, leaving any return value undefined.
|
|
|
|
|
/// Ristretto explicitly returns 0 or sqrt((SQRT_M1 * u) / v).
|
2022-08-15 11:37:00 +02:00
|
|
|
pub fn sqrt_ratio_i(u: FieldElement, v: FieldElement) -> (Choice, FieldElement) {
|
2022-08-18 17:11:55 -04:00
|
|
|
let i = SQRT_M1;
|
|
|
|
|
|
2022-08-15 11:37:00 +02:00
|
|
|
let v3 = v.square() * v;
|
|
|
|
|
let v7 = v3.square() * v;
|
2023-02-23 04:05:47 -05:00
|
|
|
// Candidate root
|
2022-08-31 03:33:19 -04:00
|
|
|
let mut r = (u * v3) * (u * v7).pow(MOD_5_8);
|
2022-08-15 11:37:00 +02:00
|
|
|
|
2023-02-23 04:05:47 -05:00
|
|
|
// 8032 3.1
|
2022-08-18 17:11:55 -04:00
|
|
|
let check = v * r.square();
|
2022-08-15 11:37:00 +02:00
|
|
|
let correct_sign = check.ct_eq(&u);
|
2023-02-23 04:05:47 -05:00
|
|
|
// 8032 3.2 conditional
|
|
|
|
|
let neg_u = -u;
|
|
|
|
|
let flipped_sign = check.ct_eq(&neg_u);
|
|
|
|
|
// Ristretto Step 5
|
|
|
|
|
let flipped_sign_i = check.ct_eq(&(neg_u * i));
|
2022-08-15 11:37:00 +02:00
|
|
|
|
2023-02-23 04:05:47 -05:00
|
|
|
// 3.2 set
|
2022-08-18 17:11:55 -04:00
|
|
|
r.conditional_assign(&(r * i), flipped_sign | flipped_sign_i);
|
2022-08-15 11:37:00 +02:00
|
|
|
|
2023-02-23 04:05:47 -05:00
|
|
|
// Always return the even root, per Ristretto
|
|
|
|
|
// This doesn't break Ed25519 point decoding as that doesn't expect these steps to return a
|
|
|
|
|
// specific root
|
|
|
|
|
// Ed25519 points include a dedicated sign bit to determine which root to use, so at worst
|
|
|
|
|
// this is a pointless inefficiency
|
|
|
|
|
r.conditional_negate(r.is_odd());
|
2022-08-15 11:37:00 +02:00
|
|
|
|
2022-08-18 17:11:55 -04:00
|
|
|
(correct_sign | flipped_sign, r)
|
2022-08-15 11:37:00 +02:00
|
|
|
}
|
2022-07-10 15:20:42 -04:00
|
|
|
}
|
|
|
|
|
|
2023-03-28 04:38:01 -04:00
|
|
|
impl Sum<FieldElement> for FieldElement {
|
|
|
|
|
fn sum<I: Iterator<Item = FieldElement>>(iter: I) -> FieldElement {
|
|
|
|
|
let mut res = FieldElement::ZERO;
|
|
|
|
|
for item in iter {
|
|
|
|
|
res += item;
|
|
|
|
|
}
|
|
|
|
|
res
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a> Sum<&'a FieldElement> for FieldElement {
|
|
|
|
|
fn sum<I: Iterator<Item = &'a FieldElement>>(iter: I) -> FieldElement {
|
2023-07-08 11:29:05 -04:00
|
|
|
iter.copied().sum()
|
2023-03-28 04:38:01 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Product<FieldElement> for FieldElement {
|
|
|
|
|
fn product<I: Iterator<Item = FieldElement>>(iter: I) -> FieldElement {
|
|
|
|
|
let mut res = FieldElement::ONE;
|
|
|
|
|
for item in iter {
|
|
|
|
|
res *= item;
|
|
|
|
|
}
|
|
|
|
|
res
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a> Product<&'a FieldElement> for FieldElement {
|
|
|
|
|
fn product<I: Iterator<Item = &'a FieldElement>>(iter: I) -> FieldElement {
|
2023-07-08 11:29:05 -04:00
|
|
|
iter.copied().product()
|
2023-03-28 04:38:01 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-23 04:05:47 -05:00
|
|
|
#[test]
|
|
|
|
|
fn test_wide_modulus() {
|
|
|
|
|
let mut wide = [0; 64];
|
|
|
|
|
wide[.. 32].copy_from_slice(&MODULUS.to_le_bytes());
|
|
|
|
|
assert_eq!(wide, WIDE_MODULUS.to_le_bytes());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_sqrt_m1() {
|
|
|
|
|
// Test equivalence against the known constant value
|
|
|
|
|
const SQRT_M1_MAGIC: U256 =
|
|
|
|
|
U256::from_be_hex("2b8324804fc1df0b2b4d00993dfbd7a72f431806ad2fe478c4ee1b274a0ea0b0");
|
2023-04-20 01:11:39 -04:00
|
|
|
assert_eq!(SQRT_M1.0.retrieve(), SQRT_M1_MAGIC);
|
2023-02-23 04:05:47 -05:00
|
|
|
|
|
|
|
|
// Also test equivalence against the result of the formula from RFC-8032 (modp_sqrt_m1/sqrt8k5 z)
|
|
|
|
|
// 2 ** ((MODULUS - 1) // 4) % MODULUS
|
|
|
|
|
assert_eq!(
|
|
|
|
|
SQRT_M1,
|
2023-04-20 01:11:39 -04:00
|
|
|
FieldElement::from(2u8).pow(FieldElement(ResidueType::new(
|
|
|
|
|
&(FieldElement::ZERO - FieldElement::ONE).0.retrieve().wrapping_div(&U256::from(4u8))
|
|
|
|
|
)))
|
2023-02-23 04:05:47 -05:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-15 20:33:58 -05:00
|
|
|
#[test]
|
2022-12-24 15:09:09 -05:00
|
|
|
fn test_field() {
|
2023-02-24 06:03:56 -05:00
|
|
|
ff_group_tests::prime_field::test_prime_field_bits::<_, FieldElement>(&mut rand_core::OsRng);
|
2022-08-15 11:37:00 +02:00
|
|
|
}
|