Skip to content

Commit be7c46f

Browse files
committed
arithmetic: Rename Modulus to Mont.
1 parent 9850178 commit be7c46f

File tree

8 files changed

+38
-41
lines changed

8 files changed

+38
-41
lines changed

src/arithmetic/bigint.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ pub(crate) use {
5353
elem_sub, elem_verify_equal_consttime, elem_widen, verify_inverses_consttime, Elem,
5454
},
5555
exp::elem_exp_consttime,
56-
modulus::{IntoMont, Modulus, One},
56+
modulus::{IntoMont, Mont, One},
5757
private_exponent::PrivateExponent,
5858
},
5959
super::exp_vartime::elem_exp_vartime,
@@ -71,7 +71,7 @@ impl<M> Uninit<M> {
7171
pub fn into_elem_from_be_bytes_padded(
7272
self,
7373
input: untrusted::Input<'_>,
74-
m: &Modulus<M>,
74+
m: &Mont<M>,
7575
) -> Result<Elem<M>, error::Unspecified> {
7676
self.write_from_be_byes_padded(input)
7777
.map_err(error::erase::<LenMismatchError>)
@@ -82,7 +82,7 @@ impl<M> Uninit<M> {
8282
impl<M> Elem<M, Unencoded> {
8383
fn from_limbs(
8484
out: BoxedLimbs<M>,
85-
m: &Modulus<M>,
85+
m: &Mont<M>,
8686
) -> Result<Elem<M, Unencoded>, error::Unspecified> {
8787
limb::verify_limbs_less_than_limbs_leak_bit(out.as_ref(), m.limbs())?;
8888
Ok(Elem::assume_in_range_and_encoded_less_safe(out))

src/arithmetic/bigint/elem.rs

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use crate::polyfill::prelude::*;
1818
use super::{
1919
super::{montgomery::*, MAX_LIMBS},
2020
boxed_limbs::BoxedLimbs,
21-
unwrap_impossible_len_mismatch_error, unwrap_impossible_limb_slice_error, Modulus, Uninit,
21+
unwrap_impossible_len_mismatch_error, unwrap_impossible_limb_slice_error, Mont, Uninit,
2222
};
2323
use crate::{
2424
bits::BitLength,
@@ -98,10 +98,7 @@ impl<M, E> Elem<M, E> {
9898
/// fully reduced mod `m`.
9999
///
100100
/// WARNING: Takes a `Storage` as an in/out value.
101-
pub(super) fn from_montgomery_amm<M>(
102-
mut in_out: BoxedLimbs<M>,
103-
m: &Modulus<M>,
104-
) -> Elem<M, Unencoded> {
101+
pub(super) fn from_montgomery_amm<M>(mut in_out: BoxedLimbs<M>, m: &Mont<M>) -> Elem<M, Unencoded> {
105102
let mut one = [0; MAX_LIMBS];
106103
one[0] = 1;
107104
let one = &one[..m.limbs().len()];
@@ -118,7 +115,7 @@ pub(super) fn from_montgomery_amm<M>(
118115
#[cfg(any(test, not(target_arch = "x86_64")))]
119116
impl<M> Elem<M, R> {
120117
#[inline]
121-
pub fn into_unencoded(self, m: &Modulus<M>) -> Elem<M, Unencoded> {
118+
pub fn into_unencoded(self, m: &Mont<M>) -> Elem<M, Unencoded> {
122119
from_montgomery_amm(self.limbs, m)
123120
}
124121
}
@@ -135,7 +132,7 @@ pub fn elem_mul_into<M, AF, BF>(
135132
out: Uninit<M>,
136133
a: &Elem<M, AF>,
137134
b: &Elem<M, BF>,
138-
m: &Modulus<M>,
135+
m: &Mont<M>,
139136
) -> Result<Elem<M, <(AF, BF) as ProductEncoding>::Output>, LenMismatchError>
140137
where
141138
(AF, BF): ProductEncoding,
@@ -156,7 +153,7 @@ where
156153
pub fn elem_mul<M, AF, BF>(
157154
a: &Elem<M, AF>,
158155
b: Elem<M, BF>,
159-
m: &Modulus<M>,
156+
m: &Mont<M>,
160157
) -> Elem<M, <(AF, BF) as ProductEncoding>::Output>
161158
where
162159
(AF, BF): ProductEncoding,
@@ -173,7 +170,7 @@ where
173170
}
174171

175172
// r *= 2.
176-
pub fn elem_double<M, AF>(r: &mut Elem<M, AF>, m: &Modulus<M>) {
173+
pub fn elem_double<M, AF>(r: &mut Elem<M, AF>, m: &Mont<M>) {
177174
limb::limbs_double_mod(r.limbs.as_mut(), m.limbs())
178175
.unwrap_or_else(unwrap_impossible_len_mismatch_error)
179176
}
@@ -185,7 +182,7 @@ pub fn elem_double<M, AF>(r: &mut Elem<M, AF>, m: &Modulus<M>) {
185182
pub fn elem_reduced_once<A, M>(
186183
r: Uninit<M>,
187184
a: &Elem<A, Unencoded>,
188-
m: &Modulus<M>,
185+
m: &Mont<M>,
189186
other_modulus_len_bits: BitLength,
190187
) -> Elem<M, Unencoded> {
191188
assert_eq!(m.len_bits(), other_modulus_len_bits);
@@ -203,7 +200,7 @@ pub fn elem_reduced_once<A, M>(
203200
pub fn elem_reduced<Larger, Smaller>(
204201
r: Uninit<Smaller>,
205202
a: &Elem<Larger, Unencoded>,
206-
m: &Modulus<Smaller>,
203+
m: &Mont<Smaller>,
207204
other_prime_len_bits: BitLength,
208205
) -> Elem<Smaller, RInverse> {
209206
// This is stricter than required mathematically but this is what we
@@ -227,7 +224,7 @@ pub fn elem_reduced<Larger, Smaller>(
227224
#[inline]
228225
pub fn elem_squared<M, E>(
229226
a: Elem<M, E>,
230-
m: &Modulus<M>,
227+
m: &Mont<M>,
231228
) -> Elem<M, <(E, E) as ProductEncoding>::Output>
232229
where
233230
(E, E): ProductEncoding,
@@ -241,7 +238,7 @@ where
241238
pub fn elem_widen<Larger, Smaller>(
242239
r: Uninit<Larger>,
243240
a: Elem<Smaller, Unencoded>,
244-
m: &Modulus<Larger>,
241+
m: &Mont<Larger>,
245242
smaller_modulus_bits: BitLength,
246243
) -> Result<Elem<Larger, Unencoded>, error::Unspecified> {
247244
if smaller_modulus_bits >= m.len_bits() {
@@ -254,14 +251,14 @@ pub fn elem_widen<Larger, Smaller>(
254251
}
255252

256253
// TODO: Document why this works for all Montgomery factors.
257-
pub fn elem_add<M, E>(mut a: Elem<M, E>, b: Elem<M, E>, m: &Modulus<M>) -> Elem<M, E> {
254+
pub fn elem_add<M, E>(mut a: Elem<M, E>, b: Elem<M, E>, m: &Mont<M>) -> Elem<M, E> {
258255
limb::limbs_add_assign_mod(a.limbs.as_mut(), b.limbs.as_ref(), m.limbs())
259256
.unwrap_or_else(unwrap_impossible_len_mismatch_error);
260257
a
261258
}
262259

263260
// TODO: Document why this works for all Montgomery factors.
264-
pub fn elem_sub<M, E>(mut a: Elem<M, E>, b: &Elem<M, E>, m: &Modulus<M>) -> Elem<M, E> {
261+
pub fn elem_sub<M, E>(mut a: Elem<M, E>, b: &Elem<M, E>, m: &Mont<M>) -> Elem<M, E> {
265262
prefixed_extern! {
266263
// `r` and `a` may alias.
267264
fn LIMBS_sub_mod(
@@ -289,7 +286,7 @@ pub fn elem_sub<M, E>(mut a: Elem<M, E>, b: &Elem<M, E>, m: &Modulus<M>) -> Elem
289286
pub fn verify_inverses_consttime<M>(
290287
a: &Elem<M, R>,
291288
b: Elem<M, Unencoded>,
292-
m: &Modulus<M>,
289+
m: &Mont<M>,
293290
) -> Result<(), error::Unspecified> {
294291
let r = elem_mul(a, b, m);
295292
limb::verify_limbs_equal_1_leak_bit(r.limbs.as_ref())
@@ -317,7 +314,7 @@ pub mod testutil {
317314
pub fn consume_elem<M>(
318315
test_case: &mut crate::testutil::TestCase,
319316
name: &str,
320-
m: &Modulus<M>,
317+
m: &Mont<M>,
321318
) -> Elem<M, Unencoded> {
322319
let value = test_case.consume_bytes(name);
323320
m.alloc_uninit()

src/arithmetic/bigint/exp.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ use super::{
4545
montgomery::{RInverse, Unencoded, RRR},
4646
LimbSliceError,
4747
},
48-
elem_reduced, Elem, IntoMont, Modulus, One, PrivateExponent, Uninit,
48+
elem_reduced, Elem, IntoMont, Mont, One, PrivateExponent, Uninit,
4949
};
5050
use crate::{
5151
bits::BitLength,
@@ -94,7 +94,7 @@ fn elem_exp_consttime_inner<N, M, const STORAGE_LIMBS: usize>(
9494
base_mod_n: &Elem<N>,
9595
oneRRR: &One<M, RRR>,
9696
exponent: &PrivateExponent,
97-
m: &Modulus<M>,
97+
m: &Mont<M>,
9898
other_prime_len_bits: BitLength,
9999
) -> Result<Elem<M, Unencoded>, LimbSliceError> {
100100
use super::{
@@ -131,7 +131,7 @@ fn elem_exp_consttime_inner<N, M, const STORAGE_LIMBS: usize>(
131131
fn power<M>(
132132
table: &[Limb],
133133
mut acc: Elem<M, R>,
134-
m: &Modulus<M>,
134+
m: &Mont<M>,
135135
i: Window5,
136136
mut tmp: Elem<M, R>,
137137
) -> Result<(Elem<M, R>, Elem<M, R>), LenMismatchError> {
@@ -211,7 +211,7 @@ fn elem_exp_consttime_inner<N, M, const STORAGE_LIMBS: usize>(
211211
base_mod_n: &Elem<N>,
212212
oneRRR: &One<M, RRR>,
213213
exponent: &PrivateExponent,
214-
m: &Modulus<M>,
214+
m: &Mont<M>,
215215
other_prime_len_bits: BitLength,
216216
) -> Result<Elem<M, Unencoded>, LimbSliceError> {
217217
use super::{

src/arithmetic/bigint/modulus/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ pub(super) mod testutil;
55
mod value;
66

77
pub(crate) use self::{
8-
mont::{IntoMont, Modulus},
8+
mont::{IntoMont, Mont},
99
one::One,
1010
value::{ValidatedInput, Value},
1111
};

src/arithmetic/bigint/modulus/mont.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,15 @@ impl<M, E> IntoMont<M, E> {
8282
pub fn to_elem<L>(
8383
&self,
8484
out: Uninit<L>,
85-
l: &Modulus<L>,
85+
l: &Mont<L>,
8686
) -> Result<Elem<L, Unencoded>, error::Unspecified> {
8787
out.write_copy_of_slice_padded(self.value.limbs())
8888
.map_err(error::erase::<LenMismatchError>)
8989
.and_then(|out| Elem::from_limbs(out, l))
9090
}
9191

92-
pub(crate) fn modulus(&self, cpu_features: cpu::Features) -> Modulus<'_, M> {
93-
Modulus::from_parts_unchecked_less_safe(&self.value, self.one.n0(), cpu_features)
92+
pub(crate) fn modulus(&self, cpu_features: cpu::Features) -> Mont<'_, M> {
93+
Mont::from_parts_unchecked_less_safe(&self.value, self.one.n0(), cpu_features)
9494
}
9595

9696
pub(crate) fn one(&self) -> &One<M, E> {
@@ -116,21 +116,21 @@ impl<M: PublicModulus, E> IntoMont<M, E> {
116116
}
117117
}
118118

119-
pub struct Modulus<'a, M> {
119+
pub struct Mont<'a, M> {
120120
limbs: &'a [Limb],
121121
n0: &'a N0,
122122
len_bits: BitLength,
123123
m: PhantomData<M>,
124124
cpu_features: cpu::Features,
125125
}
126126

127-
impl<'a, M> Modulus<'a, M> {
127+
impl<'a, M> Mont<'a, M> {
128128
pub(super) fn from_parts_unchecked_less_safe(
129129
value: &'a Value<M>,
130130
n0: &'a N0,
131131
cpu: cpu::Features,
132132
) -> Self {
133-
Modulus {
133+
Mont {
134134
limbs: value.limbs(),
135135
n0,
136136
len_bits: value.len_bits(),
@@ -140,7 +140,7 @@ impl<'a, M> Modulus<'a, M> {
140140
}
141141
}
142142

143-
impl<M> Modulus<'_, M> {
143+
impl<M> Mont<'_, M> {
144144
pub fn alloc_uninit(&self) -> Uninit<M> {
145145
Uninit::new_less_safe(self.limbs.len())
146146
}

src/arithmetic/bigint/modulus/one.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use crate::polyfill::prelude::*;
1818
use super::super::{
1919
super::montgomery::{N0, R, RR, RRR},
2020
elem::{elem_double, elem_squared},
21-
modulus, Elem, Limb, Modulus, PublicModulus, Uninit,
21+
modulus, Elem, Limb, Mont, PublicModulus, Uninit,
2222
};
2323
use crate::{
2424
cpu,
@@ -43,7 +43,7 @@ impl<M, E> One<M, E> {
4343
impl<M> One<M, R> {
4444
pub(in super::super) fn fillR<'r>(
4545
out: polyfill::slice::Uninit<'r, Limb>,
46-
m: &Modulus<'_, M>,
46+
m: &Mont<'_, M>,
4747
) -> Result<&'r mut [Limb], LenMismatchError> {
4848
let r = m.limbs().len() * LIMB_BITS;
4949

@@ -94,7 +94,7 @@ impl<M> One<M, RR> {
9494
let r = w * LIMB_BITS;
9595

9696
let n0 = N0::calculate_from(m);
97-
let m = &Modulus::from_parts_unchecked_less_safe(m, &n0, cpu);
97+
let m = &Mont::from_parts_unchecked_less_safe(m, &n0, cpu);
9898

9999
let mut acc = out
100100
.write_fully_with(|out| One::fillR(out, m))
@@ -166,7 +166,7 @@ impl<M> One<M, RRR> {
166166
m: &modulus::Value<M>,
167167
cpu: cpu::Features,
168168
) -> Self {
169-
let m = &Modulus::from_parts_unchecked_less_safe(m, &n0, cpu);
169+
let m = &Mont::from_parts_unchecked_less_safe(m, &n0, cpu);
170170
let value = elem_squared(value, m);
171171
Self { value, n0 }
172172
}

src/arithmetic/bigint/private_exponent.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
1313
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1414

15-
use super::{limb, Limb, Modulus};
15+
use super::{limb, Limb, Mont};
1616
use crate::error;
1717
use alloc::boxed::Box;
1818

@@ -26,7 +26,7 @@ impl PrivateExponent {
2626
// `p` is the modulus for which the exponent is in the interval [1, `p` - 1).
2727
pub fn from_be_bytes_padded<M>(
2828
input: untrusted::Input,
29-
p: &Modulus<M>,
29+
p: &Mont<M>,
3030
) -> Result<Self, error::Unspecified> {
3131
let mut dP = p
3232
.alloc_uninit()
@@ -54,7 +54,7 @@ impl PrivateExponent {
5454
#[cfg(test)]
5555
pub fn from_be_bytes_for_test_only<M>(
5656
input: untrusted::Input,
57-
p: &Modulus<M>,
57+
p: &Mont<M>,
5858
) -> Result<Self, error::Unspecified> {
5959
use super::boxed_limbs::Uninit;
6060
use crate::{error::LenMismatchError, limb::LIMB_BYTES};

src/arithmetic/exp_vartime.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1414

1515
use super::{
16-
bigint::{elem_mul, elem_squared, Elem, Modulus, Uninit},
16+
bigint::{elem_mul, elem_squared, Elem, Mont, Uninit},
1717
montgomery::R,
1818
};
1919
use core::num::NonZeroU64;
@@ -31,7 +31,7 @@ pub(crate) fn elem_exp_vartime<M>(
3131
out: Uninit<M>,
3232
base: Elem<M, R>,
3333
exponent: NonZeroU64,
34-
m: &Modulus<M>,
34+
m: &Mont<M>,
3535
) -> Elem<M, R> {
3636
// Use what [Knuth] calls the "S-and-X binary method", i.e. variable-time
3737
// square-and-multiply that scans the exponent from the most significant

0 commit comments

Comments
 (0)