lsd2 invert a hardware fractal renderer

rev
flag d4rk{tr1pPy_fr4ctl!}c0de

d4rk{tr1pPy_fr4ctl!}c0de

The fractal the netlist renders from the flag

A 500-point reverse of a hardware netlist: you invert a fractal renderer built from Yosys core + mix blocks all the way back into the flag.

Summary

The handout is a Yosys netlist (core + mix) plus a testbench tb.v. tb.v turns a 24-character flag into six 32-bit chunks and, for every pixel (x, y), computes one 8-bit "level" per chunk (core) and folds the six levels into RGB (mix) to render a 1024×1024 image. The shipped chall.png is the author's render of the real flag, so the challenge is exactly invert "flag → image".

Two things make it tractable:

  1. mix is GF(2)-affine per channel, not a colour blend — each output channel is an XOR of a constant with a handful of linear maps of individual levels. Each channel therefore exposes only a subset of the six levels, which lets you solve the chunks one channel at a time.
  2. core is a Newton-fractal iteration in Q32.32 fixed point on a cubic whose coefficients are the chunk bytes, so each chunk's level field can be reproduced exactly offline in C (~1 µs/level) after it is reversed from a cycle-accurate trace.

Recovered chunks: d4rk | {tr1 | pPy_ | fr4c | tl!} | c0de ⇒ d4rk{tr1pPy_fr4ctl!}c0de (24 chars), confirmed accepted on the scoreboard.

Solution / Exploit

1. mix is XOR-affine (48-bit → 24-bit)

Measured with the verilator mix and then proven exact:

output
r = 0x60 ^ R0(l0) ^ R3(l3) ^ R5(l5)
g = 0x00 ^ G1(l1) ^ G3(l3) ^ G4(l4)
b = 0x60 ^ B2(l2) ^ B4(l4) ^ B5(l5)

Each R*/G*/B* is an 8×8 GF(2) map applied to a single level, and the channel output is XOR-linear in its contributing levels. Verified on 200/200 random 6-tuples. So:

  • r-channel sees levels l0, l3, l5
  • g-channel sees l1, l3, l4
  • b-channel sees l2, l4, l5

2. core = Newton-fractal iteration in Q32.32

Reversed from a cycle-accurate trace of the microcoded netlist (op_trace_full.log, produced by the verilator harness op_tb_dbg.v + the instrumented netlist copy chall_dbg.v):

  • Chunk bytes b3..b0 (MSB first) become cubic coefficients H_i = (b_i − 79)·178956971, i.e. (b − 79)/24 in Q32.32 (79 = const_rom[0], 1/24 = const_rom[1]).
  • z0 = ((px − 512)·7/1024, (py − 512)·7/1024) (const_rom[2] = 512, const_rom[3] = 7/1024).
  • Newton's method on p(z) = H0·z³ + H1·z² + H2·z + H3: unit_a is a complex MAC y = a·b + c with m=1 ⇒ conjugate form; unit_b is a per-lane (a<<32)/b. Iterate z ← z − p(z)/p'(z) until both lanes of the step are within 1/2^20 (up to ~255 iterations).
  • Colouring: t = step·2^20; u = |t|²; C1 = unit_c(u); a = 9/8192 − C1; b = a·(ln2/2); C2 = unit_c(b); c = C2 − 1/4096; d = n/65536 − c; e = d/20; f = 0.9922·e; q = floor(sqrt(f_raw)) (unit_d = integer sqrt, unit_c = 65536·log2(raw)).
  • Implemented as op_lvl.c (C, ~1 µs/level). Validated against the verilator core oracle: 1200/1200 exact on random (chunk, px, py) and text-like chunks (op_val_result.json), after fixing three details the trace exposed:
    1. the complex MAC keeps full precision until one final >>32;
    2. convergence needs both lanes inside 1/2^20;
    3. q is clamped/saturated at 255.

3. Chunk recovery (16 pixels spread over chall.png)

The required values come from chall.png. The flag format is d4rk{...}c0de, so the first chunk (d4rk = l0) and the last (c0de = l5) are known oracle-exact. The remaining chunks fall out one channel at a time (op_solve.c, stages 1/2/3):

  • l3 appears only in the r-channel ⇒ brute-force c3 (stage 1, 94³·… candidates). A narrow-charset run (op_s1b.out) yields three 16/16 hits — fr4c, gs4d, jy0f — because R3 has rank 4 (256/16 = 16 level values share every R3 image), so collisions are expected. Disambiguation is not done on the r-channel: it comes from the end-to-end oracle check in §4 (only c3 = fr4c makes all three channels reproduce chall.png). Result: c3 = "fr4c" (0x66723463).
  • l1/l4 appear only in the g-channel ⇒ meet-in-the-middle on G1(l1) ^ G4(l4) (stage 2): unique 16/16 hit c1 = "{tr1", c4 = "tl!}".
  • l2 appears only in the b-channel ⇒ brute-force c2 (stage 3): 16/16 hits pPy_ (0x7050795f) and a decoy .N%?; resolved on 40 fresh oracle pixels (pPy_ 40/40, decoy 30/40).

4. Verification (independent of the model, via the netlist oracle)

obj_probe/simprobe (a compiled chall_net.v core) plus the exact mix module:

  • flag d4rk{tr1pPy_fr4ctl!}c0de → 120/120 random pixels reproduce chall.png exactly (r, g, b), and 60/60 on a second pixel set.
  • every case variant of that flag fails (4/60, 5/60, 3/60, …), except ...fr4ctl1}... (23/60 ⇒ wrong), so the string is uniquely determined.

Ad-hoc re-verification (work/op_verify.py, which recompiles the changed sources against read-only copies of the netlist oracles in a temp cwd — the shared handout dir is untouched):

output
1. level model vs netlist `core` oracle ....... 400/400 exact
2. op_solve stage 1 on a synthetic render ..... finds c3=lag_ 16/16 (self-test)
3. op_solve stage 1 on chall.png .............. unique c3=fr4c 16/16
4. flag -> oracle levels -> mix -> chall.png .. 50/50 pixels identical
5. format ..................................... len 24, d4rk{ ... }c0de
=> ALL CHECKS PASSED. Flag also confirmed ACCEPTED on the scoreboard.

op_lvl.c now defaults to the validated configuration (DIVMODE 0 = floor, EXITMODE 1 = both step lanes inside 1/2^20); that pair is what gives 100 % agreement with the netlist.

Recovered chunks

output
d4rk | {tr1 | pPy_ | fr4c | tl!} | c0de

Flag(s)

output
d4rk{tr1pPy_fr4ctl!}c0de

Alternates

  • The deliverable README table lists d4rk{tr1pPy_fr4ct!} (no l in fr4ctl, no c0de suffix). The source is authoritative and differs: RESULT-lsd2.md and HANDOFF.md both give d4rk{tr1pPy_fr4ctl!}c0de and record it as accepted on the scoreboard, and the 24-character field (d4rk{ + tr1pPy_ + fr4c + tl!} + c0de) matches tb.v's "24-char flag" exactly. The README value appears to be a truncated/typo'd copy.

Notes / Pitfalls

  • mix is not a colour blend. It is an XOR-affine bit map; naively "inverting the colour" gives nothing. The per-channel structure (r: l0,l3,l5; g: l1,l3,l4; b: l2,l4,l5) is exactly what makes a staged search possible.
  • r-channel collisions are expected, not a bug. R3 has rank 4, so 16 different level values share every R3 image — hence three 16/16 hits for c3. You cannot disambiguate on the r-channel alone; use the end-to-end oracle reproduction across all three channels.
  • The ~40 hours render comment in tb.v is wrong. One level is ~1 µs in C / ~0.2 s in verilator, so a full 1024×1024 render is practical.
  • Old lsd (year 1) was the same idea. lsd2 just moved the field to Q32.32 fixed point and made the iteration Newton's method on a cubic whose coefficients are the chunk.
  • core details that only the trace reveals. Full-precision complex MAC until one final >>32; both-lanes 1/2^20 exit condition; saturated q. Getting any of these wrong breaks agreement with the netlist.