2022-07-10 15:20:42 -04:00
|
|
|
use core::ops::{Add, AddAssign, Sub, SubAssign, Neg, Mul, MulAssign};
|
|
|
|
|
|
|
|
|
|
use rand_core::RngCore;
|
|
|
|
|
|
2022-08-18 22:02:31 +02:00
|
|
|
use subtle::{Choice, CtOption, ConstantTimeEq, 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
|
|
|
|
2022-07-10 15:20:42 -04:00
|
|
|
use crypto_bigint::{Encoding, U256, U512};
|
|
|
|
|
|
|
|
|
|
use ff::{Field, PrimeField, FieldBits, PrimeFieldBits};
|
|
|
|
|
|
2022-07-10 16:48:08 -04:00
|
|
|
use crate::{choice, constant_time, math_op, math, from_wrapper, from_uint};
|
2022-07-10 15:20:42 -04:00
|
|
|
|
2022-07-15 01:26:07 -04:00
|
|
|
const FIELD_MODULUS: U256 =
|
|
|
|
|
U256::from_be_hex("7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffed");
|
2022-07-10 15:20:42 -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
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, Default, Debug)]
|
2022-07-10 15:20:42 -04:00
|
|
|
pub struct FieldElement(U256);
|
|
|
|
|
|
2022-08-12 22:00:55 +02:00
|
|
|
pub const EDWARDS_D: FieldElement = FieldElement(U256::from_be_hex(
|
|
|
|
|
"52036cee2b6ffe738cc740797779e89800700a4d4141d8ab75eb4dca135978a3",
|
|
|
|
|
));
|
|
|
|
|
|
2022-07-15 01:26:07 -04:00
|
|
|
pub const SQRT_M1: FieldElement = FieldElement(U256::from_be_hex(
|
|
|
|
|
"2b8324804fc1df0b2b4d00993dfbd7a72f431806ad2fe478c4ee1b274a0ea0b0",
|
|
|
|
|
));
|
2022-07-10 15:20:42 -04:00
|
|
|
|
2022-07-10 16:48:08 -04:00
|
|
|
constant_time!(FieldElement, U256);
|
2022-07-10 15:20:42 -04:00
|
|
|
math!(
|
2022-07-10 16:48:08 -04:00
|
|
|
FieldElement,
|
|
|
|
|
FieldElement,
|
|
|
|
|
|x, y| U256::add_mod(&x, &y, &FIELD_MODULUS),
|
|
|
|
|
|x, y| U256::sub_mod(&x, &y, &FIELD_MODULUS),
|
|
|
|
|
|x, y| {
|
2022-07-10 15:20:42 -04:00
|
|
|
#[allow(non_snake_case)]
|
|
|
|
|
let WIDE_MODULUS: U512 = U512::from((U256::ZERO, FIELD_MODULUS));
|
|
|
|
|
debug_assert_eq!(FIELD_MODULUS.to_le_bytes()[..], WIDE_MODULUS.to_le_bytes()[.. 32]);
|
|
|
|
|
|
2022-07-10 16:48:08 -04:00
|
|
|
let wide = U256::mul_wide(&x, &y);
|
2022-07-10 15:20:42 -04:00
|
|
|
U256::from_le_slice(
|
2022-07-15 01:26:07 -04:00
|
|
|
&U512::from((wide.1, wide.0)).reduce(&WIDE_MODULUS).unwrap().to_le_bytes()[.. 32],
|
2022-07-10 15:20:42 -04:00
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
);
|
2022-07-10 16:48:08 -04:00
|
|
|
from_uint!(FieldElement, U256);
|
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 {
|
|
|
|
|
Self(self.0.neg_mod(&FIELD_MODULUS))
|
|
|
|
|
}
|
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 {
|
|
|
|
|
fn random(mut rng: impl RngCore) -> Self {
|
|
|
|
|
let mut bytes = [0; 64];
|
|
|
|
|
rng.fill_bytes(&mut bytes);
|
|
|
|
|
|
|
|
|
|
#[allow(non_snake_case)]
|
|
|
|
|
let WIDE_MODULUS: U512 = U512::from((U256::ZERO, FIELD_MODULUS));
|
|
|
|
|
debug_assert_eq!(FIELD_MODULUS.to_le_bytes()[..], WIDE_MODULUS.to_le_bytes()[.. 32]);
|
|
|
|
|
|
2022-07-15 01:26:07 -04:00
|
|
|
FieldElement(U256::from_le_slice(
|
|
|
|
|
&U512::from_be_bytes(bytes).reduce(&WIDE_MODULUS).unwrap().to_le_bytes()[.. 32],
|
|
|
|
|
))
|
2022-07-10 15:20:42 -04:00
|
|
|
}
|
|
|
|
|
|
2022-07-15 01:26:07 -04:00
|
|
|
fn zero() -> Self {
|
|
|
|
|
Self(U256::ZERO)
|
|
|
|
|
}
|
|
|
|
|
fn one() -> Self {
|
|
|
|
|
Self(U256::ONE)
|
|
|
|
|
}
|
|
|
|
|
fn square(&self) -> Self {
|
|
|
|
|
*self * self
|
|
|
|
|
}
|
|
|
|
|
fn double(&self) -> Self {
|
|
|
|
|
*self + self
|
|
|
|
|
}
|
2022-07-10 15:20:42 -04:00
|
|
|
|
|
|
|
|
fn invert(&self) -> CtOption<Self> {
|
|
|
|
|
CtOption::new(self.pow(-FieldElement(U256::from(2u64))), !self.is_zero())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn sqrt(&self) -> CtOption<Self> {
|
|
|
|
|
let c1 = SQRT_M1;
|
|
|
|
|
let c2 = FIELD_MODULUS.saturating_add(&U256::from(3u8)).checked_div(&U256::from(8u8)).unwrap();
|
|
|
|
|
|
|
|
|
|
let tv1 = self.pow(FieldElement(c2));
|
|
|
|
|
let tv2 = tv1 * c1;
|
|
|
|
|
let res = Self::conditional_select(&tv2, &tv1, tv1.square().ct_eq(self));
|
|
|
|
|
debug_assert_eq!(res * res, *self);
|
|
|
|
|
CtOption::new(Self::conditional_select(&tv2, &tv1, tv1.square().ct_eq(self)), 1.into())
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-15 01:26:07 -04:00
|
|
|
fn is_zero(&self) -> Choice {
|
|
|
|
|
self.0.ct_eq(&U256::ZERO)
|
|
|
|
|
}
|
|
|
|
|
fn cube(&self) -> Self {
|
|
|
|
|
*self * self * self
|
|
|
|
|
}
|
|
|
|
|
fn pow_vartime<S: AsRef<[u64]>>(&self, _exp: S) -> Self {
|
|
|
|
|
unimplemented!()
|
|
|
|
|
}
|
2022-07-10 15:20:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl PrimeField for FieldElement {
|
|
|
|
|
type Repr = [u8; 32];
|
|
|
|
|
const NUM_BITS: u32 = 255;
|
|
|
|
|
const CAPACITY: u32 = 254;
|
|
|
|
|
fn from_repr(bytes: [u8; 32]) -> CtOption<Self> {
|
|
|
|
|
let res = Self(U256::from_le_bytes(bytes));
|
|
|
|
|
CtOption::new(res, res.0.add_mod(&U256::ZERO, &FIELD_MODULUS).ct_eq(&res.0))
|
|
|
|
|
}
|
2022-07-15 01:26:07 -04:00
|
|
|
fn to_repr(&self) -> [u8; 32] {
|
|
|
|
|
self.0.to_le_bytes()
|
|
|
|
|
}
|
2022-07-10 15:20:42 -04:00
|
|
|
|
|
|
|
|
const S: u32 = 2;
|
2022-07-15 01:26:07 -04:00
|
|
|
fn is_odd(&self) -> Choice {
|
2022-08-12 22:05:48 +02:00
|
|
|
(self.to_repr()[0] & 1).into()
|
2022-07-15 01:26:07 -04:00
|
|
|
}
|
|
|
|
|
fn multiplicative_generator() -> Self {
|
|
|
|
|
2u64.into()
|
|
|
|
|
}
|
2022-07-10 15:20:42 -04:00
|
|
|
fn root_of_unity() -> Self {
|
2022-07-15 01:26:07 -04:00
|
|
|
FieldElement(U256::from_be_hex(
|
|
|
|
|
"2b8324804fc1df0b2b4d00993dfbd7a72f431806ad2fe478c4ee1b274a0ea0b0",
|
|
|
|
|
))
|
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> {
|
|
|
|
|
FIELD_MODULUS.to_le_bytes().into()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl FieldElement {
|
|
|
|
|
pub fn from_square(value: [u8; 32]) -> FieldElement {
|
|
|
|
|
let value = U256::from_le_bytes(value);
|
|
|
|
|
FieldElement(value) * FieldElement(value)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn pow(&self, other: FieldElement) -> FieldElement {
|
|
|
|
|
let mut res = FieldElement(U256::ONE);
|
|
|
|
|
let mut m = *self;
|
|
|
|
|
for bit in other.to_le_bits() {
|
|
|
|
|
res *= FieldElement::conditional_select(&FieldElement(U256::ONE), &m, choice(bit));
|
|
|
|
|
m *= m;
|
|
|
|
|
}
|
|
|
|
|
res
|
|
|
|
|
}
|
2022-08-15 11:37:00 +02:00
|
|
|
|
|
|
|
|
pub fn sqrt_ratio_i(u: FieldElement, v: FieldElement) -> (Choice, FieldElement) {
|
|
|
|
|
let v3 = v.square() * v;
|
|
|
|
|
let v7 = v3.square() * v;
|
|
|
|
|
let mut r = (u * v3) *
|
|
|
|
|
(u * v7).pow((-FieldElement::from(5u8)) * FieldElement::from(8u8).invert().unwrap());
|
|
|
|
|
let check = (v) * r.square();
|
|
|
|
|
let i = SQRT_M1;
|
|
|
|
|
|
|
|
|
|
let correct_sign = check.ct_eq(&u);
|
|
|
|
|
let flipped_sign = check.ct_eq(&(-u));
|
|
|
|
|
let flipped_sign_i = check.ct_eq(&((-u) * i));
|
|
|
|
|
|
|
|
|
|
let r_prime = i * r;
|
|
|
|
|
|
|
|
|
|
r.conditional_assign(&r_prime, flipped_sign | flipped_sign_i);
|
|
|
|
|
|
|
|
|
|
let r_is_negative = r.is_odd();
|
2022-08-18 22:02:31 +02:00
|
|
|
r.conditional_negate(r_is_negative);
|
2022-08-15 11:37:00 +02:00
|
|
|
|
|
|
|
|
let was_non_zero_square = correct_sign | flipped_sign;
|
|
|
|
|
|
|
|
|
|
(was_non_zero_square, r)
|
|
|
|
|
}
|
2022-07-10 15:20:42 -04:00
|
|
|
}
|
|
|
|
|
|
2022-08-12 22:00:55 +02:00
|
|
|
#[test]
|
2022-08-18 22:02:31 +02:00
|
|
|
fn test_conditional_negate() {
|
|
|
|
|
let one = FieldElement::one();
|
|
|
|
|
let true_choice = choice(true);
|
|
|
|
|
let false_choice = choice(false);
|
2022-08-12 22:05:48 +02:00
|
|
|
|
2022-08-18 22:02:31 +02:00
|
|
|
let mut var = one;
|
|
|
|
|
|
|
|
|
|
var.conditional_negate(false_choice);
|
|
|
|
|
assert_eq!(var, FieldElement::one());
|
|
|
|
|
|
|
|
|
|
var.conditional_negate(true_choice);
|
|
|
|
|
assert_eq!(var, -FieldElement::one());
|
|
|
|
|
|
|
|
|
|
var.conditional_negate(false_choice);
|
|
|
|
|
assert_eq!(var, -FieldElement::one());
|
2022-08-12 22:05:48 +02:00
|
|
|
}
|
|
|
|
|
|
2022-08-13 19:43:43 -04:00
|
|
|
#[test]
|
2022-08-12 22:00:55 +02:00
|
|
|
fn test_edwards_d() {
|
|
|
|
|
let a = -FieldElement(U256::from_u32(121665));
|
|
|
|
|
let b = FieldElement(U256::from_u32(121666));
|
|
|
|
|
|
|
|
|
|
assert_eq!(EDWARDS_D, a * b.invert().unwrap());
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-18 22:02:31 +02:00
|
|
|
#[test]
|
|
|
|
|
fn test_is_odd() {
|
|
|
|
|
assert_eq!(0, FieldElement::zero().is_odd().unwrap_u8());
|
|
|
|
|
assert_eq!(1, FieldElement::one().is_odd().unwrap_u8());
|
|
|
|
|
assert_eq!(0, FieldElement::one().double().is_odd().unwrap_u8());
|
|
|
|
|
|
|
|
|
|
// 0 is even, yet the modulus is odd
|
|
|
|
|
// -1 moves to the even value before the modulus
|
|
|
|
|
assert_eq!(0, (-FieldElement::one()).is_odd().unwrap_u8());
|
|
|
|
|
assert_eq!(1, (-FieldElement::one().double()).is_odd().unwrap_u8());
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-10 15:20:42 -04:00
|
|
|
#[test]
|
|
|
|
|
fn test_mul() {
|
|
|
|
|
assert_eq!(FieldElement(FIELD_MODULUS) * FieldElement::one(), FieldElement::zero());
|
|
|
|
|
assert_eq!(FieldElement(FIELD_MODULUS) * FieldElement::one().double(), FieldElement::zero());
|
2022-07-10 16:48:08 -04:00
|
|
|
assert_eq!(SQRT_M1.square(), -FieldElement::one());
|
2022-07-10 15:20:42 -04:00
|
|
|
}
|
2022-08-15 11:37:00 +02:00
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_sqrt_ratio_i() {
|
|
|
|
|
let zero = FieldElement::zero();
|
|
|
|
|
let one = FieldElement::one();
|
|
|
|
|
let two = one + one;
|
|
|
|
|
let three = two + one;
|
|
|
|
|
|
|
|
|
|
let (choice, sqrt) = FieldElement::sqrt_ratio_i(zero, zero);
|
|
|
|
|
assert_eq!(sqrt, zero);
|
|
|
|
|
assert_eq!(sqrt.is_odd().unwrap_u8(), 0);
|
|
|
|
|
assert_eq!(choice.unwrap_u8(), 1);
|
|
|
|
|
|
|
|
|
|
let (choice, sqrt) = FieldElement::sqrt_ratio_i(one, zero);
|
|
|
|
|
assert_eq!(sqrt, zero);
|
|
|
|
|
assert_eq!(sqrt.is_odd().unwrap_u8(), 0);
|
|
|
|
|
assert_eq!(choice.unwrap_u8(), 0);
|
|
|
|
|
|
|
|
|
|
let (choice, sqrt) = FieldElement::sqrt_ratio_i(two, one);
|
|
|
|
|
assert_eq!(sqrt.square(), two * SQRT_M1);
|
|
|
|
|
assert_eq!(sqrt.is_odd().unwrap_u8(), 0);
|
|
|
|
|
assert_eq!(choice.unwrap_u8(), 0);
|
|
|
|
|
|
|
|
|
|
let (choice, sqrt) = FieldElement::sqrt_ratio_i(three, one);
|
|
|
|
|
assert_eq!(sqrt.square(), three);
|
|
|
|
|
assert_eq!(sqrt.is_odd().unwrap_u8(), 0);
|
|
|
|
|
assert_eq!(choice.unwrap_u8(), 1);
|
|
|
|
|
|
|
|
|
|
let (choice, sqrt) = FieldElement::sqrt_ratio_i(one, three);
|
|
|
|
|
assert_eq!(sqrt.square() * three, one);
|
|
|
|
|
assert_eq!(sqrt.is_odd().unwrap_u8(), 0);
|
|
|
|
|
assert_eq!(choice.unwrap_u8(), 1);
|
|
|
|
|
}
|