Use crypto-bigint's reduction in ed448

Achieves feasible performance in the ed448 which makes it potentially viable
for real world usage.

Accordingly prepares a new release, updating the README.
This commit is contained in:
Luke Parker
2023-04-19 02:25:19 -04:00
parent 21026136bd
commit 334873b6a5
6 changed files with 33 additions and 11 deletions

View File

@@ -69,6 +69,7 @@ macro_rules! field {
(
$FieldName: ident,
$MODULUS_PADDED_STR: ident,
$MODULUS_STR: ident,
$MODULUS: ident,
$WIDE_MODULUS: ident,
@@ -89,12 +90,14 @@ macro_rules! field {
use rand_core::RngCore;
use generic_array::{typenum::U57, GenericArray};
use crypto_bigint::{Integer, NonZero, Encoding};
use crypto_bigint::{Integer, NonZero, Encoding, impl_modulus};
use ff::{Field, PrimeField, FieldBits, PrimeFieldBits, helpers::sqrt_ratio_generic};
use $crate::backend::u8_from_bool;
impl_modulus!(CryptoBigIntModulus, U512, $MODULUS_PADDED_STR);
fn reduce(x: U1024) -> U512 {
U512::from_le_slice(&x.rem(&NonZero::new($WIDE_MODULUS).unwrap()).to_le_bytes()[.. 64])
}
@@ -121,9 +124,12 @@ macro_rules! field {
&y,
&$MODULUS.0
));
math_op!($FieldName, $FieldName, Mul, mul, MulAssign, mul_assign, |x, y| reduce(U1024::from(
U512::mul_wide(&x, &y)
)));
math_op!($FieldName, $FieldName, Mul, mul, MulAssign, mul_assign, |x, y| {
use crypto_bigint::modular::constant_mod::{ResidueParams, Residue};
Residue::<CryptoBigIntModulus, { CryptoBigIntModulus::LIMBS }>::new(&x)
.mul(&Residue::<CryptoBigIntModulus, { CryptoBigIntModulus::LIMBS }>::new(&y))
.retrieve()
});
from_wrapper!($FieldName, U512, u8);
from_wrapper!($FieldName, U512, u16);

View File

@@ -6,6 +6,13 @@ use crypto_bigint::{U512, U1024};
#[derive(Clone, Copy, PartialEq, Eq, Default, Debug, Zeroize)]
pub struct FieldElement(pub(crate) U512);
const MODULUS_PADDED_STR: &str = concat!(
"00000000000000",
"00",
"fffffffffffffffffffffffffffffffffffffffffffffffffffffffe",
"ffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
);
const MODULUS_STR: &str = concat!(
"fffffffffffffffffffffffffffffffffffffffffffffffffffffffe",
"ffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
@@ -33,6 +40,7 @@ pub(crate) const Q_4: FieldElement =
field!(
FieldElement,
MODULUS_PADDED_STR,
MODULUS_STR,
MODULUS,
WIDE_MODULUS,

View File

@@ -6,6 +6,13 @@ use crypto_bigint::{U512, U1024};
#[derive(Clone, Copy, PartialEq, Eq, Default, Debug, Zeroize)]
pub struct Scalar(pub(crate) U512);
const MODULUS_PADDED_STR: &str = concat!(
"00000000000000",
"00",
"3fffffffffffffffffffffffffffffffffffffffffffffffffffffff",
"7cca23e9c44edb49aed63690216cc2728dc58f552378c292ab5844f3",
);
const MODULUS_STR: &str = concat!(
"3fffffffffffffffffffffffffffffffffffffffffffffffffffffff",
"7cca23e9c44edb49aed63690216cc2728dc58f552378c292ab5844f3",
@@ -30,6 +37,7 @@ const WIDE_MODULUS: U1024 = U1024::from_be_hex(concat!(
field!(
Scalar,
MODULUS_PADDED_STR,
MODULUS_STR,
MODULUS,
WIDE_MODULUS,