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

Tonelli-Shanks

In a congruence of the form r2 ≔ a mod p, Tonelli-Shanks calculates r.

Tonelli-Shanks doesn't work for composite (non-prime) moduli. Finding square roots modulo composites is computationally equivalent to integer factorization - that is, it's a hard problem.

The main use-case for this algorithm is finding elliptic curve co-ordinates. Its operation is somewhat complex so we're not going to discuss the details. But there is a python implementation

def legendre(a,p):
    return pow(a, (p-1) // 2,p)

def tonelli(a,p):
    assert(legendre(a,p) ==1, "not a quadratic-residue")
    q = p - 1
    s = 0
    while q % 2 == 0:
        q //= 2
        s += 1

    if s == 1 :
        return pow(a, (p + 1)//4, p)

    for z in range(2, p):
        if p - 1 == legendre(z, p):
            break

    c = pow(z, q, p)
    r = pow(a, (q+1) // 2, p)
    t = pow(a, q, p)

    m = s

    t2 = 0

    while (t - 1) % p != 0:
        t2 = (t * t) % p
        for i in range(1, m):
            if (t2 -1) % p == 0:
                break
            t2 = (t2 * t2) % p
        b = pow(c, 1 << (m - i - 1), p)
        r = (r * b) % p
        c = (b * b) % p
        t = (t * c) % p
        m = i
    return r

Note the line 13 ? If s == 1 it's possible to quickly get the square root value with the following equation :

a=ap+14ā€Šmodā€Šp\sqrt a = a^{\frac{p+1}{4}} \bmod pa​=a4p+1​modp

This occur cause a property of primes numbers : all primes that aren't 2 are of the form p ≔ 1 mod 4 or p ≔ 3 mod 4, since all odd numbers obey these congruences.

(ap+14)2≔ap+12≔a(pāˆ’12+1)≔apāˆ’12ā‹…amod  p(a^{\frac{p+1}{4}})^2 \equiv a^{\frac{p+1}{2}} \equiv a^{(\frac{p-1}{2}+1)} \equiv a^{\frac{p-1}{2}} \cdot a \mod p(a4p+1​)2≔a2p+1​≔a(2pāˆ’1​+1)≔a2pāˆ’1​⋅amodp

It's possible cause p + 1 is divisible by 4 unlike the case p ≔ 1 mod 4 and a is a quadritic residue so it follows that :

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

Resources

PreviousQuadratic residuesNextChinese Remainder Theorem

Last updated 2 years ago

Take a look the case p ≔ 3 mod 4 with a derivation

Fermat's little theorem
Significance of 3mod4 in squares and square roots mod n?Cryptography Stack Exchange
Logo