oops forgot macros.rs

This commit is contained in:
Neemek 2025-11-03 17:15:31 +01:00
parent 7aa4fbd900
commit 0702313755
Signed by: neemek
GPG key ID: 84FFE4D7D40AB25E

52
src/macros.rs Normal file
View file

@ -0,0 +1,52 @@
#[macro_export]
macro_rules! ladder_impl {
($point:ident, $swap_fn:ident) => {
impl<N> $point<N>
where
N: AddMod
+ SubMod
+ MulMod<Output = N>
+ InvMod<Output = N>
+ Copy
+ Integer
+ Div<Output = N>,
{
pub fn ladder(&self, rhs: &N) -> $point<N> {
let mut x_0 = *self;
let mut x_1 = x_0.double();
for i in (0..rhs.bits() - 1).rev() {
if !bool::from(rhs.bit(i)) {
x_1 = x_0.add(&x_1, &self);
x_0 = x_0.double();
} else {
x_0 = x_0.add(&x_1, &self);
x_1 = x_1.double();
}
}
x_0
}
pub fn ct_ladder(&self, rhs: &N) -> $point<N> {
let max = N::zero().wrapping_sub(&N::one());
let mut x_0 = *self;
let mut x_1 = x_0.double();
let mut prev: u8 = 0;
for i in (0..rhs.bits() - 1).rev() {
let curr: u8 = bool::from(rhs.bit(i)).into();
$swap_fn(&mut x_0, &mut x_1, curr ^ prev, max);
prev = curr;
x_1 = x_0.add(&x_1, &self);
x_0 = x_0.double();
}
$swap_fn(&mut x_0, &mut x_1, prev, max);
x_0
}
}
};
}