Professional Documents
Culture Documents
Jean-Sébastien Coron
University of Luxembourg
with 0 ≤ ai < B.
If a 6= 0, we must have ak−1 6= 0.
Choice of B
One generally takes B = 2v for some v .
carry ← 0
for i = 0 to ` − 1 do
tmp ← ai + bi + carry
carry ← btmp/Bc; ci ← tmp mod B
for i = ` to k − 1 do
tmp ← ai + carry
carry ← btmp/Bc; ci ← tmp mod B
ck ← carry
In every loop iteration
0 ≤ tmp ≤ 2B − 1, carry ∈ {0, 1}.
Complexity: O(k)
carry ← 0
for i = 0 to ` − 1 do
tmp ← ai + bi + carry
carry ← btmp/Bc; ci ← tmp mod B
for i = ` to k − 1 do
tmp ← ai + carry
carry ← btmp/Bc; ci ← tmp mod B
ck ← carry
In every loop iteration
0 ≤ tmp ≤ 2B − 1, carry ∈ {0, 1}.
Complexity: O(k)
→ carry ← 0
for i = 0 to ` − 1 do
tmp ← ai + bi + carry
carry ← btmp/Bc; ci ← tmp mod B
for i = ` to k − 1 do
tmp ← ai + carry
carry ← btmp/Bc; ci ← tmp mod B
ck ← carry
ai 6 4 7 k=3
bi 8 5 `=2
ci
i tmp carry 0
Jean-Sébastien Coron Computing with large integers
Addition: example in base B = 10
carry ← 0
for i = 0 to ` − 1 do
→ tmp ← ai + bi + carry
carry ← btmp/Bc; ci ← tmp mod B
for i = ` to k − 1 do
tmp ← ai + carry
carry ← btmp/Bc; ci ← tmp mod B
ck ← carry
↓
ai 6 4 7 k=3
bi 8 5 `=2
ci
i 0 tmp 12 carry 0
Jean-Sébastien Coron Computing with large integers
Addition: example in base B = 10
carry ← 0
for i = 0 to ` − 1 do
tmp ← ai + bi + carry
→ carry ← btmp/Bc; ci ← tmp mod B
for i = ` to k − 1 do
tmp ← ai + carry
carry ← btmp/Bc; ci ← tmp mod B
ck ← carry
↓
ai 6 4 7 k=3
bi 8 5 `=2
ci 2
i 0 tmp 12 carry 1
Jean-Sébastien Coron Computing with large integers
Addition: example in base B = 10
carry ← 0
for i = 0 to ` − 1 do
→ tmp ← ai + bi + carry
carry ← btmp/Bc; ci ← tmp mod B
for i = ` to k − 1 do
tmp ← ai + carry
carry ← btmp/Bc; ci ← tmp mod B
ck ← carry
↓
ai 6 4 7 k=3
bi 8 5 `=2
ci 2
i 1 tmp 13 carry 1
Jean-Sébastien Coron Computing with large integers
Addition: example in base B = 10
carry ← 0
for i = 0 to ` − 1 do
tmp ← ai + bi + carry
→ carry ← btmp/Bc; ci ← tmp mod B
for i = ` to k − 1 do
tmp ← ai + carry
carry ← btmp/Bc; ci ← tmp mod B
ck ← carry
↓
ai 6 4 7 k=3
bi 8 5 `=2
ci 3 2
i 1 tmp 13 carry 1
Jean-Sébastien Coron Computing with large integers
Addition: example in base B = 10
carry ← 0
for i = 0 to ` − 1 do
tmp ← ai + bi + carry
carry ← btmp/Bc; ci ← tmp mod B
for i = ` to k − 1 do
→ tmp ← ai + carry
carry ← btmp/Bc; ci ← tmp mod B
ck ← carry
↓
ai 6 4 7 k=3
bi 8 5 `=2
ci 3 2
i 2 tmp 7 carry 1
Jean-Sébastien Coron Computing with large integers
Addition: example in base B = 10
carry ← 0
for i = 0 to ` − 1 do
tmp ← ai + bi + carry
carry ← btmp/Bc; ci ← tmp mod B
for i = ` to k − 1 do
tmp ← ai + carry
→ carry ← btmp/Bc; ci ← tmp mod B
ck ← carry
↓
ai 6 4 7 k=3
bi 8 5 `=2
ci 7 3 2
i 2 tmp 7 carry 0
Jean-Sébastien Coron Computing with large integers
Addition: example in base B = 10
carry ← 0
for i = 0 to ` − 1 do
tmp ← ai + bi + carry
carry ← btmp/Bc; ci ← tmp mod B
for i = ` to k − 1 do
tmp ← ai + carry
carry ← btmp/Bc; ci ← tmp mod B
→ ck ← carry
↓
ai 6 4 7 k=3
bi 8 5 `=2
ci 0 7 3 2
i tmp 7 carry 0
Jean-Sébastien Coron Computing with large integers
Subtraction
Schoolbook method
5 3 2
× 8 3 5
2 6 6 0
1 5 9 6
4 2 5 6
4 4 4 2 2 0
Drawback: storage of intermediate results
Space complexity O(n2 ) for n digits
We can do much better by accumulating
the intermediate results
ai 3 7 k=2
bi 8 5 `=2
ci
i j tmp carry 0
ai 3 7 k=2
bi 8 5 `=2
ci 0 0 0 0
i j tmp carry 0
bi 8 5 `=2
ci 0 0 0 0
i 0 j tmp carry 0
ci 0 0 0 0
i 0 j 0 tmp 35 carry 0
ci 0 0 0 5
i 0 j 0 tmp 35 carry 3
ci 0 0 0 5
i 0 j 1 tmp 59 carry 3
ci 0 0 9 5
i 0 j 1 tmp 59 carry 5
bi 8 5 `=2
ci 0 5 9 5
i 0 j tmp 59 carry 5
bi 8 5 `=2
ci 0 5 9 5
i 1 j tmp 59 carry 0
ci 0 5 9 5
i 1 j 0 tmp 24 carry 0
ci 0 5 4 5
i 1 j 0 tmp 24 carry 2
ci 0 5 4 5
i 1 j 1 tmp 31 carry 2
ci 0 1 4 5
i 1 j 1 tmp 31 carry 3
ai 3 7 k=2
bi 8 5 `=2
ci 3 1 4 5
i 1 j tmp 31 carry 3
Euclidean division
Given a ≥ 0 and b > 0, compute q and r such that
a = b · q + r, 0≤r <b
Algorithm overview
Input: a = (ak−1 . . . a0 )B and b = (b`−1 . . . b0 )B with b`−1 6= 0.
Output: q = (qm−1 . . . q0 )B with m := k − ` + 1, and r
r ←a
for i = m − 1 downto 0 do
qi ← br /(B i b)c
r ← r − B i · qi · b
output (q, r )
Euclidean division:
Input: a = (ak−1 . . . a0 )B and b = (b`−1 . . . b0 )B with b`−1 6= 0.
Output: q = (qm−1 . . . q0 )B with m := k − ` + 1, and r
r ←a
for i = m − 1 downto 0 do
qi ← br /(B i b)c
r ← r − B i · qi · b
output (q, r )
Property
One can show inductively that 0 ≤ r < B i · b after step i
Therefore, 0 ≤ r < b eventually.
Euclidean division:
Input: a = (ak−1 . . . a0 )B and b = (b`−1 . . . b0 )B with b`−1 6= 0.
Output: q = (qm−1 . . . q0 )B with m := k − ` + 1, and r
r ←a
for i = m − 1 downto 0 do
qi ← br /(B i b)c
r ← r − B i · qi · b
output (q, r )
How to compute qi = br /(B i · b)c
Test all possible values of 0 ≤ qi < B
Not efficient, except if B is small.
Possible to do much better, by predicting qi from the most
significant digits of r and b;
see Shoup’s book.
Binary Euclidean division algorithm
We assume B = 2v and first convert
a, b to binary representation (B = 2)
Euclidean division:
Input: a = (ak−1 . . . a0 )B and b = (b`−1 . . . b0 )B with b`−1 6= 0.
Output: q = (qm−1 . . . q0 )B with m := k − ` + 1, and r
r ←a
for i = m − 1 downto 0 do
qi ← br /(B i b)c
r ← r − B i · qi · b
output (q, r )
How to compute qi = br /(B i · b)c
Test all possible values of 0 ≤ qi < B
Not efficient, except if B is small.
Possible to do much better, by predicting qi from the most
significant digits of r and b;
see Shoup’s book.
Binary Euclidean division algorithm
We assume B = 2v and first convert
a, b to binary representation (B = 2)
Final step
(
B0 = b
c0 = ab (mod n)
Computing a + b mod n
First compute a + b in Z, then reduce modulo n
Complexity: O(len(n))
Computing a · b mod n
First compute a · b in Z, then reduce modulo n
Complexity: O(len(n)2 )
Computing ab mod n
Complexity: O(len(n)3 )
1 ∗ 1 n−1
|Ln | = |Z | ≤ |Z∗n | ≤
m n 2 2
h
L0n = {a ∈ Z∗n : am2 = 1 and
j+1 j
for j = 0, . . . , h − 1, am2 = 1 implies am2 = ±1}
h
L0n = {a ∈ Z∗n : am2 = 1 and
j+1 j
for j = 0, . . . , h − 1, am2 = 1 implies am2 = ±1}
Property
If n is prime, then the Miller-Rabin test always declares n as
prime.
If n ≥ 3 is composite, then the probability that the
t
Miller-Rabin test outputs “prime” is less than 14
Most widely used test in practice.
With t = 40, error probabitility less than 2−80 . Much less than
the probability of a hardware failure.
Can test the primality of a 512-bit integer in less than a
second.
Complexity: O(t · log3 n)