Post · 23 min read· pp. 368–376

Math Survival Kit for Developers

Introduction

When working with cryptographic applications you need to understand some of the underlying math (at least, if you want to do things properly). For example, the RSA cryptographic system (which was one of the earliest methods and most widely adopted, until it lost ground to better methods, such as those based on elliptic curves) works by encrypting a message MM (expressed as a number in the range 1,2,3,...n−1n-1, with nn a large composite number) with a public key ee doing the following calculation: E(M)=Me(modn)E(M)=M^e \pmod{n} If you want to decrypt the message, you need the private key, dd and perform: M=E(M)d(modn)M=E(M)^d \pmod{n}. Now, what do all these calculations mean and why does RSA work? The trick relies on Euler's theorem and the fact that dd and ee are related by d×e≡1(modϕ(n))d\times e \equiv 1 \pmod{\phi(n)}, so that when we apply ee and afterward dd, "it is the same as" elevating the message to 1. Of course, there are quite many symbols you might not understand, but some key concepts are, in fact, quite straightforward. They are just shrouded in the mist by all the math jargon, which makes things very easy to state for those knowing the meaning, but it can be quite challenging for someone who is not acquainted with them.

Another problem frequently showing up is finding prime numbers (in general, very large numbers, with 100 or more digits) or determining whether a certain number is prime or composite. For example, in zk-SNARKs (zero-knowledge succinct non-interactive arguments of knowledge), one of the key ingredients is the ability to perform (something similar to) homomorphic encryption. This is achieved in practice by pairing two elliptic curves over two sets of numbers, where the total number of elements is the prime mm, satisfying m=k×2N+1m=k\times 2^N+1, with kk an odd number and NN a large number. We say that mm has large 2-adicity and is expressed in compact form as m−1≡0(mod2N)m-1\equiv 0 \pmod{2^N} or $2^N \mid m-1$ (this is read as $2^N$ divides m−1m-1). In RSA, the number nn is the product of two large prime numbers, pp and qq, that is, n=p×qn=p\times q. If you choose two primes that are very close to each other, your cryptographic system could be easily broken using Fermat's method to factorize numbers.

We see, therefore, that we need to understand the math behind it to know which tricks we can apply to solve a problem easily, how to break a cryptographic system or what are the limitations or weaknesses of our own systems. We will be explaining many key ideas of number theory and abstract algebra to help you build the foundations you need to deal with cryptography.

Natural numbers, Integers, Rational, Real and Complex numbers.

Natural numbers are those we use to count objects and were the first things we learned at school: $1,2,3,4...$ are natural numbers. The set (collection) of these numbers is frequently represented by N\mathbb{N}. Numbers like −1-1, −2-2, $0$, etc, are part of the integers; the set is represented by Z\mathbb{Z} (from German, zahlen, numbers). Numbers that can be expressed as the ratio of two integers aa and bb (with b≠0b\neq 0) are called rational, r=a/br=a/b and the set is denoted by Q\mathbb{Q}. Rational numbers can be extended with the addition of irrational numbers (such as π\pi and ee) to form the set of real numbers R\mathbb{R}. You might have also heard of the complex numbers C\mathbb{C}, which contain numbers such as ii, where i2=−1i^2=-1.

In the integers, we have the four basic operations: addition, subtraction, multiplication and division. Let's focus first on addition and subtraction: If we take aa and bb in Z\mathbb{Z}, then c=a+bc=a+b and d=a−bd=a-b are also in Z\mathbb{Z}. We say the sum and subtraction are closed operations on the set. 2. If we add $0$ to any number aa, we get aa, that is, a+0=0+a=aa+0=0+a=a. $0$ is the additive identity of Z\mathbb{Z}. 3. We know that if we sum aa and −a-a we get $0$. That is, a+(−a)=a−a=0a+(-a)=a-a=0, so subtracting is the same as adding −a-a. −a-a is the additive inverse of aa. 4. Given aa, bb and cc, a+(b+c)=(a+b)+ca+(b+c)=(a+b)+c. This is the associative property.

Groups

The above properties show that the set of integers Z\mathbb{Z} with the .+..+. operation form an algebraic group. Other sets, combined with different operations, have the same mathematical structure. For example, positive rational numbers with multiplication have a group structure. n×nn\times n invertible matrices form a group under the matrix multiplication. Vector spaces form a group under the addition (if you take any two vectors uu and vv, their sum is always in the vector space). Elliptic curve cryptography uses the fact that adding two points over an elliptic curve always results in a third point over the curve. Groups appear in many applications in Mathematics, Physics and Chemistry. We can define a group as a (non-empty) set GG together with a binary operation (that is, an operation that takes two input elements from the set GG) ×\times satisfying: G1. If aa and bb are in the set, then a×b=ca\times b=c is also in the set. G2. There is an identity element, ee, such that e×a=a×e=ae\times a=a\times e=a. G3. If aa is in the set, there is some bb in the set such that a×b=ea\times b=e. We say that bb is the inverse of aa and denote it b=a−1b=a^{-1}. G4. For a,b,ca,b,c, a×(b×c)=(a×b)×ca\times (b\times c)=(a\times b)\times c.

The notation in groups is sometimes confusing and people can freely use additive (+) or multiplicative (×\times) notation, and call their identities either $0$ or $1$. This doesn't matter much, since the binary operation can be quite weird (such as "addition" on elliptic curves). If you can start by looking at things a little bit more abstractly, it will pay off very quickly.

Exercise: Take the space of n×nn\times n matrices, such that their determinant is non-zero (that is, the set of invertible matrices). Show that this is a group.

We also learned at school that addition in the integers is commutative (that is, the order of the factors does not change the result a+b=b+aa+b=b+a). Not all groups satisfy this condition, though. For those privileged groups, we have the name Abelian (or commutative) group. An Abelian group has an additional condition: G5. If aa, bb are in GG, a×b=b×aa\times b = b\times a.

When we look at multiplication and division in the integers, we see that there are some problems.

  1. If aa and bb are integers, a×b=ca\times b=c is also an integer. The operation is closed under multiplication.
  2. If we multiply any number by $1$, we get the same number. $1$ is the multiplicative identity.
  3. Given a,b,ca,b,c, we have a×(b×c)=(a×b)×ca\times (b\times c)=(a\times b)\times c.
  4. Given aa, bb and cc, a×(b+c)=a×b+a×ca\times (b+c)=a\times b+a\times c and (b+c)×a=b×a+c×a(b+c)\times a=b\times a+c\times a. This is the distributive property.
  5. If aa and b≠0b\neq 0 are integers, their division a/ba/b is not necessarily an integer. For example, a=3a=3 and b=2b=2 results in c=3/2c=3/2, which is a rational (not integer) number. In other words, the operation is not closed.

Rings and fields

The set Z\mathbb{Z} together with addition and multiplication forms a ring. The polynomials with ordinary addition and multiplication also form a ring. n×nn\times n matrices also form a ring under addition and multiplication. Formally, a ring is a set RR with two operations ++ and ×\times such that:

  1. R is an abelian group under ++ (that is, R fulfills all the conditions for a group G1 to G4, plus commutativity, G5).
  2. There is a multiplicative identity ee, such that a×e=e×a=aa\times e=e\times a=a. Frequently, we use e=1e=1.
  3. Multiplication is associative.
  4. We have the distributive property of multiplication concerning addition.

Exercise: Check that the n×nn\times n matrices form a ring with ordinary matrix addition and multiplication.

If we look at the rational numbers, for any non-zero element, we have a multiplicative inverse. For example, $1/5$ is the multiplicative inverse of $5$, since $5\times 1/5=1$. The division is now a closed operation. Besides, multiplication is also commutative. Q\mathbb{Q} with the ordinary addition and multiplication is a field. Other examples of fields are R\mathbb{R} and C\mathbb{C}. When the number of elements in the set is finite (such as $4$, $2^{255}-19$, etc), the field is known as a finite field. These will be very important for cryptography.

Some concepts from number theory

Divisibility

We will start by talking about divisibility. Given two natural numbers, aa and bb, we say that aa divides bb (and write it a∣ba\mid b) if there is another number cc such that a×c=ba\times c=b. aa is called a divisor of bb. If aa does not divide bb, we write a∤ba\nmid b and we can write b=q×a+rb=q\times a+r, where r<ar<a, with qq the quotient and rr the remainder of the division. If aa divides b×cb\times c, then a∣ba\mid b or a∣ca\mid c. Another fact is that if a∣ba\mid b and a∣ca\mid c, then a∣(x×b+y×c)a\mid (x\times b+y\times c) for any numbers x,yx,y.

Prime Numbers

A number p>1p>1 is called prime if its only divisors are $1$ and itself. Otherwise, the number is composite. Examples of prime numbers are $2,3,5,7,11,13,17,19,23,29,31,...$. The fundamental theorem of arithmetic tells us that any number can be expressed in a unique way (up to ordering) as a product of powers of prime numbers. For example, $20=2^2\times 5$, $186=2\times 3\times 31$, $5=5$, etc. Finding prime numbers is crucial for cryptography. One easy way (but by no means practical for large numbers) to see whether a number pp is prime or not consists in checking whether it is divisible by all primer numbers smaller than pp. The problem is that, if pp is very large, this can be pretty inefficient. There are some better and faster algorithms, but we will cover them some other time.

Exercise: Find all prime numbers that are smaller than 100.

Greatest common divisor and Euclid's algorithm

An important concept is that of the greatest common divisor: given two numbers aa and bb we want to find the largest number cc such that c∣ac\mid a and c∣bc\mid b. We denote this by c=gcd(a,b)c=gcd(a,b) or simply c=(a,b)c=(a,b). For example, $20=2^2\times 5$ and $50=2\times 5^2$. Both numbers are divisible by $1,2,5,10$. $10$ is the greatest number dividing both and so gcd(20,50)=10gcd(20,50)=10. Two numbers a,ba,b are called relatively prime (or coprime) if gcd(a,b)=1gcd(a,b)=1. If aa and bb are both prime (and different), $1$ is the only common divisor. However, $8$ and $9$ are not prime themselves ($8=2^3$ and $9=3^2$), but their only common divisor is $1$ and are coprime.

The greatest common divisor satisfies the following equation, for some xx and yy: x×a+y×b=gcd(a,b)x\times a+y\times b=gcd(a,b) The greatest common divisor can be found very efficiently using the Euclidean algorithm and the numbers xx and yy can also be found with little extra cost using the extended Euclidean algorithm.

To understand the algorithm, let's look at an example: say we want to calculate the gcd(2502,864). The algorithm takes advantage that the remainder is always less than the divisor, so we can "chop down" the larger number; this chopping does not affect the largest common divisor.

  1. Let's find the remainder of $2502/864$, r0=774r_0=774.
  2. Let's find the remainder of $864/774$, r1=90r_1=90.
  3. The remainder of $774/90$ is r2=54r_2=54.
  4. r3=36r_3=36
  5. r4=18r_4=18
  6. r5=0r_5=0, since $36$ is divisible by $18$. So, the greatest common divisor is $18$.

We can see, from the factorization of $864=2^5\times 3^3$ and $2502=2\times 3^2\times 139$, that the gcdgcd is equal to $2\times 3^2=18$. The advantage is that we found it in a few steps (6) and we didn't have to know the factorization (which, for large numbers can be really hard to find. As a matter of fact, that is the key to the RSA cryptosystem).

Congruences and modular arithmetic

One problem we face with computers is that the numbers we can work with are limited. Besides, in some cases, we are not interested in a number itself, but rather in its belonging to a certain class or group. For example, when we bet on a roulette, we can choose whether the result will be even or odd. If it is even, then r=2×kr=2\times k, for some k∈0,1,2,3...18k \in {0,1,2,3...18}. If it is odd, then r=2×k+1r=2\times k+1. We notice that if we want to check parity, we only need to look at the remainder, which can take two values in this case: $0$ or $1$. In fact, when we want to check whether a number is even in the computer, we look at the leftmost bit and check whether it is zero or not. For the case of $2$, we see that any number aa satisfies either: a≡0(mod2)a\equiv 0 \pmod{2} a≡1(mod2)a\equiv 1 \pmod{2} We say that aa is congruent to $0$ (or $1$) modulo $2$. This way, we split all the numbers into two categories: even and odd. We can do the same for any number p>1p>1, remembering that the remainder is $0 \leq r \leq p-1$. This can also be seen as a≡r(modp)a\equiv r \pmod{p} as p∣a−rp\mid a-r or a=k×p+ra=k\times p+r. This notation was invented by Gauss and is really powerful to study a lot of complex problems. We can perform usual operations such as addition and multiplication, but we have to be careful of how things work, given that results will always have to be in the range $0 \leq r \leq p-1$ (As a side note, you could choose a different range, such as −2,−1,0,1,2,p−3{-2,-1,0,1,2,p-3}, but it can be confusing and we should better stick to our first choice).

In the case of the sum, we can add them just as regular numbers and, if the result exceeds pp, take the remainder. For example, let's take p=7p=7, so the elements we have are 0,1,2,3,4,5,6{0,1,2,3,4,5,6}. First, we see that $0$ is an element of the set and that adding it to any number does not change the result. If we add $2$ and $3$ the result is $5$. If we add $5$ and $4$, we get $9$, but $4+5=9\equiv 2 \pmod{7}$ $2$ is just the remainder of the division of $9$ by $7$. We see that the result stays in the original set. What happens when we add $4$ and $3$? $4+3=7\equiv 0 \pmod{7}$ We get $0$! That is because $7$ is divisible by itself and the remainder is $0$. We see that $4$ is the additive inverse of $3$ under this arithmetic. Similarly, $1$ and $6$ are each other's inverse, $2$ and $5$. We can recognize that the set 0,1,2,3,4,5,6{0,1,2,3,4,5,6} with the sum done modulo $7$ is an abelian group. Subtraction can be easily defined as adding the inverse of the number or just performing ordinary subtraction and then taking the result modulo pp.

With multiplication we get something similar. For example, $4\times 5=20\equiv 6 \pmod{7}$. Taking the modulo operation ensures that we always stay inside the set. We also see that $1$ works as the multiplicative identity since any number multiplied by $1$ stays the same. Let's look at what happens with $6\times 6$: $6\times 6=36\equiv 1 \pmod{7}$. We multiplied $6$ by itself and got $1$! We talked before that division a/ba/b could be restated as a×b−1a\times b^{-1}, where b×b−1=1=b−1×bb\times b^{-1} = 1 = b^{-1} \times b. We see that $6$ is its own multiplicative inverse with the multiplication modulo pp. We can also see that: $3\times 5 = 15\equiv 1 \pmod{7}$ $2\times 4 = 8\equiv 1 \pmod{7}$ So, $3 = 5^{-1}$ and $2 = 4^{-1}$! This can sound weird, but we have to remember that we are working with congruence. We can understand the precise meaning of this by rephrasing. Let's take the case of $6$ and $6$. There are two numbers a=q1×7+6a = q_1\times 7+6 and b=q2×7+6b = q_2\times 7+6 (because that is what the congruence means). Let's take the product a×ba\times b: a×b=(q1×7+6)×(q2×7+6)a\times b = (q_1\times 7+6)\times (q_2\times 7+6) Let's apply the distributive law: a×b=q1×q2×72+6×7×(q1+q2)+36a\times b = q_1\times q_2 \times 7^2+6\times 7\times (q_1+q_2)+36 Let's split this further $36=1+35=1+7\times 5$ and regroup, taking as a common factor $7$: a×b=7×(q1×q2×7+6×(q1+q2)+5)+1a\times b = 7\times (q_1\times q_2\times 7+6\times(q_1+q_2)+5)+1 The first term is divisible by $7$, so it is congruent to $0$. Or, if we subtract $1$ to a×ba\times b, we see that it is divisible by $7$ (since it is the product of $7$ and an integer).

We can see that, if pp is prime, then the set 0,1,2,...p−1{0,1,2,...p-1} with addition and multiplication modulo pp is a finite field.

Exercise: Prove that this is indeed a finite field.

Z/nZ\mathbb{Z}/n\mathbb{Z} as a group. Cyclic groups.

You will frequently see these sets are denoted as Z/pZ\mathbb{Z}/p\mathbb{Z}. We have to be very careful if we want to work with nn not prime in Z/nZ\mathbb{Z}/n\mathbb{Z} (in this case, it is not a finite field either). For example, let's try to solve this equation: (x+2)×(x+1)≡0(mod12)(x+2)\times(x+1)\equiv 0 \pmod{12} We could use our knowledge of math and, when the product of two numbers is $0$, at least one of them is $0$ (spoiler's alert: this will go wrong):

  1. (x+2)≡0(mod12)(x+2)\equiv 0 \pmod{12}. If x=10x=10, then x+2=12≡0x+2=12\equiv 0, since it is divisible by 12.
  2. (x+1)≡0(mod12)(x+1)\equiv 0 \pmod{12}. If x=11x=11, then x+1=12≡0x+1=12\equiv 0, since it is divisible by 12. Let's pick now $2$ and see what happens: (2+2)×(2+1)=12≡0(mod12)(2+2)\times(2+1)=12\equiv 0 \pmod{12}. So $2$ is a solution to the equation, but $2+2\equiv 4\not\equiv 0$ and $2+1\equiv 3\not\equiv 0$. This happens because $12$ is not a prime number.

As a matter of fact, given aa and nn, we have that aa has an inverse (modulo nn) if and only if gcd(a,n)=1gcd(a,n)=1, that is, aa and nn are coprime. In the previous example, $3$ is not coprime to $12$ (they have $3$ as a common divisor).

If the set is not too large, we can find inverses just by trial and error. However, it would be nice to have some results that help us compute inverses and how to calculate (integer) powers of numbers.

Let's focus on a prime number pp and take all the non-zero elements of the set, (Z/pZ)⋆(\mathbb{Z}/p\mathbb{Z})^\star. Let's fix p=7p=7, so (Z/pZ)⋆=1,2,3,4,5,6(\mathbb{Z}/p\mathbb{Z})^\star = {1,2,3,4,5,6} and let's focus on multiplication over the set. We can define the power an=a×a×a×...×aa^n=a\times a\times a\times ...\times a. Obviously, $1$ is not interesting, because $1^n=1$, so let's take $5$: $5^1\equiv 5 \pmod{7}$ $5^2\equiv 4 \pmod{7}$ $5^3\equiv 6 \pmod{7}$ $5^4\equiv 2 \pmod{7}$ $5^5\equiv 3 \pmod{7}$ $5^6\equiv 1 \pmod{7}$ $5^7\equiv 5 \pmod{7}$ $5^8\equiv 4 \pmod{7}$ $5^{13}\equiv 5 \pmod{7}$ We see that the powers of $5$ span all the elements of the group. We also see that numbers repeat themselves at an interval of $6$, that is $4 = 5^2 = 5^8 = 5^{14}...$. Let's look at $3$: $3^1\equiv 3 \pmod{7}$ $3^2\equiv 2 \pmod{7}$ $3^3\equiv 6 \pmod{7}$ $3^4\equiv 4 \pmod{7}$ $3^5\equiv 5 \pmod{7}$ $3^6\equiv 1 \pmod{7}$ $3^7\equiv 3 \pmod{7}$ We got all the elements (albeit in a different order). Finally, let's look at $2$: $2^1\equiv 2 \pmod{7}$ $2^2\equiv 4 \pmod{7}$ $2^3\equiv 1 \pmod{7}$ $2^4\equiv 2 \pmod{7}$ This time we didn't span all the elements of the group and we got to the same number after $3$. We will show that these results are valid in general (provided we're working modulo a prime number).

First, we can prove that the set (Z/pZ)⋆(\mathbb{Z}/ p\mathbb{Z})^\star together with multiplication forms an abelian group (the product can never give 0 since all the numbers are not divisible by pp). Second, the group is finite, since the number of elements is finite (6 in our example); its order is $6$. We also saw that by repeatedly multiplying $5$ by itself (that is, taking powers of $5$), we can generate all the elements of the group (note that everything repeats after $6$, which is the order of the group). Since the group can be generated by one of its elements, it is a (finite) cyclic group.

For an element aa, the lowest positive integer nn such that an≡1(modp)a^n\equiv 1 \pmod{p} is known as the order of aa. The elements of the group with their respective order in parentheses are: $1 (1)$, $2 (3)$, $3 (6)$, $4 (2)$, $5(6)$, $6(2)$. We can see that the orders of each element divide the order of the group, $6$. We will present the following theorems, which show that this is not a coincidence.

Three useful theorems and the magic behind RSA

Fermat's Little Theorem: If pp is prime, then, for any integer aa we have that ap−aa^p-a is divisible by pp: ap≡a(modp)a^p\equiv a \pmod{p}. Exercise: Check that this is indeed valid for all elements of (Z/7Z)⋆(\mathbb{Z}/7\mathbb{Z})^\star If aa is coprime to pp, we can write this equivalently: ap−1≡1(modp)a^{p-1}\equiv 1 \pmod{p} An interesting consequence is that we can calculate inverses by doing a−1=ap−2a^{-1} = a^{p-2}, even though in some cases we are overestimating the power (for example, $6\times 6\equiv 1 \pmod{7}$).

Euler's theorem: If aa and nn are positive coprime integers, then aϕ(n)≡1(modn)a^{\phi(n)}\equiv 1 \pmod{n}, where ϕ(n)\phi(n) is Euler's phi (or totient) function.

Euler's ϕ(n)\phi(n) function counts the numbers m<nm < n that are coprime to nn. For example, if we take n=5n = 5, the numbers $1,2,3,4$ are all coprime to $5$ and ϕ(5)=4\phi(5) = 4 (this is reasonable, since $5$ is prime). If we take $8$, we have 1,2,3,4,5,6,7{ 1 , 2 , 3 , 4 , 5 , 6 , 7}; however, only $1,3,5,7$ are coprime to $8$, so ϕ(8)=4\phi(8)=4. For prime numbers, we have ϕ(p)=p−1\phi(p)=p-1 so, Euler's theorem gives us Fermat's theorem as a particular case. Another useful property is that if mm and nn are relatively prime, then ϕ(m×n)=ϕ(n)×ϕ(m)\phi(m\times n)=\phi(n)\times \phi(m) This shows that ϕ\phi is a multiplicative function. In particular, if nn is the product of two primes, pp and qq, then ϕ(n)=(p−1)×(q−1)\phi(n) = (p-1)\times (q-1) RSA's working principle is here. The public key ee and private dd are multiplicative inverses, modulo ϕ(n)\phi(n), d×e≡1(modϕ(n))d\times e \equiv 1 \pmod{\phi(n)} This means that d×e=1+kϕ(n)d\times e = 1+k\phi(n) for some integer kk, so when we compute Me×d=M1+kϕ(n)=M×Mkϕ(n)≡M(modn)M^{ e \times d } = M^{ 1 + k \phi(n) } = M \times M^{ k \phi(n) } \equiv M \pmod{n} since Mkϕ(n)=(Mϕ(n))k≡1k(modn)M^{k \phi(n) } = {(M^{ \phi(n) })}^k \equiv 1^k \pmod{n}. RSA is only as hard as it is factoring the number nn and over the years the length of the keys has increased significantly (it is around 2000 to 4000 bits); elliptic curves, on the other hand, give the same level of security for shorter keys.

Subgroups. Lagrange's theorem

We saw that the order of (Z/7Z)⋆(\mathbb{Z}/7\mathbb{Z})^\star was 6 and that if we take any element aa, doing a6≡1(mod7)a^6\equiv 1 \pmod{7}. However, for $2$ we can do $2^3\equiv 1 \pmod{7}$. A subgroup HH is a subset of GG, that is itself a group, that is, satisfies G1-G4. For example, if we consider the subset H=1H={1}, this is a subgroup of order $1$. Why? Because $1\times 1=1$, so the operation is closed and all other properties follow from the operations of the group GG. GG is also a subgroup of itself. These two are called the trivial subgroups of GG (which are not very interesting). The set 1,2,4{1,2,4} is a subgroup of (Z/7Z)⋆(\mathbb{Z}/7\mathbb{Z})^\star. To check this, we need to see that if an element is in the set, so is its inverse, the identity belongs to the set and the operation is closed. Let's check this:

  • $1$ is in the set and $1$ is its own inverse.
  • The operation is closed, because $2\times 2\equiv 4 \pmod{7}$, because $4\times 4=16\equiv 2 \pmod{7}$ and because $2\times 4=8\equiv 1 \pmod{7}$ (we don't need to check the products with $1$ since that is obvious). We also checked the inverses, since $4=2^{-1}$.

The subset 1,2,4{1,2,4} forms a subgroup of order $3$. Lagrange's theorem states that the order of a subgroup divides the order of the group. We have another subgroup 1,6{1,6}, which is of order $2$. These are non-trivial subgroups. If the order of a group is prime, then its only subgroups are the trivial subgroups (since pp is prime, the subgroups can only be of order $1$ and pp). A group whose only subgroups are the trivial ones is known as a simple group. For example, Z/7Z\mathbb{Z}/7\mathbb{Z} with addition is the group 0,1,2,3,4,5,6{0,1,2,3,4,5,6} of order $7$. There are no subgroups other than the whole group and 0{0}. Note that the order of each element (other than zero, which has order $1$) is $7$, since $7\times a=a+a+a+a+a+a+a$ is divisible by $7$ and, therefore, congruent to $0$ modulo $7$. The fact that some groups can be broken down into smaller subgroups is of concern when working with elliptic curves: if the group is not of prime order, it can be broken down into smaller groups and an attacker may break the system by performing searches on these subgroups.

The discrete logarithm problem

Given a group, we can apply the operation repeatedly on a point gg to get to a point PP, that is gk=g×g×g×...×g=Pg^k=g\times g\times g\times ... \times g=P. For example, in (Z/7Z)⋆(\mathbb{Z}/7\mathbb{Z})^\star, $5$ generates all the elements by successive multiplications with itself. We could then ask how many times xx should we multiply $5$ with itself to get to $3$, that is, $5^x\equiv 3 \pmod{7}$. Since we know that the order of the group is $6$, we should only concern ourselves with numbers $0-6$. If we look above or try all combinations, $5^5\equiv 3 \pmod{7}$ so x=5x=5. Similarly, if we look for yy such that $5^y\equiv 4 \pmod{7}$, we get y=2y=2. The problem of finding xx so that gk=Pg^k=P is known as the discrete logarithm problem (in number theory, xx and yy are known as indices). We quickly see that this logarithm works quite differently from the common logarithm on the real numbers (though the idea is the same, given yy, find xx such that ex=ye^x=y). There is no obvious pattern, it is not increasing and if we had to search over a large set, it could be really daunting. Many cryptographic systems rely on the hardness of this problem over a finite cyclic group.

Summary

We presented some basic terms and concepts from number theory and algebra that will be useful when reading cryptography, since many key concepts and strategies rely on math. The notions of groups, rings and fields and prime numbers show up almost all the time. Soon we will continue with other important tools and concepts that will help us understand how elliptic curve cryptography works, how to perform faster operations over groups and how to combine elliptic curves to build zk-SNARKs.