#!/usr/bin/env python3
"""Exact source-negative control for the stopped line/T-chord attack.

The source paper uses the rank-three truncation of PG(3,2) as a Sylvester
matroid whose graded Moebius algebra is not quadratic.  Sylvester matroids are
automatically T-chordal.  This script reuses the dependency-free flat-Koszul
engine from the Hunt packet and verifies over Q that the truncation has a
209-dimensional spanning H_(2,4) block.

This is one predesignated source control, not a matroid search.
"""

from __future__ import annotations

import importlib.util
import sys
from pathlib import Path


HERE = Path(__file__).resolve().parent
ENGINE = HERE / "verify-flat-koszul-blocks.py"
SPEC = importlib.util.spec_from_file_location("flat_koszul_blocks", ENGINE)
assert SPEC is not None and SPEC.loader is not None
MODULE = importlib.util.module_from_spec(SPEC)
sys.modules[SPEC.name] = MODULE
SPEC.loader.exec_module(MODULE)


def truncated_pg32():
    columns = [
        tuple((value >> bit) & 1 for bit in range(4))
        for value in range(1, 16)
    ]

    def rank_function(subset):
        rows = [
            [columns[column][coordinate] for column in subset]
            for coordinate in range(4)
        ]
        return min(3, MODULE.gf_rank(rows, 2))

    return MODULE.Matroid("T(PG(3,2))", 15, rank_function)


if __name__ == "__main__":
    lines = MODULE.audit(truncated_pg32())
    degree_four = next(line for line in lines if "degree 4:" in line)
    assert "H2=209" in degree_four
    assert "nonzero-blocks=1" in degree_four
    print("\n".join(lines))
    print("degree 4 conclusion: the unique nonzero block is the rank-3 spanning block")
