mirror of
https://github.com/serai-dex/serai.git
synced 2025-12-08 12:19:24 +00:00
Consolidate macros in dalek-ff-group
This commit is contained in:
@@ -7,7 +7,7 @@ use crypto_bigint::{Encoding, U256, U512};
|
|||||||
|
|
||||||
use ff::{Field, PrimeField, FieldBits, PrimeFieldBits};
|
use ff::{Field, PrimeField, FieldBits, PrimeFieldBits};
|
||||||
|
|
||||||
use crate::{choice, from_wrapper, from_uint};
|
use crate::{choice, constant_time, math_op, math, from_wrapper, from_uint};
|
||||||
|
|
||||||
const FIELD_MODULUS: U256 = U256::from_be_hex(
|
const FIELD_MODULUS: U256 = U256::from_be_hex(
|
||||||
"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffed"
|
"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffed"
|
||||||
@@ -20,64 +20,30 @@ pub const SQRT_M1: FieldElement = FieldElement(
|
|||||||
U256::from_be_hex("2b8324804fc1df0b2b4d00993dfbd7a72f431806ad2fe478c4ee1b274a0ea0b0")
|
U256::from_be_hex("2b8324804fc1df0b2b4d00993dfbd7a72f431806ad2fe478c4ee1b274a0ea0b0")
|
||||||
);
|
);
|
||||||
|
|
||||||
macro_rules! math {
|
constant_time!(FieldElement, U256);
|
||||||
($Op: ident, $op_fn: ident, $Assign: ident, $assign_fn: ident, $function: expr) => {
|
|
||||||
impl $Op<FieldElement> for FieldElement {
|
|
||||||
type Output = Self;
|
|
||||||
fn $op_fn(self, other: FieldElement) -> Self::Output {
|
|
||||||
Self($function(&self.0, &other.0, &FIELD_MODULUS))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl $Assign<FieldElement> for FieldElement {
|
|
||||||
fn $assign_fn(&mut self, other: FieldElement) {
|
|
||||||
self.0 = $function(&self.0, &other.0, &FIELD_MODULUS);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl<'a> $Op<&'a FieldElement> for FieldElement {
|
|
||||||
type Output = Self;
|
|
||||||
fn $op_fn(self, other: &'a FieldElement) -> Self::Output {
|
|
||||||
Self($function(&self.0, &other.0, &FIELD_MODULUS))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl<'a> $Assign<&'a FieldElement> for FieldElement {
|
|
||||||
fn $assign_fn(&mut self, other: &'a FieldElement) {
|
|
||||||
self.0 = $function(&self.0, &other.0, &FIELD_MODULUS);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
math!(Add, add, AddAssign, add_assign, U256::add_mod);
|
|
||||||
math!(Sub, sub, SubAssign, sub_assign, U256::sub_mod);
|
|
||||||
math!(
|
math!(
|
||||||
Mul, mul,
|
FieldElement,
|
||||||
MulAssign, mul_assign,
|
FieldElement,
|
||||||
|a, b, _: &U256| {
|
|x, y| U256::add_mod(&x, &y, &FIELD_MODULUS),
|
||||||
|
|x, y| U256::sub_mod(&x, &y, &FIELD_MODULUS),
|
||||||
|
|x, y| {
|
||||||
#[allow(non_snake_case)]
|
#[allow(non_snake_case)]
|
||||||
let WIDE_MODULUS: U512 = U512::from((U256::ZERO, FIELD_MODULUS));
|
let WIDE_MODULUS: U512 = U512::from((U256::ZERO, FIELD_MODULUS));
|
||||||
debug_assert_eq!(FIELD_MODULUS.to_le_bytes()[..], WIDE_MODULUS.to_le_bytes()[.. 32]);
|
debug_assert_eq!(FIELD_MODULUS.to_le_bytes()[..], WIDE_MODULUS.to_le_bytes()[.. 32]);
|
||||||
|
|
||||||
let wide = U256::mul_wide(a, b);
|
let wide = U256::mul_wide(&x, &y);
|
||||||
U256::from_le_slice(
|
U256::from_le_slice(
|
||||||
&U512::from((wide.1, wide.0)).reduce(&WIDE_MODULUS).unwrap().to_le_bytes()[.. 32]
|
&U512::from((wide.1, wide.0)).reduce(&WIDE_MODULUS).unwrap().to_le_bytes()[.. 32]
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
from_uint!(FieldElement, U256);
|
||||||
|
|
||||||
impl Neg for FieldElement {
|
impl Neg for FieldElement {
|
||||||
type Output = Self;
|
type Output = Self;
|
||||||
fn neg(self) -> Self::Output { Self(self.0.neg_mod(&FIELD_MODULUS)) }
|
fn neg(self) -> Self::Output { Self(self.0.neg_mod(&FIELD_MODULUS)) }
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ConstantTimeEq for FieldElement {
|
|
||||||
fn ct_eq(&self, other: &Self) -> Choice { self.0.ct_eq(&other.0) }
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ConditionallySelectable for FieldElement {
|
|
||||||
fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self {
|
|
||||||
FieldElement(U256::conditional_select(&a.0, &b.0, choice))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Field for FieldElement {
|
impl Field for FieldElement {
|
||||||
fn random(mut rng: impl RngCore) -> Self {
|
fn random(mut rng: impl RngCore) -> Self {
|
||||||
let mut bytes = [0; 64];
|
let mut bytes = [0; 64];
|
||||||
@@ -119,8 +85,6 @@ impl Field for FieldElement {
|
|||||||
fn pow_vartime<S: AsRef<[u64]>>(&self, _exp: S) -> Self { unimplemented!() }
|
fn pow_vartime<S: AsRef<[u64]>>(&self, _exp: S) -> Self { unimplemented!() }
|
||||||
}
|
}
|
||||||
|
|
||||||
from_uint!(FieldElement, U256);
|
|
||||||
|
|
||||||
impl PrimeField for FieldElement {
|
impl PrimeField for FieldElement {
|
||||||
type Repr = [u8; 32];
|
type Repr = [u8; 32];
|
||||||
const NUM_BITS: u32 = 255;
|
const NUM_BITS: u32 = 255;
|
||||||
@@ -174,5 +138,5 @@ impl FieldElement {
|
|||||||
fn test_mul() {
|
fn test_mul() {
|
||||||
assert_eq!(FieldElement(FIELD_MODULUS) * FieldElement::one(), FieldElement::zero());
|
assert_eq!(FieldElement(FIELD_MODULUS) * FieldElement::one(), FieldElement::zero());
|
||||||
assert_eq!(FieldElement(FIELD_MODULUS) * FieldElement::one().double(), FieldElement::zero());
|
assert_eq!(FieldElement(FIELD_MODULUS) * FieldElement::one().double(), FieldElement::zero());
|
||||||
assert_eq!(FieldElement(SQRT_M1).square(), -FieldElement::one());
|
assert_eq!(SQRT_M1.square(), -FieldElement::one());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,10 +6,12 @@ use core::{
|
|||||||
iter::{Iterator, Sum}
|
iter::{Iterator, Sum}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use subtle::{ConstantTimeEq, ConditionallySelectable};
|
||||||
|
|
||||||
use rand_core::RngCore;
|
use rand_core::RngCore;
|
||||||
use digest::{consts::U64, Digest};
|
use digest::{consts::U64, Digest};
|
||||||
|
|
||||||
use subtle::{Choice, CtOption, ConstantTimeEq, ConditionallySelectable};
|
use subtle::{Choice, CtOption};
|
||||||
|
|
||||||
pub use curve25519_dalek as dalek;
|
pub use curve25519_dalek as dalek;
|
||||||
|
|
||||||
@@ -65,60 +67,77 @@ macro_rules! deref_borrow {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[doc(hidden)]
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! constant_time {
|
||||||
|
($Value: ident, $Inner: ident) => {
|
||||||
|
impl ConstantTimeEq for $Value {
|
||||||
|
fn ct_eq(&self, other: &Self) -> Choice { self.0.ct_eq(&other.0) }
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ConditionallySelectable for $Value {
|
||||||
|
fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self {
|
||||||
|
$Value($Inner::conditional_select(&a.0, &b.0, choice))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[doc(hidden)]
|
||||||
|
#[macro_export]
|
||||||
|
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))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl $Assign<$Other> for $Value {
|
||||||
|
fn $assign_fn(&mut self, other: $Other) {
|
||||||
|
self.0 = $function(self.0, other.0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl<'a> $Assign<&'a $Other> for $Value {
|
||||||
|
fn $assign_fn(&mut self, other: &'a $Other) {
|
||||||
|
self.0 = $function(self.0, other.0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[doc(hidden)]
|
||||||
|
#[macro_export]
|
||||||
macro_rules! math {
|
macro_rules! math {
|
||||||
($Value: ident, $Factor: ident, $Product: ident) => {
|
($Value: ident, $Factor: ident, $add: expr, $sub: expr, $mul: expr) => {
|
||||||
impl Add<$Value> for $Value {
|
math_op!($Value, $Value, Add, add, AddAssign, add_assign, $add);
|
||||||
type Output = Self;
|
math_op!($Value, $Value, Sub, sub, SubAssign, sub_assign, $sub);
|
||||||
fn add(self, other: $Value) -> Self::Output { Self(self.0 + other.0) }
|
math_op!($Value, $Factor, Mul, mul, MulAssign, mul_assign, $mul);
|
||||||
}
|
}
|
||||||
impl AddAssign for $Value {
|
}
|
||||||
fn add_assign(&mut self, other: $Value) { self.0 += other.0 }
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> Add<&'a $Value> for $Value {
|
macro_rules! math_neg {
|
||||||
type Output = Self;
|
($Value: ident, $Factor: ident, $add: expr, $sub: expr, $mul: expr) => {
|
||||||
fn add(self, other: &'a $Value) -> Self::Output { Self(self.0 + other.0) }
|
math!($Value, $Factor, $add, $sub, $mul);
|
||||||
}
|
|
||||||
impl<'a> AddAssign<&'a $Value> for $Value {
|
|
||||||
fn add_assign(&mut self, other: &'a $Value) { self.0 += other.0 }
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Sub<$Value> for $Value {
|
|
||||||
type Output = Self;
|
|
||||||
fn sub(self, other: $Value) -> Self::Output { Self(self.0 - other.0) }
|
|
||||||
}
|
|
||||||
impl SubAssign for $Value {
|
|
||||||
fn sub_assign(&mut self, other: $Value) { self.0 -= other.0 }
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> Sub<&'a $Value> for $Value {
|
|
||||||
type Output = Self;
|
|
||||||
fn sub(self, other: &'a $Value) -> Self::Output { Self(self.0 - other.0) }
|
|
||||||
}
|
|
||||||
impl<'a> SubAssign<&'a $Value> for $Value {
|
|
||||||
fn sub_assign(&mut self, other: &'a $Value) { self.0 -= other.0 }
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Neg for $Value {
|
impl Neg for $Value {
|
||||||
type Output = Self;
|
type Output = Self;
|
||||||
fn neg(self) -> Self::Output { Self(-self.0) }
|
fn neg(self) -> Self::Output { Self(-self.0) }
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Mul<$Factor> for $Value {
|
|
||||||
type Output = $Product;
|
|
||||||
fn mul(self, other: $Factor) -> Self::Output { Self(self.0 * other.0) }
|
|
||||||
}
|
|
||||||
impl MulAssign<$Factor> for $Value {
|
|
||||||
fn mul_assign(&mut self, other: $Factor) { self.0 *= other.0 }
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> Mul<&'a $Factor> for $Value {
|
|
||||||
type Output = Self;
|
|
||||||
fn mul(self, b: &'a $Factor) -> $Product { Self(b.0 * self.0) }
|
|
||||||
}
|
|
||||||
impl<'a> MulAssign<&'a $Factor> for $Value {
|
|
||||||
fn mul_assign(&mut self, other: &'a $Factor) { self.0 *= other.0 }
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -147,7 +166,8 @@ macro_rules! from_uint {
|
|||||||
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
|
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
|
||||||
pub struct Scalar(pub DScalar);
|
pub struct Scalar(pub DScalar);
|
||||||
deref_borrow!(Scalar, DScalar);
|
deref_borrow!(Scalar, DScalar);
|
||||||
math!(Scalar, Scalar, Scalar);
|
constant_time!(Scalar, DScalar);
|
||||||
|
math_neg!(Scalar, Scalar, DScalar::add, DScalar::sub, DScalar::mul);
|
||||||
from_uint!(Scalar, DScalar);
|
from_uint!(Scalar, DScalar);
|
||||||
|
|
||||||
impl Scalar {
|
impl Scalar {
|
||||||
@@ -164,16 +184,6 @@ impl Scalar {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ConstantTimeEq for Scalar {
|
|
||||||
fn ct_eq(&self, other: &Self) -> Choice { self.0.ct_eq(&other.0) }
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ConditionallySelectable for Scalar {
|
|
||||||
fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self {
|
|
||||||
Scalar(DScalar::conditional_select(a, b, choice))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Field for Scalar {
|
impl Field for Scalar {
|
||||||
fn random(mut rng: impl RngCore) -> Self {
|
fn random(mut rng: impl RngCore) -> Self {
|
||||||
let mut r = [0; 64];
|
let mut r = [0; 64];
|
||||||
@@ -244,7 +254,8 @@ macro_rules! dalek_group {
|
|||||||
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
||||||
pub struct $Point(pub $DPoint);
|
pub struct $Point(pub $DPoint);
|
||||||
deref_borrow!($Point, $DPoint);
|
deref_borrow!($Point, $DPoint);
|
||||||
math!($Point, Scalar, $Point);
|
constant_time!($Point, $DPoint);
|
||||||
|
math_neg!($Point, Scalar, $DPoint::add, $DPoint::sub, $DPoint::mul);
|
||||||
|
|
||||||
pub const $BASEPOINT_POINT: $Point = $Point(constants::$BASEPOINT_POINT);
|
pub const $BASEPOINT_POINT: $Point = $Point(constants::$BASEPOINT_POINT);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user