#!/usr/bin/env python3 """Encode a hidden offset or decode a rank for the published 120-vertex ramp at step 23.""" from argparse import ArgumentParser from bisect import bisect_left from hashlib import sha256 from pathlib import Path import json from verify import RAMP, absolute, normalize HERE = Path(__file__).resolve().parent def main(): cli = ArgumentParser(description=__doc__) cli.add_argument('operation',choices=['encode','decode']) cli.add_argument('value',type=int,help='signed offset to encode, or integer rank to decode') cli.add_argument('--zero-fiber',action='store_true',help='condition on scalar terminal zero') args = cli.parse_args() raw=(HERE/'ramp23.bin').read_bytes() if sha256(raw).hexdigest() != 'c1ad270e07f005a7b8c370013259c7f37ffc808b0bedae15eb2c405266c48567': raise ValueError('unexpected endpoint table') masks = [raw[i:i+15] for i in range(0,len(raw),15)] if args.zero_fiber: record = json.loads((HERE/'results.json').read_bytes())['scalar_zero_fiber_offsets'] selected = sorted({row['unrestricted_rank'] for row in record['runs']}) masks = [masks[i] for i in selected] if args.operation=='encode' and not 0<=args.value1: raise ValueError('terminal state is not binary after normalization') mask = int(''.join(map(str,bits)),2).to_bytes(15,'big') rank = bisect_left(masks,mask) if rank>=len(masks) or masks[rank]!=mask: raise ValueError('unreachable terminal') else: rank=args.value if not 0<=rank