#!/usr/bin/env python3 """Verify the length-106 periodic Golay pair using only Python's standard library.""" import argparse import json import hashlib from math import gcd from pathlib import Path N = 106 P = 53 def require(condition, message): if not condition: raise ValueError(message) def parse_row(text): require(isinstance(text, str) and len(text) == N and set(text) <= {'+', '-'}, 'Expected 106 signs') return tuple(1 if c == '+' else -1 for c in text) def paf(row, shift): return sum(x * row[(i + shift) % len(row)] for i, x in enumerate(row)) def difference_multiset(blocks): counts = [0] * N for block in blocks: for i in block: for j in block: counts[(i - j) % N] += 1 return counts def cyclic_product(left, right): result = [0] * N for i, a in enumerate(left): for j, b in enumerate(right): result[(i + j) % N] += a * b return result def sections(row): """Return integer rows on Z/53 from the sum and difference of CRT layers.""" even = [row[h if h % 2 == 0 else h + P] for h in range(P)] odd = [row[h + P if h % 2 == 0 else h] for h in range(P)] return ([(a + b) // 2 for a, b in zip(even, odd)], [(a - b) // 2 for a, b in zip(even, odd)]) def affine_stabilizer(support): return [(u, t) for u in range(1, P) for t in range(P) if {(u*i + t) % P for i in support} == support] def canonical_pair(a, b): """Canonical representative for the Lumsden--Kotsireas--Bright Section 2.1 group.""" def canonical_row(row): x = ''.join('1' if z < 0 else '0' for z in row) neg = str.maketrans('01', '10') variants = [x, x[::-1], x.translate(neg), x[::-1].translate(neg)] return min((v + v)[i:i+N] for v in variants for i in range(N)) candidates = [] for u in range(1, N): if gcd(u, N) != 1: continue for e in (0, 1): transformed = [canonical_row([row[u*i % N] * (-1)**(e*i) for i in range(N)]) for row in (a, b)] candidates.append(tuple(sorted(transformed))) return min(candidates) def verify(w): require(w['length'] == N, 'Wrong period') a, b = (parse_row(w[name]) for name in ('A', 'B')) neg_a = [i for i, x in enumerate(a) if x == -1] neg_b = [i for i, x in enumerate(b) if x == -1] require(w['negative_indices'] == {'A': neg_a, 'B': neg_b}, 'Negative index lists disagree') sums = [sum(a), sum(b)] alternating = [sum(x * (-1)**i for i, x in enumerate(row)) for row in (a, b)] require(sorted(map(abs,sums)) == [4,14] and sorted(map(abs,alternating)) == [4,14], 'Incorrect sum-of-squares signatures') if 'row_sums' in w: require(w['row_sums'] == {'ordinary': sums, 'alternating': alternating}, 'Stored row sums disagree') combined = [paf(a, d) + paf(b, d) for d in range(N)] require(combined == [212] + [0] * 105, 'Failed periodic complementarity') raw = difference_multiset([neg_a, neg_b]) require(sorted((len(neg_a), len(neg_b))) == [46,55] and raw == [101] + [48]*105, 'Incorrect difference family for the unmodified rows') blocks = [[i for i,x in enumerate(row) if x == (-1 if sum(row)>0 else 1)] for row in (a,b)] normalized = difference_multiset(blocks) require(sorted(map(len,blocks)) == [46,51] and normalized == [97] + [44]*105, 'Incorrect (106;46,51;44) difference family') # The coefficient vector of A(-X^23), moved forward by the stated offset. translation = w['one_row_certificate']['translation'] sign = w['one_row_certificate'].get('global_sign',1) require(sign in (-1,1), 'Incorrect signed affine factor') require(isinstance(translation,int) and 0 <= translation < N and translation % 2 == 1, 'Expected an odd CRT offset') twist = [0] * N for i, x in enumerate(a): twist[(23*i + translation) % N] = sign * (-1)**i * x require(twist == list(b), 'Failed signed affine relation') require(gcd(23, N) == 1 and 23**2 % N == N-1, 'Wrong multiplier order') theta = lambda i: (23*i + translation) % N require(all(theta(theta(i)) == (24*translation-i) % N for i in range(N)), 'Wrong square of the affine map') star = lambda z: [z[-j % N] for j in range(N)] aa = cyclic_product(a, star(a)) bb = cyclic_product(b, star(b)) require([x+y for x,y in zip(aa,bb)] == [212] + [0]*105, 'Failed integral cyclic polynomial identity') # All 104 displacements away from 0,53 fall into four-element # multiplier orbits. Shift 53 is automatic since 23*53 = 53 mod 106. seen = {0, P} orbits = [] for d in range(1, N): if d in seen: continue orbit = {d, (23*d)%N, (-d)%N, (-23*d)%N} require(len(orbit) == 4 and not orbit & seen, 'Incorrect shift partition') seen.update(orbit) x, y = paf(a,d), paf(a,(23*d)%N) require(y == -(-1)**d*x, f'Failure in shift orbit containing {d}') orbits.append([d, x, y]) require(len(orbits) == 26 and len(seen) == N, 'Incomplete orbit certificate') require(w['one_row_certificate']['orbit_rows'] == orbits, 'Stored orbit table disagrees') require(w['one_row_certificate']['multiplier'] == 23, 'Stored affine multiplier disagrees') x, v = sections(a) y, z = sections(b) require([2*sum(x),2*sum(v),2*sum(y),2*sum(z)] == [sums[0],alternating[0],sums[1],alternating[1]], 'Incorrect CRT row sums') for i in range(P): j = (23*i + translation) % P require(z[j] == -sign*x[i] and y[j] == sign*v[i], 'Failed signed CRT twist') for r, s in ((x,y),(v,z)): require([paf(r,d) + paf(s,d) for d in range(P)] == [P]+[0]*(P-1), 'Failed ternary sector complementarity') sx = {i for i, value in enumerate(x) if value} sz = {i for i, value in enumerate(z) if value} require(len(sx) == len(sz) and 0 < len(sx) <= 26 and {(23*i+translation)%P for i in sx} == sz, 'Incorrect two small supports') require(affine_stabilizer(sx) == affine_stabilizer(sz) == [(1,0)], 'Unexpected affine symmetry of an individual small support') peak = max(abs(paf(row,d)) for row in (a,b) for d in range(1,N)) canonical = canonical_pair(a,b) fingerprint = hashlib.sha256(('\n'.join(canonical)+'\n').encode('ascii')).hexdigest() if 'equivalence' in w: require(w['equivalence']['max_absolute_off_origin_paf'] == peak and w['equivalence']['canonical_sha256'] == fingerprint, 'Stored equivalence invariants disagree') return {'length': N, 'ordinary_sums': sums, 'alternating_sums': alternating, 'max_absolute_off_origin_paf': peak, 'canonical_sha256': fingerprint, 'nonzero_shifts_checked': N-1, 'origin_paf_sum': 212, 'raw_difference_parameters': [106,len(neg_a),len(neg_b),48], 'normalized_difference_parameters': [106,46,51,44], 'one_row_shift_orbits': len(orbits), 'small_support_size': len(sx), 'twist_multiplier': 23, 'twist_translation': translation, 'twist_sign': sign, 'individual_small_support_affine_stabilizer_sizes': [1,1]} if __name__ == '__main__': parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('witness', nargs='*', type=Path, help='default: verify all bundled witnesses') args = parser.parse_args() root = Path(__file__).resolve().parent files = args.witness or [root / 'witness.json', *sorted(root.glob('witness[0-9]*.json'))] results = [dict(witness=path.name, **verify(json.loads(path.read_text()))) for path in files] require(len({r['canonical_sha256'] for r in results}) == len(results), 'Two supplied representatives are equivalent') if not args.witness and [p.name for p in files] == ['witness.json', 'witness2.json', 'witness3.json', 'witness4.json']: require([r['max_absolute_off_origin_paf'] for r in results] == [14,18,30,34], 'Published inequivalence peaks disagree') print(json.dumps(results, indent=2))