Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit becfdf1

Browse files
committed
Revert "Add article about Integer factorization (#413)"
This reverts commit 4c2e503.
1 parent 4c2e503 commit becfdf1

File tree

10 files changed

+316
-535
lines changed

10 files changed

+316
-535
lines changed

img/pollard_rho.png

-8.92 KB
Binary file not shown.

src/algebra/factorization.md

Lines changed: 0 additions & 424 deletions
This file was deleted.

src/algebra/montgomery_multiplication.md

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
<!--?title Montgomery Multiplication -->
22
# Montgomery Multiplication
33

4-
Many algorithms in number theory, like [prime testing](./algebra/primality_tests.html) or [integer factorization](./algebra/factorization.html), and in cryptography, like RSA, require lots of operations modulo a large number.
4+
Many algorithms in number theory, like [prime testing](./algebra/primality_tests.html) or factorization, and in cryptography, like RSA, require lots of operations modulo a large number.
5+
56
A multiplications like $x y \bmod{n}$ is quite slow to compute with the typical algorithms, since it requires a division to know how many times $n$ has to be subtracted from the product.
67
And division is a really expensive operation, especially with big numbers.
78

89
The **Montgomery (modular) multiplication** is a method that allows computing such multiplications faster.
9-
Instead of dividing the product and subtracting the $n$ multiple times, it adds multiples of $n$ to cancel out the lower bits and then just discards the lower bits.
10+
Instead of dividing the product and subtracting $n$ multiple times, it adds multiples of $n$ to cancel out the lower bits and then just discards the lower bits.
1011

1112
## Montgomery representation
1213

@@ -15,7 +16,7 @@ The algorithm works only in the **Montgomery space**.
1516
And we need to transform our numbers into that space, before we can start multiplying.
1617

1718
For the space we need a positive integer $r \ge n$ coprime to $n$, i.e. $\gcd(n, r) = 1$.
18-
In practice we always choose $r$ to be $2^m$ for a positive integer $m$, since multiplications, divisions and modulo $r$ operations can then be efficiently implemented using shifts and bit operations.
19+
In practice we always choose $r$ to be $2^m$ for a positive integer $m$, since multiplications, divisions and modulo $r$ operations can then be efficiently implemented using shifts and other bit operations.
1920
$n$ will be an odd number in pretty much all applications, since it is not hard to factorize an even number.
2021
So every power of $2$ will be coprime to $n$.
2122

@@ -33,7 +34,8 @@ You can add two elements ($x \cdot r + y \cdot r \equiv (x + y) \cdot r \bmod n$
3334
All with the usual algorithms.
3435

3536
However this is not the case for multiplication.
36-
We expect the result:
37+
38+
We expect the result to be:
3739
$$\bar{x} * \bar{y} = \overline{x \cdot y} = (x \cdot y) \cdot r \bmod n.$$
3840
But the normal multiplication will give us:
3941
$$\bar{x} \cdot \bar{y} = (x \cdot y) \cdot r \cdot r \bmod n.$$
@@ -50,25 +52,24 @@ $$r \cdot r^{-1} + n \cdot n^{\prime} = 1.$$
5052
Both $r^{-1}$ and $n^{\prime}$ can be computed using the [Extended Euclidean algorithm](./algebra/extended-euclid-algorithm.html).
5153

5254
Using this identity we can write $x \cdot r^{-1}$ as:
53-
$$\begin{array}{rl}
55+
$$\begin{aligned}
5456
x \cdot r^{-1} &= x \cdot r \cdot r^{-1} / r = x \cdot (-n \cdot n^{\prime} + 1) / r \\\\
5557
&= (-x \cdot n \cdot n^{\prime} + x) / r \equiv (-x \cdot n \cdot n^{\prime} + l \cdot r \cdot n + x) / r \bmod n\\\\
5658
&\equiv ((-x \cdot n^{\prime} + l \cdot r) \cdot n + x) / r \bmod n\\\\
57-
\end{array}$$
59+
\end{aligned}$$
5860

5961
The equivalences hold for any arbitrary integer $l$.
60-
This means, that we can add an arbitrary multiple of $r$ to $x \cdot n^{\prime}$, or in other words, we can compute $q := x \cdot n^{\prime}$ modulo $r$.
62+
This means, that we can add or subtract an arbitrary multiple of $r$ to $x \cdot n^{\prime}$, or in other words, we can compute $q := x \cdot n^{\prime}$ modulo $r$.
6163

6264
This gives us the following algorithm to compute $x \cdot r^{-1} \bmod n$:
6365

64-
```
66+
```text
6567
function reduce(x):
6668
q = (x mod r) * n' mod r
6769
a = (x - q * n) / r
6870
if a < 0:
6971
a += n
7072
return a
71-
endfunction
7273
```
7374

7475
Since $x < n \cdot n < r \cdot n$ (even if $x$ is the product of a multiplication) and $q \cdot n < r \cdot n$ we know that $-n < (x - q \cdot n) / r < n$.
@@ -85,13 +86,13 @@ For computing the inverse $n^{\prime} := n^{-1} \bmod r$ efficiently, we can use
8586
$$a \cdot x \equiv 1 \bmod 2^k \Longrightarrow a \cdot x \cdot (2 - a \cdot x) \equiv 1 \bmod 2^{2k}$$
8687
This can easily be proven.
8788
If we have $a \cdot x = 1 + m \cdot 2^k$, then we have:
88-
$$\begin{array}{rl}
89+
$$\begin{aligned}
8990
a \cdot x \cdot (2 - a \cdot x) &= 2 \cdot a \cdot x - (a \cdot x)^2 \\\\
9091
&= 2 \cdot (1 + m \cdot 2^k) - (1 + m \cdot 2^k)^2 \\\\
9192
&= 2 + 2 \cdot m \cdot 2^k - 1 - 2 \cdot m \cdot 2^k - m^2 \cdot 2^{2k} \\\\
9293
&= 1 - m^2 \cdot 2^{2k} \\\\
9394
&\equiv 1 \bmod 2^{2k}.
94-
\end{array}$$
95+
\end{aligned}$$
9596

9697
This means we can start with $x = 1$ as the inverse of $a$ modulo $2^1$, apply the trick a few times and in each iteration we double the number of correct bits of $x$.
9798

@@ -169,19 +170,27 @@ There are faster ways.
169170
170171
You can notice the following relation:
171172
$$\bar{x} := x \cdot r \bmod n = x \cdot r^2 / r = x * r^2$$
172-
Transforming a number into spaces is just a multiplication inside the space of the number with $r^2$.
173+
174+
Transforming a number into the space is just a multiplication inside the space of the number with $r^2$.
173175
Therefore we can precompute $r^2 \bmod n$ and just perform a multiplication instead of shifting the number 128 times.
174176
177+
In the following code we initialize `r2` with `-n % n`, which is equivalent to $r - n \equiv r \bmod n$, shift it 4 times to get $r \cdot 2^4 \bmod n$.
178+
This number can be interpreted as $2^4$ in Montgomery space.
179+
If we square it $5$ times, we get $(2^4)^{2^5} = (2^4)^{32} = 2^{128} = r$ in Montgomery space, which is exactly $r^2 \bmod n$.
180+
175181
```
176182
struct Montgomery {
177-
Montgomery(u128 n) : mod(n), inv(1), r2(1) {
183+
Montgomery(u128 n) : mod(n), inv(1), r2(-n % n) {
178184
for (int i = 0; i < 7; i++)
179185
inv *= 2 - n * inv;
180-
for (int i = 0; i < 256; i++) {
186+
187+
for (int i = 0; i < 4; i++) {
181188
r2 <<= 1;
182189
if (r2 >= mod)
183190
r2 -= mod;
184191
}
192+
for (int i = 0; i < 5; i++)
193+
r2 = mul(r2, r2);
185194
}
186195

187196
u128 init(u128 x) {

src/algebra/phi-function.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ int phi(int n) {
5757
}
5858
```
5959
60-
## Application in Euler's theorem ## {#application}
60+
## Application in Euler's theorem
6161
6262
The most famous and important property of Euler's totient function is expressed in **Euler's theorem**:
6363
$$a^{\phi(m)} \equiv 1 \pmod m$$ if $a$ and $m$ are relatively prime.

0 commit comments

Comments
 (0)