1use alloc::vec::Vec;
2use core::mem;
3use core::ops::Shl;
4use num_traits::One;
56use crate::big_digit::{self, BigDigit, DoubleBigDigit};
7use crate::biguint::BigUint;
89struct MontyReducer {
10 n0inv: BigDigit,
11}
1213// k0 = -m**-1 mod 2**BITS. Algorithm from: Dumas, J.G. "On Newton–Raphson
14// Iteration for Multiplicative Inverses Modulo Prime Powers".
15fn inv_mod_alt(b: BigDigit) -> BigDigit {
16match (&(b & 1), &0) {
(left_val, right_val) => {
if *left_val == *right_val {
let kind = ::core::panicking::AssertKind::Ne;
::core::panicking::assert_failed(kind, &*left_val, &*right_val,
::core::option::Option::None);
}
}
};assert_ne!(b & 1, 0);
1718let mut k0 = BigDigit::wrapping_sub(2, b);
19let mut t = b - 1;
20let mut i = 1;
21while i < big_digit::BITS {
22 t = t.wrapping_mul(t);
23 k0 = k0.wrapping_mul(t + 1);
2425 i <<= 1;
26 }
27if true {
match (&k0.wrapping_mul(b), &1) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val,
&*right_val, ::core::option::Option::None);
}
}
};
};debug_assert_eq!(k0.wrapping_mul(b), 1);
28k0.wrapping_neg()
29}
3031impl MontyReducer {
32fn new(n: &BigUint) -> Self {
33let n0inv = inv_mod_alt(n.data[0]);
34MontyReducer { n0inv }
35 }
36}
3738/// Computes z mod m = x * y * 2 ** (-n*_W) mod m
39/// assuming k = -1/m mod 2**_W
40/// See Gueron, "Efficient Software Implementations of Modular Exponentiation".
41/// <https://eprint.iacr.org/2011/239.pdf>
42/// In the terminology of that paper, this is an "Almost Montgomery Multiplication":
43/// x and y are required to satisfy 0 <= z < 2**(n*_W) and then the result
44/// z is guaranteed to satisfy 0 <= z < 2**(n*_W), but it may not be < m.
45#[allow(clippy::many_single_char_names)]
46fn montgomery(x: &BigUint, y: &BigUint, m: &BigUint, k: BigDigit, n: usize) -> BigUint {
47// This code assumes x, y, m are all the same length, n.
48 // (required by addMulVVW and the for loop).
49 // It also assumes that x, y are already reduced mod m,
50 // or else the result will not be properly reduced.
51if !(x.data.len() == n && y.data.len() == n && m.data.len() == n) {
{
::core::panicking::panic_fmt(format_args!("{0:?} {1:?} {2:?} {3}", x,
y, m, n));
}
};assert!(
52 x.data.len() == n && y.data.len() == n && m.data.len() == n,
53"{:?} {:?} {:?} {}",
54 x,
55 y,
56 m,
57 n
58 );
5960let mut z = BigUint::ZERO;
61z.data.resize(n * 2, 0);
6263let mut c: BigDigit = 0;
64for i in 0..n {
65let c2 = add_mul_vvw(&mut z.data[i..n + i], &x.data, y.data[i]);
66let t = z.data[i].wrapping_mul(k);
67let c3 = add_mul_vvw(&mut z.data[i..n + i], &m.data, t);
68let cx = c.wrapping_add(c2);
69let cy = cx.wrapping_add(c3);
70 z.data[n + i] = cy;
71if cx < c2 || cy < c3 {
72 c = 1;
73 } else {
74 c = 0;
75 }
76 }
7778if c == 0 {
79z.data = z.data[n..].to_vec();
80 } else {
81 {
82let (first, second) = z.data.split_at_mut(n);
83sub_vv(first, second, &m.data);
84 }
85z.data = z.data[..n].to_vec();
86 }
8788z89}
9091#[inline(always)]
92fn add_mul_vvw(z: &mut [BigDigit], x: &[BigDigit], y: BigDigit) -> BigDigit {
93let mut c = 0;
94for (zi, xi) in z.iter_mut().zip(x.iter()) {
95let (z1, z0) = mul_add_www(*xi, y, *zi);
96let (c_, zi_) = add_ww(z0, c, 0);
97*zi = zi_;
98 c = c_ + z1;
99 }
100101c102}
103104/// The resulting carry c is either 0 or 1.
105#[inline(always)]
106fn sub_vv(z: &mut [BigDigit], x: &[BigDigit], y: &[BigDigit]) -> BigDigit {
107let mut c = 0;
108for (i, (xi, yi)) in x.iter().zip(y.iter()).enumerate().take(z.len()) {
109let zi = xi.wrapping_sub(*yi).wrapping_sub(c);
110 z[i] = zi;
111// see "Hacker's Delight", section 2-12 (overflow detection)
112c = ((yi & !xi) | ((yi | !xi) & zi)) >> (big_digit::BITS - 1)
113 }
114115c116}
117118/// z1<<_W + z0 = x+y+c, with c == 0 or 1
119#[inline(always)]
120fn add_ww(x: BigDigit, y: BigDigit, c: BigDigit) -> (BigDigit, BigDigit) {
121let yc = y.wrapping_add(c);
122let z0 = x.wrapping_add(yc);
123let z1 = if z0 < x || yc < y { 1 } else { 0 };
124125 (z1, z0)
126}
127128/// z1 << _W + z0 = x * y + c
129#[inline(always)]
130fn mul_add_www(x: BigDigit, y: BigDigit, c: BigDigit) -> (BigDigit, BigDigit) {
131let z = xas DoubleBigDigit * yas DoubleBigDigit + cas DoubleBigDigit;
132 ((z >> big_digit::BITS) as BigDigit, zas BigDigit)
133}
134135/// Calculates x ** y mod m using a fixed, 4-bit window.
136#[allow(clippy::many_single_char_names)]
137pub(super) fn monty_modpow(x: &BigUint, y: &BigUint, m: &BigUint) -> BigUint {
138if !(m.data[0] & 1 == 1) {
::core::panicking::panic("assertion failed: m.data[0] & 1 == 1")
};assert!(m.data[0] & 1 == 1);
139let mr = MontyReducer::new(m);
140let num_words = m.data.len();
141142let mut x = x.clone();
143144// We want the lengths of x and m to be equal.
145 // It is OK if x >= m as long as len(x) == len(m).
146if x.data.len() > num_words {
147x %= m;
148// Note: now len(x) <= numWords, not guaranteed ==.
149}
150if x.data.len() < num_words {
151x.data.resize(num_words, 0);
152 }
153154// rr = 2**(2*_W*len(m)) mod m
155let mut rr = BigUint::one();
156rr = (rr.shl(2 * num_wordsas u64 * u64::from(big_digit::BITS))) % m;
157if rr.data.len() < num_words {
158rr.data.resize(num_words, 0);
159 }
160// one = 1, with equal length to that of m
161let mut one = BigUint::one();
162one.data.resize(num_words, 0);
163164let n = 4;
165// powers[i] contains x^i
166let mut powers = Vec::with_capacity(1 << n);
167powers.push(montgomery(&one, &rr, m, mr.n0inv, num_words));
168powers.push(montgomery(&x, &rr, m, mr.n0inv, num_words));
169for i in 2..1 << n {
170let r = montgomery(&powers[i - 1], &powers[1], m, mr.n0inv, num_words);
171 powers.push(r);
172 }
173174// initialize z = 1 (Montgomery 1)
175let mut z = powers[0].clone();
176z.data.resize(num_words, 0);
177let mut zz = BigUint::ZERO;
178zz.data.resize(num_words, 0);
179180// same windowed exponent, but with Montgomery multiplications
181for i in (0..y.data.len()).rev() {
182let mut yi = y.data[i];
183let mut j = 0;
184while j < big_digit::BITS {
185if i != y.data.len() - 1 || j != 0 {
186 zz = montgomery(&z, &z, m, mr.n0inv, num_words);
187 z = montgomery(&zz, &zz, m, mr.n0inv, num_words);
188 zz = montgomery(&z, &z, m, mr.n0inv, num_words);
189 z = montgomery(&zz, &zz, m, mr.n0inv, num_words);
190 }
191 zz = montgomery(
192&z,
193&powers[(yi >> (big_digit::BITS - n)) as usize],
194 m,
195 mr.n0inv,
196 num_words,
197 );
198 mem::swap(&mut z, &mut zz);
199 yi <<= n;
200 j += n;
201 }
202 }
203204// convert to regular number
205zz = montgomery(&z, &one, m, mr.n0inv, num_words);
206207zz.normalize();
208// One last reduction, just in case.
209 // See golang.org/issue/13907.
210if zz >= *m {
211// Common case is m has high bit set; in that case,
212 // since zz is the same length as m, there can be just
213 // one multiple of m to remove. Just subtract.
214 // We think that the subtract should be sufficient in general,
215 // so do that unconditionally, but double-check,
216 // in case our beliefs are wrong.
217 // The div is not expected to be reached.
218zz -= m;
219if zz >= *m {
220zz %= m;
221 }
222 }
223224zz.normalize();
225zz226}