use cryptix_bigint::{ops::BigIntOpsExt, property::IsBigInt};
use crate::{OddModular, PrimeModular};
use super::primefield::FpElement;
pub trait Montgomery<I>
where
Self: PrimeModular<I> + OddModular<I> + Sized,
I: BigIntOpsExt,
{
const R_P: FpElement<I, Self>;
const R_INV_P: FpElement<I, Self>;
const RR_P: FpElement<I, Self>;
const NEG_P_INV_B: <I as IsBigInt>::Dig;
}
pub trait MontgomeryOps<I, M>
where
I: BigIntOpsExt,
M: PrimeModular<I> + OddModular<I> + Montgomery<I>,
Self: Copy + From<FpElement<I, M>>
{
fn mont_mul(self, rhs: Self) -> Self;
fn mont_mul_fp(self, rhs: FpElement<I, M>) -> Self;
fn mont_sqr(self) -> Self {
self.mont_mul(self)
}
fn mont_rdc(self) -> Self;
fn mont_form(self) -> Self {
self.mont_mul_fp(M::RR_P)
}
fn mont_exp(self, exp: I) -> Self {
let mut a: Self = M::R_P.into();
let x = self.mont_mul_fp(M::RR_P);
let t = exp.bit_len();
for i in (0..t).rev() {
a = a.mont_sqr();
if exp.bit(i) {
a = a.mont_mul(x)
}
}
a.mont_rdc()
}
fn mont_inv(self) -> Self;
}