🏳️
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
  • Algorithm
  • Python
  • Resources
  1. Cryptography
  2. General knowledge
  3. Maths
  4. Modular arithmetic

Chinese Remainder Theorem

PreviousTonelli-ShanksNextModular binomial

Last updated 2 years ago

The Chinese Remainder Theorem (CRT) is a mathematical theorem that states that if we have a system of linear congruences (equations of the form "x ≡ a mod m") with pairwise coprime moduli, then there exists a unique solution for x modulo the product of the moduli.

The theorem is called "Chinese" because it was first stated by the ancient Chinese mathematician Sun Tzu Suan Ching.

The CRT is particularly useful in solving systems of simultaneous modular equations that are difficult or impossible to solve individually. For example, if we have two congruences:

x≡3 mod 4x≡2 mod 5x \equiv 3 \bmod 4 \\ x \equiv 2 \bmod 5x≡3mod4x≡2mod5

It is not possible to find a single solution for x that satisfies both congruences.

However, using the CRT, we can find a unique solution for x that satisfies both congruences modulo the product of the moduli (20).

The CRT has many applications in cryptography, number theory, and computer science. For example, it is used in RSA encryption to speed up the calculation of the modular inverse, and in constructing public-key cryptosystems based on integer factorization.

Algorithm

In order to calculate a solution, two variables are needed :

N=n1⋅n2⋅n3⋅...⋅niEi=[Nn1,Nn2,Nn3,...,Nni]N = n_1 \cdot n_2 \cdot n_3 \cdot ... \cdot n_i \\\text{}\\ E_i = \left[ \frac{N}{n_1}, \frac{N}{n_2}, \frac{N}{n_3},...,\frac{N}{n_i}\right]N=n1​⋅n2​⋅n3​⋅...⋅ni​Ei​=[n1​N​,n2​N​,n3​N​,...,ni​N​]

The Ei values are composed multiplying all the n_values each others except n at position i.

For each elements of E , the inverse (mod ni) y can be calculated using the :

Ei⋅yi=1 mod niE_i \cdot y_i = 1 \bmod n_iEi​⋅yi​=1modni​

Then a solution for x can be calculated using the following equation :

x=a1⋅E1⋅y1+a2⋅E2⋅y2+a3⋅E3⋅y3+...+ai⋅Ei⋅yix = a_1 \cdot E_1 \cdot y_1 + a_2 \cdot E_2 \cdot y_2 + a_3 \cdot E_3 \cdot y_3 + ... + a_i \cdot E_i \cdot y_ix=a1​⋅E1​⋅y1​+a2​⋅E2​⋅y2​+a3​⋅E3​⋅y3​+...+ai​⋅Ei​⋅yi​

The unique solution is then x % N

Python

# remind, x = a mod n
# All the a values
a = [2,4,8]
# All the n values
n = [4,5,17]

N = 1

# Calcul N value
for i in n:
    N = N * i

# Compute E
E = [N // n[i] for i in range(len(n))]

# Get all y values
y = [pow(E[i],n[i]-2,n[i]) for i in range(len(n))]

# do the calcul x = ai * Ni * yi ... 
x = 0
for i in range(len(n)):
        x += a[i]*E[i]*y[i]
# Get the uniq solution
x %= N
print(x)

Resources

Fermat's Little theorem
Chinese remainder theoremWikipedia
Logo