Greatest Common Divisor
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 ofa: {1,2,3,4,6,12}
and the divisors ofb: {1,2,4,8}
. Comparing these two, we see thatgcd(a,b) = 4
.Now imagine we take
a = 11
,b = 17
. Botha
andb
are prime numbers. As a prime number has only itself and1
as divisors,gcd(a,b) = 1
.We say that for any two integers
a,b
, ifgcd(a,b) = 1
then a and b are coprime integers.If
a
andb
are prime, they are also coprime. Ifa
is prime andb
<a
thena
andb
are 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)
fora = 66528, b = 52920
and enter it below.
Euclid's Algorithm
pseudocode:
|
|
|
|
Solution
|
|
> gcd(12, 8)
4
> gcd(66528, 52920)
1512
Extended GCD
Let
a
andb
be positive integers.The extended Euclidean algorithm is an efficient way to find integers
u,v
such thata * u + b * v = gcd(a,b)
Using the two primes
p = 26513, q = 32321
, find the integersu,v
such thatp * u + q * v = gcd(p,q)
Enter whichever of u and v is the lower number as the flag.
Solution
I watched this video and read this article to better understand the extended Euclidean Algorithm.
|
|
> gcdExtended(26513, 32321)
(1, 10245, -8404)
> min(10245, -8404)
-8404
Modular Arithmetic 1
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
bym
, the remainder isb
. This tells you that if m divides a (this can be written asm | a
) thena ≡ 0 mod m
.Calculate the following integers:
11 ≡ x mod 6
8146798528947 ≡ y mod 17
The solution is the smaller of the two integers.
Solution
if a ≡ b mod m
, then b ≡ a mod m
So:
11 ≡ x mod 6 -> x ≡ 11 mod 6 -> x ≡ 5
8146798528947 ≡ y mod 17 -> y ≡ 8146798528947 mod 17 -> y ≡ 4
> min(11 % 6, 8146798528947 % 17)
4
Modular Arithmetic 2
We’ll pick up from the last challenge and imagine we’ve picked a modulus
p
, and we will restrict ourselves to the case whenp
is prime.The integers modulo
p
define a field, denotedFp
.A finite field
Fp
is the set of integers{0,1,...,p-1}
, and under both addition and multiplication there is an inverse elementb
for every elementa
in the set, such thata + b = 0
anda * b = 1
.Lets say we pick
p = 17
. Calculate3^17 mod 17
. Now do the same but with5^17 mod 17
.What would you expect to get for
7^16 mod 17
? Try calculating that.This interesting fact is known as Fermat’s little theorem. We’ll be needing this (and its generalizations) when we look at RSA cryptography.
Now take the prime
p = 65537
. Calculate273246787654^65536 mod 65537
.Did you need a calculator?
Solution
In summary, Fermat’s Little Theorem states that:
if p is prime, for every integer a:
pow(a, p) = a mod p
and, if p is prime and a is an integer coprime with p:
pow(a, p-1) = 1 mod p
So:
|
|
Modular Inverting
As we’ve seen, we can work within a finite field
Fp
, adding and multiplying elements, and always obtain another element of the field.For all elements
g
in the field, there exists a unique integerd
such thatg * d ≡ 1 mod p
.This is the multiplicative inverse of
g
.Example:
7 * 8 = 56 ≡ 1 mod 11
What is the inverse element:
3 * d ≡ 1 mod 13
?
Solution
Followed this tutorial.
|
|
> print(imod(7,11))
8
> print(imod(3,13))
9
Alternate solution(s):
|
|
|
|
Quadratic Residues
We’ve looked at multiplication and division in modular arithmetic, but what does it mean to take the square root modulo an integer?
For the following discussion, let’s work modulo
p = 29
. We can take the integera = 11
and calculatea^2 = 5 mod 29
.As
a = 11
,a^2 = 5
, we say the square root of5
is11
.This feels good, but now let’s think about the square root of
18
. From the above, we know we need to find some integer a such thata^2 = 18
Your first idea might be to start with
a = 1
and loop toa = p-1
. In this discussion p isn’t too large and we can quickly look.Have a go, try coding this and see what you find. If you’ve coded it right, you’ll find that for all
a ∈ Fp*
you never find an a such thata^2 = 18
.What we are seeing, is that for the elements of
F*p
, not every element has a square root. In fact, what we find is that for roughly one half of the elements ofFp*
, there is no square root.In other words,
x
is a quadratic residue when it is possible to take the square root ofx
modulo an integerp
.In the below list there are two non-quadratic residues and one quadratic residue.
Find the quadratic residue and then calculate its square root. Of the two possible roots, submit the smaller one as the flag.
p = 29
ints = [14, 6, 11]
Solution
|
|
8
Legendre Symbol
In Quadratic Residues we learnt what it means to take the square root modulo an integer. We also saw that taking a root isn’t always possible.
In the previous case when
p = 29
, even the simplest method of calculating the square root was fast enough, but asp
gets larger, this method becomes wildly unreasonable.Lucky for us, we have a way to check whether an integer is a quadratic residue with a single calculation thanks to Legendre. In the following, we will assume we are working modulo a prime p.
Before looking at Legendre’s symbol, let’s take a brief detour to see an interesting property of quadratic (non-)residues.
Quadratic Residue * Quadratic Residue = Quadratic Residue
Quadratic Residue * Quadratic Non-residue = Quadratic Non-residue
Quadratic Non-residue * Quadratic Non-residue = Quadratic Residue
So what’s the trick? The Legendre Symbol gives an efficient way to determine whether an integer is a quadratic residue modulo an odd prime p.
Legendre’s Symbol:
(a / p) ≡ a^(p-1)/2 mod p
obeys:
(a / p) = 1 if a is a quadratic residue and a ≢ 0 mod p
(a / p) = -1 if a is a quadratic non-residue mod p
(a / p) = 0 if a ≡ 0 mod p
Which means given any integer a, calculating
pow(a,(p-1)/2,p)
is enough to determine if a is a quadratic residue.Now for the flag. Given the following 1024 bit prime and 10 integers, find the quadratic residue and then calculate its square root; the square root is your flag. Of the two possible roots, submit the larger one as your answer.
Challenge files:
- output.txt
file: output.txt
|
|
Solution
Tonelli–Shanks algorithm is used to find the square root of n mod p
.
Note: Tonelli-Shanks doesn’t work for composite (non-prime) moduli. Finding square roots modulo composites is computationally equivalent to integer factorization.
Since p = 3 mod 4
is hinted to us, we can conclude:
|
|
93291799125366706806545638475797430512104976066103610269938025709952247020061090804870186195285998727680200979853848718589126765742550855954805290253592144209552123062161458584575060939481368210688629862036958857604707468372384278049741369153506182660264876115428251983455344219194133033177700490981696141526
Modular Square Root
In Legendre Symbol we introduced a fast way to determine whether a number is a square root modulo a prime. We can go further: there are algorithms for efficiently calculating such roots. The best one in practice is called Tonelli-Shanks, which gets its funny name from the fact that it was first described by an Italian in the 19th century and rediscovered independently by Daniel Shanks in the 1970s.
All primes that aren’t 2 are of the form
p ≡ 1 mod 4
orp ≡ 3 mod 4
, since all odd numbers obey these congruences. As the previous challenge hinted, in thep ≡ 3 mod 4
case, a really simple formula for computing square roots can be derived directly from Fermat’s little theorem. That leaves us still with thep ≡ 1 mod 4
case, so a more general algorithm is required.In a congruence of the form
r2 ≡ a mod p
, Tonelli-Shanks calculatesr
.The main use-case for this algorithm is finding elliptic curve co-ordinates. Its operation is somewhat complex so we’re not going to discuss the details, however, implementations are easy to find and Sage has one built-in.
Find the square root of
a
modulo the 2048-bit primep
. Give the smaller of the two roots as your answer.Challenge files:
- output.txt
file: output.txt
|
|
Solution
|
|
2362339307683048638327773298580489298932137505520500388338271052053734747862351779647314176817953359071871560041125289919247146074907151612762640868199621186559522068338032600991311882224016021222672243139362180461232646732465848840425458257930887856583379600967761738596782877851318489355679822813155123045705285112099448146426755110160002515592418850432103641815811071548456284263507805589445073657565381850521367969675699760755310784623577076440037747681760302434924932113640061738777601194622244192758024180853916244427254065441962557282572849162772740798989647948645207349737457445440405057156897508368531939120
Chinese Remainder Theorem
The Chinese Remainder Theorem gives a unique solution to a set of linear congruences if their moduli are coprime.
This means, that given a set of arbitrary integers
ai
, and pairwise coprime integersni
, such that the following linear congruences hold:
x ≡ a1 mod n1
x ≡ a2 mod n2
...
x ≡ an mod nn
There is a unique solution
x ≡ a mod N
whereN = n1 * n2 * ... * nn
.In cryptography, we commonly use the Chinese Remainder Theorem to help us reduce a problem of very large integers into a set of several, easier problems.
Given the following set of linear congruences:
x ≡ 2 mod 5
x ≡ 3 mod 11
x ≡ 5 mod 17
Find the integer
a
such thatx ≡ a mod 935
Solution
Watched this video to understand the process of crt
.
|
|
872
Alternate solution(s):
|
|
Adrien’s Signs
Adrien’s been looking at ways to encrypt his messages with the help of symbols and minus signs. Can you find a way to recover the flag?
Challenge files:
- source.py
- output.txt
file: source.py
|
|
file: output.txt
|
|
Solution
First hint to the problem is the title: Adrien's Signs
. The Legendre symbol
was introduced by Adrien-Marie Legendre, so it would seem wise to use it here. Let’s look more into the code to see what’s going on.
Basically, the byte letters of the plaintext flag are each converted to 8 bits (with padding of zeros). These are then joined together to make a large string of bits. For every bit, a**randit(1,p) % p
is done to create n
. If the bit happens to be 1
, n
is appended to the ciphertext list – otherwise n = -n % p
occurs before appending n
to the list.
So, we just need to determine whether the value is a 0
or 1
for each bit of information.
First, we ensure a
is indeed prime.
Then, we check to make sure that (a / p) = 1
by the Legendre symbol
to prove a
is quadratic residue of mod p
. Since this is the case, all encrypted values that are also quadratic residues of mod p
will leak the value 1
back to us. In the case of the bit being 0
, -n mod p = a^e mod p
which becomes n = -a^e
or (-1/p)
:
Therefore, when encrypted values are non-quadratic residue, it is a 0
bit.
|
|
b'crypto{p4tterns_1n_re5idu3s}'
Modular Binomials
Rearrange the following equations to get the primes p,q
N = p*q
c1 = (2*p + 3*q)**e1 mod N
c2 = (5*p + 7*q)**e2 mod N
Challenge files:
- data.txt
file: data.txt
|
|
Solution
Unintended solution is factordb!
p = 112274000169258486390262064441991200608556376127408952701514962644340921899196091557519382763356534106376906489445103255177593594898966250176773605432765983897105047795619470659157057093771407309168345670541418772427807148039207489900810013783673957984006269120652134007689272484517805398390277308001719431273
q = 132760587806365301971479157072031448380135765794466787456948786731168095877956875295282661565488242190731593282663694728914945967253173047324353981530949360031535707374701705328450856944598803228299967009004598984671293494375599408764139743217465012770376728876547958852025425539298410751132782632817947101601
Alternate solution(s):
|
|
|
|