oops forgot macros.rs
This commit is contained in:
parent
7aa4fbd900
commit
0702313755
1 changed files with 52 additions and 0 deletions
52
src/macros.rs
Normal file
52
src/macros.rs
Normal 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
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue