change decode_reducing_polynomial definition so it can encode x^XLEN + x + 1
authorJacob Lifshay <programmerjake@gmail.com>
Fri, 17 May 2024 08:10:57 +0000 (01:10 -0700)
committerJacob Lifshay <programmerjake@gmail.com>
Fri, 17 May 2024 08:10:57 +0000 (01:10 -0700)
also adds a test that all degree <= XLEN irreducible polynomials can be encoded

gf_reference/decode_reducing_polynomial.py
gf_reference/test_cl_gfb_gfp.py
src/nmigen_gf/hdl/decode_reducing_polynomial.py

index 254d3018fa614deed987a3a0b6ba6163f97265aa..40e2ac8a6500c3c6f6b0f333e57eb55262dc47ad 100644 (file)
@@ -6,7 +6,7 @@ def decode_reducing_polynomial():
         Note: the returned integer is `XLEN + 1` bits wide.
     """
     v = ST.GFBREDPOLY & ((1 << ST.XLEN) - 1)  # mask to XLEN bits
-    if v == 0 or v == 2:  # GF(2)
+    if v == 0:  # GF(2)
         return 0b10  # degree = 1, poly = x
     if (v & 1) == 0:
         # all reducing polynomials of degree > 1 must have the LSB set,
index a831639cbfa7d4edbfdf13abae6d3a78201f3fed..748aec0c75f41f576e5bc6cc87e8545bafc65292 100644 (file)
@@ -12,6 +12,9 @@ from nmigen_gf.reference.gfpmadd import gfpmadd
 from nmigen_gf.reference.gfpmsub import gfpmsub
 from nmigen_gf.reference.gfpmsubr import gfpmsubr
 from nmigen_gf.reference.pack_poly import pack_poly, unpack_poly
+from nmigen_gf.reference.decode_reducing_polynomial import \
+    decode_reducing_polynomial
+from nmigen_gf.reference.is_irreducible import is_irreducible
 import unittest
 
 
@@ -550,6 +553,24 @@ class TestGFBInstructions(unittest.TestCase):
         ST.reinit(XLEN=XLEN, GFBREDPOLY=REDPOLY)
         return red_poly
 
+    def test_all_red_polys_covered(self):
+        for XLEN in 2, 4, 8, 16:
+            for rpoly in range(2 ** (XLEN + 1)):
+                if not is_irreducible(rpoly):
+                    continue
+                REDPOLY = rpoly
+                if rpoly == 2:
+                    REDPOLY = 0
+                elif rpoly >> XLEN:
+                    REDPOLY %= 2 ** XLEN
+                    REDPOLY &= ~1
+                with self.subTest(XLEN=XLEN, rpoly=hex(rpoly),
+                                  REDPOLY=hex(REDPOLY)):
+                    ST.reinit(XLEN=XLEN, GFBREDPOLY=REDPOLY)
+                    output = decode_reducing_polynomial()
+                    with self.subTest(output=hex(output)):
+                        self.assertEqual(rpoly, output)
+
     def test_gfbmul(self):
         # AES's finite field reducing polynomial
         red_poly = self.init_aes_red_poly()
index f50dee83bef3c3fa34d6fae9f5694ef879f100d1..d45477b7e80d73218dd8eab8b7aa8d8c619a6600 100644 (file)
@@ -34,7 +34,7 @@ class DecodeReducingPolynomial(Elaboratable):
     def elaborate(self, platform):
         m = Module()
         v = self.REDPOLY
-        with m.If((v == 0) | (v == 0b10)):  # GF(2)
+        with m.If(v == 0):  # GF(2)
             # degree = 1, poly = x
             m.d.comb += self.reducing_polynomial.eq(0b10)
         with m.Elif(v[0] == 0):