Writeups
  • UMASS CTF 2024
    • Crypto-Brutal Mogging
    • Crypto - Shuffling as a Service
  • GPN CTF 2024
    • Crypto-Boombox
  • WANICTF 2024
    • Crypto - Replacement
    • Crypto - Easy Calc
    • Crypto - dance
    • Crypto - speedy
    • Crypto - Many Xor Shift
    • Crypto - uf
  • UIUCTF 2024
    • Crypto-Without a Trace
    • Crypto-Determined
    • Crypto-Naptime
    • Crypto-Snore Signatures
    • Crypto-Groups
  • DeadSec CTF 2024
    • Crypto - Raul Rosas
    • Crypto - SSP
    • Crypto - Password Guesser
  • corCTF 2024
    • Crypto - Steps
    • Crypto - Monkfish
    • Crypto - Anglerfish
  • LITCTF 2024
    • Crypto- Symmetric RSA
    • Crypto -Truly Symmetric RSA
  • IrisCTF 2025
    • Crypto - knutsacque
  • UofTCTF 2025
    • Misc - Simple Signing
  • HTB CyberApocalypse
    • Crypto - Copperbox
  • BreachCTF 2025
    • Crypto - Taaffeite Encryption
    • Crypto - Big Stuff
Powered by GitBook
On this page
  • Full Implementation
  • Challenge
  1. WANICTF 2024

Crypto - Easy Calc

😆 - Laika

PreviousCrypto - ReplacementNextCrypto - dance

Last updated 11 months ago

Challenge

def f(s, p):
    u = 0
    for i in range(p):
        u += p - i
        u *= s
        u %= p
    return u
    
p = getPrime(1024)
s = random.randint(1, p - 1)

A = f(s, p)

Given the value of p and A, we have to find the value of s, which is used in encrypting the flag

f(s,p) generates a polynomial of the form

f(s,p)=(∑i=0p−1(p−i)⋅si)mod  pf(s, p) = \left( \sum_{i=0}^{p-1} (p - i) \cdot s^{i} \right) \mod p f(s,p)=(i=0∑p−1​(p−i)⋅si)modp

Since it is under modulo p, therefore we can write the polynomial as

f(s,p)=(∑i=0p−1(−i)⋅si)mod  pf(s, p) = \left( \sum_{i=0}^{p-1} (- i) \cdot s^{i} \right) \mod p f(s,p)=(i=0∑p−1​(−i)⋅si)modp

On expanding, we get

f(s,p)=−(1⋅s1+2⋅s2+3⋅s3+...+(p−1)⋅sp−1)mod  p \quad f(s, p) = - \left( 1 \cdot s^{1} + 2 \cdot s^{2} + 3 \cdot s^{3} + ... + (p-1) \cdot s^{p-1} \right) \mod pf(s,p)=−(1⋅s1+2⋅s2+3⋅s3+...+(p−1)⋅sp−1)modp

This form of series is known as an AGP series or arithmetico-geometric progression. While there is a direct formula to calculate the sum of the series, in this case, it is easier to use the method which is used to derive the formula to solve rather then substituting into the formula itself.

Let2f(s,p)=−(1⋅s1+2⋅s2+3⋅s3+...+(p−1)⋅sp−1)mod  p2222222222222s⋅f(s,p)=−(0⋅s1+1⋅s2+2⋅s3+...+(p−2)⋅sp−1+(p−1)⋅sp)mod  p\phantom{} Let \phantom{2} f(s, p) = - \left( 1 \cdot s^{1} + 2 \cdot s^{2} + 3 \cdot s^{3} + ... + (p-1) \cdot s^{p-1} \right) \mod p \phantom{22222222222} \\ \phantom{22} s\cdot f(s, p) = - \left( 0 \cdot s^{1} + 1 \cdot s^{2} + 2 \cdot s^{3} + ... + (p-2) \cdot s^{p-1} + (p-1) \cdot s^{p} \right) \mod p \phantom{} Let2f(s,p)=−(1⋅s1+2⋅s2+3⋅s3+...+(p−1)⋅sp−1)modp2222222222222s⋅f(s,p)=−(0⋅s1+1⋅s2+2⋅s3+...+(p−2)⋅sp−1+(p−1)⋅sp)modp

On subtracting the two equations, we get

(1−s)⋅f(s,p)=−(s1+s2+s3+...+sp−1+(p−1)⋅sp)mod  p(1-s) \cdot f(s,p) = -( s^{1}+s^{2}+s^{3}+ ...+s^{p-1}+ (p-1) \cdot s^{p} ) \mod p (1−s)⋅f(s,p)=−(s1+s2+s3+...+sp−1+(p−1)⋅sp)modp

We now use the series formula for geometric progressions to simplify

(1−s)⋅f(s,p)=−((s⋅(1−sp−1))1−s+(p−1)⋅sp)mod  p(1-s) \cdot f(s,p) = -(\frac{( s \cdot (1- s^{p-1}))}{1-s }+ (p-1) \cdot s^{p} ) \mod p (1−s)⋅f(s,p)=−(1−s(s⋅(1−sp−1))​+(p−1)⋅sp)modp

From Fermat's theorem, we get

1−sp−1≡0(modn)1 - s^{p-1} \equiv 0 \pmod n1−sp−1≡0(modn)
p−1⋅sp≡−s(modp)p-1 \cdot s^{p} \equiv -s \pmod pp−1⋅sp≡−s(modp)

Substituting into the equation, we get

f(s,p)=s1−smod  pf(s,p) = \frac{s}{1-s} \mod pf(s,p)=1−ss​modp

Finally, we get

f(s,p)=A=11−s−1(modp)f(s,p) = A = \frac{1}{1-s} -1 \pmod pf(s,p)=A=1−s1​−1(modp)
s≡−(A+1)−1+1(modp){s} \equiv -(A+1)^{-1} + 1 \pmod p s≡−(A+1)−1+1(modp)

Therefore, we can simply attain the value of s by a single line of code, after which we get the flag

s= inverse(-A-1,p)+1
#FLAG{Do_the_math396691ba7d7270a}

Full Implementation