MODULAR ARITHMETIC
The Greatest Common Divisor (GCD), sometimes known as the highest common factor, is the largest number which divides two positive integers (a,b).
For a = 12, b = 8
we can calculate the divisors of a: {1,2,3,4,6,12}
and the divisors of b: {1,2,4,8}
. Comparing these two, we see that gcd(a,b) = 4
.
Now imagine we take a = 11, b = 17
. Both a
and b
are prime numbers. As a prime number has only itself and 1
as divisors, gcd(a,b) = 1
.
We say that for any two integers a,b
, if gcd(a,b) = 1
then a
and b
are coprime integers.
If a
and b
are prime, they are also coprime. If a
is prime and b < a
then a
and b
are coprime.
Think about the case for a
prime and b > a
, why are these not necessarily coprime?
There are many tools to calculate the GCD of two integers, but for this task we recommend looking up Euclid's Algorithm.
Try coding it up; it's only a couple of lines. Use a = 12, b = 8
to test it.
Now calculate gcd(a,b)
for a = 66528, b = 52920
and enter it below.
Let a
and b
be positive integers.
The extended Euclidean algorithm is an efficient way to find integers u,v
such thata * u + b * v = gcd(a,b)
Later, when we learn to decrypt RSA, we will need this algorithm to calculate the modular inverse of the public exponent.
Using the two primes p = 26513, q = 32321
, find the integers u,v
such thatp * u + q * v = gcd(p,q)
Enter whichever of u
and v
is the lower number as the flag.
Imagine you lean over and look at a cryptographer's notebook. You see some notes in the margin:
4 + 9 = 1
5 - 7 = 10
2 + 3 = 5
At first you might think they've gone mad. Maybe this is why there are so many data leaks nowadays you'd think, but this is nothing more than modular arithmetic modulo 12 (albeit with some sloppy notation).
You may not have been calling it modular arithmetic, but you've been doing these kinds of calculations since you learnt to tell the time (look again at those equations and think about adding hours).
Formally, "calculating time" is described by the theory of congruences. We say that two integers are congruent modulo m if a ≡ b mod m
.
Another way of saying this, is that when we divide the integer a
by m
, the remainder is b
. This tells you that if m divides a (this can be written as m | a
) then a ≡ 0 mod m
.
Calculate the following integers:
11 ≡ x mod 6
8146798528947 ≡ y mod 17
The solution is the smaller of the two integers.
We'll pick up from the last challenge and imagine we've picked a modulus p
, and we will restrict ourselves to the case when p
is prime.
The integers modulo p
define a field, denoted Fp
.
If the modulus is not prime, the set of integers modulo n
define a ring.
A finite field Fp
is the set of integers {0,1,...,p-1}
, and under both addition and multiplication there is an inverse element b
for every element a
in the set, such that a + b = 0
and a * b = 1
.
Note that the identity element for addition and multiplication is different! This is because the identity when acted with the operator should do nothing: a + 0 = a
and a * 1 = a
.
Lets say we pick p = 17
. Calculate 317 mod 17
. Now do the same but with 517 mod 17
.
What would you expect to get for 716 mod 17
? Try calculating that.
This interesting fact is known as Fermat's little theorem. We'll be needing this (and its generalisations) when we look at RSA cryptography.
Now take the prime p = 65537
. Calculate 27324678765465536 mod 65537
.
Did you need a calculator?