Modular Arithmetic
最后更新时间:
文章总字数:
CryptoHack教程的第二章:模运算。看来这一章整章都是围着数学转了。
数学公式好像要刷新一下网页才能看到,不知道怎么回事(
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 of a:{1,2,3,4,6,12}
and the divisors of b:{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
thena
andb
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.
第一节GCD,我们熟知的最大公因数。具体就不再赘述了,我们可以用欧几里得算法手算或者写代码
1 |
|
计算print(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 that
1a * u + b * v = gcd(a,b)
Using the two primes
p = 26513, q = 32321
, find the integersu,v
such that
1p * u + q * v = gcd(p,q)
Enter whichever of
u
andv
is the lower number as the flag.
第二节讲的是扩展的欧几里得算法。
不管是手算还是写代码都有点麻烦,我的建议是直接用gmpy2
库里的gcdext()
来计算。
这里放一个别的大佬写的
1 |
|
结果是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 = 5At 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 17The solution is the smaller of the two integers.
这一节开始引入了模运算的概念,很容易得到x=5,y=4
答案是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
.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 elementb
for every elementa
in the set, such thata + b = 0
anda * 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
anda * 1 = a
.Lets say we pick
p = 17
. Calculate 3^17^ mod 17. Now do the same but with 5^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 generalisations) when we look at RSA cryptography.
Now take the prime
p = 65537
. Calculate 273246787654^65536^ mod 65537.Did you need a calculator?
这里讲了很多,实际上就是介绍了费马小定理:
如果
所以答案很显然就是1.
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
?Think about the little theorem we just worked with. How does this help you find the inverse of an element?
这一节讲的是逆元,题目中提示我们用上一节中的定理计算。
所以
3^11^模13与9同余,答案为9.
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 calculatea2 = 5 mod 29
.As
a = 11, a2 = 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 integera
such thata2 = 18
Your first idea might be to start with
a = 1
and loop toa = p-1
. In this discussionp
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 ana
such thata2 = 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.We say that an integer
x
is a Quadratic Residue if there exists ana
such thata2 = x mod p
. If there is no such solution, then the integer is a Quadratic Non-Residue.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.
If
a2 = x
then (-a)2 = x. So ifx
is a quadratic residue in some finite field, then there are always two solutions fora
.
这一节讲的是二次剩余与二次非剩余。
这里用程序遍历每个数依次计算即可,算得6是模29的二次剩余,6模29最小的根为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 ResidueSo 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 pWhich means given any integer
a
, calculatingpow(a,(p-1)//2,p)
is enough to determine ifa
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:
这一节讲了勒让德符号判断一个数是否为模p二次剩余,在gmpy2
库中就有一个legendre()
可以直接使用。
先计算一下哪个是平方剩余
1 |
|
ints[5]为1,其余都为-1,所以只有ints[5]为平方剩余。
费马小定理推导求解
在提示中提到可以用费马小定理解
那么很显然,
1 |
|
得到答案
1 |
|
SageMath列方程求解
也可以用SageMath解决。
1 |
|
用SageMath求解它的根,得到两个数,最大的即为答案,与上一个方法相同。
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
.Tonelli-Shanks doesn’t work for composite (non-prime) moduli. Finding square roots modulo composites is computationally equivalent to integer factorization - that is, it’s a hard problem.
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
这一节讲了求模p平方根的方法,叫Tonelli-Shanks但实际上我根本没明白他到底在讲些什么
实际上计算还是很简单的,Python中有很多库都支持计算模p平方根(比如sympy
中的sqrt_mod
)。
这里继续用上一题中的SageMath计算
1 |
|
得到答案为
1 |
|
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:Note “pairwise coprime integers” means that if we have a set of integers
{n1, n2, ..., ni}
, all pairs of integers selected from the set are coprime:gcd(ni, nj) = 1
.x ≡ a1 mod n1
x ≡ a2 mod n2
…
x ≡ an mod nnThere 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 17Find the integer
a
such thatx ≡ a mod 935
Starting with the congruence with the largest modulus, use that for
x ≡ a mod p
we can writex = a + k*p
for arbitrary integerk
.
这节课介绍的是求一元一次同余方程组的中国剩余定理。
我们可以用遍历找到答案,或是用数学公式求解
1 |
|
答案为872。
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
非常简单粗暴,给了一个源码和output让我们解答。
那就让我们先来看看源码,大致意思是把flag中每个字符转换为8位二进制数组成二进制字符串,然后判断字符串中的字符,如果是1则将n直接添加到ciphertext中,如果是0则将-n模p的余数添加到ciphertext中。
e是大于1小于p的随机正整数,n是a^e^模p的余数。
计算一下勒让德符号legendre(a,p)
结果为1,所以a是模p的二次剩余。由二次剩余的性质可知n也是模p的二次剩余,-n则是模p的二次非剩余。
所以plaintext中每个1对应ciphertext中一个模p二次剩余数,反之同理。
得到plaintext后每隔8位转为byte字符就是flag了。
1 |
|
答案为crypto{p4tterns_1n_re5idu3s}
Modular Binomials
Rearrange the following equations to get the primes
p,q
N = pq
c1 = (2p + 3q)^e1 mod N
c2 = (5p + 7q)^e2 mod NChallenge files:
- data.txt
将右式展开后,因为
同理,二式也可以进行化简
两式相减
可见右式为
1 |
|
得到结果为
1 |
|
那么到这里第二章的内容也结束了!这一章全是数学知识,孩子学得很开心,脑子快要烧了。