from pathlib import Path import hashlib import json import struct
MAGIC = b"QFLAC2\x00\x00" UINT32_LEN = 4 BITS_PER_BYTE = 8 VORBIS_COMMENT = 4
def dot_gf2(left, right): return sum(a & b for a, b in zip(left, right)) & 1
def recover_secret(rows, width): for value in range(1, 1 << width): secret = [(value >> (width - 1 - bit)) & 1 for bit in range(width)] if all(dot_gf2([int(ch) for ch in row], secret) == 0 for row in rows): return secret raise ValueError("no non-zero Simon period vector found")
def pack_lsb_first(bits): packed = bytearray() for offset in range(0, len(bits), BITS_PER_BYTE): byte = 0 for bit_index, bit in enumerate(bits[offset:offset + BITS_PER_BYTE]): byte |= bit << bit_index packed.append(byte) return bytes(packed)
def sha256_counter(label, source, size): output = bytearray() counter = 0 label_bytes = label.encode() while len(output) < size: output += hashlib.sha256( label_bytes + source + struct.pack(">I", counter) ).digest() counter += 1 return bytes(output[:size])
def parse_qflac(parts): blob = b"".join(path.read_bytes() for path in parts) if not blob.startswith(MAGIC): raise ValueError("bad QFLAC2 magic")
offset = len(MAGIC) manifest_len = struct.unpack_from("<I", blob, offset)[0] offset += UINT32_LEN manifest = json.loads(blob[offset:offset + manifest_len]) offset += manifest_len
metadata_len = struct.unpack_from("<I", blob, offset)[0] offset += UINT32_LEN metadata = blob[offset:offset + metadata_len] offset += metadata_len
records = [] tag_len = manifest["tag_len"] for payload_len in manifest["payload_lengths"]: ciphertext = blob[offset:offset + payload_len] offset += payload_len tag = blob[offset:offset + tag_len] offset += tag_len records.append((ciphertext, tag))
if offset != len(blob): raise ValueError("QFLAC2 parser did not consume the full concatenated blob") return manifest, metadata, records
def extract_title(flac_bytes): if flac_bytes[:4] != b"fLaC": raise ValueError("decrypted target is not FLAC")
offset = 4 while True: header = flac_bytes[offset] offset += 1 is_last = bool(header & 0x80) block_type = header & 0x7F block_len = int.from_bytes(flac_bytes[offset:offset + 3], "big") offset += 3 block = flac_bytes[offset:offset + block_len] offset += block_len
if block_type == VORBIS_COMMENT: pos = UINT32_LEN + int.from_bytes(block[:UINT32_LEN], "little") comment_count = int.from_bytes(block[pos:pos + UINT32_LEN], "little") pos += UINT32_LEN for _ in range(comment_count): size = int.from_bytes(block[pos:pos + UINT32_LEN], "little") pos += UINT32_LEN comment = block[pos:pos + size].decode("utf-8", "replace") pos += size if comment.lower().startswith("title="): return comment.split("=", 1)[1] if is_last: break raise ValueError("missing FLAC Title tag")
def main(): base = Path(__file__).resolve().parent measurement = json.loads((base / "记忆的宝藏.json").read_text(encoding="utf-8"))
bits = [] secrets = [] for index in range(measurement["instance_count"]): secret = recover_secret( measurement["samples"][str(index)], measurement["secret_bits"], ) secrets.append("".join(str(bit) for bit in secret)) bits.extend(secret)
packed = pack_lsb_first(bits) manifest, metadata, records = parse_qflac([ base / "quantum_echo1.qflac", base / "quantum_echo2.qflac", ])
auxiliary_plain = sha256_counter( manifest["auxiliary"]["label"], packed, manifest["payload_lengths"][0], ) stream = bytes(a ^ b for a, b in zip(records[0][0], auxiliary_plain)) target_plain = bytes(a ^ b for a, b in zip(records[1][0], stream))
recovered = metadata + target_plain (base / "recovered.flac").write_bytes(recovered) title = extract_title(recovered)
print("Simon secrets:", ",".join(secrets)) print("packed_measurements:", packed.hex()) print("title:", title) print(f"flag: 0xGame{{{title}}}") print("wrote: recovered.flac")
if __name__ == "__main__": main()
|