🏳️
The CTF Recipes
  • Introduction
  • Cryptography
    • Introduction
    • General knowledge
      • Encoding
        • Character encoding
          • ASCII
          • Unicode
          • UTF-8
        • Data encoding
          • Base16
          • Base32
          • Base64
      • Maths
        • Modular arithmetic
          • Greatest Common Divisor
          • Fermat's little theorem
          • Quadratic residues
          • Tonelli-Shanks
          • Chinese Remainder Theorem
          • Modular binomial
      • Padding
        • PKCS#7
    • Misc
      • XOR
    • Mono-alphabetic substitution
      • Index of coincidence
      • frequency analysis
      • Well known algorithms
        • 🔴Scytale
        • 🔴ROT
        • 🔴Polybe
        • 🔴Vigenere
        • 🔴Pigpen cipher
        • 🔴Affine cipher
    • Symmetric Cryptography
      • AES
        • Block Encryption procedure
          • Byte Substitution
          • Shift Row
          • Mix Column
          • Add Key
          • Key Expansion / Key Schedule
        • Mode of Operation
          • ECB
            • Block shuffling
              • Challenge example
            • ECB Oracle
              • Challenge example
          • CBC
            • Bit flipping
              • Challenge example
            • Padding oracle
              • Challenge example
          • OFB
            • Key stream reconstruction
            • Encrypt to Uncrypt
  • 🛠️Pwn
    • General knowledge
      • STACK
        • Variables storage
        • Stack frame
      • PLT and GOT
      • HEAP
        • HEAP operations
        • Chunk
        • Bins
        • Chunk allocation and reallocation
      • Syscall
    • Architectures
      • aarch32
        • Registers
        • Instruction set
        • Calling convention
      • aarch64
        • Registers
        • Instruction set
        • Calling convention
      • mips32
        • Registers
        • Instruction set
        • Calling convention
      • mips64
        • Registers
        • Instruction set
        • Calling convention
      • x86 / x64
        • Registers
        • Instruction set
        • Calling convention
    • Stack exploitation
      • Stack Buffer Overflow
        • Dangerous functions
          • gets
          • memcpy
          • sprintf
          • strcat
          • strcpy
        • Basics
          • Challenge example
        • Instruction pointer Overwrite
          • Challenge example
        • De Bruijn Sequences
        • Stack reading
          • Challenge example
      • Format string
        • Dangerous functions
          • printf
          • fprintf
        • Placeholder
        • Data Leak
          • Challenge example
        • Data modification
          • Challenge example
      • Arbitrary code execution
        • Shellcode
        • ret2reg
        • Code reuse attack
          • Ret2plt
          • Ret2dlresolve
          • GOT Overwrite
          • Ret2LibC
          • Leaking LibC
          • Ret2csu
          • Return Oriented Programming - ROP
          • Sigreturn Oriented Programming - SROP
          • Blind Return Oriented Programming - BROP
            • Challenge example
          • 🔴Call Oriented Programming - COP
          • 🔴Jump Oriented Programming - JOP
          • One gadget
        • Stack pivoting
    • 🛠️Heap exploitation
      • Heap overflow
        • Challenge example
      • Use after free
        • Challenge example
      • 🛠️Double free
      • 🔴Unlink exploit
    • Protections
      • Stack Canaries
      • No eXecute
      • PIE
      • ASLR
      • RELRO
    • Integer overflow
Powered by GitBook
On this page
  • Modular inversion
  • Using extended Euclid's algorithm
  • Continuing the Fermat's little theorem
  • Resource
  1. Cryptography
  2. General knowledge
  3. Maths
  4. Modular arithmetic

Fermat's little theorem

This page is about modular arithmetic. The integers modulo p define a field, denoted Fp.\

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.

Fermat's Little Theorem is a result in number theory that states that if **a ** is an integer and p is a prime number, then for all integers a:

ap−1≡1 mod pa^{p-1} \equiv 1 \bmod pap−1≡1modp

This means that :

ap−1−1p=0\frac{a^{p-1}-1}{p}=0pap−1−1​=0

In cryptography, it is used in the modular exponentiation algorithm, which is a basic building block in many public key encryption algorithms such as the RSA algorithm.

Modular inversion

Modular inversion, also known as modular reciprocal, is the process of finding the multiplicative inverse of an integer a modulo p.

The multiplicative inverse of a modulo p is an integer b such that :

a∗b≡1 mod pa * b \equiv 1 \bmod pa∗b≡1modp

b and is unique for each a and m couple

There is two methods in order to calculate the modular inverse of a number

Using extended Euclid's algorithm

a−1=u mod pa^{-1} = u \bmod pa−1=umodp

Where u is solution for :

a∗u+p∗v=1a*u+p*v = 1a∗u+p∗v=1

This equation is solved using the extended Euclid's algorithm. Exemple with a = 3 and p = 13

a = 3, p = 13
gcd, u, v = egcd(a,p)
# 3 * -4 + 13 * 1 = 1
x = u % p
# 9 = -4 % 13
assert(a * x % p == 1 % p)

Continuing the Fermat's little theorem

The theorem says :

ap−1≡1 mod p  ⟺  ap−1 mod p=1a^{p-1} \equiv 1 \bmod p \iff a^{p-1} \bmod p = 1ap−1≡1modp⟺ap−1modp=1

The equation can be continued :

ap−1≡1 mod pap−1∗a−1≡a−1 mod pap−2∗a∗a−1≡a−1 mod pap−2≡a−1 mod p  ⟺  ap−2 mod p=a−1a^{p-1} \equiv 1 \bmod p \\ a^{p-1} * a^{-1} \equiv a^{-1} \bmod p \\ a^{p-2} * a * a^{-1} \equiv a^{-1} \bmod p \\ a^{p-2} \equiv a^{-1} \bmod p \iff a^{p-2} \bmod p = a^{-1}ap−1≡1modpap−1∗a−1≡a−1modpap−2∗a∗a−1≡a−1modpap−2≡a−1modp⟺ap−2modp=a−1

Using the same example as before :

# 3 * x ≡ 1 mod 13
a = 3
p = 13
# x = a^(p-2) % p
# x = 3^(13-2) % 13
x = pow(a, p-2, p)
# x = 9
assert(a * x % p == 1 % p)

Resource

PreviousGreatest Common DivisorNextQuadratic residues

Last updated 2 years ago

The permit to quickly find the modular inverse such as :

extended euclid's algorithm
Pierre de FermatWikipedia
Logo