All pages
Powered by GitBook
1 of 2

Division and Greatest common divisor

Author: Zademn

Introduction

Two of the skills a cryptographer must master are:

  1. Knowing his way and being comfortable to work with numbers.

  2. Understanding and manipulating abstract objects.

This chapter of fundamentals proposes to prepare you for understanding the basics of number theory and abstract algebra .We will start with the most basic concepts such as division and build up knowledge until you, future cryptographer, are able to follow and understand the proofs and intricacies of the cryptosystems that make our everyday life secure.

We will provide examples and snippets of code and be sure to play with them. If math is not your strongest suit, we highly suggest to pause and ponder for each concept and take it slow.

For the math-savy people we cover advanced topics in specific chapters on the subjects of number theory and group theory.

So what are we waiting for? Let's jump right in!

Division

Let Z={…,−1,0,1,2,3… }\mathbb{Z} = \{\dots , -1, 0, 1, 2, 3 \dots \}Z={…,−1,0,1,2,3…}be the set denoting the integers.

Definition - Divisibility

For a,b,∈Za, b, \in \mathbb{Z} a,b,∈Zwe say that aaadivides bbbif there is some k∈Zk \in \mathbb{Z}k∈Zsuch that a⋅k=ba \cdot k = ba⋅k=b

Notation: a∣ba | ba∣b

Example

For a=2,b=6a = 2, b = 6a=2,b=6 we have 2∣62 | 62∣6 because we can find k=3k = 3k=3such that 6=2⋅36 = 2 \cdot 36=2⋅3.

Properties

  • a∣a, 1∣a and a∣0a | a, \ 1 | a \text{ and } a | 0a∣a, 1∣a and a∣0

  • a∣ba | ba∣b and a∣c a | c a∣c implies a∣(bu+cv) ∀u,v,∈Za | (bu + cv) \ \forall u, v, \in \mathbb{Z}a∣(bu+cv) ∀u,v,∈Z

    • Example: Let b=6,u=5b = 6, u = 5b=6,u=5 and c=9,v=2c = 9, v = 2 c=9,v=2

    • 3∣63 | 63∣6 and 3∣9⇒3∣(6⋅5+9⋅2)  ⟺  3∣483 | 9 \Rightarrow 3 | (6 \cdot 5 + 9 \cdot 2) \iff 3 | 483∣9⇒3∣(6⋅5+9⋅2)⟺3∣48 . We can find k=16k = 16k=16such that 48=3⋅1648 = 3 \cdot 1648=3⋅16

  • a∣ba | ba∣b and b∣c b | c b∣c implies a∣c a | ca∣c

  • if a∣ba|ba∣band b∣ab|ab∣a then a=±ba = \pm ba=±b

Definition - Division with remainder

Let a,b∈Za, b \in \mathbb{Z}a,b∈Zwith b≥1b≥1b≥1,

There exists unique q,r∈Zq, r \in \mathbb{Z}q,r∈Zsuch that a=bq+r\boxed{a = bq + r}a=bq+r​and 0≤r<b0 \leq r < b0≤r<b

qq q is called the quotient and rrr the remainder

Examples:

  • To find q,rq, rq,r python offers us the divmod() function that takes a,ba, ba,bas arguments

q, r = divmod(6, 2)
print(q, r)
# 3 0 

q, r = divmod(13, 5)
print(q, r)
# 2 3 
# Note that 13 = 2 * 5 + 3
  • If we want to find only the quotient qqq we can use the // operator

  • If we want to find the remainder rrr we can use the modulo % operator

q = 13 // 5
print(q)
# 2

r = 13 % 5
print(r)
# 3

Exercises:

  1. Now it's your turn! Play with the proprieties of the division in Python and see if they hold.

Greatest common divisor

Definition

Let a,b∈Za, b \in \mathbb{Z}a,b∈Z be 2 integers. The greatest common divisor is the largest integer d∈Zd \in \mathbb{Z}d∈Zsuch that d∣ad | ad∣aand d∣bd | bd∣b

Notation: gcd⁡(a,b)=d\gcd(a, b) = dgcd(a,b)=d

Examples:

# In python we can import math to get the GCD algo
import math
print(math.gcd(18, 12)) # -> 6
# Sage has it already!
print(gcd(18, 12)) # -> 6

Remark:

  • for all other common divisors ccc of a,ba, ba,bwe have c∣dc | dc∣d

Things to think about

What can we say about numbersa,b a, ba,b with gcd⁡(a,b)=1\gcd(a, b) = 1gcd(a,b)=1? How are their divisors?

Euclidean Algorithm

Introduction

Although we have functions that can compute our gcd⁡\gcdgcdeasily it's important enough that we need to give and study an algorithm for it: the euclidean algorithm.

It's extended version will help us calculate modular inverses which we will define a bit later.

Euclidean Algorithm

Important Remark

If a=b⋅q+ra = b \cdot q + ra=b⋅q+rand d=gcd⁡(a,b)d = \gcd(a, b)d=gcd(a,b) then d∣rd | rd∣r. Therefore gcd⁡(a,b)=gcd⁡(b,r)\gcd(a, b) = \gcd(b, r)gcd(a,b)=gcd(b,r)

Algorithm

We write the following:

a=q0⋅b+r0b=q1⋅r0+r1r0=q2⋅r1+r2⋮rn−2=rn−1⋅qn−1+rnrn=0 a = q_0 \cdot b + r_0 \\ b = q_1 \cdot r_0 + r_1 \\ r_0 = q_2 \cdot r_1 + r_2 \\ \vdots \\ r_{n-2} = r_ {n-1} \cdot q_{n - 1} + r_n \\ r_n = 0a=q0​⋅b+r0​b=q1​⋅r0​+r1​r0​=q2​⋅r1​+r2​⋮rn−2​=rn−1​⋅qn−1​+rn​rn​=0

Or iteratively rk−2=qk⋅rk−1+rkr_{k-2} = q_k \cdot r_{k-1} + r_krk−2​=qk​⋅rk−1​+rk​ until we find a 000. Then we stop

Now here's the trick:

gcd⁡(a,b)=gcd(b,r0)=gcd(r0,r1)=⋯=gcd⁡(rn−2,rn−1)=rn−1=d\gcd(a, b) = gcd(b, r_0) = gcd(r_0, r_1) = \dots = \gcd(r_{n-2}, r_{n-1}) = r_{n-1} = dgcd(a,b)=gcd(b,r0​)=gcd(r0​,r1​)=⋯=gcd(rn−2​,rn−1​)=rn−1​=d

If d=gcd⁡(a,b)d = \gcd(a, b)d=gcd(a,b) then ddd divides r0,r1,...rn−1r_0, r_1, ... r_{n-1}r0​,r1​,...rn−1​

Pause and ponder. Make you you understand why that works.

Example:

Calculate gcd⁡(24,15)\gcd(24, 15)gcd(24,15)

24=1⋅15+915=1⋅9+69=1⋅6+36=2⋅3+0⇒3=gcd⁡(24,15)24 = 1 \cdot 15 + 9 \\ 15 = 1 \cdot 9 + 6 \\ 9 = 1 \cdot 6 + 3 \\ 6 = 2 \cdot 3 + 0 \Rightarrow 3 = \gcd(24, 15) 24=1⋅15+915=1⋅9+69=1⋅6+36=2⋅3+0⇒3=gcd(24,15)

Code

def my_gcd(a, b):
    # If a < b swap them
    if a < b: 
        a, b = b, a
    # If we encounter 0 return a
    if b == 0: 
        return a
    else:
        r = a % b
        return my_gcd(b, r)

print(my_gcd(24, 15))
# 3

Exercises:

  1. Pick 2 numbers and calculate their gcd⁡\gcdgcdby hand.

  2. Implement the algorithm in Python / Sage and play with it. Do not copy paste the code

Extended Euclidean Algorithm

This section needs to be expanded a bit.

Bezout's identity

Let d=gcd⁡(a,b)d = \gcd(a, b)d=gcd(a,b). Then there exists u,vu, vu,v such that au+bv=dau + bv = dau+bv=d

The extended euclidean algorithm aims to find d=gcd⁡(a,b), and u,vd = \gcd(a, b), \text{ and }u, vd=gcd(a,b), and u,vgiven a,ba, ba,b

# In sage we have the `xgcd` function
a = 24
b = 15
g, u, v = xgcd(a, b)
print(g, u, v)
# 3 2 -3 

print(u * a + v * b)
# 3 -> because 24 * 2 - 15 * 3 = 48 - 45 = 3