From c67ac1247f378a0988d9df2e6bb3f4792bf31b76 Mon Sep 17 00:00:00 2001 From: Carles Fernandez Date: Fri, 26 Mar 2021 12:40:35 +0100 Subject: [PATCH] Add optional erasure positions to RS decoder --- src/core/system_parameters/reed_solomon.cc | 41 +++--------------- src/core/system_parameters/reed_solomon.h | 17 +++++--- .../galileo_e6b_reed_solomon_test.cc | 42 +++++++++++++++++++ 3 files changed, 60 insertions(+), 40 deletions(-) diff --git a/src/core/system_parameters/reed_solomon.cc b/src/core/system_parameters/reed_solomon.cc index d7d59d111..9324c6d68 100644 --- a/src/core/system_parameters/reed_solomon.cc +++ b/src/core/system_parameters/reed_solomon.cc @@ -540,26 +540,24 @@ void ReedSolomon::encode_rs_8(const uint8_t* data, uint8_t* parity) const } -int ReedSolomon::decode(std::vector& data_to_decode) const +int ReedSolomon::decode(std::vector& data_to_decode, const std::vector& erasure_positions) const { if (data_to_decode.size() != d_symbols_per_block) { std::cerr << "Reed Solomon error: bad input length\n"; return -1; } - std::vector decoded_data(d_data_in_block, 0); - int result = decode_rs_8(data_to_decode.data(), nullptr, 0, d_pad); + int result = decode_rs_8(data_to_decode.data(), erasure_positions.data(), erasure_positions.size(), d_pad); return result; } -int ReedSolomon::decode_rs_8(uint8_t* data, int* eras_pos, int no_eras, int pad) const +int ReedSolomon::decode_rs_8(uint8_t* data, const int* eras_pos, int no_eras, int pad) const { if (pad < 0 || pad > 222) { return -1; } - int retval; int deg_lambda; int el; int deg_omega; @@ -621,16 +619,7 @@ int ReedSolomon::decode_rs_8(uint8_t* data, int* eras_pos, int no_eras, int pad) { // if syndrome is zero, data[] is a codeword and there are no // errors to correct. So return data[] unmodified - count = 0; - if (eras_pos != nullptr) - { - for (i = 0; i < count; i++) - { - eras_pos[i] = loc[i]; - } - } - retval = count; - return retval; + return 0; } memset(&lambda[1], 0, d_nroots * sizeof(lambda[0])); @@ -761,16 +750,7 @@ int ReedSolomon::decode_rs_8(uint8_t* data, int* eras_pos, int no_eras, int pad) { // deg(lambda) unequal to number of roots => uncorrectable // error detected - count = -1; - if (eras_pos != nullptr) - { - for (i = 0; i < count; i++) - { - eras_pos[i] = loc[i]; - } - } - retval = count; - return retval; + return -1; } // Compute err+eras evaluator poly omega(x) = s(x)*lambda(x) (modulo @@ -821,14 +801,5 @@ int ReedSolomon::decode_rs_8(uint8_t* data, int* eras_pos, int no_eras, int pad) } } - if (eras_pos != nullptr) - { - for (i = 0; i < count; i++) - { - eras_pos[i] = loc[i]; - } - } - retval = count; - - return retval; + return count; } diff --git a/src/core/system_parameters/reed_solomon.h b/src/core/system_parameters/reed_solomon.h index 5dface4bd..2b1d01dae 100644 --- a/src/core/system_parameters/reed_solomon.h +++ b/src/core/system_parameters/reed_solomon.h @@ -79,11 +79,18 @@ public: const std::vector>& gen_matrix = std::vector>{}); /*! - * \brief Decode an encoded block. The decoded symbols are at the first - * 255-nroots elements of the input vector. Returns the number of corrected - * errors or -1 if decoding failed. + * \brief Decode an encoded block. + * + * The decoded symbols are at the first 255-nroots elements + * of the input vector. + * + * The second parameter is optional, and contains a vector of erasure + * positions to be passed to the decoding algorithm. + * + * Returns the number of corrected errors or -1 if decoding failed. */ - int decode(std::vector& data_to_decode) const; + int decode(std::vector& data_to_decode, + const std::vector& erasure_positions = std::vector{}) const; /*! * \brief Encode data with the generator matrix (for testing purposes) @@ -98,7 +105,7 @@ public: private: static const int d_symbols_per_block = 255; // the total number of symbols in a RS block. - int decode_rs_8(uint8_t* data, int* eras_pos, int no_eras, int pad) const; + int decode_rs_8(uint8_t* data, const int* eras_pos, int no_eras, int pad) const; int mod255(int x) const; int rs_min(int a, int b) const; diff --git a/src/tests/unit-tests/system-parameters/galileo_e6b_reed_solomon_test.cc b/src/tests/unit-tests/system-parameters/galileo_e6b_reed_solomon_test.cc index 9520174fb..a6ecad1ba 100644 --- a/src/tests/unit-tests/system-parameters/galileo_e6b_reed_solomon_test.cc +++ b/src/tests/unit-tests/system-parameters/galileo_e6b_reed_solomon_test.cc @@ -493,3 +493,45 @@ TEST(ReedSolomonTest, Decode111ErrorsCustomConstructor) std::vector decoded(encoded_input.begin(), encoded_input.begin() + 32); EXPECT_TRUE(expected_output == decoded); } + + +TEST(ReedSolomonTest, Decode112Errors) +{ + std::vector encoded_input = { + 71, 12, 25, 210, 178, 81, 243, 9, 112, 98, 196, 203, 48, 125, 114, 165, 181, 193, 71, 174, 168, 42, 31, 128, 245, 87, 150, 58, 192, 66, 130, 179, 133, 210, 122, 224, 75, 138, 20, 205, 14, 245, 209, 187, 246, 228, 12, 39, 244, 238, 223, 217, 84, 233, 137, 168, 153, 8, 94, 26, 99, 169, 149, 203, 115, 69, 211, 43, 70, 96, 70, 38, 160, 1, 232, 153, 223, 165, 93, 205, 101, 170, 60, 188, 198, 82, 168, 79, 95, 23, 118, 215, 187, 136, 24, 99, 252, 3, 144, 166, 117, 45, 168, 239, 77, 42, 246, 33, 122, 97, 242, 236, 13, 217, 96, 186, 71, 250, 242, 177, 125, 87, 27, 13, 118, 181, 178, 12, 27, 66, 31, 74, 127, 46, 112, 127, 116, 122, 190, 71, 240, 95, 78, 194, 113, 80, 46, 126, 74, 136, 118, 133, 105, 176, 47, 230, 162, 195, 93, 157, 72, 119, 13, 232, 151, 200, 191, 143, 75, 161, 111, 29, 158, 16, 181, 165, 92, 39, 17, 218, 228, 58, 176, 233, 55, 211, 195, 73, 37, 137, 232, 241, 150, 236, 152, 153, 53, 74, 81, 91, 160, 244, 21, 95, 176, 179, 141, 39, 61, 136, 16, 58, 160, 51, 210, 31, 134, 63, 203, 96, 219, 44, 231, 61, 220, 0, 241, 220, 207, 17, 52, 150, 117, 54, 222, 128, 101, 213, 164, 234, 74, 224, 57, 246, 70, 27, 202, 229, 4, 243, 128, 211, 158, 199, 4}; + + // Introduce 112 errors (this should make the decoder fail): + for (int i = 0; i < 224; i += 2) + { + encoded_input[i] = 0; + } + + auto rs = std::make_unique(); + + int result = rs->decode(encoded_input); + EXPECT_TRUE(result == -1); +} + + +TEST(ReedSolomonTest, Decode113ErrorsWithErasure) +{ + std::vector expected_output = {71, 12, 25, 210, 178, 81, 243, 9, 112, 98, 196, 203, 48, 125, 114, 165, 181, 193, 71, 174, 168, 42, 31, 128, 245, 87, 150, 58, 192, 66, 130, 179}; + + std::vector encoded_input = { + 71, 12, 25, 210, 178, 81, 243, 9, 112, 98, 196, 203, 48, 125, 114, 165, 181, 193, 71, 174, 168, 42, 31, 128, 245, 87, 150, 58, 192, 66, 130, 179, 133, 210, 122, 224, 75, 138, 20, 205, 14, 245, 209, 187, 246, 228, 12, 39, 244, 238, 223, 217, 84, 233, 137, 168, 153, 8, 94, 26, 99, 169, 149, 203, 115, 69, 211, 43, 70, 96, 70, 38, 160, 1, 232, 153, 223, 165, 93, 205, 101, 170, 60, 188, 198, 82, 168, 79, 95, 23, 118, 215, 187, 136, 24, 99, 252, 3, 144, 166, 117, 45, 168, 239, 77, 42, 246, 33, 122, 97, 242, 236, 13, 217, 96, 186, 71, 250, 242, 177, 125, 87, 27, 13, 118, 181, 178, 12, 27, 66, 31, 74, 127, 46, 112, 127, 116, 122, 190, 71, 240, 95, 78, 194, 113, 80, 46, 126, 74, 136, 118, 133, 105, 176, 47, 230, 162, 195, 93, 157, 72, 119, 13, 232, 151, 200, 191, 143, 75, 161, 111, 29, 158, 16, 181, 165, 92, 39, 17, 218, 228, 58, 176, 233, 55, 211, 195, 73, 37, 137, 232, 241, 150, 236, 152, 153, 53, 74, 81, 91, 160, 244, 21, 95, 176, 179, 141, 39, 61, 136, 16, 58, 160, 51, 210, 31, 134, 63, 203, 96, 219, 44, 231, 61, 220, 0, 241, 220, 207, 17, 52, 150, 117, 54, 222, 128, 101, 213, 164, 234, 74, 224, 57, 246, 70, 27, 202, 229, 4, 243, 128, 211, 158, 199, 4}; + + // Introduce 113 errors: + for (int i = 0; i < 226; i += 2) + { + encoded_input[i] = 0; + } + + std::vector erasure_positions{2, 4, 16, 18, 22, 54}; + + auto rs = std::make_unique(); + + int result = rs->decode(encoded_input, erasure_positions); + EXPECT_TRUE(result == 113); + std::vector decoded(encoded_input.begin(), encoded_input.begin() + 32); + EXPECT_TRUE(expected_output == decoded); +}