CH - MATH BRAINTEASERS 2 - Real Eisenstein
Challenge
import math
from decimal import *
getcontext().prec = 100
FLAG = "crypto{???????????????}"
PRIMES = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103]
h = Decimal(0.0)
for i, c in enumerate(FLAG):
h += ord(c) * Decimal(PRIMES[i]).sqrt()
ct = math.floor(h*16**64)
print(f"ciphertext: {ct}")
The challenge
The server has a list of the first 27 prime numbers and for each prime number it sums to the variable h the value ord(c) * Decimal(PRIMES[i]).sqrt()
This can be interpreted as a Knapsack problem where we have to find the right coefficients centered around a certain value to reach a target. This type of problem can be easily solved
Solution
The oracle shows us that using deterministic random seed it creates the vector a; this vector is afterwards used as coefficients for each byte of the flag. After, the total is being summed with an eror vector e, and then reduced modulo q.
There are a lot of ways to attack this cipher; the most logical way is trying to find the error vector using learnt LWE techniques.
The idea
Collect m samples (m > n) and stack them:
If e were zero, Gaussian elimination mod q would give s immediately. Because e is tiny, we can find it with a lattice: e is a very short vector hiding inside a lattice we can build from A and b.
Once we know e, we solve and read off the flag.
How do we build the lattice ??
Well, remember we are looking for a short vector
corresponding to . Basically we need a Lattice where we are reducing our b vector by coeffients of a and we can reduce by p since reducing by modulus doesnβt cost anything. We build the lattice generated by the rows of the following block matrix :
Running LLL on this basis will expose a target row where the last element is and all other components are bounded by 1 (representing the error entries). Once we extract , we resolve the exact linear system over :
Solve
import math
PRIMES = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103]
R = RealField(400)
ct = 1350995397927355657956786955603012410260017344805998076702828160316695004588429433
rowe = [floor(R(p).sqrt() * (16**64) ) for p in PRIMES][:23]
length = len(rowe)
mid = 80
M = block_matrix(ZZ,
[
[identity_matrix(R,length,length),matrix(R,length,1,rowe),0],
[zero_matrix(R,1,length),ct - (mid*sum(rowe)),mid]
])
for lin in M.LLL():
if abs(lin[-1]) != W:
continue
vett = [mid + x for x in lin[:length]]
vett1 = [mid - x for x in lin[:length]]
if all(32 < x < 128 for x in vett):
print(bytes(vett))
if all(32 < x < 128 for x in vett1):
print(bytes(vett1)) # crypto{r34l_t0_23D_m4p}
Flag
crypto{r34l_t0_23D_m4p}