๐Ÿ–Š๏ธ
Crypto
  • Crypto
  • Fundamentals
    • Divisibility, Factors and Euclid's Algorithms
    • Modular Arithmetic
    • Rings, Fields and Euler's Totient Function
  • Further Maths
    • Continued Fractions
  • RSA
    • Overview
    • Public Exponent Attacks
      • e=1
      • Small e
      • Multi-party RSA with Small e
      • Wiener's Attack
    • Choice of Primes
      • N is prime
      • Mersenne Primes
      • P=Q
      • Fermat Factorisation
    • Factorisation Methods
      • Pollard's p-1
  • Diffie-Hellman Key Exchange
    • Overview
    • Solving the DLP
      • Baby Step, Giant Step
Powered by GitBook
On this page
  • The Basics
  • Modular (Multiplicative) Inverses
  • Proof
  • An Intuitive Approach
  • Uses

Was this helpful?

  1. Fundamentals

Modular Arithmetic

An introduction to the fundamentals

The Basics

Modular Arithmetic is an incredibly important aspect of practically all asymmetric cryptography. A common way to refer to it is as clock arithmetic - imagine it's eleven o'clock right now. Three hours later it'll be two o'clock, right? Well we do the same thing in modular arithmetic:

11+3=14โ‰ก2modโ€‰โ€‰1211 + 3 = 14 \equiv 2 \mod 1211+3=14โ‰ก2mod12

โ€‹Essentially we take the sum of 111111 and 333 and then divide by 121212 and keep the remainder. Here, the 121212 is called the modulus. The triple equals โ‰ก\equivโ‰ก denotes a congruence, and we say 141414 is congruent to 222 modโ€‰โ€‰12\mod 12mod12.

We can think of this another way and say that two numbers aaa and bbb are congruent modulo mmm if their difference aโˆ’ba-baโˆ’b is divisible by mmm. For example, 262626 and 222 are congruent modulo 121212 because 26โˆ’2=2426-2=2426โˆ’2=24 which is divisible by 121212.

Modular (Multiplicative) Inverses

In normal maths, every number has an inverse - another number that, when multiplied with them, equals 111. In maths terms, we would say that for every number aaa there is another bbb where ab=1ab=1ab=1. Generally, we can say that b=1ab=\frac{1}{a}b=a1โ€‹ as that fits the equation - the inverse of 777, for example, is 17\frac{1}{7}71โ€‹.

In modular arithmetic, there are no fractions. Instead of finding bbb such that ab=1ab=1ab=1, we instead find bbb such that abโ‰ก1modโ€‰โ€‰mab \equiv 1 \mod mabโ‰ก1modm. This means that for different values of mmm there are different inverses for aaa. For example, the inverse of 3modโ€‰โ€‰53 \mod 53mod5 is 222, because 3โ‹…2=6โ‰ก1modโ€‰โ€‰53 \cdot 2 = 6 \equiv 1 \mod 53โ‹…2=6โ‰ก1mod5. This inverse is called the modular multiplicative inverse. We can therefore say that 3โˆ’1โ‰ก2modโ€‰โ€‰53^{-1} \equiv 2 \mod 53โˆ’1โ‰ก2mod5.

Yet not every number aaa has an inverse modulo mmm - for example, 222 has no inverse modulo 666. Why? This is because gcd(a,m)=1gcd(a,m)=1gcd(a,m)=1. Let's prove this in the general case and see why that is.

Proof

Assume there is bbb such that aโ‹…bโ‰ก1modโ€‰โ€‰ma \cdot b \equiv 1 \mod maโ‹…bโ‰ก1modm. This would mean that there also exists ccc such that ab=cm+1ab = cm + 1ab=cm+1 and therefore abโˆ’cm=1ab - cm = 1abโˆ’cm=1. It follows therefore that both sides must be divisible by gcd(a,m)gcd(a,m)gcd(a,m), meaning gcd(a,m)=1gcd(a,m)=1gcd(a,m)=1.

As a result, if there is an inverse of aaa modulo mmm, such an inverse can only exist if gcd(a,m)=1gcd(a,m)=1gcd(a,m)=1 โ–ก\squareโ–ก

An Intuitive Approach

If you're struggling to visualise this proof, think back to the previous example of 222 having no inverse modulo 666. Why is this? Well, regardless of how much you multiply 222 with, the sequence will go 0,2,4,0,2,4,...0, 2, 4, 0, 2, 4, ...0,2,4,0,2,4,... and loop around because it is a factor of 6.

Actually, the fact that a number it is not coprime with the modulus shows that it can never have an inverse. Again, why? Think about it this way: if they have a common factor, the remainder when one is divided by the other will also have that factor - and therefore not be 111. If we choose 444 rather than 222, we can see this - if there is a common factor (as there is of 222 in this case) there is no value xxx for which 4x4x4x will return 111 modulo 666. Any number you can multiply 444 by will always give a remainder divisible by 222 when divided by 6.

I suggest you spend some time reflecting on this fact, as it is a very important result.

Uses

The uses of modular multiplicative inverses are similar to those of regular inverses - as division. Normally, if we divide two numbers, we can treat the number being divided by as multiplying by its inverse.

8รท2=8โ‹…2โˆ’1=8โ‹…12=48 \div 2 = 8 \cdot 2^{-1} = 8 \cdot \frac{1}{2} = 48รท2=8โ‹…2โˆ’1=8โ‹…21โ€‹=4

โ€‹We can use the modular multiplicative inverse the same way - say we want to divide 333 by 222, modulo 777:

3รท2โ‰ก3โ‹…2โˆ’1โ‰ก3โ‹…4โ‰ก12โ‰ก5modโ€‰โ€‰73 \div 2 \equiv 3 \cdot 2^{-1} \equiv 3 \cdot 4 \equiv 12 \equiv 5 \mod 7 \\ 3รท2โ‰ก3โ‹…2โˆ’1โ‰ก3โ‹…4โ‰ก12โ‰ก5mod7

Note that 444 is the inverse of 222 as 2โ‹…4โ‰ก1modโ€‰โ€‰72 \cdot 4 \equiv 1 \mod 72โ‹…4โ‰ก1mod7.

And just like that we realise that dividing 333 by 222 modulo 777 gives us 555.

PreviousDivisibility, Factors and Euclid's AlgorithmsNextRings, Fields and Euler's Totient Function

Last updated 2 years ago

Was this helpful?