import itertools,json SLUG='parser-differential-security' def mismatch_count(q,k):return sum(s[0]!=s[-1] for s in itertools.product(range(q),repeat=k)) def strict(pairs): keys=[k for k,v in pairs] if len(keys)!=len(set(keys)):raise ValueError('duplicate key') return dict(pairs) def canonical(value):return json.dumps(value,sort_keys=True,separators=(',',':'),ensure_ascii=True) def run(): cases=[(q,k) for q in range(2,7) for k in range(1,6)] formula_ok=all(mismatch_count(q,k)==(0 if k==1 else q*(q-1)*q**(k-2)) for q,k in cases) pairs=[('a',1),('b',2),('c',3)] outputs=[canonical(strict(list(p))) for p in itertools.permutations(pairs)] rejected=False try:strict([('flag',0),('flag',1)]) except ValueError:rejected=True single_accepts=sum(strict([('flag',v)])=={'flag':v} for v in range(4)) first={'flag':0};last={'flag':1} checks=[dict(name='Endpoint disagreement formula versus exhaustive sequences',passed=formula_ok,cases=len(cases),detail='q=2..6 and length=1..5; finite uniform corpus only.'),dict(name='Duplicate rejection precedes map conversion',passed=rejected and single_accepts==4,cases=5,detail='Rejects conflicting duplicates and accepts each of four single-member values.'),dict(name='Restricted canonicalization is order-independent and idempotent',passed=len(set(outputs))==1 and all(canonical(json.loads(x))==x for x in outputs),cases=6,detail='Every permutation of three distinct ASCII-key/small-integer pairs; not full JCS.'),dict(name='Canonicalization cannot erase earlier semantic disagreement',passed=canonical(first)!=canonical(last) and first['flag']==0 and last['flag']==1,cases=1,detail='A first-wins approval and last-wins consumer disagree on the constructed input.')] lengths=list(range(1,9));alphabets=list(range(2,7)) return dict(slug=SLUG,checks=checks,figures=[dict(id='duplicate-length',title='Binary duplicate sequences and endpoint disagreements',x_label='Occurrences of one key',y_label='Number of sequences',series=[dict(name='All binary sequences',x=lengths,y=[2**k for k in lengths]),dict(name='First/last disagreement',x=lengths,y=[mismatch_count(2,k) for k in lengths])],caption='At length 8, 128 of 256 binary sequences disagree. Uniform enumeration is not a traffic model.',assumptions=['Only endpoint selection differs','Values are a finite alphabet','All sequences counted equally'],kind='Constructed analytical model; not a measured benchmark'),dict(id='duplicate-alphabet',title='Four duplicate occurrences over larger alphabets',x_label='Number of distinct values q',y_label='Number of sequences',series=[dict(name='All length-four sequences',x=alphabets,y=[q**4 for q in alphabets]),dict(name='Endpoint disagreements',x=alphabets,y=[mismatch_count(q,4) for q in alphabets])],caption='At q=4, 192 of 256 sequences disagree; strict single-member inputs are a separate corpus.',assumptions=['Four occurrences of one key','First-wins and last-wins policies','No inference about real implementation prevalence'],kind='Constructed analytical model; not a measured benchmark')],result_summary=dict(binary_length8_total=256,binary_length8_mismatch=mismatch_count(2,8),q4_length4_total=256,q4_length4_mismatch=mismatch_count(4,4),strict_q4_single_member_accepts=single_accepts,canonical_permutations=len(outputs))) if __name__=='__main__':print(json.dumps(run(),indent=2))