oxinum-int 0.1.1

Arbitrary-precision integers for OxiNum (UBig/IBig via dashu-int)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
//! Montgomery multiplication context for repeated modular arithmetic.
//!
//! A [`MontgomeryContext`] caches parameters for a fixed odd modulus `m` and
//! enables:
//! - [`MontgomeryContext::mul`]: Montgomery multiplication `REDC(a * b)`.
//! - [`MontgomeryContext::pow`]: Modular exponentiation via the Montgomery ladder.
//! - [`MontgomeryContext::to_mont`] / [`MontgomeryContext::from_mont`]:
//!   convert between normal form and Montgomery form.
//!
//! # Background
//!
//! Montgomery multiplication replaces expensive modular reductions by computing
//! `t * R^{-1} mod m` (REDC) where `R = 2^(n*64)` and `n` is the number of
//! 64-bit limbs of `m`. The key identity is:
//!
//! ```text
//! REDC(a * b) ≡ a * b * R^{-1}  (mod m)
//! ```
//!
//! To multiply `a` and `b` mod `m`:
//! 1. Convert to Montgomery form: `a̅ = REDC(a * R²)`, `b̅ = REDC(b * R²)`.
//! 2. Multiply: `c̅ = REDC(a̅ * b̅) ≡ a * b * R  (mod m)`.
//! 3. Convert back: `c = REDC(c̅) ≡ a * b  (mod m)`.
//!
//! # Precomputed values
//!
//! - `n` = number of 64-bit limbs in `m`.
//! - `R = 2^(n*64)`.
//! - `r_mod_m = R mod m` (represents `1` in Montgomery form).
//! - `r_squared = R² mod m` (used by `to_mont` to convert via a single REDC).
//! - `m_prime = (-m^{-1}) mod 2^64` (a single `u64`, used in REDC loop).
//!
//! # Modulus constraint
//!
//! The modulus must be **odd** and greater than 1. [`MontgomeryContext::new`]
//! returns [`OxiNumError::Domain`] for even moduli.

use super::div::divrem;
use super::uint::BigUint;
use oxinum_core::{OxiNumError, OxiNumResult};

// ---------------------------------------------------------------------------
// MontgomeryContext
// ---------------------------------------------------------------------------

/// Montgomery multiplication context for a fixed odd modulus.
///
/// See the module-level documentation for full background and usage.
///
/// # Examples
///
/// ```
/// use oxinum_int::native::{MontgomeryContext, BigUint};
///
/// let ctx = MontgomeryContext::new(BigUint::from_u64(7)).expect("odd modulus");
///
/// // 3 * 4 mod 7 = 12 mod 7 = 5
/// let a_mont = ctx.to_mont(&BigUint::from_u64(3));
/// let b_mont = ctx.to_mont(&BigUint::from_u64(4));
/// let c_mont = ctx.mul(&a_mont, &b_mont);
/// let c = ctx.from_mont(&c_mont);
/// assert_eq!(c, BigUint::from_u64(5));
/// ```
pub struct MontgomeryContext {
    /// The modulus `m` (always odd and > 1).
    m: BigUint,
    /// `R mod m` — the Montgomery representation of `1`.
    r_mod_m: BigUint,
    /// `R² mod m` — used by `to_mont` to convert `a → a*R mod m` in one REDC.
    r_squared: BigUint,
    /// `(-m^{-1}) mod 2^64` — the REDC loop constant (per-limb Hensel lift).
    m_prime: u64,
    /// Number of 64-bit limbs in `m` (determines `R = 2^(n*64)`).
    n: usize,
}

impl MontgomeryContext {
    /// Create a new `MontgomeryContext` for the given odd modulus.
    ///
    /// # Errors
    ///
    /// Returns [`OxiNumError::DivByZero`] if `m == 0` or `m == 1`.
    /// Returns [`OxiNumError::Domain`] if `m` is even.
    ///
    /// # Examples
    ///
    /// ```
    /// use oxinum_int::native::{MontgomeryContext, BigUint};
    ///
    /// assert!(MontgomeryContext::new(BigUint::from_u64(7)).is_ok());
    /// assert!(MontgomeryContext::new(BigUint::from_u64(10)).is_err()); // even
    /// ```
    pub fn new(m: BigUint) -> OxiNumResult<Self> {
        if m.is_zero() || m.is_one() {
            return Err(OxiNumError::DivByZero);
        }

        let limbs = m.as_limbs();

        // Modulus must be odd: lowest bit of the lowest limb must be 1.
        if limbs[0] & 1 == 0 {
            return Err(OxiNumError::Domain(
                "Montgomery multiplication requires an odd modulus".into(),
            ));
        }

        let n = limbs.len();
        let m0 = limbs[0]; // lowest 64-bit limb of m

        // -----------------------------------------------------------------------
        // Compute m_prime = (-m0^{-1}) mod 2^64 via a 6-step Hensel lift.
        //
        // We want:  m0 * m_prime ≡ -1 (mod 2^64)
        // Equivalently: m_prime = 2^64 - (m0^{-1} mod 2^64).
        //
        // Starting from the observation that m0 * 1 ≡ 1 (mod 2) (m0 is odd),
        // each iteration doubles the number of correct bits:
        //   t_{i+1} = t_i * (2 - m0 * t_i)   (mod 2^{2^i})
        // After 6 steps we have 64 bits.
        // -----------------------------------------------------------------------
        let mut t = 1u64;
        for _ in 0..6 {
            t = t.wrapping_mul(2u64.wrapping_sub(m0.wrapping_mul(t)));
        }
        // t = m0^{-1} mod 2^64.  We need -t mod 2^64.
        let m_prime: u64 = t.wrapping_neg();

        // -----------------------------------------------------------------------
        // Compute R mod m, where R = 2^(n*64).
        // Build the BigUint [0, 0, ..., 0, 1] of n+1 limbs (value = 2^(n*64)),
        // then reduce modulo m.
        // -----------------------------------------------------------------------
        let mut r_limbs = vec![0u64; n + 1];
        r_limbs[n] = 1u64;
        let r_big = BigUint::from_le_limbs(&r_limbs);
        let (_q, r_mod_m) = divrem(&r_big, &m);

        // R² mod m = (R mod m)² mod m.
        let r_mod_m_sq = r_mod_m.clone() * r_mod_m.clone();
        let (_q2, r_squared) = divrem(&r_mod_m_sq, &m);

        Ok(MontgomeryContext {
            m,
            r_mod_m,
            r_squared,
            m_prime,
            n,
        })
    }

    // -----------------------------------------------------------------------
    // Public interface
    // -----------------------------------------------------------------------

    /// Convert `a` (in normal form) to Montgomery form: `a_mont = (a * R) mod m`.
    ///
    /// Implemented as `REDC(a * R²)` so that it costs one REDC call.
    #[inline]
    pub fn to_mont(&self, a: &BigUint) -> BigUint {
        self.mont_mul(a, &self.r_squared)
    }

    /// Convert `a_mont` (in Montgomery form) back to normal form:
    /// `a = (a_mont * R^{-1}) mod m`.
    ///
    /// Implemented as `REDC(a_mont * 1)`.
    #[inline]
    pub fn from_mont(&self, a_mont: &BigUint) -> BigUint {
        self.mont_mul(a_mont, &BigUint::one())
    }

    /// Montgomery multiplication: `(a * b * R^{-1}) mod m`.
    ///
    /// Both `a` and `b` must already be in Montgomery form (i.e., values
    /// returned by [`to_mont`](Self::to_mont) or previous calls to `mul`).
    ///
    /// To multiply two normal values `x` and `y` mod `m`:
    /// ```
    /// # use oxinum_int::native::{MontgomeryContext, BigUint};
    /// # let ctx = MontgomeryContext::new(BigUint::from_u64(7)).unwrap();
    /// # let (x, y) = (BigUint::from_u64(3), BigUint::from_u64(4));
    /// let a = ctx.to_mont(&x);
    /// let b = ctx.to_mont(&y);
    /// let c_mont = ctx.mul(&a, &b);
    /// let c = ctx.from_mont(&c_mont);   // c ≡ x * y (mod 7)
    /// assert_eq!(c, BigUint::from_u64(5));
    /// ```
    #[inline]
    pub fn mul(&self, a: &BigUint, b: &BigUint) -> BigUint {
        self.mont_mul(a, b)
    }

    /// Modular exponentiation using the Montgomery ladder: `base^exp mod m`.
    ///
    /// `base` is in normal (non-Montgomery) form. The result is also in normal
    /// form.
    ///
    /// # Examples
    ///
    /// ```
    /// use oxinum_int::native::{MontgomeryContext, BigUint};
    ///
    /// // 2^10 mod 13 = 1024 mod 13 = 10
    /// let ctx = MontgomeryContext::new(BigUint::from_u64(13)).expect("odd");
    /// assert_eq!(ctx.pow(&BigUint::from_u64(2), &BigUint::from_u64(10)),
    ///            BigUint::from_u64(10));
    /// ```
    pub fn pow(&self, base: &BigUint, exp: &BigUint) -> BigUint {
        if exp.is_zero() {
            return BigUint::one();
        }

        let base_mont = self.to_mont(base);
        // 1 in Montgomery form = r_mod_m (since 1 * R mod m = R mod m).
        let mut result_mont = self.r_mod_m.clone();
        let mut current_mont = base_mont;

        let bits = exp.bit_length();
        for i in 0..bits {
            if exp.test_bit(i) {
                result_mont = self.mont_mul(&result_mont, &current_mont);
            }
            if i + 1 < bits {
                current_mont = self.mont_mul(&current_mont, &current_mont);
            }
        }

        self.from_mont(&result_mont)
    }

    /// Returns a reference to the modulus.
    #[inline]
    pub fn modulus(&self) -> &BigUint {
        &self.m
    }

    // -----------------------------------------------------------------------
    // REDC — Montgomery reduction
    // -----------------------------------------------------------------------

    /// REDC: computes `(a * b * R^{-1}) mod m`.
    ///
    /// Algorithm (adapted for pure-BigUint operations):
    ///
    /// 1. `t = a * b`
    /// 2. For each limb index `i` in `0..n`:
    ///    - Extract the `i`-th 64-bit limb of `t`: `t_i = (t >> (64*i)) & (2^64-1)`.
    ///    - Compute `q_i = t_i * m_prime  (wrapping, mod 2^64)`.
    ///    - `t += q_i * m * 2^(64*i)`.
    /// 3. `t >>= n * 64`.
    /// 4. If `t >= m`: `t -= m`.
    ///
    /// This is O(n²) in BigUint operations, which is acceptable for the current
    /// implementation. A native limb-array REDC operating in O(n) additional
    /// operations is deferred to a later optimization pass.
    fn mont_mul(&self, a: &BigUint, b: &BigUint) -> BigUint {
        let mut t: BigUint = a.clone() * b.clone();

        for i in 0..self.n {
            // Extract the i-th 64-bit limb of t.
            // Strategy: shift t right by i*64 bits, then take the lowest limb.
            let shift = (i as u64) * 64;
            let t_shifted = t.shr_bits(shift);
            let t_i: u64 = match t_shifted.as_limbs() {
                [] => 0u64,
                [lo, ..] => *lo,
            };

            // q_i = t_i * m_prime  (mod 2^64, wrapping)
            let q_i: u64 = t_i.wrapping_mul(self.m_prime);

            if q_i != 0 {
                // addition = q_i * m * 2^(i*64)
                let q_big = BigUint::from_u64(q_i);
                let addition = (q_big * self.m.clone()).shl_bits(shift);
                // t += addition  (using Add which is non-negative)
                t += addition;
            }
            // Note: even when q_i == 0, the loop still moves forward because
            // the subsequent right-shift at step 3 handles the accumulated t.
        }

        // Step 3: t >>= n * 64
        t = t.shr_bits((self.n as u64) * 64);

        // Step 4: conditional subtraction to bring t into [0, m).
        if t >= self.m {
            t = t.checked_sub(&self.m).unwrap_or_else(BigUint::zero);
        }

        t
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use crate::native::{mod_mul, mod_pow};

    fn bu(n: u64) -> BigUint {
        BigUint::from_u64(n)
    }

    #[test]
    fn new_rejects_zero() {
        assert!(MontgomeryContext::new(bu(0)).is_err());
    }

    #[test]
    fn new_rejects_one() {
        assert!(MontgomeryContext::new(bu(1)).is_err());
    }

    #[test]
    fn new_rejects_even() {
        assert!(MontgomeryContext::new(bu(10)).is_err());
        assert!(MontgomeryContext::new(bu(2)).is_err());
        assert!(MontgomeryContext::new(bu(100)).is_err());
    }

    #[test]
    fn new_accepts_odd() {
        assert!(MontgomeryContext::new(bu(7)).is_ok());
        assert!(MontgomeryContext::new(bu(13)).is_ok());
        assert!(MontgomeryContext::new(bu(65537)).is_ok());
    }

    #[test]
    fn roundtrip_to_from_mont() {
        let ctx = MontgomeryContext::new(bu(7)).expect("odd");
        for a in 0u64..7 {
            let a_mont = ctx.to_mont(&bu(a));
            let a_back = ctx.from_mont(&a_mont);
            assert_eq!(a_back, bu(a), "roundtrip failed for a={a}");
        }
    }

    #[test]
    fn mul_basic_3x4_mod7() {
        let ctx = MontgomeryContext::new(bu(7)).expect("odd");
        let a_mont = ctx.to_mont(&bu(3));
        let b_mont = ctx.to_mont(&bu(4));
        let c_mont = ctx.mul(&a_mont, &b_mont);
        let c = ctx.from_mont(&c_mont);
        assert_eq!(c, bu(5)); // 3 * 4 = 12 ≡ 5 (mod 7)
    }

    #[test]
    fn pow_2_10_mod13() {
        let ctx = MontgomeryContext::new(bu(13)).expect("odd");
        let result = ctx.pow(&bu(2), &bu(10));
        // 2^10 = 1024 = 78*13 + 10
        assert_eq!(result, bu(10));
    }

    #[test]
    fn pow_zero_exp() {
        let ctx = MontgomeryContext::new(bu(7)).expect("odd");
        // Any base to the 0 = 1
        assert_eq!(ctx.pow(&bu(5), &bu(0)), bu(1));
    }

    #[test]
    fn fermat_little_theorem() {
        // For prime p and a not divisible by p: a^(p-1) ≡ 1 (mod p)
        for &p in &[7u64, 13, 101, 65537] {
            let ctx = MontgomeryContext::new(bu(p)).expect("odd prime");
            for a in 2u64..p.min(8) {
                let result = ctx.pow(&bu(a), &bu(p - 1));
                assert_eq!(result, bu(1), "Fermat failed for a={a}, p={p}");
            }
        }
    }

    #[test]
    fn montgomery_vs_schoolbook() {
        // For a variety of odd moduli, check that Montgomery mul == schoolbook mod_mul.
        let odd_moduli = [7u64, 13, 101, 4093, 65537];
        for &m in &odd_moduli {
            let ctx = MontgomeryContext::new(bu(m)).expect("ctx");
            for a in [0u64, 1, 2, m - 1, m / 2 + 1] {
                for b in [0u64, 1, 3, m - 1, (m / 2).saturating_add(1)] {
                    let a = a.min(m - 1);
                    let b = b.min(m - 1);
                    let expected = mod_mul(&bu(a), &bu(b), &bu(m)).expect("schoolbook");
                    let a_mont = ctx.to_mont(&bu(a));
                    let b_mont = ctx.to_mont(&bu(b));
                    let got_mont = ctx.mul(&a_mont, &b_mont);
                    let got = ctx.from_mont(&got_mont);
                    assert_eq!(
                        got, expected,
                        "Montgomery vs schoolbook mismatch: a={a}, b={b}, m={m}"
                    );
                }
            }
        }
    }

    #[test]
    fn montgomery_pow_vs_mod_pow() {
        // Cross-validate Montgomery pow against schoolbook mod_pow.
        let odd_moduli = [7u64, 13, 101, 65537];
        let exps = [0u64, 1, 2, 10, 100, 1000];
        for &m in &odd_moduli {
            let ctx = MontgomeryContext::new(bu(m)).expect("ctx");
            for a in [1u64, 2, 3, m - 1] {
                for &e in &exps {
                    let expected = mod_pow(&bu(a), &bu(e), &bu(m)).expect("mod_pow");
                    let got = ctx.pow(&bu(a), &bu(e));
                    assert_eq!(
                        got, expected,
                        "Montgomery pow vs mod_pow: a={a}, e={e}, m={m}"
                    );
                }
            }
        }
    }
}