Skip to content

Commit 296bd55

Browse files
authored
Residue rename (#485)
Fixes #351 - `Residue` -> `ConstMontyForm` - `DynResidue` -> `MontyForm` - `BoxedResidue` -> `BoxedMontyForm` - `*ResidueParams` -> `*MontyParams` - `residue_params` -> `params` - `params.r` -> `params.one`
1 parent eb85103 commit 296bd55

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+1280
-1273
lines changed

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ all-features = true
5252
rustdoc-args = ["--cfg", "docsrs"]
5353

5454
[[bench]]
55-
name = "boxed_residue"
55+
name = "boxed_monty"
5656
harness = false
5757
required-features = ["alloc"]
5858

@@ -62,15 +62,15 @@ harness = false
6262
required-features = ["alloc"]
6363

6464
[[bench]]
65-
name = "dyn_residue"
65+
name = "const_monty"
6666
harness = false
6767

6868
[[bench]]
6969
name = "limb"
7070
harness = false
7171

7272
[[bench]]
73-
name = "residue"
73+
name = "monty"
7474
harness = false
7575

7676
[[bench]]

benches/boxed_residue.rs renamed to benches/boxed_monty.rs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use criterion::{
33
BenchmarkGroup, Criterion,
44
};
55
use crypto_bigint::{
6-
modular::{BoxedResidue, BoxedResidueParams},
6+
modular::{BoxedMontyForm, BoxedMontyParams},
77
BoxedUint, NonZero, RandomMod,
88
};
99
use num_bigint::BigUint;
@@ -17,7 +17,7 @@ fn to_biguint(uint: &BoxedUint) -> BigUint {
1717
}
1818

1919
fn bench_montgomery_ops<M: Measurement>(group: &mut BenchmarkGroup<'_, M>) {
20-
let params = BoxedResidueParams::new(
20+
let params = BoxedMontyParams::new(
2121
BoxedUint::random(&mut OsRng, UINT_BITS) | BoxedUint::one_with_precision(UINT_BITS),
2222
)
2323
.unwrap();
@@ -26,7 +26,7 @@ fn bench_montgomery_ops<M: Measurement>(group: &mut BenchmarkGroup<'_, M>) {
2626
b.iter_batched(
2727
|| {
2828
let modulus = NonZero::new(params.modulus().clone()).unwrap();
29-
BoxedResidue::new(BoxedUint::random_mod(&mut OsRng, &modulus), params.clone())
29+
BoxedMontyForm::new(BoxedUint::random_mod(&mut OsRng, &modulus), params.clone())
3030
},
3131
|x| black_box(x).invert(),
3232
BatchSize::SmallInput,
@@ -36,8 +36,10 @@ fn bench_montgomery_ops<M: Measurement>(group: &mut BenchmarkGroup<'_, M>) {
3636
group.bench_function("multiplication, BoxedUint*BoxedUint", |b| {
3737
b.iter_batched(
3838
|| {
39-
let x = BoxedResidue::new(BoxedUint::random(&mut OsRng, UINT_BITS), params.clone());
40-
let y = BoxedResidue::new(BoxedUint::random(&mut OsRng, UINT_BITS), params.clone());
39+
let x =
40+
BoxedMontyForm::new(BoxedUint::random(&mut OsRng, UINT_BITS), params.clone());
41+
let y =
42+
BoxedMontyForm::new(BoxedUint::random(&mut OsRng, UINT_BITS), params.clone());
4143
(x, y)
4244
},
4345
|(x, y)| black_box(x * y),
@@ -59,12 +61,12 @@ fn bench_montgomery_ops<M: Measurement>(group: &mut BenchmarkGroup<'_, M>) {
5961
});
6062

6163
let m = BoxedUint::random(&mut OsRng, UINT_BITS) | BoxedUint::one_with_precision(UINT_BITS);
62-
let params = BoxedResidueParams::new(m).unwrap();
64+
let params = BoxedMontyParams::new(m).unwrap();
6365
group.bench_function("modpow, BoxedUint^BoxedUint", |b| {
6466
b.iter_batched(
6567
|| {
6668
let x = BoxedUint::random(&mut OsRng, UINT_BITS);
67-
let x_m = BoxedResidue::new(x, params.clone());
69+
let x_m = BoxedMontyForm::new(x, params.clone());
6870
let p = BoxedUint::random(&mut OsRng, UINT_BITS)
6971
| (BoxedUint::one_with_precision(UINT_BITS) << (UINT_BITS - 1));
7072
(x_m, p)
@@ -92,41 +94,41 @@ fn bench_montgomery_ops<M: Measurement>(group: &mut BenchmarkGroup<'_, M>) {
9294
}
9395

9496
fn bench_montgomery_conversion<M: Measurement>(group: &mut BenchmarkGroup<'_, M>) {
95-
group.bench_function("BoxedResidueParams::new", |b| {
97+
group.bench_function("BoxedMontyParams::new", |b| {
9698
b.iter_batched(
9799
|| BoxedUint::random(&mut OsRng, UINT_BITS) | BoxedUint::one_with_precision(UINT_BITS),
98-
|modulus| black_box(BoxedResidueParams::new(modulus)),
100+
|modulus| black_box(BoxedMontyParams::new(modulus)),
99101
BatchSize::SmallInput,
100102
)
101103
});
102104

103-
group.bench_function("BoxedResidueParams::new_vartime", |b| {
105+
group.bench_function("BoxedMontyParams::new_vartime", |b| {
104106
b.iter_batched(
105107
|| BoxedUint::random(&mut OsRng, UINT_BITS) | BoxedUint::one_with_precision(UINT_BITS),
106-
|modulus| black_box(BoxedResidueParams::new_vartime(modulus)),
108+
|modulus| black_box(BoxedMontyParams::new_vartime(modulus)),
107109
BatchSize::SmallInput,
108110
)
109111
});
110112

111-
let params = BoxedResidueParams::new(
113+
let params = BoxedMontyParams::new(
112114
BoxedUint::random(&mut OsRng, UINT_BITS) | BoxedUint::one_with_precision(UINT_BITS),
113115
)
114116
.unwrap();
115-
group.bench_function("BoxedResidue::new", |b| {
117+
group.bench_function("BoxedMontyForm::new", |b| {
116118
b.iter_batched(
117119
|| BoxedUint::random(&mut OsRng, UINT_BITS),
118-
|x| black_box(BoxedResidue::new(x, params.clone())),
120+
|x| black_box(BoxedMontyForm::new(x, params.clone())),
119121
BatchSize::SmallInput,
120122
)
121123
});
122124

123-
let params = BoxedResidueParams::new(
125+
let params = BoxedMontyParams::new(
124126
BoxedUint::random(&mut OsRng, UINT_BITS) | BoxedUint::one_with_precision(UINT_BITS),
125127
)
126128
.unwrap();
127-
group.bench_function("BoxedResidue::retrieve", |b| {
129+
group.bench_function("BoxedMontyForm::retrieve", |b| {
128130
b.iter_batched(
129-
|| BoxedResidue::new(BoxedUint::random(&mut OsRng, UINT_BITS), params.clone()),
131+
|| BoxedMontyForm::new(BoxedUint::random(&mut OsRng, UINT_BITS), params.clone()),
130132
|x| black_box(x.retrieve()),
131133
BatchSize::SmallInput,
132134
)

benches/residue.rs renamed to benches/const_monty.rs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use criterion::{
22
black_box, criterion_group, criterion_main, measurement::Measurement, BatchSize,
33
BenchmarkGroup, Criterion,
44
};
5-
use crypto_bigint::{impl_modulus, modular::ResidueParams, Invert, Inverter, Random, U256};
5+
use crypto_bigint::{impl_modulus, modular::ConstMontyParams, Invert, Inverter, Random, U256};
66
use rand_core::OsRng;
77

88
#[cfg(feature = "alloc")]
@@ -14,20 +14,20 @@ impl_modulus!(
1414
"ffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551"
1515
);
1616

17-
type Residue = crypto_bigint::modular::Residue<Modulus, { U256::LIMBS }>;
17+
type ConstMontyForm = crypto_bigint::modular::ConstMontyForm<Modulus, { U256::LIMBS }>;
1818

1919
fn bench_montgomery_conversion<M: Measurement>(group: &mut BenchmarkGroup<'_, M>) {
20-
group.bench_function("Residue creation", |b| {
20+
group.bench_function("ConstMontyForm creation", |b| {
2121
b.iter_batched(
2222
|| U256::random(&mut OsRng),
23-
|x| black_box(Residue::new(&x)),
23+
|x| black_box(ConstMontyForm::new(&x)),
2424
BatchSize::SmallInput,
2525
)
2626
});
2727

28-
group.bench_function("Residue retrieve", |b| {
28+
group.bench_function("ConstMontyForm retrieve", |b| {
2929
b.iter_batched(
30-
|| Residue::new(&U256::random(&mut OsRng)),
30+
|| ConstMontyForm::new(&U256::random(&mut OsRng)),
3131
|x| black_box(x.retrieve()),
3232
BatchSize::SmallInput,
3333
)
@@ -37,7 +37,7 @@ fn bench_montgomery_conversion<M: Measurement>(group: &mut BenchmarkGroup<'_, M>
3737
fn bench_montgomery_ops<M: Measurement>(group: &mut BenchmarkGroup<'_, M>) {
3838
group.bench_function("invert, U256", |b| {
3939
b.iter_batched(
40-
|| Residue::new(&U256::random(&mut OsRng)),
40+
|| ConstMontyForm::new(&U256::random(&mut OsRng)),
4141
|x| black_box(x).invert(),
4242
BatchSize::SmallInput,
4343
)
@@ -46,7 +46,7 @@ fn bench_montgomery_ops<M: Measurement>(group: &mut BenchmarkGroup<'_, M>) {
4646
group.bench_function("Bernstein-Yang invert, U256", |b| {
4747
b.iter_batched(
4848
|| {
49-
let x = Residue::new(&U256::random(&mut OsRng));
49+
let x = ConstMontyForm::new(&U256::random(&mut OsRng));
5050
let inverter = Modulus::precompute_inverter();
5151
(x, inverter)
5252
},
@@ -58,8 +58,8 @@ fn bench_montgomery_ops<M: Measurement>(group: &mut BenchmarkGroup<'_, M>) {
5858
group.bench_function("multiplication, U256*U256", |b| {
5959
b.iter_batched(
6060
|| {
61-
let x = Residue::new(&U256::random(&mut OsRng));
62-
let y = Residue::new(&U256::random(&mut OsRng));
61+
let x = ConstMontyForm::new(&U256::random(&mut OsRng));
62+
let y = ConstMontyForm::new(&U256::random(&mut OsRng));
6363
(x, y)
6464
},
6565
|(x, y)| black_box(x * y),
@@ -71,7 +71,7 @@ fn bench_montgomery_ops<M: Measurement>(group: &mut BenchmarkGroup<'_, M>) {
7171
b.iter_batched(
7272
|| {
7373
let x = U256::random(&mut OsRng);
74-
let x_m = Residue::new(&x);
74+
let x_m = ConstMontyForm::new(&x);
7575
let p = U256::random(&mut OsRng) | (U256::ONE << (U256::BITS - 1));
7676
(x_m, p)
7777
},
@@ -87,10 +87,10 @@ fn bench_montgomery_ops<M: Measurement>(group: &mut BenchmarkGroup<'_, M>) {
8787
|b| {
8888
b.iter_batched(
8989
|| {
90-
let bases_and_exponents: Vec<(Residue, U256)> = (1..=i)
90+
let bases_and_exponents: Vec<(ConstMontyForm, U256)> = (1..=i)
9191
.map(|_| {
9292
let x = U256::random(&mut OsRng);
93-
let x_m = Residue::new(&x);
93+
let x_m = ConstMontyForm::new(&x);
9494
let p = U256::random(&mut OsRng) | (U256::ONE << (U256::BITS - 1));
9595
(x_m, p)
9696
})
@@ -99,7 +99,9 @@ fn bench_montgomery_ops<M: Measurement>(group: &mut BenchmarkGroup<'_, M>) {
9999
bases_and_exponents
100100
},
101101
|bases_and_exponents| {
102-
black_box(Residue::multi_exponentiate(bases_and_exponents.as_slice()))
102+
black_box(ConstMontyForm::multi_exponentiate(
103+
bases_and_exponents.as_slice(),
104+
))
103105
},
104106
BatchSize::SmallInput,
105107
)

benches/dyn_residue.rs renamed to benches/monty.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use criterion::{
33
BenchmarkGroup, Criterion,
44
};
55
use crypto_bigint::{
6-
modular::{DynResidue, DynResidueParams},
6+
modular::{MontyForm, MontyParams},
77
Invert, Inverter, PrecomputeInverter, Random, U256,
88
};
99
use rand_core::OsRng;
@@ -12,39 +12,39 @@ use rand_core::OsRng;
1212
use crypto_bigint::MultiExponentiate;
1313

1414
fn bench_montgomery_conversion<M: Measurement>(group: &mut BenchmarkGroup<'_, M>) {
15-
group.bench_function("DynResidueParams creation", |b| {
15+
group.bench_function("MontyParams creation", |b| {
1616
b.iter_batched(
1717
|| U256::random(&mut OsRng) | U256::ONE,
18-
|modulus| black_box(DynResidueParams::new(&modulus)),
18+
|modulus| black_box(MontyParams::new(&modulus)),
1919
BatchSize::SmallInput,
2020
)
2121
});
2222

23-
let params = DynResidueParams::new(&(U256::random(&mut OsRng) | U256::ONE)).unwrap();
24-
group.bench_function("DynResidue creation", |b| {
23+
let params = MontyParams::new(&(U256::random(&mut OsRng) | U256::ONE)).unwrap();
24+
group.bench_function("MontyForm creation", |b| {
2525
b.iter_batched(
2626
|| U256::random(&mut OsRng),
27-
|x| black_box(DynResidue::new(&x, params)),
27+
|x| black_box(MontyForm::new(&x, params)),
2828
BatchSize::SmallInput,
2929
)
3030
});
3131

32-
let params = DynResidueParams::new(&(U256::random(&mut OsRng) | U256::ONE)).unwrap();
33-
group.bench_function("DynResidue retrieve", |b| {
32+
let params = MontyParams::new(&(U256::random(&mut OsRng) | U256::ONE)).unwrap();
33+
group.bench_function("MontyForm retrieve", |b| {
3434
b.iter_batched(
35-
|| DynResidue::new(&U256::random(&mut OsRng), params),
35+
|| MontyForm::new(&U256::random(&mut OsRng), params),
3636
|x| black_box(x.retrieve()),
3737
BatchSize::SmallInput,
3838
)
3939
});
4040
}
4141

4242
fn bench_montgomery_ops<M: Measurement>(group: &mut BenchmarkGroup<'_, M>) {
43-
let params = DynResidueParams::new(&(U256::random(&mut OsRng) | U256::ONE)).unwrap();
43+
let params = MontyParams::new(&(U256::random(&mut OsRng) | U256::ONE)).unwrap();
4444

4545
group.bench_function("invert, U256", |b| {
4646
b.iter_batched(
47-
|| DynResidue::new(&U256::random(&mut OsRng), params),
47+
|| MontyForm::new(&U256::random(&mut OsRng), params),
4848
|x| black_box(x).invert(),
4949
BatchSize::SmallInput,
5050
)
@@ -53,7 +53,7 @@ fn bench_montgomery_ops<M: Measurement>(group: &mut BenchmarkGroup<'_, M>) {
5353
group.bench_function("Bernstein-Yang invert, U256", |b| {
5454
b.iter_batched(
5555
|| {
56-
let x = DynResidue::new(&U256::random(&mut OsRng), params);
56+
let x = MontyForm::new(&U256::random(&mut OsRng), params);
5757
let inverter = x.params().precompute_inverter();
5858
(x, inverter)
5959
},
@@ -65,8 +65,8 @@ fn bench_montgomery_ops<M: Measurement>(group: &mut BenchmarkGroup<'_, M>) {
6565
group.bench_function("multiplication, U256*U256", |b| {
6666
b.iter_batched(
6767
|| {
68-
let x = DynResidue::new(&U256::random(&mut OsRng), params);
69-
let y = DynResidue::new(&U256::random(&mut OsRng), params);
68+
let x = MontyForm::new(&U256::random(&mut OsRng), params);
69+
let y = MontyForm::new(&U256::random(&mut OsRng), params);
7070
(x, y)
7171
},
7272
|(x, y)| black_box(x * y),
@@ -78,7 +78,7 @@ fn bench_montgomery_ops<M: Measurement>(group: &mut BenchmarkGroup<'_, M>) {
7878
b.iter_batched(
7979
|| {
8080
let x = U256::random(&mut OsRng);
81-
let x_m = DynResidue::new(&x, params);
81+
let x_m = MontyForm::new(&x, params);
8282
let p = U256::random(&mut OsRng) | (U256::ONE << (U256::BITS - 1));
8383
(x_m, p)
8484
},
@@ -94,10 +94,10 @@ fn bench_montgomery_ops<M: Measurement>(group: &mut BenchmarkGroup<'_, M>) {
9494
|b| {
9595
b.iter_batched(
9696
|| {
97-
let bases_and_exponents: Vec<(DynResidue<{ U256::LIMBS }>, U256)> = (1..=i)
97+
let bases_and_exponents: Vec<(MontyForm<{ U256::LIMBS }>, U256)> = (1..=i)
9898
.map(|_| {
9999
let x = U256::random(&mut OsRng);
100-
let x_m = DynResidue::new(&x, params);
100+
let x_m = MontyForm::new(&x, params);
101101
let p = U256::random(&mut OsRng) | (U256::ONE << (U256::BITS - 1));
102102
(x_m, p)
103103
})
@@ -106,7 +106,7 @@ fn bench_montgomery_ops<M: Measurement>(group: &mut BenchmarkGroup<'_, M>) {
106106
bases_and_exponents
107107
},
108108
|bases_and_exponents| {
109-
black_box(DynResidue::<{ U256::LIMBS }>::multi_exponentiate(
109+
black_box(MontyForm::<{ U256::LIMBS }>::multi_exponentiate(
110110
bases_and_exponents.as_slice(),
111111
))
112112
},

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@
110110
//! assert_eq!(b, U256::ZERO);
111111
//! ```
112112
//!
113-
//! It also supports modular arithmetic over constant moduli using `Residue`,
114-
//! and over moduli set at runtime using `DynResidue`.
113+
//! It also supports modular arithmetic over constant moduli using `ConstMontyForm`,
114+
//! and over moduli set at runtime using `MontyForm`.
115115
//! That includes modular exponentiation and multiplicative inverses.
116116
//! These features are described in the [`modular`] module.
117117
//!

0 commit comments

Comments
 (0)