Skip to content

Commit a89cf76

Browse files
committed
arithmetic: Move Modulus::oneR to One::fillR.
``` git difftool HEAD^1:src/arithmetic/bigint/modulus.rs \ src/arithmetic/bigint/one.rs ```
1 parent a168627 commit a89cf76

File tree

3 files changed

+40
-36
lines changed

3 files changed

+40
-36
lines changed

src/arithmetic/bigint.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ fn elem_exp_consttime_inner<N, M, const STORAGE_LIMBS: usize>(
458458
}
459459

460460
// table[0] = base**0 (i.e. 1).
461-
let _: &[Limb] = m.oneR(entry_uninit(table, 0, num_limbs))?;
461+
let _: &[Limb] = One::fillR(entry_uninit(table, 0, num_limbs), m)?;
462462

463463
// table[1] = base*R == (base/R * RRR)/R
464464
let _: &[Limb] = limbs_mul_mont(
@@ -623,7 +623,7 @@ fn elem_exp_consttime_inner<N, M, const STORAGE_LIMBS: usize>(
623623
// All entries in `table` will be Montgomery encoded.
624624

625625
// t0 = table[0] = base**0 (i.e. 1).
626-
let t0 = m.oneR(acc.as_mut().into())?;
626+
let t0 = One::fillR(acc.as_mut().into(), m)?;
627627
scatter5(t0, table, LeakyWindow5::_0)?;
628628

629629
// acc = base**1 (i.e. base).

src/arithmetic/bigint/modulus.rs

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use crate::{
1818
cpu,
1919
error::{self, LenMismatchError},
2020
limb::{self, Limb, LIMB_BITS},
21-
polyfill::{self, LeadingZerosStripped},
21+
polyfill::LeadingZerosStripped,
2222
};
2323
use core::marker::PhantomData;
2424

@@ -141,38 +141,6 @@ pub struct Modulus<'a, M> {
141141
}
142142

143143
impl<M> Modulus<'_, M> {
144-
pub(super) fn oneR<'r>(
145-
&self,
146-
out: polyfill::slice::Uninit<'r, Limb>,
147-
) -> Result<&'r mut [Limb], LenMismatchError> {
148-
let r = self.limbs.len() * LIMB_BITS;
149-
150-
// out = 2**r - m where m = self.
151-
let out = limb::limbs_negative_odd(out, self.limbs)?;
152-
153-
let lg_m = self.len_bits().as_bits();
154-
let leading_zero_bits_in_m = r - lg_m;
155-
156-
// When m's length is a multiple of LIMB_BITS, which is the case we
157-
// most want to optimize for, then we already have
158-
// out == 2**r - m == 2**r (mod m).
159-
if leading_zero_bits_in_m != 0 {
160-
debug_assert!(leading_zero_bits_in_m < LIMB_BITS);
161-
// Correct out to 2**(lg m) (mod m). `limbs_negative_odd` flipped
162-
// all the leading zero bits to ones. Flip them back.
163-
*out.last_mut().unwrap() &= (!0) >> leading_zero_bits_in_m;
164-
165-
// Now we have out == 2**(lg m) (mod m). Keep doubling until we get
166-
// to 2**r (mod m).
167-
for _ in 0..leading_zero_bits_in_m {
168-
limb::limbs_double_mod(out, self.limbs)?;
169-
}
170-
}
171-
172-
// Now out == 2**r (mod m) == 1*R.
173-
Ok(out)
174-
}
175-
176144
pub fn alloc_uninit(&self) -> Uninit<M> {
177145
Uninit::new_less_safe(self.limbs.len())
178146
}

src/arithmetic/bigint/one.rs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,49 @@ use super::{
1818
};
1919
use crate::{
2020
error::LenMismatchError,
21+
limb,
2122
limb::{Limb, LIMB_BITS},
23+
polyfill,
2224
};
2325
use core::mem::size_of;
2426

2527
// The value 1, Montgomery-encoded some number of times.
2628
pub struct One<M, E>(Elem<M, E>);
2729

30+
impl<M> One<M, R> {
31+
pub(super) fn fillR<'r>(
32+
out: polyfill::slice::Uninit<'r, Limb>,
33+
m: &Modulus<'_, M>,
34+
) -> Result<&'r mut [Limb], LenMismatchError> {
35+
let r = m.limbs().len() * LIMB_BITS;
36+
37+
// out = 2**r - m where m = self.
38+
let out = limb::limbs_negative_odd(out, m.limbs())?;
39+
40+
let lg_m = m.len_bits().as_bits();
41+
let leading_zero_bits_in_m = r - lg_m;
42+
43+
// When m's length is a multiple of LIMB_BITS, which is the case we
44+
// most want to optimize for, then we already have
45+
// out == 2**r - m == 2**r (mod m).
46+
if leading_zero_bits_in_m != 0 {
47+
debug_assert!(leading_zero_bits_in_m < LIMB_BITS);
48+
// Correct out to 2**(lg m) (mod m). `limbs_negative_odd` flipped
49+
// all the leading zero bits to ones. Flip them back.
50+
*out.last_mut().unwrap() &= (!0) >> leading_zero_bits_in_m;
51+
52+
// Now we have out == 2**(lg m) (mod m). Keep doubling until we get
53+
// to 2**r (mod m).
54+
for _ in 0..leading_zero_bits_in_m {
55+
limb::limbs_double_mod(out, m.limbs())?;
56+
}
57+
}
58+
59+
// Now out == 2**r (mod m) == 1*R.
60+
Ok(out)
61+
}
62+
}
63+
2864
impl<M> One<M, RR> {
2965
// Returns RR = = R**2 (mod n) where R = 2**r is the smallest power of
3066
// 2**LIMB_BITS such that R > m.
@@ -41,7 +77,7 @@ impl<M> One<M, RR> {
4177
let r = w * LIMB_BITS;
4278

4379
let mut acc = out
44-
.write_fully_with(|out| m.oneR(out))
80+
.write_fully_with(|out| One::fillR(out, m))
4581
.map(Elem::<M, R>::assume_in_range_and_encoded_less_safe)?;
4682

4783
// 2**t * R can be calculated by t doublings starting with R.

0 commit comments

Comments
 (0)