"""attack_q1q2.py — (i) Q2 counterexample search, (ii) Q1 drop characterisation. Exact full-period K* (Python ints, no floats). For each admissible (P,p,R): drop = K*(P,R) - K*(Pp,R) (Q1 failure magnitude; >=0 conditional, can be <0 in hard regime) Q2 = K*(Pp,R) <= K*(P,R u {p}) - 1 Reports the max drop (and whether drop <= 1 always) and any Q2 violation. Run: python3 attack_q1q2.py """ from __future__ import annotations import os, sys from itertools import combinations sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) from util import primes_upto, Kstar # noqa: E402 OUT = os.path.join(os.path.dirname(os.path.abspath(__file__)), "attack_q1q2.txt") L = [] def emit(s=""): print(s); L.append(s) def sweep(P, ps, Rpool0, rmax): q2_total = q2_viol = 0 max_drop = -10**9 max_drop_ex = None drop_gt1 = 0 drop_dist = {} for p in ps: if P % p == 0: continue Pp = P * p Rp = [q for q in Rpool0 if Pp % q != 0 and q != p] for r in range(0, rmax + 1): for R in combinations(Rp, r): R = list(R) kP = Kstar(P, R) kPp = Kstar(Pp, R) kPp1 = Kstar(P, R + [p]) drop = kP - kPp drop_dist[drop] = drop_dist.get(drop, 0) + 1 if drop > max_drop: max_drop = drop max_drop_ex = (P, p, R, kP, kPp) if drop > 1: drop_gt1 += 1 q2_total += 1 if kPp > kPp1 - 1: q2_viol += 1 emit(f" Q2 VIOLATION P={P} p={p} R={R}: K*(Pp)={kPp} > K*(P,R u {{p}})-1={kPp1-1}") emit(f"P={P} (p in {ps}, |R|<={rmax}): {q2_total-q2_viol}/{q2_total} Q2 hold ({q2_viol} viol); " f"max drop={max_drop} at {max_drop_ex}; #drop>1 = {drop_gt1}") emit(f" drop distribution: {dict(sorted(drop_dist.items()))}") return max_drop, max_drop_ex def main(): emit("attack_q1q2.py — Q2 search + Q1 drop characterisation") emit() emit("=" * 72) emit("(1) P=30 (D=3, cheap, large R) — the regime where C1 failed") emit("=" * 72) sweep(30, primes_upto(31)[3:], [q for q in primes_upto(31) if q >= 7], 4) emit() emit("=" * 72) emit("(2) P=210 (D=15)") emit("=" * 72) sweep(210, [11, 13, 17], [13, 17, 19, 23], 3) emit() emit("=" * 72) emit("(3) P=2310 (D=135), bounded") emit("=" * 72) sweep(2310, [13, 17], [17, 19, 23], 2) emit() with open(OUT, "w") as f: f.write("\n".join(L) + "\n") print(f"\n[wrote {OUT}]") if __name__ == "__main__": main()