"""Exact checker for the shared-frame manuscript. Python standard library. Usage: python3 OMEGA_frame_verify.py integer-models.json """ import hashlib import json import sys from fractions import Fraction as F from itertools import product from math import isqrt from pathlib import Path Q, S = 339949771344778, 10**15 q = F(Q, S) EXPECTED = "67901498a864914aaddb4069bab9dab1eb60b9534e680896ccc1f517387d865d" def check(test): if not test: raise ValueError("certificate check failed") def arctan_sum(k, n): return sum(F((-1)**j, (2*j+1)*k**(2*j+1)) for j in range(n)) def constants(): lower = 16*arctan_sum(5,26) - 4*arctan_sum(239,27) upper = 16*arctan_sum(5,27) - 4*arctan_sum(239,26) P = 10**24 r = isqrt(5*P**2) phi_lo, phi_hi = F(P+r,2*P), F(P+r+1,2*P) lo = phi_lo/(phi_lo+upper) hi = phi_hi/(phi_hi+lower) check(q+F(929,10**18) < lo < hi < q+F(930,10**18)) check(F(4196,12343) < q < lo < hi < F(7039,20706)) check(7039*12343-4196*20706 == 1) def encode(word): x = F(1,2) for b in word: check(type(b) is int and b in (0,1)) x = q*x+(1-q)*b return [len(word),hex(x.numerator),hex(x.denominator)] def decode(code): check(type(code) is list and len(code)==3) n, hn, hd = code check(type(hn) is str and type(hd) is str) check(type(n) is int and n >= 0) x = F(int(hn,16),int(hd,16)) check(0 < x < 1 and hex(x.numerator)==hn and hex(x.denominator)==hd) word = [] for _ in range(n): bit = int(x > F(1,2)) x = (x-(1-q)*bit)/q check(0 < x < 1) word.append(bit) check(x == F(1,2)) return word[::-1] def scalar_tests(): constants() for depth in range(11): for word in product((0,1),repeat=depth): code = encode(word) check(decode(code)==list(word)) check(int(code[2],16)==2*q.denominator**depth) invalid = ([1,"0x1","0x2"], [0,"0x2","0x4"], [-1,"0x1","0x2"], [0,"0X1","0x2"], [0,"0x1","0x0"], [True,"0x1","0x2"]) for code in invalid: try: decode(code) except (ValueError, ZeroDivisionError): continue raise ValueError("accepted invalid coordinate") target = F(188993196013694905208867290399176049095000131, 250000000000000000000000000000000000000000000) n,a,b = encode([1,0,1]) check(F(int(a,16),int(b,16))==target) lower = upper = 1 for _ in range(23): lower = S*lower//Q upper = -(-S*upper//Q) check((lower,upper)==(32524186965,64283265548)) for start in (-2,-1,0,1,399,10**110+1): d, ledger = start,[] for _ in range(23): d,r = divmod(Q*d,S) ledger.append(r) weighted = sum(r*S**i*Q**(22-i) for i,r in enumerate(ledger)) check(Q**23*start==S**23*d+weighted) for r in reversed(ledger): d,rem = divmod(S*d+r,Q) check(rem==0) check(d==start) def tables(w): def dot(row,col): return sum(a*b for a,b in zip(row,col)) emit = list(zip(*w['emit'])) edge = list(zip(*w['next'])) rec = list(zip(*w['rec'])) outputs, successors = [], [] for state in range(8): bits = [1 if state & (1<=0 for col,b in zip(edge,w['nextbias'])] digits.append(max(range(10),key=scores.__getitem__)) steps.append(sum(int(bit)<