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

Skip to content

Commit 4c2e503

Browse files
authored
Add article about Integer factorization (#413)
1 parent f668d9c commit 4c2e503

File tree

10 files changed

+535
-316
lines changed

10 files changed

+535
-316
lines changed

img/pollard_rho.png

8.92 KB
Loading

src/algebra/factorization.md

Lines changed: 424 additions & 0 deletions
Large diffs are not rendered by default.

src/algebra/montgomery_multiplication.md

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

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-
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.
65
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.
76
And division is a really expensive operation, especially with big numbers.
87

98
The **Montgomery (modular) multiplication** is a method that allows computing such multiplications faster.
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.
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.
1110

1211
## Montgomery representation
1312

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

1817
For the space we need a positive integer $r \ge n$ coprime to $n$, i.e. $\gcd(n, r) = 1$.
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.
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.
2019
$n$ will be an odd number in pretty much all applications, since it is not hard to factorize an even number.
2120
So every power of $2$ will be coprime to $n$.
2221

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

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

5452
Using this identity we can write $x \cdot r^{-1}$ as:
55-
$$\begin{aligned}
53+
$$\begin{array}{rl}
5654
x \cdot r^{-1} &= x \cdot r \cdot r^{-1} / r = x \cdot (-n \cdot n^{\prime} + 1) / r \\\\
5755
&= (-x \cdot n \cdot n^{\prime} + x) / r \equiv (-x \cdot n \cdot n^{\prime} + l \cdot r \cdot n + x) / r \bmod n\\\\
5856
&\equiv ((-x \cdot n^{\prime} + l \cdot r) \cdot n + x) / r \bmod n\\\\
59-
\end{aligned}$$
57+
\end{array}$$
6058

6159
The equivalences hold for any arbitrary integer $l$.
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$.
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$.
6361

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

66-
```text
64+
```
6765
function reduce(x):
6866
q = (x mod r) * n' mod r
6967
a = (x - q * n) / r
7068
if a < 0:
7169
a += n
7270
return a
71+
endfunction
7372
```
7473

7574
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$.
@@ -86,13 +85,13 @@ For computing the inverse $n^{\prime} := n^{-1} \bmod r$ efficiently, we can use
8685
$$a \cdot x \equiv 1 \bmod 2^k \Longrightarrow a \cdot x \cdot (2 - a \cdot x) \equiv 1 \bmod 2^{2k}$$
8786
This can easily be proven.
8887
If we have $a \cdot x = 1 + m \cdot 2^k$, then we have:
89-
$$\begin{aligned}
88+
$$\begin{array}{rl}
9089
a \cdot x \cdot (2 - a \cdot x) &= 2 \cdot a \cdot x - (a \cdot x)^2 \\\\
9190
&= 2 \cdot (1 + m \cdot 2^k) - (1 + m \cdot 2^k)^2 \\\\
9291
&= 2 + 2 \cdot m \cdot 2^k - 1 - 2 \cdot m \cdot 2^k - m^2 \cdot 2^{2k} \\\\
9392
&= 1 - m^2 \cdot 2^{2k} \\\\
9493
&\equiv 1 \bmod 2^{2k}.
95-
\end{aligned}$$
94+
\end{array}$$
9695

9796
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$.
9897

@@ -170,27 +169,19 @@ There are faster ways.
170169
171170
You can notice the following relation:
172171
$$\bar{x} := x \cdot r \bmod n = x \cdot r^2 / r = x * r^2$$
173-
174-
Transforming a number into the space is just a multiplication inside the space of the number with $r^2$.
172+
Transforming a number into spaces is just a multiplication inside the space of the number with $r^2$.
175173
Therefore we can precompute $r^2 \bmod n$ and just perform a multiplication instead of shifting the number 128 times.
176174
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-
181175
```
182176
struct Montgomery {
183-
Montgomery(u128 n) : mod(n), inv(1), r2(-n % n) {
177+
Montgomery(u128 n) : mod(n), inv(1), r2(1) {
184178
for (int i = 0; i < 7; i++)
185179
inv *= 2 - n * inv;
186-
187-
for (int i = 0; i < 4; i++) {
180+
for (int i = 0; i < 256; i++) {
188181
r2 <<= 1;
189182
if (r2 >= mod)
190183
r2 -= mod;
191184
}
192-
for (int i = 0; i < 5; i++)
193-
r2 = mul(r2, r2);
194185
}
195186

196187
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
60+
## Application in Euler's theorem ## {#application}
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)