Skip to content

Commit 3b73e08

Browse files
authored
Documentation improvements (#528)
- Expand documentation on `Limb` representations - Bernstein-Yang doc cleanups
1 parent b811e5e commit 3b73e08

File tree

3 files changed

+29
-19
lines changed

3 files changed

+29
-19
lines changed

src/limb.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,11 @@ pub type Word = u64;
5353
#[cfg(target_pointer_width = "64")]
5454
pub type WideWord = u128;
5555

56-
/// Big integers are represented as an array of smaller CPU word-size integers
57-
/// called "limbs".
56+
/// Big integers are represented as an array/vector of smaller CPU word-size integers called
57+
/// "limbs".
58+
///
59+
/// The [`Limb`] type uses a 32-bit or 64-bit saturated representation, depending on the target.
60+
/// All bits of an inner [`Word`] are used to represent larger big integer types.
5861
// Our PartialEq impl only differs from the default one by being constant-time, so this is safe
5962
#[allow(clippy::derived_hash_with_manual_eq)]
6063
#[derive(Copy, Clone, Default, Hash)]

src/modular/bernstein_yang.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ impl<const SAT_LIMBS: usize, const UNSAT_LIMBS: usize> Inverter
142142

143143
/// Returns the multiplicative inverse of the argument modulo 2^62. The implementation is based
144144
/// on the Hurchalla's method for computing the multiplicative inverse modulo a power of two.
145+
///
145146
/// For better understanding the implementation, the following paper is recommended:
146147
/// J. Hurchalla, "An Improved Integer Multiplicative Inverse (modulo 2^w)",
147148
/// https://arxiv.org/pdf/2204.04342.pdf
@@ -238,8 +239,9 @@ const fn jump(f: &[u64], g: &[u64], mut delta: i64) -> (i64, Matrix) {
238239
}
239240

240241
/// Returns the updated values of the variables f and g for specified initial ones and
241-
/// Bernstein-Yang transition matrix multiplied by 2^62. The returned vector is
242-
/// "matrix * (f, g)' / 2^62", where "'" is the transpose operator.
242+
/// Bernstein-Yang transition matrix multiplied by 2^62.
243+
///
244+
/// The returned vector is "matrix * (f, g)' / 2^62", where "'" is the transpose operator.
243245
const fn fg<const LIMBS: usize>(
244246
f: Int62L<LIMBS>,
245247
g: Int62L<LIMBS>,
@@ -252,10 +254,12 @@ const fn fg<const LIMBS: usize>(
252254
}
253255

254256
/// Returns the updated values of the variables d and e for specified initial ones and
255-
/// Bernstein-Yang transition matrix multiplied by 2^62. The returned vector is congruent modulo
256-
/// M to "matrix * (d, e)' / 2^62 (mod M)", where M is the modulus the inverter was created for
257-
/// and "'" stands for the transpose operator. Both the input and output values lie in the
258-
/// interval (-2 * M, M).
257+
/// Bernstein-Yang transition matrix multiplied by 2^62.
258+
///
259+
/// The returned vector is congruent modulo M to "matrix * (d, e)' / 2^62 (mod M)", where M is the
260+
/// modulus the inverter was created for and "'" stands for the transpose operator.
261+
///
262+
/// Both the input and output values lie in the interval (-2 * M, M).
259263
const fn de<const LIMBS: usize>(
260264
modulus: &Int62L<LIMBS>,
261265
inverse: i64,
@@ -313,7 +317,7 @@ impl<const LIMBS: usize> Int62L<LIMBS> {
313317
ret
314318
};
315319

316-
/// Convert from 64-bit saturated representation used by `Uint` to the 62-bit unsaturated
320+
/// Convert from 32/64-bit saturated representation used by `Uint` to the 62-bit unsaturated
317321
/// representation used by `Int62L`.
318322
///
319323
/// Returns a big unsigned integer as an array of 62-bit chunks, which is equal modulo
@@ -332,13 +336,13 @@ impl<const LIMBS: usize> Int62L<LIMBS> {
332336
Self(output)
333337
}
334338

335-
/// Convert from 62-bit unsaturated representation used by `Int62L` to the 64-bit saturated
339+
/// Convert from 62-bit unsaturated representation used by `Int62L` to the 32/64-bit saturated
336340
/// representation used by `Uint`.
337341
///
338-
/// Returns a big unsigned integer as an array of 64-bit chunks, which is equal modulo
342+
/// Returns a big unsigned integer as an array of 32/64-bit chunks, which is equal modulo
339343
/// 2 ^ (64 * S) to the input big unsigned integer stored as an array of 62-bit chunks.
340344
///
341-
/// The ordering of the chunks in these arrays is little-endian
345+
/// The ordering of the chunks in these arrays is little-endian.
342346
#[allow(trivial_numeric_casts, clippy::wrong_self_convention)]
343347
pub const fn to_uint<const SAT_LIMBS: usize>(&self) -> Uint<SAT_LIMBS> {
344348
debug_assert!(!self.is_negative(), "can't convert negative number to Uint");

src/modular/bernstein_yang/boxed.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,9 @@ fn divsteps(
119119
}
120120

121121
/// Returns the updated values of the variables f and g for specified initial ones and
122-
/// Bernstein-Yang transition matrix multiplied by 2^62. The returned vector is
123-
/// "matrix * (f, g)' / 2^62", where "'" is the transpose operator.
122+
/// Bernstein-Yang transition matrix multiplied by 2^62.
123+
///
124+
/// The returned vector is "matrix * (f, g)' / 2^62", where "'" is the transpose operator.
124125
fn fg(f: &mut BoxedInt62L, g: &mut BoxedInt62L, t: Matrix) {
125126
// TODO(tarcieri): reduce allocations
126127
let mut f2 = &*f * t[0][0];
@@ -136,10 +137,12 @@ fn fg(f: &mut BoxedInt62L, g: &mut BoxedInt62L, t: Matrix) {
136137
}
137138

138139
/// Returns the updated values of the variables d and e for specified initial ones and
139-
/// Bernstein-Yang transition matrix multiplied by 2^62. The returned vector is congruent modulo
140-
/// M to "matrix * (d, e)' / 2^62 (mod M)", where M is the modulus the inverter was created for
141-
/// and "'" stands for the transpose operator. Both the input and output values lie in the
142-
/// interval (-2 * M, M).
140+
/// Bernstein-Yang transition matrix multiplied by 2^62.
141+
///
142+
/// The returned vector is congruent modulo M to "matrix * (d, e)' / 2^62 (mod M)", where M is the
143+
/// modulus the inverter was created for and "'" stands for the transpose operator.
144+
///
145+
/// Both the input and output values lie in the interval (-2 * M, M).
143146
fn de(modulus: &BoxedInt62L, inverse: i64, t: Matrix, d: &mut BoxedInt62L, e: &mut BoxedInt62L) {
144147
let mask = BoxedInt62L::MASK as i64;
145148
let mut md = t[0][0] * d.is_negative() as i64 + t[0][1] * e.is_negative() as i64;
@@ -179,7 +182,7 @@ fn de(modulus: &BoxedInt62L, inverse: i64, t: Matrix, d: &mut BoxedInt62L, e: &m
179182
#[derive(Clone, Debug)]
180183
pub(crate) struct BoxedInt62L(Box<[u64]>);
181184

182-
/// Convert from 64-bit saturated representation used by `Uint` to the 62-bit unsaturated
185+
/// Convert from 32/64-bit saturated representation used by `Uint` to the 62-bit unsaturated
183186
/// representation used by `BoxedInt62L`.
184187
///
185188
/// Returns a big unsigned integer as an array of 62-bit chunks, which is equal modulo

0 commit comments

Comments
 (0)