Lesson 3 of 6

The tiny machine's lock

Goal

Emulate the machine with an input instruction, then reverse it to get the key

01

Explanation

PROGRAM is written for a tiny machine like the one in the Program Analysis track, but with an added input instruction (IN) that reads a byte from a key the user types. So the program is a lock: it takes each byte of the key, transforms it with fixed instructions, and compares the result with the target bytes stored in TARGET. The right key is the one that produces exactly that target, and it's the flag you're after.

0x00  HALT       stop
0x01  LOAD imm   acc = imm
0x02  ADD  imm   acc = (acc + imm) & 0xFF
0x03  XOR  imm   acc = acc ^ imm
0x04  SUB  imm   acc = (acc - imm) & 0xFF
0x05  OUT        output.append(acc)
0x06  IN         acc = next input byte

The accumulator (acc) starts at zero, and IN fills it with the next byte of the input key, so IN takes no operand. To get the key, reverse the lock: for each position, try input bytes until your emulator produces the target byte, or read that position's instructions and undo them in reverse order.

02

Your task

Write a function run(program, data) that executes the program, reads the next input byte from data at every IN, and returns the output as bytes. Then write a function recover(program, target) that returns, as bytes, the input key that makes the program output target. Finally, store the flag as a string in flag. The checks build other locks, so hard-coding the flag alone isn't enough.

Checks

6 checks
  1. The emulator reads input at IN
  2. flag holds the lock's flag
  3. Recovers the lock's key from its target
  4. Reverses another hidden lock
  5. Arithmetic wraps around modulo 256
  6. Stops at HALT
03

Hints

0/3
Track lessons0 of 6 done

Six challenges of rising difficulty where you reverse a check function or bytecode and extract the key with your own code, not by copying it.

Advanced level, 6 lessons, 2 hours in total.

main.py
OutputPython (Pyodide)

Loading Python. This only happens once. Your browser keeps it after that.

–/6

Checks start once the environment is ready.

6 checks listed under “Your task”.

Track lessons0 of 6 done

Six challenges of rising difficulty where you reverse a check function or bytecode and extract the key with your own code, not by copying it.

Advanced level, 6 lessons, 2 hours in total.