use crypto_bigint::modular::ConstMontyForm;
use crypto_bigint::{NonZero, U576, const_monty_params};
const_monty_params!(FieldP, U576, PARAM_P, "E-521's field modulus, 2^521 - 1");
const PARAM_P: &str = PP;
const PP: &str = "00000000000001ffffffffffffffffffffffffffffffffffffffffffffffffff\
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\
ffffffffffffffff";
const PARAM_L: U576 = U576::from_be_hex(
"000000000000007fffffffffffffffffffffffffffffffffffffffffffffffff\
fffffffffffffffd15b6c64746fc85f736b8af5e7ec53f04fbd8c4569a8f1f45\
40ea2435f5180d6b",
);
const PARAM_D_NEG: U576 = U576::from_u64(376014);
const PARAM_BX: U576 = U576::from_be_hex(
"00000000000000752cb45c48648b189df90cb2296b2878a3bfd9f42fc6c818ec\
8bf3c9c0c6203913f6ecc5ccc72434b1ae949d568fc99c6059d0fb13364838aa\
302a940a2f19ba6c",
);
const PARAM_BY: U576 = U576::from_u64(12);
type Fe = ConstMontyForm<FieldP, { U576::LIMBS }>;
fn fe(v: &U576) -> Fe {
Fe::new(v)
}
fn fe_pow(x: Fe, e: &U576) -> Fe {
x.pow_amm(e)
}
fn fe_inv(x: Fe) -> Fe {
let e = p_minus(2);
fe_pow(x, &e)
}
fn p_minus(k: u64) -> U576 {
U576::from_be_hex(PP).wrapping_sub(&U576::from_u64(k))
}
#[derive(Clone, Copy)]
struct Point {
x: Fe,
y: Fe,
}
impl Point {
fn identity() -> Self {
Self {
x: fe(&U576::ZERO),
y: fe(&U576::ONE),
}
}
fn eq(&self, other: &Self) -> bool {
self.x == other.x && self.y == other.y
}
fn add(&self, other: &Self) -> Self {
let d = fe(&U576::ZERO) - fe(&PARAM_D_NEG);
let one = fe(&U576::ONE);
let t = d * self.x * other.x * self.y * other.y;
let x3 = (self.x * other.y + other.x * self.y) * fe_inv(one + t);
let y3 = (self.y * other.y - self.x * other.x) * fe_inv(one - t);
Self { x: x3, y: y3 }
}
fn mul(&self, k: &U576) -> Self {
let mut acc = Self::identity();
let mut base = *self;
for i in 0..U576::BITS {
if bool::from(k.bit(i)) {
acc = acc.add(&base);
}
base = base.add(&base);
}
acc
}
}
fn decompress(enc: &[u8; 66]) -> Option<Point> {
let sign = (enc[65] >> 7) & 1;
let mut le = *enc;
le[65] &= 0x7f; let mut be = le;
be.reverse();
let mut wide = [0u8; 72];
wide[6..].copy_from_slice(&be);
let y_int = U576::from_be_slice(&wide);
if y_int >= U576::from_be_hex(PP) {
return None; }
let y = fe(&y_int);
let one = fe(&U576::ONE);
let d = fe(&U576::ZERO) - fe(&PARAM_D_NEG);
let num = one - y * y;
let den = one - d * y * y;
if den == fe(&U576::ZERO) {
return None;
}
let x2 = num * fe_inv(den);
let exp = U576::from_be_hex(PP)
.wrapping_add(&U576::ONE)
.shr_vartime(2);
let mut x = fe_pow(x2, &exp);
if x * x != x2 {
return None; }
let x_canon = x.retrieve();
let x_is_odd = bool::from(x_canon.is_odd());
if x_canon == U576::ZERO && sign == 1 {
return None;
}
if x_is_odd != (sign == 1) {
x = fe(&U576::ZERO) - x;
}
Some(Point { x, y })
}
#[must_use]
pub fn verify(public_key: &[u8; 66], signature: &[u8; 132], message: &[u8]) -> bool {
use shake::digest::{ExtendableOutput, Update, XofReader};
let mut r_enc = [0u8; 66];
let mut s_enc = [0u8; 66];
r_enc.copy_from_slice(&signature[..66]);
s_enc.copy_from_slice(&signature[66..]);
let Some(a_point) = decompress(public_key) else {
return false;
};
let Some(r_point) = decompress(&r_enc) else {
return false;
};
let s_int = {
let mut be = s_enc;
be.reverse();
let mut wide = [0u8; 72];
wide[6..].copy_from_slice(&be);
U576::from_be_slice(&wide)
};
if s_int >= PARAM_L {
return false;
}
let mut xof = shake::Shake256::default();
xof.update(b"SigEd521");
xof.update(&[0u8]); xof.update(&[0u8]); xof.update(&r_enc);
xof.update(public_key);
xof.update(message);
let mut h = [0u8; 132];
xof.finalize_xof().read(&mut h);
let k = reduce_mod_l(&h);
let b_point = Point {
x: fe(&PARAM_BX),
y: fe(&PARAM_BY),
};
let lhs = b_point.mul(&s_int);
let rhs = r_point.add(&a_point.mul(&k));
lhs.eq(&rhs)
}
fn reduce_mod_l(h_le: &[u8; 132]) -> U576 {
let mut acc = U576::ZERO;
let two56 = U576::from_u64(256);
for byte in h_le.iter().rev() {
let l = NonZero::new(PARAM_L).expect("L is non-zero");
acc = acc.mul_mod(&two56, &l);
let add = U576::from_u64(u64::from(*byte));
acc = acc.add_mod(&add, &l);
}
acc
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_base_point_is_on_the_curve_and_has_order_l() {
let b = Point {
x: fe(&PARAM_BX),
y: fe(&PARAM_BY),
};
let one = fe(&U576::ONE);
let d = fe(&U576::ZERO) - fe(&PARAM_D_NEG);
let lhs = b.x * b.x + b.y * b.y;
let rhs = one + d * b.x * b.x * b.y * b.y;
assert_eq!(lhs, rhs, "B must satisfy the E-521 curve equation");
let lb = b.mul(&PARAM_L);
assert!(
lb.eq(&Point::identity()),
"[L]B must be the identity, i.e. B generates the order-L subgroup"
);
}
}