2023-04-19 08:02:59 +00:00
|
|
|
use zeroize::{DefaultIsZeroes, Zeroize};
|
2022-08-29 07:32:59 +00:00
|
|
|
|
2023-04-19 08:02:59 +00:00
|
|
|
use crypto_bigint::{
|
2023-05-09 08:12:13 +00:00
|
|
|
U448, U896, U1024,
|
2023-04-19 08:02:59 +00:00
|
|
|
modular::constant_mod::{ResidueParams, Residue},
|
|
|
|
};
|
2022-08-29 07:32:59 +00:00
|
|
|
|
2023-05-09 08:12:13 +00:00
|
|
|
const MODULUS_STR: &str = concat!(
|
2023-04-19 06:25:19 +00:00
|
|
|
"3fffffffffffffffffffffffffffffffffffffffffffffffffffffff",
|
|
|
|
"7cca23e9c44edb49aed63690216cc2728dc58f552378c292ab5844f3",
|
|
|
|
);
|
|
|
|
|
2023-05-09 08:12:13 +00:00
|
|
|
impl_modulus!(ScalarModulus, U448, MODULUS_STR);
|
2023-04-19 08:02:59 +00:00
|
|
|
type ResidueType = Residue<ScalarModulus, { ScalarModulus::LIMBS }>;
|
|
|
|
|
|
|
|
/// Ed448 Scalar field element.
|
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, Default, Debug)]
|
|
|
|
pub struct Scalar(pub(crate) ResidueType);
|
|
|
|
|
|
|
|
impl DefaultIsZeroes for Scalar {}
|
|
|
|
|
2022-08-29 07:32:59 +00:00
|
|
|
// 2**446 - 13818066809895115352007386748515426880336692474882178609894547503885
|
2023-05-09 08:12:13 +00:00
|
|
|
pub(crate) const MODULUS: U448 = U448::from_be_hex(MODULUS_STR);
|
|
|
|
|
|
|
|
const WIDE_MODULUS: U896 = U896::from_be_hex(concat!(
|
|
|
|
"00000000000000000000000000000000000000000000000000000000",
|
|
|
|
"00000000000000000000000000000000000000000000000000000000",
|
2022-08-31 07:33:19 +00:00
|
|
|
"3fffffffffffffffffffffffffffffffffffffffffffffffffffffff",
|
|
|
|
"7cca23e9c44edb49aed63690216cc2728dc58f552378c292ab5844f3",
|
2023-04-19 08:02:59 +00:00
|
|
|
));
|
2022-08-31 07:33:19 +00:00
|
|
|
|
2023-05-09 08:12:13 +00:00
|
|
|
const WIDE_REDUCTION_MODULUS: NonZero<U1024> = NonZero::from_uint(U1024::from_be_hex(concat!(
|
|
|
|
"00000000000000000000000000000000",
|
|
|
|
"00000000000000000000000000000000000000000000000000000000",
|
|
|
|
"00000000000000000000000000000000000000000000000000000000",
|
2022-08-31 07:33:19 +00:00
|
|
|
"3fffffffffffffffffffffffffffffffffffffffffffffffffffffff",
|
|
|
|
"7cca23e9c44edb49aed63690216cc2728dc58f552378c292ab5844f3",
|
2023-05-09 08:12:13 +00:00
|
|
|
)));
|
2022-08-29 07:32:59 +00:00
|
|
|
|
2023-03-28 08:38:01 +00:00
|
|
|
field!(
|
|
|
|
Scalar,
|
2023-04-19 08:02:59 +00:00
|
|
|
ResidueType,
|
2023-03-28 08:38:01 +00:00
|
|
|
MODULUS_STR,
|
|
|
|
MODULUS,
|
|
|
|
WIDE_MODULUS,
|
|
|
|
446,
|
|
|
|
2,
|
|
|
|
concat!(
|
2023-05-09 08:12:13 +00:00
|
|
|
"04000000000000000000000000000000000000000000000000000000",
|
|
|
|
"00000000000000000000000000000000000000000000000000000000",
|
2023-03-28 08:38:01 +00:00
|
|
|
),
|
|
|
|
);
|
2022-08-29 07:32:59 +00:00
|
|
|
|
|
|
|
impl Scalar {
|
2023-03-21 00:10:00 +00:00
|
|
|
/// Perform a wide reduction to obtain a non-biased Scalar.
|
2022-08-29 07:32:59 +00:00
|
|
|
pub fn wide_reduce(bytes: [u8; 114]) -> Scalar {
|
2023-08-02 18:17:57 +00:00
|
|
|
let mut bytes_128 = [0; 128];
|
|
|
|
bytes_128[.. 114].copy_from_slice(&bytes);
|
|
|
|
let wide = U1024::from_le_slice(&bytes_128);
|
2023-05-09 08:12:13 +00:00
|
|
|
Scalar(Residue::new(&U448::from_le_slice(
|
|
|
|
&wide.rem(&WIDE_REDUCTION_MODULUS).to_le_bytes()[.. 56],
|
|
|
|
)))
|
2022-08-29 07:32:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2023-03-28 08:38:01 +00:00
|
|
|
fn test_scalar() {
|
|
|
|
ff_group_tests::prime_field::test_prime_field_bits::<_, Scalar>(&mut rand_core::OsRng);
|
2022-08-29 07:32:59 +00:00
|
|
|
}
|