From 6ce3c616eb67fd45ed5f2a9a82cd1417e92e64fb Mon Sep 17 00:00:00 2001 From: Bryan Biedenkapp Date: Sun, 22 Nov 2026 10:59:14 -0500 Subject: [PATCH] refactor and rewrite BCH handling to be more intelligent and do actual decode error correction; --- src/common/edac/BCH.cpp | 222 +++++++++++++++++++++++++++------------ src/common/edac/BCH.h | 35 +++++- src/common/p25/NID.cpp | 80 ++++++-------- tests/edac/BCH_Tests.cpp | 73 +++++++++++++ 4 files changed, 288 insertions(+), 122 deletions(-) create mode 100644 tests/edac/BCH_Tests.cpp diff --git a/src/common/edac/BCH.cpp b/src/common/edac/BCH.cpp index c5a39b2e..a1fe998e 100644 --- a/src/common/edac/BCH.cpp +++ b/src/common/edac/BCH.cpp @@ -4,77 +4,10 @@ * GPLv2 Open Source. Use is subject to license terms. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * + * Copyright (C) 2016 Jonathan Naylor, G4KLX + * Copyright (C) 2026 Bryan Biedenkapp, N2PLL + * */ -/* -* File: bch3.c -* Title: Encoder/decoder for binary BCH codes in C (Version 3.1) -* Author: Robert Morelos-Zaragoza -* Date: August 1994 -* Revised: June 13, 1997 -* -* =============== Encoder/Decoder for binary BCH codes in C ================= -* -* Version 1: Original program. The user provides the generator polynomial -* of the code (cumbersome!). -* Version 2: Computes the generator polynomial of the code. -* Version 3: No need to input the coefficients of a primitive polynomial of -* degree m, used to construct the Galois Field GF(2**m). The -* program now works for any binary BCH code of length such that: -* 2**(m-1) - 1 < length <= 2**m - 1 -* -* Note: You may have to change the size of the arrays to make it work. -* -* The encoding and decoding methods used in this program are based on the -* book "Error Control Coding: Fundamentals and Applications", by Lin and -* Costello, Prentice Hall, 1983. -* -* Thanks to Patrick Boyle (pboyle@era.com) for his observation that 'bch2.c' -* did not work for lengths other than 2**m-1 which led to this new version. -* Portions of this program are from 'rs.c', a Reed-Solomon encoder/decoder -* in C, written by Simon Rockliff (simon@augean.ua.oz.au) on 21/9/89. The -* previous version of the BCH encoder/decoder in C, 'bch2.c', was written by -* Robert Morelos-Zaragoza (robert@spectra.eng.hawaii.edu) on 5/19/92. -* -* NOTE: -* The author is not responsible for any malfunctioning of -* this program, nor for any damage caused by it. Please include the -* original program along with these comments in any redistribution. -* -* For more information, suggestions, or other ideas on implementing error -* correcting codes, please contact me at: -* -* Robert Morelos-Zaragoza -* 5120 Woodway, Suite 7036 -* Houston, Texas 77056 -* -* email: r.morelos-zaragoza@ieee.org -* -* COPYRIGHT NOTICE: This computer program is free for non-commercial purposes. -* You may implement this program for any non-commercial application. You may -* also implement this program for commercial purposes, provided that you -* obtain my written permission. Any modification of this program is covered -* by this copyright. -* -* == Copyright (c) 1994-7, Robert Morelos-Zaragoza. All rights reserved. == -* -* m = order of the Galois field GF(2**m) -* n = 2**m - 1 = size of the multiplicative group of GF(2**m) -* length = length of the BCH code -* t = error correcting capability (max. no. of errors the code corrects) -* d = 2*t + 1 = designed min. distance = no. of consecutive roots of g(x) + 1 -* k = n - deg(g(x)) = dimension (no. of information bits/codeword) of the code -* p[] = coefficients of a primitive polynomial used to generate GF(2**m) -* g[] = coefficients of the generator polynomial, g(x) -* alpha_to [] = log table of GF(2**m) -* index_of[] = antilog table of GF(2**m) -* data[] = information bits = coefficients of data polynomial, i(x) -* bb[] = coefficients of redundancy polynomial x^(length-k) i(x) modulo g(x) -* numerr = number of errors -* errpos[] = error positions -* recd[] = coefficients of the received polynomial -* decerror = number of decoding errors (in _message_ positions) -* -*/ #include "Defines.h" #include "edac/BCH.h" #include "Utils.h" @@ -82,6 +15,7 @@ using namespace edac; #include +#include // --------------------------------------------------------------------------- // Constants @@ -89,6 +23,10 @@ using namespace edac; const int length = 63; const int k = 16; +const int t = 11; +const int fieldOrder = 6; +const int fieldSize = 1 << fieldOrder; +const int primitivePolynomial = 0x43; // x^6 + x + 1 const int g[] = { 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, @@ -126,10 +64,154 @@ void BCH::encode(uint8_t* nid) } } +/* Decodes and corrects a BCH(63,16,23) P25 NID codeword. */ + +bool BCH::decode(uint8_t* nid, uint32_t& errors) +{ + assert(nid != nullptr); + + errors = 0U; + uint8_t corrected[8U]; + ::memcpy(corrected, nid, sizeof(corrected)); + + uint8_t syndromes[2 * t + 1] = { 0U }; + bool valid = true; + for (int syndrome = 1; syndrome <= 2 * t; syndrome++) { + uint8_t value = 0U; + for (int bit = 0; bit < length; bit++) { + if (READ_BIT(corrected, bit) != 0U) + value ^= alphaPower(syndrome * (length - 1 - bit)); + } + syndromes[syndrome] = value; + valid = valid && (value == 0U); + } + + if (valid) + return true; + + uint8_t locator[2 * t + 1] = { 0U }; + uint8_t previous[2 * t + 1] = { 0U }; + locator[0U] = 1U; + previous[0U] = 1U; + + int degree = 0; + int shift = 1; + uint8_t previousDiscrepancy = 1U; + + for (int step = 0; step < 2 * t; step++) { + uint8_t discrepancy = syndromes[step + 1]; + for (int i = 1; i <= degree; i++) + discrepancy ^= multiply(locator[i], syndromes[step + 1 - i]); + + if (discrepancy == 0U) { + shift++; + continue; + } + + uint8_t saved[2 * t + 1]; + for (int i = 0; i <= 2 * t; i++) + saved[i] = locator[i]; + + const uint8_t scale = multiply(discrepancy, inverse(previousDiscrepancy)); + for (int i = 0; i + shift <= 2 * t; i++) + locator[i + shift] ^= multiply(scale, previous[i]); + + if ((2 * degree) <= step) { + degree = step + 1 - degree; + for (int i = 0; i <= 2 * t; i++) + previous[i] = saved[i]; + previousDiscrepancy = discrepancy; + shift = 1; + } + else { + shift++; + } + } + + if ((degree <= 0) || (degree > t)) + return false; + + for (int polynomialPosition = 0; polynomialPosition < length; polynomialPosition++) { + const uint8_t root = alphaPower(-polynomialPosition); + uint8_t evaluation = locator[degree]; + for (int i = degree - 1; i >= 0; i--) + evaluation = multiply(evaluation, root) ^ locator[i]; + + if (evaluation == 0U) { + const int bit = length - 1 - polynomialPosition; + WRITE_BIT(corrected, bit, READ_BIT(corrected, bit) == 0U); + errors++; + } + } + + if (errors != static_cast(degree)) + return false; + + for (int syndrome = 1; syndrome <= 2 * t; syndrome++) { + uint8_t value = 0U; + for (int bit = 0; bit < length; bit++) { + if (READ_BIT(corrected, bit) != 0U) + value ^= alphaPower(syndrome * (length - 1 - bit)); + } + if (value != 0U) + return false; + } + + ::memcpy(nid, corrected, sizeof(corrected)); + return true; +} + // --------------------------------------------------------------------------- // Private Class Members // --------------------------------------------------------------------------- +/* Multiplies two elements of GF(2^6). */ + +uint8_t BCH::multiply(uint8_t a, uint8_t b) +{ + uint8_t product = 0U; + + while (b != 0U) { + if ((b & 0x01U) != 0U) + product ^= a; + + b >>= 1; + a <<= 1; + if ((a & fieldSize) != 0U) + a ^= primitivePolynomial; + } + + return product; +} + +/* Returns alpha raised to an exponent in GF(2^6). */ + +uint8_t BCH::alphaPower(int exponent) +{ + while (exponent < 0) + exponent += length; + exponent %= length; + + uint8_t value = 1U; + for (int i = 0; i < exponent; i++) + value = multiply(value, 0x02U); + + return value; +} + +/* Returns the multiplicative inverse of a nonzero GF(2^6) element. */ + +uint8_t BCH::inverse(uint8_t value) +{ + assert(value != 0U); + + uint8_t result = 1U; + for (int i = 0; i < fieldSize - 2; i++) + result = multiply(result, value); + + return result; +} + /* Compute redundancy bb[], the coefficients of b(x). The redundancy polynomial b(x) is the remainder after dividing x^(length-k)*data(x) by the generator polynomial g(x). */ diff --git a/src/common/edac/BCH.h b/src/common/edac/BCH.h index f5e8d1af..ba604f93 100644 --- a/src/common/edac/BCH.h +++ b/src/common/edac/BCH.h @@ -5,6 +5,7 @@ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * Copyright (C) 2016 Jonathan Naylor, G4KLX + * Copyright (C) 2026 Bryan Biedenkapp, N2PLL * */ /** @@ -41,12 +42,42 @@ namespace edac ~BCH(); /** - * @brief Encodes input data with BCH. - * @param data Data to encode with BCH. + * @brief Encodes input data with BCH(63,16,23). + * @param[in,out] data Eight-byte buffer. The first 16 bits are + * encoded into the following 47 bits; bit 64 is not modified. */ void encode(uint8_t* data); + /** + * @brief Decodes and corrects a BCH(63,16,23). + * @param[in,out] data Eight-byte buffer. Only the first 63 bits + * participate in BCH decoding; bit 64 is not modified. + * @param[out] errors Number of corrected bits in the BCH codeword. + * @returns True if the codeword was valid or corrected, otherwise false. + */ + bool decode(uint8_t* data, uint32_t& errors); + private: + /** + * @brief Multiplies two elements of GF(2^6). + * @param[in] a Left field element. + * @param[in] b Right field element. + * @returns Product in GF(2^6). + */ + static uint8_t multiply(uint8_t a, uint8_t b); + /** + * @brief Raises the primitive field element to an integer exponent. + * @param[in] exponent Exponent, reduced modulo 63. + * @returns Field element corresponding to alpha raised to the exponent. + */ + static uint8_t alphaPower(int exponent); + /** + * @brief Computes the multiplicative inverse of a field element. + * @param[in] value Nonzero element of GF(2^6). + * @returns Multiplicative inverse of value. + */ + static uint8_t inverse(uint8_t value); + /** * @brief Compute redundancy bb[], the coefficients of b(x). The redundancy polynomial b(x) is the * remainder after dividing x^(length-k)*data(x) by the generator polynomial g(x). diff --git a/src/common/p25/NID.cpp b/src/common/p25/NID.cpp index b8fb38f7..96ba40e6 100644 --- a/src/common/p25/NID.cpp +++ b/src/common/p25/NID.cpp @@ -24,7 +24,7 @@ using namespace p25::defines; // Constants // --------------------------------------------------------------------------- -const uint32_t MAX_NID_ERRS = 7U;//5U; +const uint32_t MAX_NID_ERRS = 11U; // --------------------------------------------------------------------------- // Public Class Members @@ -68,56 +68,36 @@ bool NID::decode(const uint8_t* data) uint8_t nid[P25_NID_LENGTH_BYTES]; P25Utils::decode(data, nid, 48U, 114U); - // handle digital "squelch" NAC - if ((m_nac == NAC_DIGITAL_SQ) || (m_nac == NAC_REUSE_RX_NAC)) { - uint32_t nac = ((nid[0U] << 4) + (nid[1U] >> 4)) & 0xFFFU; - cleanupArrays(); - createRxTxNID(nac); // bryanb: I hate this and it'll be slow - } - - uint32_t errs = P25Utils::compare(nid, m_rxTx[DUID::LDU1], P25_NID_LENGTH_BYTES); - if (errs < MAX_NID_ERRS) { - m_duid = DUID::LDU1; - return true; - } - - errs = P25Utils::compare(nid, m_rxTx[DUID::LDU2], P25_NID_LENGTH_BYTES); - if (errs < MAX_NID_ERRS) { - m_duid = DUID::LDU2; - return true; - } - - errs = P25Utils::compare(nid, m_rxTx[DUID::PDU], P25_NID_LENGTH_BYTES); - if (errs < MAX_NID_ERRS) { - m_duid = DUID::PDU; - return true; - } - - errs = P25Utils::compare(nid, m_rxTx[DUID::TSDU], P25_NID_LENGTH_BYTES); - if (errs < MAX_NID_ERRS) { - m_duid = DUID::TSDU; - return true; - } - - errs = P25Utils::compare(nid, m_rxTx[DUID::HDU], P25_NID_LENGTH_BYTES); - if (errs < MAX_NID_ERRS) { - m_duid = DUID::HDU; - return true; - } - - errs = P25Utils::compare(nid, m_rxTx[DUID::TDULC], P25_NID_LENGTH_BYTES); - if (errs < MAX_NID_ERRS) { - m_duid = DUID::TDULC; - return true; - } - - errs = P25Utils::compare(nid, m_rxTx[DUID::TDU], P25_NID_LENGTH_BYTES); - if (errs < MAX_NID_ERRS) { - m_duid = DUID::TDU; - return true; + edac::BCH bch; + uint32_t errs = 0U; + if (!bch.decode(nid, errs)) + return false; + + const uint8_t duid = nid[1U] & 0x0FU; + const bool expectedParity = (((duid >> 1) ^ duid) & 0x01U) != 0U; + const bool receivedParity = (nid[7U] & 0x01U) != 0U; + if (receivedParity != expectedParity) + errs++; + if (errs > MAX_NID_ERRS) + return false; + + const uint32_t nac = ((nid[0U] << 4) | (nid[1U] >> 4)) & 0xFFFU; + if ((m_nac != NAC_DIGITAL_SQ) && (m_nac != NAC_REUSE_RX_NAC) && (nac != m_nac)) + return false; + + switch (duid) { + case DUID::HDU: + case DUID::TDU: + case DUID::LDU1: + case DUID::TSDU: + case DUID::PDU: + case DUID::LDU2: + case DUID::TDULC: + m_duid = static_cast(duid); + return true; + default: + return false; } - - return false; } /* Encodes P25 network identifier data. */ diff --git a/tests/edac/BCH_Tests.cpp b/tests/edac/BCH_Tests.cpp new file mode 100644 index 00000000..81db55ab --- /dev/null +++ b/tests/edac/BCH_Tests.cpp @@ -0,0 +1,73 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Digital Voice Modem - Test Suite + * GPLv2 Open Source. Use is subject to license terms. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright (C) 2025,2026 Bryan Biedenkapp, N2PLL + * + */ + +#include "common/Defines.h" +#include "common/edac/BCH.h" + +#include +#include +#include + +using namespace edac; + +TEST_CASE("BCH encodes the P25 NID generator matrix", "[edac][bch][p25]") +{ + uint8_t nid[8U] = { 0x29U, 0x35U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U }; + const uint8_t expected[8U] = { 0x29U, 0x35U, 0x55U, 0xEFU, 0x2CU, 0x65U, 0x34U, 0x36U }; + + BCH bch; + bch.encode(nid); + + REQUIRE(::memcmp(nid, expected, sizeof(nid)) == 0); +} + +TEST_CASE("BCH corrects every single-bit position", "[edac][bch][p25]") +{ + const uint8_t expected[8U] = { 0x29U, 0x35U, 0x55U, 0xEFU, 0x2CU, 0x65U, 0x34U, 0x36U }; + BCH bch; + + for (uint32_t bit = 0U; bit < 63U; bit++) { + uint8_t nid[8U]; + ::memcpy(nid, expected, sizeof(nid)); + WRITE_BIT(nid, bit, READ_BIT(nid, bit) == 0U); + + uint32_t errors = 0U; + REQUIRE(bch.decode(nid, errors)); + REQUIRE(errors == 1U); + REQUIRE(::memcmp(nid, expected, sizeof(nid)) == 0); + } +} + +TEST_CASE("BCH corrects eleven distributed errors", "[edac][bch][p25]") +{ + const uint8_t expected[8U] = { 0x29U, 0x35U, 0x55U, 0xEFU, 0x2CU, 0x65U, 0x34U, 0x36U }; + const uint32_t errorBits[11U] = { 0U, 3U, 9U, 14U, 20U, 27U, 33U, 39U, 46U, 54U, 62U }; + uint8_t nid[8U]; + ::memcpy(nid, expected, sizeof(nid)); + for (uint32_t bit : errorBits) + WRITE_BIT(nid, bit, READ_BIT(nid, bit) == 0U); + + BCH bch; + uint32_t errors = 0U; + REQUIRE(bch.decode(nid, errors)); + REQUIRE(errors == 11U); + REQUIRE(::memcmp(nid, expected, sizeof(nid)) == 0); +} + +TEST_CASE("BCH leaves the separate P25 parity bit unchanged", "[edac][bch][p25]") +{ + uint8_t nid[8U] = { 0x29U, 0x35U, 0x55U, 0xEFU, 0x2CU, 0x65U, 0x34U, 0x37U }; + + BCH bch; + uint32_t errors = 0U; + REQUIRE(bch.decode(nid, errors)); + REQUIRE(errors == 0U); + REQUIRE((nid[7U] & 0x01U) == 0x01U); +}