From 1b041671a37502608fe577110193398f18b7fee8 Mon Sep 17 00:00:00 2001 From: Jacob Lifshay Date: Fri, 17 May 2024 01:10:57 -0700 Subject: [PATCH] change decode_reducing_polynomial definition so it can encode x^XLEN + x + 1 also adds a test that all degree <= XLEN irreducible polynomials can be encoded --- gf_reference/decode_reducing_polynomial.py | 2 +- gf_reference/test_cl_gfb_gfp.py | 21 +++++++++++++++++++ .../hdl/decode_reducing_polynomial.py | 2 +- 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/gf_reference/decode_reducing_polynomial.py b/gf_reference/decode_reducing_polynomial.py index 254d301..40e2ac8 100644 --- a/gf_reference/decode_reducing_polynomial.py +++ b/gf_reference/decode_reducing_polynomial.py @@ -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, diff --git a/gf_reference/test_cl_gfb_gfp.py b/gf_reference/test_cl_gfb_gfp.py index a831639..748aec0 100644 --- a/gf_reference/test_cl_gfb_gfp.py +++ b/gf_reference/test_cl_gfb_gfp.py @@ -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() diff --git a/src/nmigen_gf/hdl/decode_reducing_polynomial.py b/src/nmigen_gf/hdl/decode_reducing_polynomial.py index f50dee8..d45477b 100644 --- a/src/nmigen_gf/hdl/decode_reducing_polynomial.py +++ b/src/nmigen_gf/hdl/decode_reducing_polynomial.py @@ -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): -- 2.30.2