Professional Documents
Culture Documents
Adam Zlehovszky
December 18, 2023
1 Representation of Integers
1.1 Euclidean division
If a, b ∈ Z with b ̸= 0 then ∃!q, r ∈ Z such that a = qb + r where 0 ≤ r < |b|.
This is the Euclidean division or long division of the dividend a with the divisor
b. The results of the division are the quotient q and the remainder r. The
standard notation for the remainder is a mod b. In algorithmic setting we use
q, r ← divmod (a, b).
n = nd−1 bd−1 + · · · + n2 b2 + n1 b + n0
= nd−1 bd−2 + · · · + n2 b + n1 b + n0
1
1.3 Operations on Integers
1.3.1 Addition
Let us assume that we have two unsigned integers stored as digits in a number
system with base b:
d(i) −1
(i)
X
(i)
n = nk bk ,
k=0
(i)
In Algorithm 1 we assume that nk = 0 if k ≥ d(i) for i = 1, 2. The time
complexity of the standard addition is O d(s) .
1.3.2 Multiplication
(i)
Let n ’s defined same as above for i = 1, 2. We will compute the digits of the
Pd(p) −1
product p = n(1) · n(2) = k=0 pk bk with the naive multiplication method:
The time-complexity
of Algorithm 2 is O(d(1) · d(2) ) = O(d2 ), where d =
(1) (2)
max d , d .
Karatsuba’s idea for faster multiplication can be demonstrated on two-digit
numbers. Let
x = x1 b + x0 , and y = y1 b + y0
with 0 ≤ xi , yi < b integers. Naive multiplication of x and y is
z = xy = (x1 b + x0 ) (y1 b + y0 )
= x1 y1 b2 + (x1 y0 + x0 y1 ) b + x0 y0
= z1 b2 + z1 b + z0 .
2
Algorithm 2 Naive multiplication
1: procedure NaiveMultiplication(n(1) , n(2) )
2: d(p) ← d(1) + d(2)
3: for k = 0, . . . , d(p) − 1 do
4: pk ← 0
5: end for
6: for j = 0, . . . , d(2) − 1 do
7: c←0
8: for i = 0, . . . , d(1) − 1 do
(1) (2)
9: c, pi+j ← divmod pi+j + ni nj + c, b
10: end for
11: pd(1) +j ← c
12: end for
13: return p
14: end procedure
z1 = x1 y0 + x0 y1
= x1 y0 + x0 y1 − x1 y1 + x1 y1 − x0 y0 + x0 y0
= (x1 + x0 ) y1 + (x1 + x0 ) y0 − x1 y1 − x0 y0
= (x1 + x0 ) (y1 + y0 ) − x1 y1 − x0 y0
= (x1 + x0 ) (y1 + y0 ) − z2 − z0 .
This is 3 multiplication and 3 additions. By extending this idea to more than two
digits recursively, the multiplication algorithm performs O(dlog2 3 ) ≈ O(d1.58 )
single-digit multiplication.
Fast Fourier Transform based algorithms can achieve O(d log d) complexity.
1.4 Exponentiation
We want to compute xn for some 1 ≤ n ∈ Z and x that has multiplication as
an operation.
xn = x
| · x{z· · · x} .
n times
3
This way we can compute xn with log2 n = s multiplications with the algorithm
below:
Pd−1
Fast exponentiation If we write n = k=0 nk 2k in binary, then
Pd−1
nk 2 k )
xn = x( k=0
d−1
k
x(nk 2 )
Y
=
k=0
d−1
k n
x(2 ) .
k
Y
=
k=0
nk
Since y = y if nk = 1 and y = 1 otherwise, we arrive at the following
algorithm:
4
2 Number Theory and its Applications
2.1 Divisibility
If aq = b, then we say that a is a divisor of b, or b is a multiple of a. The
notation is a | b. Otherwise a̸ | b. If a | b, then long division has remainder of 0
and in case of integers ab is an integer as well.
The following properties are natural consequences of the definition.
1. For every a, we have that a | a.
2. a | 0, for every a.
3. If 0 | a, then a = 0.
4. If a | b and b | c, then a | c.
If ε | a for every a, then we call ε a unit element. The unit elements of Z are
±1.
If a | b and b | a and a ̸= b, then we call a and b associated elements. Two
elements a and b are associated if and only if a = εb for some unit element ε.
Consequently, a and b are associated integers if and only if |a| = |b|.
Let p ̸= 0 be a non-unit element. We say that p is an irreducible element
if p = ab implies that a or b is an associated element of p (and the other is a
unit). If an element is not irreducible, then it is composite. We call p a prime
element if p | ab implies that p | a or p | b. If p is an irreducible element, then
it is also a prime element. In case of integers, the reverse is also true, i.e. every
prime element is irreducible.
n = pα αr
1 · · · pr
1
where pi ’s are different prime numbers (positive prime elements of Z) and αi > 0
for all i = 1, . . . , r.
Theorem 2 (Euclid). There are infinitely many prime numbers.
5
Proof. Let us assume that there are only finite many primes p1 , . . . , pn . In this
case the long division of n = p1 · · · pn + 1 with pi yields a remainder of 1 for
every prime. This means that n does not have canonical form, which contradicts
Theorem 1.
2. gcd(0, 0) = 0,
3. gcd(a, b) = gcd(b, a),
4. gcd(a, b) = gcd(a − b, b) or in general gcd(a − qb, b) = gcd(a, b) for any q.
Specifically gcd(a mod b, b) = gcd(a, b).
6
Let us define the recurrence relation r0 = a, r1 = b and rn+2 = rn mod rn+1 .
In this case the equations above become
gcd(r0 , r1 ) = gcd(r2 , r1 )
= gcd (r2 , r3 )
= gcd (r4 , r3 ) .
gcd(r0 , r1 ) = gcd(r1 , r2 )
= gcd (r2 , r3 )
= gcd (r3 , r4 )
gcd(a, b) = gcd(rN −1 , 0) = rN −1 .
We can transform the recursion into a loop. At any given step, we only need
rn and rn+1 to produce rn+2 .
For later applications we not only need the gcd(a, b) = d but two additional
elements x and y, such that
d = ax + by.
7
We can extend Euclidean algorithm to calculate x and y. For this, we are going
to ensure that during the algorithm we are updating producing xn and yn for
each rn , such that
rn = axn + byn .
This will be our loop invariant property, i.e. this property will be true for n and
n + 1 whenever we enter the body of the loop and we will make sure that it is
true for n + 2 after the last statement of the body. Before the first execution of
the loop body, the values x0 = 1, y0 = 0, x1 = 0 and y0 = 1 will suffice. Let
q = ⌊rn /rn+1 ⌋. During the body of the loop we have that
so xn+2 = xn − qxn+1 and yn+2 = yn − qyn+1 will work. Again, we only need
tha last two value of x and y during the calculations. We call the procedure
Extended Euclidean algorithm:
ax + by = c.
a (x′ m) + (x′ m) = d · m = c,
8
If there is a pair of solution, then there are infinitely many solutions in the
form of
b a
x = x0 + k , y = y0 − k
d d
where k ∈ Z and there are no other solutions.
a = {a + km : k ∈ Z} .
Let us denote
Zm = 0, 1, . . . , m − 1
the set of residue classes.
Theorem 5 (The compability of integer operations with the residue classes).
If a ≡ b (mod m) and c ≡ d (mod m), then
• a + c ≡ b + d (mod m) and
• ac ≡ bd (mod m).
This can be stated on residue class level as well:
1. a + b = a′ + b′ : a′ ∈ a, b′ ∈ b = a + b and
2. a · b = a′ b′ : a′ ∈ a, b′ ∈ b = ab.
a +m b = (a + b) mod m, a ·m b = ab mod m.
9
2. With the extended Euclidean algorithm, we can compute 1 = ax + my.
Rearranging this, we have that ax − 1 = −ym. So m | ax − 1, which is by
definition means that ax ≡ 1 (mod m).
since c | bcx and y = d−1 (mod c). The other congruence relation can proven
in the same way.
We can extend this method for solving more than two equations by replacing
two of them with the common solution calculated with the help of previous
theorem.
10
2.7 Euler’s totient function
For 0 < n ∈ Z we can define Euler’s totient function as
r
Y 1
φ(n) = n 1− .
1
pi
Theorem 10. A positive integer p is a prime number if and only if the only
solutions of x2 ≡ (mod p) are x ≡ ±1 (mod m). On the residue system level
this means that p is a prime number if and only if in Zp the square roots of 1
are 1 and p − 1.
11
Let n − 1 = 2s q, where q is odd. Combining Fermat’s little theorem and the
previous property of prime numbers, the sequence
s s−1
a2 q
mod n, a2 q
mod n, . . . , aq mod n
must start with at least one 1 and if there are any elements not equal to 1, the
first such element must be n − 1.
Based on these arguments we arrive the Miller-Rabin primality testing algo-
rithm. The probability of false positive response p < 14 . If we repeat the trial k
times independently, then p < 41k .
12
divide any other factor of p − 1), then g k mod p ̸= 1. If the canonical form of
p − 1 = pα αr
1 · · · pr , then the maximal divisors of p − 1 are
1
p−1
i = 1, . . . , r.
pi
We call p a Sophie Germain prime, if it is a prime number and p = 2q + 1,
where q is also a prime number. In this case the it is enough to test g 2 mod p ̸=
1 and g q mod p ̸= 1.
Let b ∈ Z∗p , 0 ≤ k ≤ p − 1 and a = bk mod p. In this case the discrete
logarithm or index of a in base b in Z∗p is k. The notation is logb a = k or
indb a = k (mod p).
The discrete logarithm problem (DLP) is to find k from b and a. Currently
there is no efficient algorithm known to compute k.
B a = (g b )a = g ba = g ab = (g a )b = Ab ,
they have common secret key. The security of the key exchange depends on
that we assume DLP cannot be solved efficiently.
3 RSA
Let us fix two large prime numbers, p and q and calculate their product n = pq.
Also search for such integers e and d such that ed ≡ 1 mod φ(n), i.e. ed =
k(p − 1)(q − 1) + 1 for some k ∈ Z. We can make n and e public and keep p, q
and d private. If 0 < m < n and c = me mod n, then
since mp−1 ≡ 1 (mod p). By the same argument we have that m ≡ cd (mod q).
We can solve this system with the Chinese remainder theorem and the solution
is m = cd mod n.
Let dp = d mod (p − 1) and d = k(p − 1) + dp . In this case
Also if dq = d mod (q − 1), then cd ≡ cdq (mod q). Let m1 = cdp mod p and
m2 = cdq (mod q). In this case if m solves the system
13
then m solves the original problem.
Let us search for the solution tin the form of m = m2 + hq. In this case
m ≡ m2 (mod q). If m2 + hq ≡ m1 (mod m), then hq ≡ m1 − m2 (mod p).
Let us denote qinv = q −1 (mod p). In this case with h = qinv (m1 − m2 ) mod p,
then m solves the system.
The security of RSA stands on the assumption that the factorization of n
into p and q cannot be done efficiently. Due to this fact calculating φ(n) without
knowing p and q is hard as well. This means that even by knowing e, it is hard
to compute d.
We can use RSA to sign messages digitally. If σ = md (mod n), then we
can check the signature of the the m, σ pair by checking that m ≡ σ e (mod n).
f = f0 + f1 x + f2 x2 + · · · + fn xn .
f = f0 + f1 x + f2 x2 + · · · + fn xn + 0xn+1 + 0xn+1 + · · ·
In some sense, we can say that if the degree of polynomial is greater than the
degree of another, then the first one is ”larger”.
14
coefficients of the result of addition and multiplication from the coefficients of
the operands.
Let
f = f0 + f1 x + · · · + fn xn ,
and
g = g0 + g1 x + · · · + gm xm + 0xm+1 + · · · 0xn .
hk xk = f0 gk xk + f1 xgk−1 xk−1 + · · · + fk xk g0 ,
so
k
X k
X X
hk = fi gk−i = fk−j gj = fj gj .
i=0 j=0 i+j=k
Proof. We only prove the existence. Let n = deg(f ) and m = deg(g). If n < m,
then q = 0, r = f works.
Otherwise for the polynomial f ∗ = f − fn gm
−1 n−m
x g we have that deg(f ∗ ) <
deg(f ). We repeat this process until r = deg(f ∗ ) < m and record fn gm
−1 n−m
x
into q.
If we send r0 b1 . . . bk−1 , then we always send even number of bits that are set
to 1. We reject the received message if there are odd number of bits to to 1 in
it. This method is called parity check, r0 is the parity bit. This simplest form
15
of error detection method is able to detect all such errors where odd number of
bits are flipped. But it is incapable to detect even number of errors.
We can think of the bits b0 b1 . . . bk−1 as the coefficients of the polynomial
16
where g (i) ∈ F [x] is irreducible, with main coefficient of 1, 0 < αi ∈ Z for
i = 1, . . . , r.
Polynomials have greatest common divisor, we can distinguish one by taking
the one with main coefficient of 1. Extended Euclidean algorithms works for
polynomials.
We can define congruence relation with a fixed modulus non-constant m ∈
F [x]. We can distinguish a unique element from every residue class with degree
less than deg(m). These elements form the residue system F [x]m .
The structure of F [x]m is the same as with Zm . If gcd(a, n) = 1, then a has
modular inverse which can be computed with extended Euclidean algorithm.
If m is irreducible, then every non-zero element in F [x]m has a modular
inverse, so F [x]m is a field.
17
4.5 Irreducible polynomials
Theorem 14 (The Fundamental Theorem of Algebra). If 1 ≤ n ∈ Z, f ∈ C[x]
with deg(f ) = n, then there exists c ∈ C such that f (c) = 0.
The irreducible polynomials of C[x] exactly the linear polynomials.
The irreducible polynomials of R[x] are the linear polynomials and such
quadratic polynomials that do not have real root, for. e.g. x2 + 1.
If p is a prime number and 1 ≤ n ∈ Z, then xn − p ∈ Q[x] is irreducible.
For every 1 ≤ n ∈ Z, there exists f ∈ Zp [x] such that f is irreducible.
f = s + f1 x + · · · fk−1 xk−1
18
points they must be the same polynomial, i.e. f = g. The members present can
recover the secret s = g(0).
Encoding
Qt Let
us define the generator polynomial of the encoding with g =
i t
i=1 x − α where t = n − k. The encoded message is c = mx − r, where
r = mxt mod g.
19
Decoding Let us assume that we receive c′ = c+e. Since g | c, every root of g
must be a root of c as well. Let syndrome polynomial s = s0 + s1 x + · · · st−1 xt−1
where si = c′ (αi+1 ). If e = 0, then s = 0 and we can accept c′ as a transmission
without any error. We can extract the original message as the last k coefficients
of c′ .
If s ̸= 0, then there was error during transmission, so e ̸= 0. We can modify
the extended Euclidean algorithm to produce the error location polynomial L
and the error value polynomial E in Algorithm 9.
Therefore
E(α−j )
ej =
αj Lj (α−j )
is the error at location j.
4.9.1 Example
Let F = Z11 , n = 10, k = 6 so t = n−k = 4. In Z1 1 we have that ord11 (2) = 10,
Q4
so we can use i=1 x − 2i for encoding. Note that every operator is done with
respect to mod 11. In particular, divisor is done by multiplication with the
modular inverse.
Let us assume that we receive c′ = 4 + 4x + 8x2 + 4x3 + x4 + 10x5 + 10x6 +
6x + 2x8 . To calculate the coefficients of the syndrome polynomial s we plug
7
20
in the values 2, 22 = 4, 23 = 8 and 24 mod 11 = 5 into c′
s0 = 4 + 4 · 2 + 8 · 22 + 4 · 23 + 24 + 10 · 25 + 10 · 26 + 6 · 27 + 2 · 28 = 0,
s1 = 4 + 4 · 4 + 8 · 42 + 4 · 43 + 44 + 10 · 45 + 10 · 46 + 6 · 47 + 2 · 48 = 10,
s2 = 4 + 4 · 4 + 8 · 82 + 4 · 83 + 84 + 10 · 85 + 10 · 86 + 6 · 87 + 2 · 88 = 2,
s3 = 4 + 4 · 5 + 8 · 52 + 4 · 53 + 54 + 10 · 55 + 10 · 56 + 6 · 57 + 2 · 58 = 5.
and
E 5−1
2
e4 = 4 −1
= = 2,
2 L4 (5 ) 5·9
so the error is e = 3x2 + 2x4 .
21