Lesson 3 of 6
The tiny machine's lock
- 20 min
- 6 checks
- 3 hints
- Go to task
Goal
Emulate the machine with an input instruction, then reverse it to get the key
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.
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- The emulator reads input at IN
- flag holds the lock's flag
- Recovers the lock's key from its target
- Reverses another hidden lock
- Arithmetic wraps around modulo 256
- Stops at HALT