šŸ³ļø
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
  • Interesting properties
  • Legendre Symbol
  1. Cryptography
  2. General knowledge
  3. Maths
  4. Modular arithmetic

Quadratic residues

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.

A quadratic residue is a number that is the result of squaring another number. In other words, a number x is a quadratic residue (modulo p) if there exists another number a such that

x2≔aā€Šmodā€Špx^2 \equiv a \bmod px2≔amodp

The set of all quadratic residues modulo **p ** is called the set of quadratic residues of p.

The concept of quadratic residues is important in number theory and cryptography.

For example, the problem of finding quadratic residues is known as the "quadratic residue problem", and it is a hard problem in the sense that solving it for large integers is computationally infeasible.

This makes it useful for creating secure encryption algorithms.

Here is an example :

An integer a=21 has the following square mod 31 : a² ≔ 7 mod 31 .

As a = 21, a² = 7 , the square root of 7 is 21

Note : For the elements of Fp, not every element has a square root. In fact, what we find is that for roughly one half of the elements of Fp, there is no square root.

Theses elements are called quadratic non-residue

Interesting properties

Quadratic Residue * Quadratic Residue = Quadratic Residue
Quadratic Residue * Quadratic Non-residue = Quadratic Non-residue
Quadratic Non-residue * Quadratic Non-residue = Quadratic Residue

Legendre Symbol

There is a method to determine if an integer is a quadratic residue or not with a single calculation thanks to Legendre.

ap≔apāˆ’12ā€Šmodā€Šp\frac{a}{p} \equiv a^{\frac{p-1}{2}} \bmod ppa​≔a2pāˆ’1​modp

This equation obeys :

(a / p) = 1 if a is a quadratic residue and a ≢ 0 mod p
(a / p) = -1 if a is a quadratic non-residue mod p
(a / p) = 0 if a ≔ 0 mod p

Which means given any integer a, calculating pow(a,(p-1)//2,p) is enough to determine if a is a quadratic residue.

PreviousFermat's little theoremNextTonelli-Shanks

Last updated 2 years ago