mirror of
https://github.com/serai-dex/serai.git
synced 2024-11-16 17:07:35 +00:00
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:
parent
21026136bd
commit
334873b6a5
6 changed files with 33 additions and 11 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -5091,7 +5091,7 @@ checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "minimal-ed448"
|
name = "minimal-ed448"
|
||||||
version = "0.3.0"
|
version = "0.3.1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"crypto-bigint 0.5.1",
|
"crypto-bigint 0.5.1",
|
||||||
"ff 0.13.0",
|
"ff 0.13.0",
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "minimal-ed448"
|
name = "minimal-ed448"
|
||||||
version = "0.3.0"
|
version = "0.3.1"
|
||||||
description = "Unaudited, inefficient implementation of Ed448 in Rust"
|
description = "Unaudited, inefficient implementation of Ed448 in Rust"
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
repository = "https://github.com/serai-dex/serai/tree/develop/crypto/ed448"
|
repository = "https://github.com/serai-dex/serai/tree/develop/crypto/ed448"
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
# Minimal Ed448
|
# Minimal Ed448
|
||||||
|
|
||||||
Inefficient, barebones implementation of Ed448 bound to the ff/group API,
|
Barebones implementation of Ed448 bound to the ff/group API, rejecting torsion
|
||||||
rejecting torsion to achieve a PrimeGroup definition. This likely should not be
|
to achieve a PrimeGroup definition.
|
||||||
used and was only done so another library under Serai could confirm its
|
|
||||||
completion. It is minimally tested, yet should be correct for what it has. This
|
This library has not been audited. While it is complete, and decently tested,
|
||||||
has not undergone auditing.
|
any usage of it should be carefully considered.
|
||||||
|
|
||||||
constant time and no_std.
|
constant time and no_std.
|
||||||
|
|
|
@ -69,6 +69,7 @@ macro_rules! field {
|
||||||
(
|
(
|
||||||
$FieldName: ident,
|
$FieldName: ident,
|
||||||
|
|
||||||
|
$MODULUS_PADDED_STR: ident,
|
||||||
$MODULUS_STR: ident,
|
$MODULUS_STR: ident,
|
||||||
$MODULUS: ident,
|
$MODULUS: ident,
|
||||||
$WIDE_MODULUS: ident,
|
$WIDE_MODULUS: ident,
|
||||||
|
@ -89,12 +90,14 @@ macro_rules! field {
|
||||||
use rand_core::RngCore;
|
use rand_core::RngCore;
|
||||||
|
|
||||||
use generic_array::{typenum::U57, GenericArray};
|
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 ff::{Field, PrimeField, FieldBits, PrimeFieldBits, helpers::sqrt_ratio_generic};
|
||||||
|
|
||||||
use $crate::backend::u8_from_bool;
|
use $crate::backend::u8_from_bool;
|
||||||
|
|
||||||
|
impl_modulus!(CryptoBigIntModulus, U512, $MODULUS_PADDED_STR);
|
||||||
|
|
||||||
fn reduce(x: U1024) -> U512 {
|
fn reduce(x: U1024) -> U512 {
|
||||||
U512::from_le_slice(&x.rem(&NonZero::new($WIDE_MODULUS).unwrap()).to_le_bytes()[.. 64])
|
U512::from_le_slice(&x.rem(&NonZero::new($WIDE_MODULUS).unwrap()).to_le_bytes()[.. 64])
|
||||||
}
|
}
|
||||||
|
@ -121,9 +124,12 @@ macro_rules! field {
|
||||||
&y,
|
&y,
|
||||||
&$MODULUS.0
|
&$MODULUS.0
|
||||||
));
|
));
|
||||||
math_op!($FieldName, $FieldName, Mul, mul, MulAssign, mul_assign, |x, y| reduce(U1024::from(
|
math_op!($FieldName, $FieldName, Mul, mul, MulAssign, mul_assign, |x, y| {
|
||||||
U512::mul_wide(&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, u8);
|
||||||
from_wrapper!($FieldName, U512, u16);
|
from_wrapper!($FieldName, U512, u16);
|
||||||
|
|
|
@ -6,6 +6,13 @@ use crypto_bigint::{U512, U1024};
|
||||||
#[derive(Clone, Copy, PartialEq, Eq, Default, Debug, Zeroize)]
|
#[derive(Clone, Copy, PartialEq, Eq, Default, Debug, Zeroize)]
|
||||||
pub struct FieldElement(pub(crate) U512);
|
pub struct FieldElement(pub(crate) U512);
|
||||||
|
|
||||||
|
const MODULUS_PADDED_STR: &str = concat!(
|
||||||
|
"00000000000000",
|
||||||
|
"00",
|
||||||
|
"fffffffffffffffffffffffffffffffffffffffffffffffffffffffe",
|
||||||
|
"ffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
|
||||||
|
);
|
||||||
|
|
||||||
const MODULUS_STR: &str = concat!(
|
const MODULUS_STR: &str = concat!(
|
||||||
"fffffffffffffffffffffffffffffffffffffffffffffffffffffffe",
|
"fffffffffffffffffffffffffffffffffffffffffffffffffffffffe",
|
||||||
"ffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
|
"ffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
|
||||||
|
@ -33,6 +40,7 @@ pub(crate) const Q_4: FieldElement =
|
||||||
|
|
||||||
field!(
|
field!(
|
||||||
FieldElement,
|
FieldElement,
|
||||||
|
MODULUS_PADDED_STR,
|
||||||
MODULUS_STR,
|
MODULUS_STR,
|
||||||
MODULUS,
|
MODULUS,
|
||||||
WIDE_MODULUS,
|
WIDE_MODULUS,
|
||||||
|
|
|
@ -6,6 +6,13 @@ use crypto_bigint::{U512, U1024};
|
||||||
#[derive(Clone, Copy, PartialEq, Eq, Default, Debug, Zeroize)]
|
#[derive(Clone, Copy, PartialEq, Eq, Default, Debug, Zeroize)]
|
||||||
pub struct Scalar(pub(crate) U512);
|
pub struct Scalar(pub(crate) U512);
|
||||||
|
|
||||||
|
const MODULUS_PADDED_STR: &str = concat!(
|
||||||
|
"00000000000000",
|
||||||
|
"00",
|
||||||
|
"3fffffffffffffffffffffffffffffffffffffffffffffffffffffff",
|
||||||
|
"7cca23e9c44edb49aed63690216cc2728dc58f552378c292ab5844f3",
|
||||||
|
);
|
||||||
|
|
||||||
const MODULUS_STR: &str = concat!(
|
const MODULUS_STR: &str = concat!(
|
||||||
"3fffffffffffffffffffffffffffffffffffffffffffffffffffffff",
|
"3fffffffffffffffffffffffffffffffffffffffffffffffffffffff",
|
||||||
"7cca23e9c44edb49aed63690216cc2728dc58f552378c292ab5844f3",
|
"7cca23e9c44edb49aed63690216cc2728dc58f552378c292ab5844f3",
|
||||||
|
@ -30,6 +37,7 @@ const WIDE_MODULUS: U1024 = U1024::from_be_hex(concat!(
|
||||||
|
|
||||||
field!(
|
field!(
|
||||||
Scalar,
|
Scalar,
|
||||||
|
MODULUS_PADDED_STR,
|
||||||
MODULUS_STR,
|
MODULUS_STR,
|
||||||
MODULUS,
|
MODULUS,
|
||||||
WIDE_MODULUS,
|
WIDE_MODULUS,
|
||||||
|
|
Loading…
Reference in a new issue