"""Audit the exact totals for both two-row comparison games in certificate v1. Run with Python 3 and the path to the extracted checks directory. Every support branch is considered, including roots with a repeated eliminant, a possible tie by a fixed player, or a possibly singular active Jacobian. The arithmetic helpers reproduce those of the earlier independent audit. """ from argparse import ArgumentParser from collections import Counter, defaultdict from fractions import Fraction as F from itertools import combinations, permutations, product import json from math import isqrt, prod from pathlib import Path def evaluate(row, owner, x): terms = row.copy() for j in reversed(range(len(x))): if j != owner: terms = [v + (w-v)*x[j] for v,w in zip(terms[::2], terms[1::2])] return terms[0] class Interval: """Closed interval with rational endpoints.""" def __init__(self, lo, hi=None): self.lo = F(lo) self.hi = self.lo if hi is None else F(hi) assert self.lo <= self.hi def __add__(self, other): b = interval(other) return Interval(self.lo+b.lo,self.hi+b.hi) __radd__ = __add__ def __neg__(self): return Interval(-self.hi,-self.lo) def __sub__(self, other): return self+(-interval(other)) def __rsub__(self, other): return interval(other)+(-self) def __mul__(self, other): b=interval(other) v = (self.lo*b.lo,self.lo*b.hi,self.hi*b.lo,self.hi*b.hi) return Interval(min(v),max(v)) __rmul__ = __mul__ def __truediv__(self, other): b=interval(other) assert b.lo*b.hi > 0 return self*Interval(1/b.hi,1/b.lo) def sign(self): if self.lo>0: return 1 if self.hi<0: return -1 assert self.lo == self.hi == 0, ("ambiguous sign", self.lo, self.hi) return 0 def interval(v): return v if isinstance(v,Interval) else Interval(v) def roots(c,b,a): if a==0: assert b!=0 or c!=0, "identically zero eliminant" return [] if not b else [interval(-c/b)] disc = b*b-4*a*c if disc<0: return [] if disc==0: return [interval(-b/(2*a))] integ = disc.numerator*disc.denominator root = isqrt(integ) if root*root == integ: rad=interval(F(root,disc.denominator)) else: scale=2**160 low=isqrt(integ*scale**2) rad=Interval(F(low,scale*disc.denominator),F(low+1,scale*disc.denominator)) return [(-b-rad)/(2*a), (-b+rad)/(2*a)] def inside(x): x=interval(x) return x.sign()>0 and (1-x).sign()>0 def replace(x,coords,values): x=x.copy() for j,val in zip(coords,values): x[j]=val return x def solve_remainder(x, equations, unknown): n=len(unknown) assert n==len(equations) if n==0: yield x return if n==1: a,b=[equations[0](replace(x,unknown,[F(v)])) for v in (0,1)] if a==b: assert a!=0, "flat zero linear branch" elif inside(-a/(b-a)): yield replace(x,unknown,[-a/(b-a)]) return rows=[] for eq in equations: p00,p01,p10,p11=[eq(replace(x,unknown,list(map(F,bits)))) for bits in product(range(2),repeat=2)] rows.append((p00,p10-p00,p01-p00,p11-p10-p01+p00)) (a,b,c,d),(e,f,g,h)=rows # P=(a+bu)+(c+du)v and Q=(e+fu)+(g+hu)v. Eliminate v. for u in roots(a*g-e*c, a*h+b*g-e*d-f*c, b*h-f*d): if not inside(u): continue linear=[(s+t*u,l+k*u) for s,t,l,k in rows] usable=next(((A,B) for A,B in linear if B.sign()),None) if usable is None: assert any(A.sign() for A,B in linear), "vertical curve of solutions" continue v=-usable[0]/usable[1] if inside(v): yield replace(x,unknown,[u,v]) def determinant_sign(columns): return -1 if sum(u>v for u,v in combinations(columns,2))%2 else 1 def audit_game(path, expected_layers): data = json.loads(path.read_text()) denominator = data["threshold_denominator"] thresholds = data["thresholds"] corners = data["corner_differences"] players = range(5) assert denominator > 0 for j in players: column = [thresholds[i][j] for i in range(3) if i != j] assert all(0 < v < denominator for v in column) assert len(set(column)) == len(column) def payoff(i, profile): if i < 3: return prod(denominator * profile[j] - thresholds[i][j] for j in players if j != i) return evaluate(corners[i - 3], i, profile) counts = Counter() layers = Counter() boxes_on_support = defaultdict(list) for state in product("01*", repeat=5): active = [i for i in players if state[i] == "*"] factor_rows = [i for i in active if i < 3] remaining_equations = [lambda x, i=i: payoff(i, x) for i in active if i >= 3] for targets in permutations(active, len(factor_rows)): assignment = list(zip(factor_rows, targets)) if any(i == j for i, j in assignment): continue starting = [None if bit == "*" else F(int(bit)) for bit in state] for i, j in assignment: starting[j] = F(thresholds[i][j], denominator) unknown = [j for j in active if j not in targets] counts["branches"] += 1 for profile in solve_remainder(starting, remaining_equations, unknown): counts["active solutions"] += 1 # Identify different roots on the same support by disjoint rational enclosures. box = [interval(profile[j]) for j in active] for previous in boxes_on_support[state]: assert any(a.hi < b.lo or b.hi < a.lo for a, b in zip(box, previous)) boxes_on_support[state].append(box) margins = [interval((2 * int(bit) - 1) * payoff(i, profile)).sign() for i, bit in enumerate(state) if bit != "*"] # For multiaffine payoffs a first difference is the exact partial derivative. jac = [[payoff(i, replace(profile, [j], [F(1)])) - payoff(i, replace(profile, [j], [F(0)])) for j in active] for i in active] determinant = sum(determinant_sign(cols) * prod(jac[i][j] for i, j in enumerate(cols)) for cols in permutations(range(len(active)))) if interval(determinant).sign() == 0: counts["singular"] += 1 if 0 in margins: counts["inactive ties"] += 1 if -1 not in margins: layers[len(active)] += 1 actual = [layers[k] for k in range(6)] assert counts["branches"] == 366 assert counts["singular"] == counts["inactive ties"] == 0 assert actual == expected_layers print(f"{path.name}: {counts['branches']} branches, {counts['active solutions']} distinct " f"active solutions, {counts['singular']} singular, {counts['inactive ties']} inactive ties") print(f" Equilibria by mixers: {actual}. Boundary {sum(actual[:-1])}, " f"interior {actual[-1]}, total {sum(actual)}.") if __name__ == "__main__": parser = ArgumentParser(description=__doc__) parser.add_argument("checks", type=Path, help="checks directory extracted from certificate v1") directory = parser.parse_args().checks audit_game(directory / "payoffs-189.json", [16, 0, 40, 41, 48, 44]) audit_game(directory / "payoffs-boundary.json", [16, 0, 40, 41, 57, 27])