Skip to content

Commit 6687023

Browse files
committed
Pass Uints by reference
1 parent d4d99aa commit 6687023

28 files changed

+136
-121
lines changed

benches/bench.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,12 @@ fn bench_modpow<'a, M: Measurement>(group: &mut BenchmarkGroup<'a, M>) {
8989

9090
let params = moduli
9191
.iter()
92-
.map(|modulus| DynResidueParams::new(*modulus))
92+
.map(|modulus| DynResidueParams::new(modulus))
9393
.collect::<Vec<_>>();
9494
let xs_m = xs
9595
.iter()
9696
.zip(params.iter())
97-
.map(|(x, p)| DynResidue::new(*x, *p))
97+
.map(|(x, p)| DynResidue::new(x, *p))
9898
.collect::<Vec<_>>();
9999

100100
group.bench_function("modpow, 4^4", |b| {

src/uint/add.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ impl<const LIMBS: usize> Uint<LIMBS> {
4444
rhs: &Self,
4545
choice: CtChoice,
4646
) -> (Self, CtChoice) {
47-
let actual_rhs = Uint::ct_select(Uint::ZERO, *rhs, choice);
47+
let actual_rhs = Uint::ct_select(&Uint::ZERO, rhs, choice);
4848
let (sum, carry) = self.adc(&actual_rhs, Limb::ZERO);
4949

5050
debug_assert!(carry.0 == 0 || carry.0 == 1);

src/uint/cmp.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use subtle::{Choice, ConstantTimeEq, ConstantTimeGreater, ConstantTimeLess};
1010
impl<const LIMBS: usize> Uint<LIMBS> {
1111
/// Return `b` if `c` is truthy, otherwise return `a`.
1212
#[inline]
13-
pub(crate) const fn ct_select(a: Uint<LIMBS>, b: Uint<LIMBS>, c: CtChoice) -> Self {
13+
pub(crate) const fn ct_select(a: &Self, b: &Self, c: CtChoice) -> Self {
1414
let mut limbs = [Limb::ZERO; LIMBS];
1515

1616
let mut i = 0;
@@ -23,7 +23,7 @@ impl<const LIMBS: usize> Uint<LIMBS> {
2323
}
2424

2525
#[inline]
26-
pub(crate) const fn ct_swap(a: Uint<LIMBS>, b: Uint<LIMBS>, c: CtChoice) -> (Self, Self) {
26+
pub(crate) const fn ct_swap(a: &Self, b: &Self, c: CtChoice) -> (Self, Self) {
2727
let new_a = Self::ct_select(a, b, c);
2828
let new_b = Self::ct_select(b, a, c);
2929

src/uint/div.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ impl<const LIMBS: usize> Uint<LIMBS> {
6161

6262
loop {
6363
let (mut r, borrow) = rem.sbb(&c, Limb::ZERO);
64-
rem = Self::ct_select(r, rem, borrow.0);
64+
rem = Self::ct_select(&r, &rem, borrow.0);
6565
r = quo.bitor(&Self::ONE);
66-
quo = Self::ct_select(r, quo, borrow.0);
66+
quo = Self::ct_select(&r, &quo, borrow.0);
6767
if bd == 0 {
6868
break;
6969
}
@@ -73,7 +73,7 @@ impl<const LIMBS: usize> Uint<LIMBS> {
7373
}
7474

7575
let is_some = Limb(mb as Word).ct_is_nonzero();
76-
quo = Self::ct_select(Self::ZERO, quo, is_some);
76+
quo = Self::ct_select(&Self::ZERO, &quo, is_some);
7777
(quo, rem, is_some)
7878
}
7979

@@ -93,7 +93,7 @@ impl<const LIMBS: usize> Uint<LIMBS> {
9393

9494
loop {
9595
let (r, borrow) = rem.sbb(&c, Limb::ZERO);
96-
rem = Self::ct_select(r, rem, borrow.0);
96+
rem = Self::ct_select(&r, &rem, borrow.0);
9797
if bd == 0 {
9898
break;
9999
}
@@ -128,8 +128,8 @@ impl<const LIMBS: usize> Uint<LIMBS> {
128128
let (lower_sub, borrow) = lower.sbb(&c.0, Limb::ZERO);
129129
let (upper_sub, borrow) = upper.sbb(&c.1, borrow);
130130

131-
lower = Self::ct_select(lower_sub, lower, borrow.0);
132-
upper = Self::ct_select(upper_sub, upper, borrow.0);
131+
lower = Self::ct_select(&lower_sub, &lower, borrow.0);
132+
upper = Self::ct_select(&upper_sub, &upper, borrow.0);
133133
if bd == 0 {
134134
break;
135135
}

src/uint/inv_mod.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ impl<const LIMBS: usize> Uint<LIMBS> {
2020
x = x.bitor(&x_i.shl_vartime(i));
2121

2222
let t = b.wrapping_sub(self);
23-
b = Self::ct_select(b, t, j.wrapping_neg()).shr_vartime(1);
23+
b = Self::ct_select(&b, &t, j.wrapping_neg()).shr_vartime(1);
2424
i += 1;
2525
}
2626
x
@@ -39,7 +39,7 @@ impl<const LIMBS: usize> Uint<LIMBS> {
3939
/// The algorithm is the same as in GMP 6.2.1's `mpn_sec_invert`.
4040
pub const fn inv_odd_mod_bounded(
4141
&self,
42-
modulus: Uint<LIMBS>,
42+
modulus: &Self,
4343
bits: usize,
4444
modulus_bits: usize,
4545
) -> (Self, CtChoice) {
@@ -50,12 +50,12 @@ impl<const LIMBS: usize> Uint<LIMBS> {
5050
let mut u = Uint::ONE;
5151
let mut v = Uint::ZERO;
5252

53-
let mut b = modulus;
53+
let mut b = *modulus;
5454

5555
// `bit_size` can be anything >= `self.bits()` + `modulus.bits()`, setting to the minimum.
5656
let bit_size = bits + modulus_bits;
5757

58-
let mut m1hp = modulus;
58+
let mut m1hp = *modulus;
5959
let (m1hp_new, carry) = m1hp.shr_1();
6060
debug_assert!(carry == Word::MAX);
6161
m1hp = m1hp_new.wrapping_add(&Uint::ONE);
@@ -69,13 +69,13 @@ impl<const LIMBS: usize> Uint<LIMBS> {
6969
// Set `self -= b` if `self` is odd.
7070
let (new_a, swap) = a.conditional_wrapping_sub(&b, self_odd);
7171
// Set `b += self` if `swap` is true.
72-
b = Uint::ct_select(b, b.wrapping_add(&new_a), swap);
72+
b = Uint::ct_select(&b, &b.wrapping_add(&new_a), swap);
7373
// Negate `self` if `swap` is true.
7474
a = new_a.conditional_wrapping_neg(swap);
7575

76-
let (new_u, new_v) = Uint::ct_swap(u, v, swap);
76+
let (new_u, new_v) = Uint::ct_swap(&u, &v, swap);
7777
let (new_u, cy) = new_u.conditional_wrapping_sub(&new_v, self_odd);
78-
let (new_u, cyy) = new_u.conditional_wrapping_add(&modulus, cy);
78+
let (new_u, cyy) = new_u.conditional_wrapping_add(modulus, cy);
7979
debug_assert!(cy == cyy);
8080

8181
let (new_a, overflow) = a.shr_1();
@@ -98,7 +98,7 @@ impl<const LIMBS: usize> Uint<LIMBS> {
9898

9999
/// Computes the multiplicative inverse of `self` mod `modulus`, where `modulus` is odd.
100100
/// Returns `(inverse, Word::MAX)` if an inverse exists, otherwise `(undefined, Word::ZERO)`.
101-
pub const fn inv_odd_mod(&self, modulus: Uint<LIMBS>) -> (Self, CtChoice) {
101+
pub const fn inv_odd_mod(&self, modulus: &Self) -> (Self, CtChoice) {
102102
self.inv_odd_mod_bounded(modulus, Uint::<LIMBS>::BITS, Uint::<LIMBS>::BITS)
103103
}
104104
}
@@ -139,7 +139,7 @@ mod tests {
139139
"558D0B64E37CD0775C0D0104AE7D98BA23C815185DD43CD8B16292FD94156767"
140140
]);
141141

142-
let (res, is_some) = a.inv_odd_mod(m);
142+
let (res, is_some) = a.inv_odd_mod(&m);
143143

144144
let expected = U1024::from_be_hex(concat![
145145
"B03623284B0EBABCABD5C5881893320281460C0A8E7BF4BFDCFFCBCCBF436A55",
@@ -166,7 +166,7 @@ mod tests {
166166
"558D0B64E37CD0775C0D0104AE7D98BA23C815185DD43CD8B16292FD94156767"
167167
]);
168168

169-
let (res, is_some) = a.inv_odd_mod_bounded(m, 768, 512);
169+
let (res, is_some) = a.inv_odd_mod_bounded(&m, 768, 512);
170170

171171
let expected = U1024::from_be_hex(concat![
172172
"0000000000000000000000000000000000000000000000000000000000000000",
@@ -183,7 +183,7 @@ mod tests {
183183
let a = U64::from(3u64);
184184
let m = U64::from(13u64);
185185

186-
let (res, is_some) = a.inv_odd_mod(m);
186+
let (res, is_some) = a.inv_odd_mod(&m);
187187

188188
assert_eq!(is_some, Word::MAX);
189189
assert_eq!(U64::from(9u64), res);
@@ -194,7 +194,7 @@ mod tests {
194194
let a = U64::from(14u64);
195195
let m = U64::from(49u64);
196196

197-
let (_res, is_some) = a.inv_odd_mod(m);
197+
let (_res, is_some) = a.inv_odd_mod(&m);
198198

199199
assert_eq!(is_some, 0);
200200
}

src/uint/modular.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ mod tests {
6464
// Divide the value R by R, which should equal 1
6565
assert_eq!(
6666
montgomery_reduction::<{ Modulus2::LIMBS }>(
67-
(Modulus2::R, Uint::ZERO),
68-
Modulus2::MODULUS,
67+
&(Modulus2::R, Uint::ZERO),
68+
&Modulus2::MODULUS,
6969
Modulus2::MOD_NEG_INV
7070
),
7171
Uint::ONE
@@ -77,8 +77,8 @@ mod tests {
7777
// Divide the value R^2 by R, which should equal R
7878
assert_eq!(
7979
montgomery_reduction::<{ Modulus2::LIMBS }>(
80-
(Modulus2::R2, Uint::ZERO),
81-
Modulus2::MODULUS,
80+
&(Modulus2::R2, Uint::ZERO),
81+
&Modulus2::MODULUS,
8282
Modulus2::MOD_NEG_INV
8383
),
8484
Modulus2::R
@@ -91,8 +91,8 @@ mod tests {
9191
let (hi, lo) = Modulus2::R.square().split();
9292
assert_eq!(
9393
montgomery_reduction::<{ Modulus2::LIMBS }>(
94-
(lo, hi),
95-
Modulus2::MODULUS,
94+
&(lo, hi),
95+
&Modulus2::MODULUS,
9696
Modulus2::MOD_NEG_INV
9797
),
9898
Modulus2::R
@@ -107,8 +107,8 @@ mod tests {
107107
let product = x.mul_wide(&Modulus2::R);
108108
assert_eq!(
109109
montgomery_reduction::<{ Modulus2::LIMBS }>(
110-
product,
111-
Modulus2::MODULUS,
110+
&product,
111+
&Modulus2::MODULUS,
112112
Modulus2::MOD_NEG_INV
113113
),
114114
x
@@ -131,8 +131,8 @@ mod tests {
131131

132132
assert_eq!(
133133
montgomery_reduction::<{ Modulus2::LIMBS }>(
134-
product,
135-
Modulus2::MODULUS,
134+
&product,
135+
&Modulus2::MODULUS,
136136
Modulus2::MOD_NEG_INV
137137
),
138138
lo
@@ -143,7 +143,7 @@ mod tests {
143143
fn test_new_retrieve() {
144144
let x =
145145
U256::from_be_hex("44acf6b7e36c1342c2c5897204fe09504e1e2efb1a900377dbc4e7a6a133ec56");
146-
let x_mod = Residue::<Modulus2, { Modulus2::LIMBS }>::new(x);
146+
let x_mod = Residue::<Modulus2, { Modulus2::LIMBS }>::new(&x);
147147

148148
// Confirm that when creating a Modular and retrieving the value, that it equals the original
149149
assert_eq!(x, x_mod.retrieve());
@@ -154,7 +154,7 @@ mod tests {
154154
let x =
155155
U256::from_be_hex("44acf6b7e36c1342c2c5897204fe09504e1e2efb1a900377dbc4e7a6a133ec56");
156156
assert_eq!(
157-
Residue::<Modulus2, { Modulus2::LIMBS }>::new(x),
157+
Residue::<Modulus2, { Modulus2::LIMBS }>::new(&x),
158158
const_residue!(x, Modulus2)
159159
);
160160
}

src/uint/modular/constant_mod.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -69,24 +69,22 @@ impl<MOD: ResidueParams<LIMBS>, const LIMBS: usize> Residue<MOD, LIMBS> {
6969
};
7070

7171
/// Instantiates a new `Residue` that represents this `integer` mod `MOD`.
72-
pub const fn new(integer: Uint<LIMBS>) -> Self {
73-
let mut modular_integer = Residue {
74-
montgomery_form: integer,
75-
phantom: PhantomData,
76-
};
77-
72+
pub const fn new(integer: &Uint<LIMBS>) -> Self {
7873
let product = integer.mul_wide(&MOD::R2);
79-
modular_integer.montgomery_form =
80-
montgomery_reduction::<LIMBS>(product, MOD::MODULUS, MOD::MOD_NEG_INV);
74+
let montgomery_form =
75+
montgomery_reduction::<LIMBS>(&product, &MOD::MODULUS, MOD::MOD_NEG_INV);
8176

82-
modular_integer
77+
Self {
78+
montgomery_form,
79+
phantom: PhantomData,
80+
}
8381
}
8482

8583
/// Retrieves the integer currently encoded in this `Residue`, guaranteed to be reduced.
8684
pub const fn retrieve(&self) -> Uint<LIMBS> {
8785
montgomery_reduction::<LIMBS>(
88-
(self.montgomery_form, Uint::ZERO),
89-
MOD::MODULUS,
86+
&(self.montgomery_form, Uint::ZERO),
87+
&MOD::MODULUS,
9088
MOD::MOD_NEG_INV,
9189
)
9290
}

src/uint/modular/constant_mod/const_inv.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ impl<MOD: ResidueParams<LIMBS>, const LIMBS: usize> Residue<MOD, LIMBS> {
1313
/// otherwise it is the falsy value (in which case the first element's value is unspecified).
1414
pub const fn invert(&self) -> (Self, CtChoice) {
1515
let (montgomery_form, is_some) = inv_montgomery_form(
16-
self.montgomery_form,
17-
MOD::MODULUS,
16+
&self.montgomery_form,
17+
&MOD::MODULUS,
1818
&MOD::R3,
1919
MOD::MOD_NEG_INV,
2020
);

src/uint/modular/constant_mod/const_mul.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ impl<MOD: ResidueParams<LIMBS>, const LIMBS: usize> Residue<MOD, LIMBS> {
1717
montgomery_form: mul_montgomery_form(
1818
&self.montgomery_form,
1919
&rhs.montgomery_form,
20-
MOD::MODULUS,
20+
&MOD::MODULUS,
2121
MOD::MOD_NEG_INV,
2222
),
2323
phantom: PhantomData,
@@ -80,7 +80,7 @@ impl<MOD: ResidueParams<LIMBS>, const LIMBS: usize> Square for Residue<MOD, LIMB
8080
Self {
8181
montgomery_form: square_montgomery_form(
8282
&self.montgomery_form,
83-
MOD::MODULUS,
83+
&MOD::MODULUS,
8484
MOD::MOD_NEG_INV,
8585
),
8686
phantom: PhantomData,

src/uint/modular/constant_mod/const_neg.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,14 @@ impl<MOD: ResidueParams<LIMBS>, const LIMBS: usize> Residue<MOD, LIMBS> {
1212
impl<MOD: ResidueParams<LIMBS>, const LIMBS: usize> Neg for Residue<MOD, LIMBS> {
1313
type Output = Self;
1414
fn neg(self) -> Self {
15-
(&self).neg()
15+
Residue::neg(&self)
16+
}
17+
}
18+
19+
impl<MOD: ResidueParams<LIMBS>, const LIMBS: usize> Neg for &Residue<MOD, LIMBS> {
20+
type Output = Residue<MOD, LIMBS>;
21+
fn neg(self) -> Residue<MOD, LIMBS> {
22+
Residue::neg(self)
1623
}
1724
}
1825

src/uint/modular/constant_mod/const_pow.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ impl<MOD: ResidueParams<LIMBS>, const LIMBS: usize> Residue<MOD, LIMBS> {
2020
) -> Residue<MOD, LIMBS> {
2121
Self {
2222
montgomery_form: pow_montgomery_form(
23-
self.montgomery_form,
23+
&self.montgomery_form,
2424
exponent,
2525
exponent_bits,
26-
MOD::MODULUS,
27-
MOD::R,
26+
&MOD::MODULUS,
27+
&MOD::R,
2828
MOD::MOD_NEG_INV,
2929
),
3030
phantom: core::marker::PhantomData,

src/uint/modular/constant_mod/macros.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ macro_rules! impl_modulus {
2727
);
2828
const R3: $crate::Uint<{ nlimbs!(<$uint_type>::BITS) }> =
2929
$crate::uint::modular::reduction::montgomery_reduction(
30-
Self::R2.square_wide(),
31-
Self::MODULUS,
30+
&Self::R2.square_wide(),
31+
&Self::MODULUS,
3232
Self::MOD_NEG_INV,
3333
);
3434
}
@@ -41,7 +41,7 @@ macro_rules! impl_modulus {
4141
macro_rules! const_residue {
4242
($variable:ident, $modulus:ident) => {
4343
$crate::uint::modular::constant_mod::Residue::<$modulus, { $modulus::LIMBS }>::new(
44-
$variable,
44+
&$variable,
4545
)
4646
};
4747
}

src/uint/modular/inv.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
use crate::{modular::reduction::montgomery_reduction, CtChoice, Limb, Uint};
22

33
pub const fn inv_montgomery_form<const LIMBS: usize>(
4-
x: Uint<LIMBS>,
5-
modulus: Uint<LIMBS>,
4+
x: &Uint<LIMBS>,
5+
modulus: &Uint<LIMBS>,
66
r3: &Uint<LIMBS>,
77
mod_neg_inv: Limb,
88
) -> (Uint<LIMBS>, CtChoice) {
99
let (inverse, is_some) = x.inv_odd_mod(modulus);
1010
(
11-
montgomery_reduction(inverse.mul_wide(r3), modulus, mod_neg_inv),
11+
montgomery_reduction(&inverse.mul_wide(r3), modulus, mod_neg_inv),
1212
is_some,
1313
)
1414
}

0 commit comments

Comments
 (0)