You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

80 lines
3.4 KiB

  1. #include "bf_decoding.h"
  2. #include "gf2x_arith_mod_xPplusOne.h"
  3. #include <string.h>
  4. int PQCLEAN_LEDAKEMLT12_LEAKTIME_bf_decoding(DIGIT err[],
  5. const POSITION_T HtrPosOnes[N0][DV],
  6. const POSITION_T QtrPosOnes[N0][M],
  7. DIGIT privateSyndrome[],
  8. uint8_t secondIterThreshold) {
  9. DIGIT currSyndrome[NUM_DIGITS_GF2X_ELEMENT];
  10. uint8_t unsatParityChecks[N0 * P];
  11. POSITION_T currQBlkPos[M], currQBitPos[M];
  12. POSITION_T syndromePosToFlip, tmp;
  13. uint32_t correlation, corrt_syndrome_based;
  14. size_t currQoneIdx, endQblockIdx, currblockoffset;
  15. int check;
  16. int iteration = 0;
  17. do {
  18. PQCLEAN_LEDAKEMLT12_LEAKTIME_gf2x_copy(currSyndrome, privateSyndrome);
  19. memset(unsatParityChecks, 0x00, N0 * P * sizeof(uint8_t));
  20. for (size_t i = 0; i < N0; i++) {
  21. for (POSITION_T valueIdx = 0; valueIdx < P; valueIdx++) {
  22. for (size_t HtrOneIdx = 0; HtrOneIdx < DV; HtrOneIdx++) {
  23. tmp = (HtrPosOnes[i][HtrOneIdx] + valueIdx) >= P ?
  24. (HtrPosOnes[i][HtrOneIdx] + valueIdx) - P :
  25. (HtrPosOnes[i][HtrOneIdx] + valueIdx);
  26. if (PQCLEAN_LEDAKEMLT12_LEAKTIME_gf2x_get_coeff(currSyndrome, tmp)) {
  27. unsatParityChecks[i * P + valueIdx]++;
  28. }
  29. }
  30. }
  31. }
  32. /* iteration based threshold determination*/
  33. corrt_syndrome_based = iteration * secondIterThreshold + (1 - iteration) * B0;
  34. // Computation of correlation with a full Q matrix
  35. for (size_t i = 0; i < N0; i++) {
  36. for (POSITION_T j = 0; j < P; j++) {
  37. currQoneIdx = endQblockIdx = 0;
  38. correlation = 0;
  39. for (size_t blockIdx = 0; blockIdx < N0; blockIdx++) {
  40. endQblockIdx += qBlockWeights[blockIdx][i];
  41. currblockoffset = blockIdx * P;
  42. for (; currQoneIdx < endQblockIdx; currQoneIdx++) {
  43. tmp = QtrPosOnes[i][currQoneIdx] + j;
  44. tmp = tmp >= P ? tmp - P : tmp;
  45. currQBitPos[currQoneIdx] = tmp;
  46. currQBlkPos[currQoneIdx] = (POSITION_T)blockIdx;
  47. correlation += unsatParityChecks[tmp + currblockoffset];
  48. }
  49. }
  50. /* Correlation based flipping */
  51. if (correlation >= corrt_syndrome_based) {
  52. PQCLEAN_LEDAKEMLT12_LEAKTIME_gf2x_toggle_coeff(err + NUM_DIGITS_GF2X_ELEMENT * i, j);
  53. for (size_t v = 0; v < M; v++) {
  54. for (size_t HtrOneIdx = 0; HtrOneIdx < DV; HtrOneIdx++) {
  55. syndromePosToFlip = (HtrPosOnes[currQBlkPos[v]][HtrOneIdx] + currQBitPos[v]);
  56. syndromePosToFlip = syndromePosToFlip >= P ? syndromePosToFlip - P : syndromePosToFlip;
  57. PQCLEAN_LEDAKEMLT12_LEAKTIME_gf2x_toggle_coeff(privateSyndrome, syndromePosToFlip);
  58. }
  59. } // end for v
  60. } // end if
  61. } // end for j
  62. } // end for i
  63. iteration = iteration + 1;
  64. check = 0;
  65. while (check < NUM_DIGITS_GF2X_ELEMENT && privateSyndrome[check++] == 0) {};
  66. } while (iteration < ITERATIONS_MAX && check < NUM_DIGITS_GF2X_ELEMENT);
  67. return (check == NUM_DIGITS_GF2X_ELEMENT);
  68. }