griin@crypto:~$
All the Writeups

CH - MATH BRAINTEASERS 2 - Real Eisenstein

CryptoHack - MATH BRAINTEASERS 2 - 20261 ottobre 2026

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:

b=Aβ‹…s+emod  qb = A \cdot s + e \mod q

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 Aβ‹…s=bβˆ’emod  qAΒ·s = b βˆ’ e \mod q and read off the flag.

How do we build the lattice ??

Well, remember we are looking for a short vector

corresponding to e=bβˆ’Aβ‹…Smod  qe = b-A\cdot S \mod q. 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 MM:

M=[AT0βˆ’Imβ‹…q0b1]M = \begin{bmatrix} A^{T} & 0\\ -I_{m} \cdot q & 0\\ b & 1 \end{bmatrix}

Running LLL on this basis will expose a target row where the last element is Β±1\pm 1 and all other components are bounded by 1 (representing the error entries). Once we extract ee, we resolve the exact linear system over Fq\mathbb{F}_q:

Aβ‹…flag=bβˆ’errβ€…β€ŠβŸΉβ€…β€Šflag=Aβˆ’1β‹…(bβˆ’err)A \cdot \text{flag} = b - \text{err} \implies \text{flag} = A^{-1} \cdot (b-\text{err})

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}