šŸ³ļø
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
  • How to calculate ?
  • Divisors listing
  • Euclid's algorithm
  • Extended Euclid's Algorithm
  • How it works ?
  • Resources
  1. Cryptography
  2. General knowledge
  3. Maths
  4. Modular arithmetic

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).

How to calculate ?

There is several methods to obtain a GCD :

Divisors listing

The first method consist, as this names sounds for, list every divisor for each integers.

For a = 12, b = 8 , the divisors of a: {1,2,3,4,6,12} and the divisors of b: {1,2,4,8}. Comparing these two, then gcd(a,b) = 4.

This method is easy and can be quickly done for small integer but if the integers are a= 54213214, b=32541548 this can take a while.

Euclid's algorithm

This is a two step algorithm :

  1. Do an Euclid's Division on the bigger integer (A) by the lower (B), keep the rest and B

  2. If the rest = 0 then gcd = B else, do the step 1 again with A = B and B = rest.

def gcd(a, b):
    if a == 0:
        return b
    return gcd(b%a, a)

Extended Euclid's Algorithm

The extended Euclidean algorithm is an algorithm for finding the greatest common divisor (GCD) of two integers and finding the coefficients of the linear combination of the two integers that give the GCD.

The extended Euclidean algorithm can be used to solve Diophantine equations, which are equations with integer solutions, and is also used in public key cryptography, such as the RSA algorithm, to find the modular inverse of an integer.

How it works ?

The extended Euclidean algorithm works by keeping track of the intermediate remainders and division results within the Euclid's algorithm and using them to calculate the coefficients.

At each step of the algorithm, the previous remainders and division results are used to calculate the new coefficients. The algorithm continues until the final remainder is zero.

def egcd(a,b):
    if b == 0:
        return a,0,1

    gcd, u1, v1 = egcd(b, a%b)

    u = v1 - (a//b) * u1
    v = u1

    return gcd, u, v

Resources

PreviousModular arithmeticNextFermat's little theorem

Last updated 2 years ago

The coefficients are often represented as the and they satisfy the equation:

GCD(a,b)=aāˆ—u+bāˆ—vGCD(a,b) = a*u + b*vGCD(a,b)=aāˆ—u+bāˆ—v
Bezout's identity
https://www.dcode.fr/pgcdwww.dcode.fr