Skip to content

Commit 6b58de4

Browse files
authored
Rename *Int62L => *UnsatInt (#637)
These types are used within the Bernstein-Yang implementation(s) and implements a 62-bit unsaturated form used by the algorithm.
1 parent e3fea9c commit 6b58de4

File tree

2 files changed

+141
-135
lines changed

2 files changed

+141
-135
lines changed

src/modular/bernstein_yang.rs

Lines changed: 63 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ use subtle::CtOption;
4646
#[derive(Clone, Debug)]
4747
pub struct BernsteinYangInverter<const SAT_LIMBS: usize, const UNSAT_LIMBS: usize> {
4848
/// Modulus
49-
pub(super) modulus: Int62L<UNSAT_LIMBS>,
49+
pub(super) modulus: UnsatInt<UNSAT_LIMBS>,
5050

5151
/// Adjusting parameter (see toplevel documentation).
52-
adjuster: Int62L<UNSAT_LIMBS>,
52+
adjuster: UnsatInt<UNSAT_LIMBS>,
5353

5454
/// Multiplicative inverse of the modulus modulo 2^62
5555
inverse: i64,
@@ -66,8 +66,8 @@ impl<const SAT_LIMBS: usize, const UNSAT_LIMBS: usize>
6666
/// Modulus must be odd. Returns `None` if it is not.
6767
pub const fn new(modulus: &Odd<Uint<SAT_LIMBS>>, adjuster: &Uint<SAT_LIMBS>) -> Self {
6868
Self {
69-
modulus: Int62L::from_uint(&modulus.0),
70-
adjuster: Int62L::from_uint(adjuster),
69+
modulus: UnsatInt::from_uint(&modulus.0),
70+
adjuster: UnsatInt::from_uint(adjuster),
7171
inverse: inv_mod2_62(modulus.0.as_words()),
7272
}
7373
}
@@ -78,16 +78,16 @@ impl<const SAT_LIMBS: usize, const UNSAT_LIMBS: usize>
7878
let (d, f) = divsteps(
7979
self.adjuster,
8080
self.modulus,
81-
Int62L::from_uint(value),
81+
UnsatInt::from_uint(value),
8282
self.inverse,
8383
);
8484

8585
// At this point the absolute value of "f" equals the greatest common divisor of the
8686
// integer to be inverted and the modulus the inverter was created for.
8787
// Thus, if "f" is neither 1 nor -1, then the sought inverse does not exist.
88-
let antiunit = f.eq(&Int62L::MINUS_ONE);
88+
let antiunit = f.eq(&UnsatInt::MINUS_ONE);
8989
let ret = self.norm(d, antiunit);
90-
let is_some = f.eq(&Int62L::ONE).or(antiunit);
90+
let is_some = f.eq(&UnsatInt::ONE).or(antiunit);
9191
ConstCtOption::new(ret.to_uint(), is_some)
9292
}
9393

@@ -98,11 +98,11 @@ impl<const SAT_LIMBS: usize, const UNSAT_LIMBS: usize>
9898
/// `Uint` limb sizes.
9999
pub(crate) const fn gcd(f: &Uint<SAT_LIMBS>, g: &Uint<SAT_LIMBS>) -> Uint<SAT_LIMBS> {
100100
let inverse = inv_mod2_62(f.as_words());
101-
let e = Int62L::<UNSAT_LIMBS>::ONE;
102-
let f = Int62L::from_uint(f);
103-
let g = Int62L::from_uint(g);
101+
let e = UnsatInt::<UNSAT_LIMBS>::ONE;
102+
let f = UnsatInt::from_uint(f);
103+
let g = UnsatInt::from_uint(g);
104104
let (_, mut f) = divsteps(e, f, g, inverse);
105-
f = Int62L::select(&f, &f.neg(), f.is_negative());
105+
f = UnsatInt::select(&f, &f.neg(), f.is_negative());
106106
f.to_uint()
107107
}
108108

@@ -111,11 +111,11 @@ impl<const SAT_LIMBS: usize, const UNSAT_LIMBS: usize>
111111
/// This version is variable-time with respect to `g`.
112112
pub(crate) const fn gcd_vartime(f: &Uint<SAT_LIMBS>, g: &Uint<SAT_LIMBS>) -> Uint<SAT_LIMBS> {
113113
let inverse = inv_mod2_62(f.as_words());
114-
let e = Int62L::<UNSAT_LIMBS>::ONE;
115-
let f = Int62L::from_uint(f);
116-
let g = Int62L::from_uint(g);
114+
let e = UnsatInt::<UNSAT_LIMBS>::ONE;
115+
let f = UnsatInt::from_uint(f);
116+
let g = UnsatInt::from_uint(g);
117117
let (_, mut f) = divsteps_vartime(e, f, g, inverse);
118-
f = Int62L::select(&f, &f.neg(), f.is_negative());
118+
f = UnsatInt::select(&f, &f.neg(), f.is_negative());
119119
f.to_uint()
120120
}
121121

@@ -124,12 +124,12 @@ impl<const SAT_LIMBS: usize, const UNSAT_LIMBS: usize>
124124
/// formula. The input integer lies in the interval (-2 * M, M).
125125
const fn norm(
126126
&self,
127-
mut value: Int62L<UNSAT_LIMBS>,
127+
mut value: UnsatInt<UNSAT_LIMBS>,
128128
negate: ConstChoice,
129-
) -> Int62L<UNSAT_LIMBS> {
130-
value = Int62L::select(&value, &value.add(&self.modulus), value.is_negative());
131-
value = Int62L::select(&value, &value.neg(), negate);
132-
value = Int62L::select(&value, &value.add(&self.modulus), value.is_negative());
129+
) -> UnsatInt<UNSAT_LIMBS> {
130+
value = UnsatInt::select(&value, &value.add(&self.modulus), value.is_negative());
131+
value = UnsatInt::select(&value, &value.neg(), negate);
132+
value = UnsatInt::select(&value, &value.add(&self.modulus), value.is_negative());
133133
value
134134
}
135135
}
@@ -187,12 +187,12 @@ const fn inv_mod2_62(value: &[Word]) -> i64 {
187187
/// This version runs in a fixed number of iterations relative to the highest bit of `f` or `g`
188188
/// as described in Figure 11.1.
189189
const fn divsteps<const LIMBS: usize>(
190-
mut e: Int62L<LIMBS>,
191-
f_0: Int62L<LIMBS>,
192-
mut g: Int62L<LIMBS>,
190+
mut e: UnsatInt<LIMBS>,
191+
f_0: UnsatInt<LIMBS>,
192+
mut g: UnsatInt<LIMBS>,
193193
inverse: i64,
194-
) -> (Int62L<LIMBS>, Int62L<LIMBS>) {
195-
let mut d = Int62L::ZERO;
194+
) -> (UnsatInt<LIMBS>, UnsatInt<LIMBS>) {
195+
let mut d = UnsatInt::ZERO;
196196
let mut f = f_0;
197197
let mut delta = 1;
198198
let mut matrix;
@@ -206,7 +206,7 @@ const fn divsteps<const LIMBS: usize>(
206206
i += 1;
207207
}
208208

209-
debug_assert!(g.eq(&Int62L::ZERO).to_bool_vartime());
209+
debug_assert!(g.eq(&UnsatInt::ZERO).to_bool_vartime());
210210
(d, f)
211211
}
212212

@@ -215,17 +215,17 @@ const fn divsteps<const LIMBS: usize>(
215215
///
216216
/// This version is variable-time with respect to `g`.
217217
const fn divsteps_vartime<const LIMBS: usize>(
218-
mut e: Int62L<LIMBS>,
219-
f_0: Int62L<LIMBS>,
220-
mut g: Int62L<LIMBS>,
218+
mut e: UnsatInt<LIMBS>,
219+
f_0: UnsatInt<LIMBS>,
220+
mut g: UnsatInt<LIMBS>,
221221
inverse: i64,
222-
) -> (Int62L<LIMBS>, Int62L<LIMBS>) {
223-
let mut d = Int62L::ZERO;
222+
) -> (UnsatInt<LIMBS>, UnsatInt<LIMBS>) {
223+
let mut d = UnsatInt::ZERO;
224224
let mut f = f_0;
225225
let mut delta = 1;
226226
let mut matrix;
227227

228-
while !g.eq(&Int62L::ZERO).to_bool_vartime() {
228+
while !g.eq(&UnsatInt::ZERO).to_bool_vartime() {
229229
(delta, matrix) = jump(&f.0, &g.0, delta);
230230
(f, g) = fg(f, g, matrix);
231231
(d, e) = de(&f_0, inverse, matrix, d, e);
@@ -281,10 +281,10 @@ const fn jump(f: &[u64], g: &[u64], mut delta: i64) -> (i64, Matrix) {
281281
///
282282
/// The returned vector is "matrix * (f, g)' / 2^62", where "'" is the transpose operator.
283283
const fn fg<const LIMBS: usize>(
284-
f: Int62L<LIMBS>,
285-
g: Int62L<LIMBS>,
284+
f: UnsatInt<LIMBS>,
285+
g: UnsatInt<LIMBS>,
286286
t: Matrix,
287-
) -> (Int62L<LIMBS>, Int62L<LIMBS>) {
287+
) -> (UnsatInt<LIMBS>, UnsatInt<LIMBS>) {
288288
(
289289
f.mul(t[0][0]).add(&g.mul(t[0][1])).shr(),
290290
f.mul(t[1][0]).add(&g.mul(t[1][1])).shr(),
@@ -299,13 +299,13 @@ const fn fg<const LIMBS: usize>(
299299
///
300300
/// Both the input and output values lie in the interval (-2 * M, M).
301301
const fn de<const LIMBS: usize>(
302-
modulus: &Int62L<LIMBS>,
302+
modulus: &UnsatInt<LIMBS>,
303303
inverse: i64,
304304
t: Matrix,
305-
d: Int62L<LIMBS>,
306-
e: Int62L<LIMBS>,
307-
) -> (Int62L<LIMBS>, Int62L<LIMBS>) {
308-
let mask = Int62L::<LIMBS>::MASK as i64;
305+
d: UnsatInt<LIMBS>,
306+
e: UnsatInt<LIMBS>,
307+
) -> (UnsatInt<LIMBS>, UnsatInt<LIMBS>) {
308+
let mask = UnsatInt::<LIMBS>::MASK as i64;
309309
let mut md =
310310
t[0][0] * d.is_negative().to_u8() as i64 + t[0][1] * e.is_negative().to_u8() as i64;
311311
let mut me =
@@ -355,9 +355,9 @@ pub(crate) const fn iterations(f_bits: u32, g_bits: u32) -> usize {
355355
///
356356
/// The arithmetic operations for this type are wrapping ones.
357357
#[derive(Clone, Copy, Debug)]
358-
pub(super) struct Int62L<const LIMBS: usize>(pub [u64; LIMBS]);
358+
pub(super) struct UnsatInt<const LIMBS: usize>(pub [u64; LIMBS]);
359359

360-
impl<const LIMBS: usize> Int62L<LIMBS> {
360+
impl<const LIMBS: usize> UnsatInt<LIMBS> {
361361
/// Number of bits in each limb.
362362
pub const LIMB_BITS: usize = 62;
363363

@@ -378,7 +378,7 @@ impl<const LIMBS: usize> Int62L<LIMBS> {
378378
};
379379

380380
/// Convert from 32/64-bit saturated representation used by `Uint` to the 62-bit unsaturated
381-
/// representation used by `Int62L`.
381+
/// representation used by `UnsatInt`.
382382
///
383383
/// Returns a big unsigned integer as an array of 62-bit chunks, which is equal modulo
384384
/// 2 ^ (62 * S) to the input big unsigned integer stored as an array of 64-bit chunks.
@@ -396,7 +396,7 @@ impl<const LIMBS: usize> Int62L<LIMBS> {
396396
Self(output)
397397
}
398398

399-
/// Convert from 62-bit unsaturated representation used by `Int62L` to the 32/64-bit saturated
399+
/// Convert from 62-bit unsaturated representation used by `UnsatInt` to the 32/64-bit saturated
400400
/// representation used by `Uint`.
401401
///
402402
/// Returns a big unsigned integer as an array of 32/64-bit chunks, which is equal modulo
@@ -521,7 +521,7 @@ impl<const LIMBS: usize> Int62L<LIMBS> {
521521
self.0[0]
522522
}
523523

524-
/// Select between two [`Int62L`] values in constant time.
524+
/// Select between two [`UnsatInt`] values in constant time.
525525
pub const fn select(a: &Self, b: &Self, choice: ConstChoice) -> Self {
526526
let mut ret = Self::ZERO;
527527
let mut i = 0;
@@ -562,9 +562,9 @@ impl<const LIMBS: usize> Int62L<LIMBS> {
562562
mod tests {
563563
use crate::{Inverter, PrecomputeInverter, U256};
564564

565-
type Int62L = super::Int62L<4>;
565+
type UnsatInt = super::UnsatInt<4>;
566566

567-
impl<const LIMBS: usize> PartialEq for crate::modular::bernstein_yang::Int62L<LIMBS> {
567+
impl<const LIMBS: usize> PartialEq for crate::modular::bernstein_yang::UnsatInt<LIMBS> {
568568
fn eq(&self, other: &Self) -> bool {
569569
self.eq(other).to_bool_vartime()
570570
}
@@ -588,38 +588,38 @@ mod tests {
588588

589589
#[test]
590590
fn int62l_add() {
591-
assert_eq!(Int62L::ZERO, Int62L::ZERO.add(&Int62L::ZERO));
592-
assert_eq!(Int62L::ONE, Int62L::ONE.add(&Int62L::ZERO));
593-
assert_eq!(Int62L::ZERO, Int62L::MINUS_ONE.add(&Int62L::ONE));
591+
assert_eq!(UnsatInt::ZERO, UnsatInt::ZERO.add(&UnsatInt::ZERO));
592+
assert_eq!(UnsatInt::ONE, UnsatInt::ONE.add(&UnsatInt::ZERO));
593+
assert_eq!(UnsatInt::ZERO, UnsatInt::MINUS_ONE.add(&UnsatInt::ONE));
594594
}
595595

596596
#[test]
597597
fn int62l_mul() {
598-
assert_eq!(Int62L::ZERO, Int62L::ZERO.mul(0));
599-
assert_eq!(Int62L::ZERO, Int62L::ZERO.mul(1));
600-
assert_eq!(Int62L::ZERO, Int62L::ONE.mul(0));
601-
assert_eq!(Int62L::ZERO, Int62L::MINUS_ONE.mul(0));
602-
assert_eq!(Int62L::ONE, Int62L::ONE.mul(1));
603-
assert_eq!(Int62L::MINUS_ONE, Int62L::MINUS_ONE.mul(1));
598+
assert_eq!(UnsatInt::ZERO, UnsatInt::ZERO.mul(0));
599+
assert_eq!(UnsatInt::ZERO, UnsatInt::ZERO.mul(1));
600+
assert_eq!(UnsatInt::ZERO, UnsatInt::ONE.mul(0));
601+
assert_eq!(UnsatInt::ZERO, UnsatInt::MINUS_ONE.mul(0));
602+
assert_eq!(UnsatInt::ONE, UnsatInt::ONE.mul(1));
603+
assert_eq!(UnsatInt::MINUS_ONE, UnsatInt::MINUS_ONE.mul(1));
604604
}
605605

606606
#[test]
607607
fn int62l_neg() {
608-
assert_eq!(Int62L::ZERO, Int62L::ZERO.neg());
609-
assert_eq!(Int62L::MINUS_ONE, Int62L::ONE.neg());
610-
assert_eq!(Int62L::ONE, Int62L::MINUS_ONE.neg());
608+
assert_eq!(UnsatInt::ZERO, UnsatInt::ZERO.neg());
609+
assert_eq!(UnsatInt::MINUS_ONE, UnsatInt::ONE.neg());
610+
assert_eq!(UnsatInt::ONE, UnsatInt::MINUS_ONE.neg());
611611
}
612612

613613
#[test]
614614
fn int62l_is_negative() {
615-
assert!(!Int62L::ZERO.is_negative().to_bool_vartime());
616-
assert!(!Int62L::ONE.is_negative().to_bool_vartime());
617-
assert!(Int62L::MINUS_ONE.is_negative().to_bool_vartime());
615+
assert!(!UnsatInt::ZERO.is_negative().to_bool_vartime());
616+
assert!(!UnsatInt::ONE.is_negative().to_bool_vartime());
617+
assert!(UnsatInt::MINUS_ONE.is_negative().to_bool_vartime());
618618
}
619619

620620
#[test]
621621
fn int62l_shr() {
622-
let n = super::Int62L([
622+
let n = super::UnsatInt([
623623
0,
624624
1211048314408256470,
625625
1344008336933394898,

0 commit comments

Comments
 (0)