2019-06-10 19:42:31 +01:00
|
|
|
#include "bf_decoding.h"
|
|
|
|
#include "gf2x_arith_mod_xPplusOne.h"
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
int PQCLEAN_LEDAKEMLT32_CLEAN_bf_decoding(DIGIT err[],
|
|
|
|
const POSITION_T HtrPosOnes[N0][DV],
|
|
|
|
const POSITION_T QtrPosOnes[N0][M],
|
2019-06-14 13:23:58 +01:00
|
|
|
DIGIT privateSyndrome[],
|
|
|
|
uint8_t threshold) {
|
2019-06-10 19:42:31 +01:00
|
|
|
|
|
|
|
uint8_t unsatParityChecks[N0 * P];
|
|
|
|
POSITION_T currQBlkPos[M], currQBitPos[M];
|
|
|
|
DIGIT currSyndrome[NUM_DIGITS_GF2X_ELEMENT];
|
|
|
|
int check;
|
|
|
|
int iteration = 0;
|
2019-06-14 13:23:58 +01:00
|
|
|
unsigned int corrt_syndrome_based;
|
2019-06-10 19:42:31 +01:00
|
|
|
|
|
|
|
do {
|
2019-06-11 21:50:33 +01:00
|
|
|
PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_copy(currSyndrome, privateSyndrome);
|
2019-06-10 19:42:31 +01:00
|
|
|
memset(unsatParityChecks, 0x00, N0 * P * sizeof(uint8_t));
|
|
|
|
for (int i = 0; i < N0; i++) {
|
|
|
|
for (int valueIdx = 0; valueIdx < P; valueIdx++) {
|
|
|
|
for (int HtrOneIdx = 0; HtrOneIdx < DV; HtrOneIdx++) {
|
|
|
|
POSITION_T tmp = (HtrPosOnes[i][HtrOneIdx] + valueIdx) >= P ? (HtrPosOnes[i][HtrOneIdx] + valueIdx) - P : (HtrPosOnes[i][HtrOneIdx] + valueIdx);
|
2019-06-11 21:50:33 +01:00
|
|
|
if (PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_get_coeff(currSyndrome, tmp)) {
|
2019-06-10 19:42:31 +01:00
|
|
|
unsatParityChecks[i * P + valueIdx]++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* iteration based threshold determination*/
|
2019-06-14 13:23:58 +01:00
|
|
|
corrt_syndrome_based = iteration ? (unsigned int) threshold : B0;
|
2019-06-10 19:42:31 +01:00
|
|
|
|
|
|
|
//Computation of correlation with a full Q matrix
|
|
|
|
for (int i = 0; i < N0; i++) {
|
|
|
|
for (int j = 0; j < P; j++) {
|
|
|
|
int currQoneIdx = 0; // position in the column of QtrPosOnes[][...]
|
|
|
|
int endQblockIdx = 0;
|
|
|
|
unsigned int correlation = 0;
|
|
|
|
|
|
|
|
for (int blockIdx = 0; blockIdx < N0; blockIdx++) {
|
|
|
|
endQblockIdx += qBlockWeights[blockIdx][i];
|
|
|
|
int currblockoffset = blockIdx * P;
|
|
|
|
for (; currQoneIdx < endQblockIdx; currQoneIdx++) {
|
2019-06-14 13:23:58 +01:00
|
|
|
POSITION_T tmp = QtrPosOnes[i][currQoneIdx] + j;
|
2019-06-10 19:42:31 +01:00
|
|
|
tmp = tmp >= P ? tmp - P : tmp;
|
|
|
|
currQBitPos[currQoneIdx] = tmp;
|
|
|
|
currQBlkPos[currQoneIdx] = blockIdx;
|
|
|
|
correlation += unsatParityChecks[tmp + currblockoffset];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Correlation based flipping */
|
|
|
|
if (correlation >= corrt_syndrome_based) {
|
2019-06-11 21:50:33 +01:00
|
|
|
PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_toggle_coeff(err + NUM_DIGITS_GF2X_ELEMENT * i, j);
|
2019-06-10 19:42:31 +01:00
|
|
|
for (int v = 0; v < M; v++) {
|
2019-06-14 13:23:58 +01:00
|
|
|
POSITION_T syndromePosToFlip;
|
2019-06-10 19:42:31 +01:00
|
|
|
for (int HtrOneIdx = 0; HtrOneIdx < DV; HtrOneIdx++) {
|
|
|
|
syndromePosToFlip = (HtrPosOnes[currQBlkPos[v]][HtrOneIdx] + currQBitPos[v] );
|
|
|
|
syndromePosToFlip = syndromePosToFlip >= P ? syndromePosToFlip - P : syndromePosToFlip;
|
2019-06-11 21:50:33 +01:00
|
|
|
PQCLEAN_LEDAKEMLT32_CLEAN_gf2x_toggle_coeff(privateSyndrome, syndromePosToFlip);
|
2019-06-10 19:42:31 +01:00
|
|
|
}
|
|
|
|
} // end for v
|
|
|
|
} // end if
|
|
|
|
} // end for j
|
|
|
|
} // end for i
|
|
|
|
|
|
|
|
iteration = iteration + 1;
|
|
|
|
check = 0;
|
|
|
|
while (check < NUM_DIGITS_GF2X_ELEMENT && privateSyndrome[check++] == 0) {};
|
|
|
|
|
|
|
|
} while (iteration < ITERATIONS_MAX && check < NUM_DIGITS_GF2X_ELEMENT);
|
|
|
|
|
|
|
|
return (check == NUM_DIGITS_GF2X_ELEMENT);
|
|
|
|
}
|