Hemimegaminx group word problem via GAP

misc
flag d4rk{d1d_y0u_s0lv3_th3_H3m1m3g4minx_by_hand_???}

d4rk{d1d_y0u_s0lv3_th3_H3m1m3g4minx_by_hand_???}

Summary

The site (https://<target>) hands out an hourly-scrambled Hemimegaminx state and accepts a move list (at most 1000 moves) at POST /api/solve; the server re-applies the moves and returns the flag when the state reaches solved. The group generated by the six face turns has order

output
587631543342363147711283200000  (~5.9e29)

so brute force / meet-in-the-middle is hopeless (a depth 4+4 = 8-move MITM found no intersection). The challenge is therefore a group-theoretic word problem, not a search: encode the puzzle as a permutation group of degree 60 and ask a group-theory engine (GAP via libgap) for a word in the generators that equals the inverse of the scrambled state. A naive GAP call with only the 6 face turns returns a 3499-letter word (3137 moves after reduction), over the 1000-move limit. Feeding GAP a dense generating set — all 504 distinct elements reachable within two face turns, each carrying its known ≤2-move expansion — shrinks the BSGS transversals, and the returned word is only 554 generators, which expands and face-cancels to 833 moves. That is submitted, the server re-applies it, and returns the flag.

Solution / Exploit

1. The service and the puzzle model

The handout is a web app. GET /api/challenge returns the hourly scramble:

output
{ id, expiresAt, state }

where state = edgePerm[15] + edgeOri[15] + cornerPerm[10] + cornerOri[10]. A move is {face: 0..5, turns: 1..4} (turns are +72°·k about a face axis). The submit endpoint is POST /api/solve with {challengeId, moves:[{face,turns}...]}; the limit is 1000 moves and the server returns the flag when the state is solved.

The engine ships as hemi-hemimegaminx.js / hemi-model.mjs. The quotient geometry is the icosahedron's 12 vertices / 30 edges / 20 triangular faces modulo antipodes: 6 face positions, 15 edge positions and 10 corner positions. The move tables are generated from vertex coordinates (φ = (1+√5)/2) rather than hand-entered cycles, so the model cannot silently disagree with the server. The generated tables are frozen in hemi-tables.json (edgeCycles, cornerCycles, edgeOrientationDelta, cornerSlotCycles, CORNER_ORIENTATION_PERMUTATIONS). A pure-Python port of the same engine (hemi_puzzle.py) is used to verify solutions locally:

python
1def apply_move(st, face, turns):
2    for _ in range(turns):
3        ep = EDGE_CYCLES[face]; ed = EDGE_ODELTA[face]
4        oldP = st['edgePerm'][:]; oldO = st['edgeOri'][:]
5        for p in range(15):
6            d = ep[p]
7            st['edgePerm'][d] = oldP[p]
8            st['edgeOri'][d] = (oldO[p] + ed[p]) % 2
9        cp = CORNER_CYCLES[face]; sc = CORNER_SLOTC[face]
10        oldCP = st['cornerPerm'][:]; oldCO = st['cornerOri'][:]
11        for p in range(10):
12            d = cp[p]
13            oldOri = COP[oldCO[p]]
14            newOri = [0, 0, 0]
15            for s in range(3):
16                newOri[sc[p][s]] = oldOri[s]
17            st['cornerPerm'][d] = oldCP[p]
18            st['cornerOri'][d] = COP_INDEX[tuple(newOri)]
19    return st

2. Encode the puzzle as a permutation group of degree 60

Each edge sticker is 2 stickers per edge (30) and each corner sticker is 3 per corner (30), for degree 60. face_perm(f) sends each sticker to its destination; this was verified consistent with applyMove / isSolved. The conversion lives in hemi_solve_sage.py:

python
1def edge_perm(f):
2    ep = EDGE_CYCLES[f]; ed = EDGE_ODELTA[f]
3    p = [0]*30
4    for pos in range(15):
5        d = ep[pos]
6        for o in range(2):
7            p[pos*2+o] = d*2 + ((o+ed[pos]) % 2)
8    return Permutation([x+1 for x in p])
9
10def corner_perm(f):
11    cp = CORNER_CYCLES[f]; sc = CORNER_SLOTC[f]
12    p = [0]*30
13    for pos in range(10):
14        d = cp[pos]
15        for s in range(3):
16            p[pos*3+s] = d*3 + sc[pos][s]
17    return Permutation(list(range(1, 31)) + [x+31 for x in p])
18
19GENS = [edge_perm(f) for f in range(NF)] + [corner_perm(f) for f in range(NF)]

state_sigma(st) turns the server state into the sticker permutation g (solved → current), inverting the piece permutations and folding in the orientation:

python
1def state_sigma(st):
2    edgePerm = st['edgePerm']; cornerPerm = st['cornerPerm']
3    edgeInv = [0]*15
4    for x in range(15):
5        edgeInv[edgePerm[x]] = x
6    cornerInv = [0]*10
7    for x in range(10):
8        cornerInv[cornerPerm[x]] = x
9    p = [0]*30
10    for i in range(15):
11        pos = edgeInv[i]; e = st['edgeOri'][pos]
12        for o in range(2):
13            p[i*2+o] = pos*2 + ((o+e) % 2)
14    q = [0]*30
15    for i in range(10):
16        pos = cornerInv[i]; O = COP[st['cornerOri'][pos]]
17        Oinv = [0, 0, 0]
18        for s in range(3):
19            Oinv[O[s]] = s
20        for g in range(3):
21            q[i*3+g] = pos*3 + Oinv[g]
22    return Permutation([x+1 for x in p] + [x+31 for x in q])

3. First GAP attempt: 6 face turns → 3499 letters (too long)

Ask GAP for a word in the generators via libgap:

python
1class WordFinder:
2    def __init__(self):
3        self.G = PermutationGroup(GENS)
4        self.Ggap = libgap(self.G)
5        self.F = libgap.FreeGroup(len(GENS))
6        self.epi = libgap.GroupHomomorphismByImages(
7            self.F, self.Ggap,
8            libgap.GeneratorsOfGroup(self.F),
9            libgap.GeneratorsOfGroup(self.Ggap))
10
11    def word(self, g):
12        w = libgap.PreImagesRepresentative(self.epi, libgap(g))
13        ext = list(libgap.ExtRepOfObj(libgap.UnderlyingElement(w)))
14        moves = []
15        for i in range(0, len(ext), 2):
16            gi = int(ext[i]); exp = int(ext[i+1])
17            face = (gi - 1) % NF
18            e = exp % 5          # face-turn order is 5
19            if e != 0:
20                moves.append([face, e])
21        return moves

The solve word is the inverse of the word for g (g = sigma.inverse()). With only the 6 face turns as generators, GAP returns a 3499-letter word — about 3137 moves after reducing exponents mod 5. That is well over the 1000-move limit, so this first attempt fails.

4. The fix: a dense generating set of short elements

Give GAP a dense generating set instead: all 504 distinct elements reachable within two face turns, each with its known ≤2-move expansion. The BSGS transversals become short, so the returned word collapses. hemi_dense_word.py builds the group from the dense generators and extracts the letter representation:

python
1import json
2from sage.libs.gap.libgap import libgap
3D = json.load(open('hemi_dense.json'))
4gens = D['gens']; tgt = D['target']
5
6def to_gap(img):
7    return libgap.PermList([img[i]+1 for i in range(60)])
8
9G = libgap.Group([to_gap(g) for g in gens])
10g_gap = to_gap(tgt)
11phi = libgap.EpimorphismFromFreeGroup(G)
12f = libgap.PreImagesRepresentative(phi, g_gap)
13letters = [int(x) for x in libgap.LetterRepAssocWord(f)]
14print("nletters", len(letters))

This returns a 554-letter word. Expanding each dense generator back into its ≤2 face turns and cancelling adjacent same-face turns gives a 833-move solution, under the 1000 limit.

5. Verify locally, invert, submit

The solve word is the inverse of the word representing the state. Verify by simulating the move list on the server state with hemi_puzzle.apply_word until is_solved is true, then:

output
POST /hemimegaminx/api/solve
{"challengeId": <id>, "moves": [{"face": f, "turns": t}, ...]}

-> HTTP 200
{"ok":true,"flag":"d4rk{d1d_y0u_s0lv3_th3_H3m1m3g4minx_by_hand_???}"}

Flag(s)

output
d4rk{d1d_y0u_s0lv3_th3_H3m1m3g4minx_by_hand_???}

Notes / Pitfalls

  • Do not search the state space. The group order is ~5.9e29 and a meet-in-the-middle at depth 4+4 = 8 moves found no intersection. The problem is a word problem, not a BFS.
  • Commutator algorithm libraries are a dead end. hemi_algs.py enumerates commutators A B A⁻¹ B⁻¹ over all length-1 and length-2 words and classifies their edge/corner action looking for pure 3-cycles and pure flips/twists. That builds a human-style solver, but it is not needed and does not directly give a bounded word.
  • A hand-written stabilizer chain also exists (hemi_wordfind.py, a Sage BSGS word finder with minimal_base / schreier_gens). It is an alternative to GAP; the GAP route with the dense generating set is what was actually used.
  • Orientation is part of the permutation. Sticker encoding must fold edge orientation ((o+ed) % 2) and corner orientation (via COP / cornerSlotCycles) into the images, or the recovered word solves the permutation but not the orientation.
  • Direction. Applying a move right-multiplies the state element; the word that solves a state is the inverse of the word equal to the state element.
  • Reduction mod 5. Each face turn has order 5, so exponents are taken mod 5 before emitting moves; that is what turns the raw 3499-letter word into ~3137 moves.