cd ~

LakeCTF 2026 - 三藏經

This post is written completely without ai, or any review, its the first draft. So there will be ridiculous typo and grammer errors, and raw thoughts.

I participated in LakeCTF 2026 quals with CyKor, thanks to keto suggesting me to play (I was kinda bored that weekend).

I don’t have a ZK proof that I haven’t used AI during playing, so believe it or not, but it was a fun time in a while to actually use brain to solve a ctf challenge.

I seriously think this invitation only, and this parent-inviting team thing was also supercool. I’m definitely sure there are teams that are unbanned yet still used AI, even my team, who knows, but this idea made the comp match the energy of noprize onsite ctfs (like lake, dice, msun). More than happy not to see some teams on the scoreboard.


I solved the lattice challenge 三藏經, and iirc ended with 4 solves. Had a look on the other hard crypto chall which also ended with 4 solves and I didn’t have a clue. There were no team that have solved both of the hard crypto challs, which is funny lol, are they completely on a different skillset or something?

Also I attempted little bit on the challenge “Heavenly Infiltrators” too, and instantly noticed the big integer can be factored and told that to teammates. But we couldn’t finish the solve as we all failed to make cado-nfs factor it in 1 minute. SageMath and cado both always took ~90s, rip. It was interesting that there are (slower than cado) tools can factor it in 1m, and the existence of non-factoring solution.

There was a write-up bounty of 50 + 50 CHF, $$ yum, definitely hoping to win that, also want that t-shirt even though i’m not sure how to receive it as cykor failed to play in the finals this year :(

I’m in need of that extra money, to pay up for HammingDiet 1.5. I’m honestly confident no one is claiming that though, but I wish someone crashes that dream of mine.

The challenge

from Crypto.Util.number import getPrime, isPrime
from random import getrandbits
from buddha import flag

b = 2**9
p, q, e = getPrime(b), getPrime(b), getPrime(b)
n = p*q

with open("scriptures.scroll", "w") as f:
  f.write(f"{ n }\n{ e }\n{ pow(int.from_bytes(flag), e, n) }")
  for _ in range(2**12):
    while not isPrime(pp:=p+getrandbits(b//3)): pass
    while not isPrime(qp:=q+getrandbits(b//3)): pass
    f.write(f"{pp*qp}\n")

RSA style challenge, with 1024 bit modulus. Multiple $(p + a)(p + b)$ values are shared where $p, q$ are 512 bit and $a, b$ are around 170 bits. Also there are insanely big number of samples. I instantly noticed that this might be a very close to bound lattice challenge.

I wish authors use secrets instead of random in these kind, or is it intentional to confuse the players, idk… still I don’t like it..

Note that after writing the ciphertext in the file, there’s no newline after writing samples lol, a bit funny. The scroll file surely also had concatenated big number on the 3rd line.

Approach

Note that this post should look very noobish, I personally think I was never that good at lattice, and it was quite a while after playing lattices with brain so even more rustier.


At first glance it looked lot similar to abpq-rsa series, but completely different style so wasn’t much of a help.

So after removing $pq$, we have lot of numbers which are $ap + bq + c$ where $p, q$ are 512 bits, $a, b$ are 170 bits, $c$ is 340 bits, or around that.

Sounds like OL could be a help to that, if we deal with $c$ pretty well.


So the general approach is to find some vectors are in both of kernal of $a, b$. And $c$ is irrelevant so those vectors are multiplied to $c$ like a random value.

if you want kernel of all $a, b, c$, they are 170, 170, 340 bit and the entire value is 680 bits, no way that will work. It’ll definitely be larger than 680 bit vector’s small kernels.

So i try something like this:

f = open("scriptures.scroll", "r")

a = f.readlines()
f.close()
n = ZZ(a[0])
e = ZZ(a[1])
c = ZZ(a[2])
# I fixed the scroll file already here, about the newline issue

a = a[3:]
a = [ZZ(aa) for aa in a]

assert len(a) == 4096
l = len(a)

diffs = [(aa - n) // 2 for aa in a]
diffs.sort()

n = 200
d = vector(diffs[:n])

FUCK = False
if FUCK:
    d = vector([getrandbits(680) for _ in range(n)])

M = Matrix(n + 1, n + 1)

for i in range(n):
    M[i, n] = d[i]
    M[i, i] = 2^(340)
M = M.LLL()

for m in M:
    print(ZZ(m.norm()^2).nbits() // 2)

with lots of different n value.

You can see that if you set FUCK = True, you replace it with a random samples, so if you succeed, the final printed bitsize should look different from setting FUCK to true or false. However no matter what you do they printed similarly, or with eye-negligible difference. So I thought I met a blocker here.

Depression

I decided I fucking suck and went back to tweaking cado-nfs to factor in 1 minute, but without codex my idea didn’t turn out well. Probably even with codex i don’t think i can manage it to work in 1m.

More cores = more setup time, so it wasn’t helpful to rent a 128 core server, fuck

So I went to have a run, and did a PB.

I hate exercising, but I hate getting fat even more.

While running, I decided that according to my calculation, the needed kernel bound is really close, only few bits off the random bound. So I decided I should try that hard-crypto-chall strat I used sometimes.

Speaking of tight bound, I sorted the samples and only used smallest n values from the start. I wasn’t like, oh shooot i should use small ones only and it might work.

The so called hard-crypto-chall strat

This strat is useless as fuck after AI, but I liked it quite a lot. I’ve used it for poLy1305CG and RBG+++.

But then, is anything not fucking useless nowadays?

Yeah money (100CHF😛) is not fucking useless. Money will not fix all my problems, but it will fix quite a few problems.

The strat is setting a TEST variable (like FUCK) and if its true, you generate a new value from the setup and have access to all private values as well. If its false, you play with the actual output value and of course don’t know the private value.

from Crypto.Util.number import getPrime, isPrime
from tqdm import trange

'''
b = 2**9
p, q = getPrime(b), getPrime(b)
ps = []
qs = []

print(p, q)

with open("test.scroll", "w") as f:
  for _ in trange(2**12):
    while not isPrime(pp:=p+getrandbits(b//3)): pass
    while not isPrime(qp:=q+getrandbits(b//3)): pass
    f.write(f"{pp, qp}\n")
exit()
'''

TEST = False

if TEST:
    p, q = 10789363062861309888798814808588032764306806785433865424678410932800524747702341515569993286408468739813230298828597052362615499991602311638646309702188421, 7397381123640907970304310727231715694974076982765963972897360096245736773528434751284687024429344945286299817798859421327712708795121248831081104931425881
    ps = []
    qs = []
    f = open("test.scroll", "r")
    for i in range(4096):
        a = list(map(int, f.readline()[1:-2].split(", ")[:2]))
        ps.append(a[0])
        qs.append(a[1])
    f.close()

    assert len(ps) == len(qs) == 4096

    ass, bss = vector([pss - p for pss in ps]), vector([qss - q for qss in qs])

    ds = [ps[i] * qs[i] - p * q for i in range(4096)]
else:
    f = open("scriptures.scroll", "r")
    N = ZZ(f.readline())
    e = ZZ(f.readline())
    c = ZZ(f.readline())

    ds = [ZZ(f.readline()) - N for i in range(4096)]
    f.close()

As 4096 samples take quite long to generate everytime, I’ve saved it in test.scroll. Only n values should be necessary but I knew that the bound is tight and the size of error is important so I tried to make the experiment the same: generating 4096 and using only first n.

if TEST: you can check with $p, q, a, b$ to check if you’re going the right way.

mmap = {}


if TEST:
    for i in range(4096):
        mmap[ds[i]] = [ps[i], qs[i], ass[i], bss[i]]
ds.sort()

if TEST:
    ass_real = [mmap[d][2] for d in ds]
    bss_real = [mmap[d][3] for d in ds]

You save the $a, b$ vector here, to check later.

for n in range(50, 70):
    for wb in range(325, 345):

        d = vector(ds[:n])
        ass = vector(ass_real[:n])
        bss = vector(bss_real[:n])

        M = Matrix(n + 1, n + 1)

        w = 2^wb

        for i in range(n):
            M[i, n] = d[i]
            M[i, i] = w
        M = Matrix(ZZ, M.BKZ()[1:, :-1] / w)

        #debug
        MM = Matrix(n, n + 2)

        w = 2^512
        for i in range(n):
            MM[i, i] = 1
            MM[i, n + 0] = ass[i] * w
            MM[i, n + 1] = bss[i] * w
        MM = MM.BKZ()[:-2, :-2]
        # print(MM)
        # print()


        MMM = []
        for mm in MM:
            MMM.append(M.solve_left(mm))
        MMM = Matrix(MMM)

        aa = n - 2 - list(MMM.T[-1]).count(0)
        bb = n - 2 - list(MMM.T[-2]).count(0)

        print(f"{n = }, {wb = }", aa, bb)

Now this main script. (I tested n from smaller values even though it says 50~70 now.)

M holds the result vectors after the actual LLL.

MM holds the small kernel vectors of $a$ and $b$.

I checked if MM’s vectors’ dependency of M vectors. M has n vectors with a full rank, and MM has n - 2 vectors. If all n - 2 vectors of MM is dependent on the FIRST n - 2 vectors of M, it’s a success.

The script prints out n, wb and how many times the last two vectors of M appears in MM vectors’ dependency. 0 is good right, because it means the first n - 1 or n - 2 vectors are real and the remainder is shit.

...
n = 43, wb = 343 18 21
n = 43, wb = 344 13 21
n = 44, wb = 325 40 40
n = 44, wb = 326 14 39
n = 44, wb = 327 37 32
n = 44, wb = 328 26 22
n = 44, wb = 329 26 21
n = 44, wb = 330 20 19
n = 44, wb = 331 0 16
n = 44, wb = 332 16 18
n = 44, wb = 333 0 14
n = 44, wb = 334 17 10
n = 44, wb = 335 23 17
n = 44, wb = 336 22 12
n = 44, wb = 337 19 16
n = 44, wb = 338 19 15
n = 44, wb = 339 25 19
n = 44, wb = 340 20 17
...

Gladly you can see lots of zeros. Both being zero should work, but honestly, I was kinda skeptical that this would work. But when the first 0 popped (means the last vector is shit) lol it was happy.

After running it more, you can find both being 0.

...
n = 52, wb = 341 17 25
n = 52, wb = 342 21 18
n = 52, wb = 343 26 23
n = 52, wb = 344 28 30
n = 53, wb = 325 0 0
n = 53, wb = 326 0 48
n = 53, wb = 327 0 44
n = 53, wb = 328 0 0
n = 53, wb = 329 0 20
n = 53, wb = 330 0 0
n = 53, wb = 331 0 29
n = 53, wb = 332 0 20
n = 53, wb = 333 0 22
n = 53, wb = 334 0 30
n = 53, wb = 335 0 19
n = 53, wb = 336 0 30
n = 53, wb = 337 28 17
n = 53, wb = 338 26 22
n = 53, wb = 339 27 23
...

To be safe than sorry, I ran it more so that it’d be in a middle param instead of the end of the line, and chose n = 61, wb = 329.

Final touch

What after 0 0? You choose the first n - 2 vectors after LLL, and we know its the same span of MM which are kernel of $a, b$.

So first n - 2 vectors’ kernel should be span of $a$ and $b$.


M = Matrix(n + 1, n + 1)

w = 2^wb

for i in range(n):
    M[i, n] = d[i]
    M[i, i] = w
M = Matrix(ZZ, M.BKZ()[1:, :-1] / w)

M = M[:-2].right_kernel_matrix().LLL()

# print(M)
# print()

if TEST:
    print(ass)
    print(bss)

    print(M.solve_left(ass))
    print(M.solve_left(bss))

The final solve_left should be a set of small integers.

a, bb = M[0] * 2, M[1] * 2

# print(a)
assert all(aa > 0 for aa in a)

for i in range(-5, 5):
    for j in list(range(-5, 0)) + list(range(1, 5)):
        b = a * i + bb * j

        if all(bbb > 0 for bbb in b):
            

            mm = Matrix(QQ, [a, b])
            ds = vector(QQ, [ds[i] - a[i] * b[i] for i in range(n)])

            try:
                pq = mm.solve_left(ds)
            except:
                continue

            p, q = ZZ(pq[0]), ZZ(pq[1])

            print(p.nbits(), q.nbits())

            print(p)
            print(q)
            print(N)
            print(p * q)

            assert p * q == N

            d = pow(e, -1, (p - 1) * (q - 1))
            m = pow(c, d, N)

            from Crypto.Util.number import *

            print(long_to_bytes(m))

Since $a$ and $b$ are always even, (prime minus prime) multiply the two vectors.

With the actual output data, M[0] * 2 turned out to be all positive, so that’s surely one of $a$ and $b$, and the other should be some combination, the loop is enumerating those candidates.

EPFL{7h47_w4sn7_supp0s3d_70_h4pp3n...4_highly_7r4in3d_m0nk3y_h4s_b33n_disp47ch3d_70_d34l_wi7h_7his_si7u47i0n} cooool!!