oneline6ryp7o

Solving a one-line Python challenge involving Mersenne numbers and cyclic bit rotations.

CryptoMiscSekaiCTFEasy

The Challenge

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:

  1. It matches SEKAI{[67]{67}}$ — a literal 6-character prefix, a 67-character body made entirely of '6' and '7', and a closing brace.
  2. The integer formed by treating the flag's raw bytes as a big-endian number is divisible by ~(6+~7)**67.

Decoding the Operator Soup

Python's ~x is -x - 1. Walking through the expression:

So the divisor is 2**67 - 1 — a Mersenne number, and the real constraint is simply:

int(flag_bytes) ≡ 0 (mod 2**67 - 1)


The Insight That Makes This Solvable

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.


Solving It

  1. Compute the remainder when the flag is all sixes (SEKAI{666...6}) — call it base_rem.
  2. Compute target = -base_rem mod (2**67 - 1). This is a 67-bit number.
  3. For each body position 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"}")

Why the Earlier Attempts Failed

Worth noting for the record, since the thread shows the failure modes clearly:

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."

Computer Repair 1
impossible stego