Solving a one-line Python challenge involving Mersenne numbers and cyclic bit rotations.
like why 67? this is the 3rd ctf where author had to use 67 for a challenge. smh.
The entire challenge is a single line:
assert __import__('re').match('SEKAI{[67]{67}}$',flag:=input()) and not int.from_bytes(flag.encode())%~(6+~7)**67
Two conditions on the flag:
SEKAI{[67]{67}}$ — a literal 6-character prefix, a 67-character body made entirely of '6' and '7', and a closing brace.~(6+~7)**67.Python's ~x is -x - 1. Walking through the expression:
~7 = -86 + (-8) = -2(-2)**67 = -(2**67) (odd exponent keeps the sign)~(-(2**67)) = 2**67 - 1So the divisor is 2**67 - 1 — a Mersenne number, and the real constraint is simply:
int(flag_bytes) ≡ 0 (mod 2**67 - 1)
With a 67-character free body, this looks like a search over 2**67 possibilities; way too big to brute force (i tried), and (as the long trial-and-error in this thread showed) naive hill-climbing or "reconstruct bits from a target integer" approaches either don't converge or produce bytes outside {0x36, 0x37}.
The trick is recognizing that flipping a single body character between '6' (0x36) and '7' (0x37) changes the overall flag integer by a known, fixed amount: 256^k for whatever byte-position k that character sits at (mod the modulus). Since the modulus is 2**67 - 1, and 256 = 2**8, those contributions are really just powers of 2 modulo 2**67 - 1 — and multiplication by 2 mod a Mersenne number 2^n - 1 is just a cyclic bit rotation.
Because gcd(8, 67) = 1, stepping through the 67 body positions and computing 2^(8k mod 67) for each one visits every residue from 2^0 to 2^66 exactly once — no repeats, full coverage. That's the whole challenge in one sentence: the 67 "flip" operations correspond bijectively to the 67 bits of a 67-bit number.
SEKAI{666...6}) — call it base_rem.target = -base_rem mod (2**67 - 1). This is a 67-bit number.i, figure out which bit of target that position controls (via the (8*(67-i)) mod 67 rotation), and flip '6' → '7' exactly where that bit is 1.Done. no search, no iteration, just a direct bit-to-character mapping computed in microseconds.
MOD = 2**67 - 1
base_rem = int.from_bytes(b"SEKAI{" + b"6"*67 + b"}") % MOD
target = (-base_rem) % MOD
body = bytearray(b"6"*67)
for i in range(67):
exp = (8 * (67 - i)) % 67
if target & (1 << exp):
body[i] = 0x37
print(b"SEKAI{" + body + b"}")
Worth noting for the record, since the thread shows the failure modes clearly:
0xff.The "muhehe" is purely number-theoretic: recognizing 2**67 - 1 as a Mersenne number turns "find a magic 67-character string" into "read off the bits of one integer."