feat(math): add dual code and syndrome operations using SymPy (#1851) - #2026
feat(math): add dual code and syndrome operations using SymPy (#1851)#2026morluto wants to merge 1 commit into
Conversation
Add two operations to the code_theory domain: - code.dual_code.compute: compute the parity check matrix (dual code) from a generator matrix over GF(p), using SymPy's exact null space computation. - code.syndrome.compute: compute the syndrome vector H*r^T mod p for a received word under a parity check matrix. Partially addresses #1851.
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
morluto
left a comment
There was a problem hiding this comment.
Review verdict: blocked — the dual is computed over the wrong field
Matrix(rows).nullspace() is ordinary SymPy matrix nullspace over characteristic zero, not over GF(p). Reducing its entries afterward does not convert a rational kernel basis into a finite-field kernel basis.
Two concrete failures:
p = 3
G = ((2, 1),)SymPy returns the rational basis vector (-1/2, 1). This code applies int(-1/2) == 0 and returns parity row (0, 1), but G * H^T == 1 (mod 3), so the advertised parity check is simply false.
p = 2
G = ((1,1,0), (1,0,1), (0,1,1))The determinant is -2, so the matrix is nonsingular over QQ but singular over GF(2). The code returns a trivial dual, while (1,1,1) is a nonzero finite-field null vector.
Use modular row reduction/nullspace over an actual GF(p) domain (for example SymPy's domain-matrix finite-field machinery), and verify G H^T = 0 mod p as a postcondition/property test.
There is a second dimension bug: code_dimension=k uses the number of supplied rows, but a generator matrix may have dependent rows. Either require full row rank over GF(p) or report the modular rank; then dual_dimension must equal n-rank(G). A duplicate-row regression should cover this.
Finally, SyndromeRequest validates received-word residues but not parity-check entries. Normalize or require canonical residues consistently.
Deep review summaryVerdict: REQUEST CHANGES — the dual code is computed over characteristic zero, not over
Concrete failures: p = 3
G = ((2, 1),)SymPy returns p = 2
G = ((1,1,0), (1,0,1), (0,1,1))This matrix has determinant Use modular row reduction or nullspace over an actual There is also a dimension bug: Finally, |
Summary
Add two operations to the existing domain, partially addressing #1851.
Operations
code.dual_code.compute— Compute the parity check matrix (dual code) from a generator matrix over GF(p), using SymPy's exact null space computation. Returns the code dimension, length, and dual dimension.code.syndrome.compute— Compute the syndrome vector H*r^T mod p for a received word under a parity check matrix over GF(p).Library choice
SymPy's provides exact rational null space computation, which we reduce modulo the prime field to get the parity check matrix. This avoids hand-rolling a custom null space algorithm and leverages a mature, tested implementation.
Tests
5 known-answer tests covering:
Continue this on Linzumi