pqc/crypto_kem/ledakemlt12/leaktime/dfr_test.c

89 lines
3.0 KiB
C
Raw Normal View History

2019-06-10 19:42:31 +01:00
#include "bf_decoding.h"
#include "dfr_test.h"
#include "gf2x_arith_mod_xPplusOne.h"
#include "qc_ldpc_parameters.h"
2019-08-21 13:28:31 +01:00
#include "sort.h"
2019-06-10 19:42:31 +01:00
#include <string.h>
2019-08-21 13:28:31 +01:00
int PQCLEAN_LEDAKEMLT12_LEAKTIME_DFR_test(POSITION_T LSparse[N0][DV * M], uint8_t *secondIterThreshold) {
2019-06-10 19:42:31 +01:00
2019-08-21 13:28:31 +01:00
POSITION_T LSparse_loc[N0][DV * M]; /* vector of N_0 sparse blocks */
uint8_t gamma[N0][N0][P] = {{{0}}};
uint32_t gammaHist[N0][DV * M + 1] = {{0}};
size_t maxMut[N0], maxMutMinusOne[N0];
size_t allBlockMaxSumst, allBlockMaxSumstMinusOne;
size_t histIdx, toAdd;
2019-06-10 19:42:31 +01:00
2019-08-24 14:48:38 +01:00
for (size_t i = 0; i < N0; i++) {
for (size_t j = 0; j < DV * M; j++) {
2019-06-10 19:42:31 +01:00
if (LSparse[i][j] != 0) {
2019-08-24 14:48:38 +01:00
LSparse_loc[i][j] = (P - LSparse[i][j]);
2019-06-10 19:42:31 +01:00
}
}
2019-08-21 13:28:31 +01:00
PQCLEAN_LEDAKEMLT12_LEAKTIME_uint32_sort(LSparse_loc[i], DV * M);
2019-06-10 19:42:31 +01:00
}
2019-08-21 13:28:31 +01:00
2019-08-24 14:48:38 +01:00
for (size_t i = 0; i < N0; i++ ) {
for (size_t j = 0; j < N0; j++) {
for (size_t k = 0; k < (DV * M); k++) {
for (size_t l = 0; l < (DV * M); l++) {
gamma[i][j][(P + LSparse_loc[i][k] - LSparse_loc[j][l]) % P]++;
2019-06-10 19:42:31 +01:00
}
}
}
}
2019-08-21 13:28:31 +01:00
2019-08-24 14:48:38 +01:00
for (size_t i = 0; i < N0; i++ ) {
for (size_t j = 0; j < N0; j++ ) {
2019-06-10 19:42:31 +01:00
gamma[i][j][0] = 0;
}
}
2019-08-21 13:28:31 +01:00
2019-06-10 19:42:31 +01:00
/* build histogram of values in gamma */
2019-08-24 14:48:38 +01:00
for (size_t i = 0; i < N0; i++ ) {
for (size_t j = 0; j < N0; j++ ) {
for (size_t k = 0; k < P; k++) {
2019-06-10 19:42:31 +01:00
gammaHist[i][gamma[i][j][k]]++;
}
}
}
2019-08-24 14:48:38 +01:00
for (size_t gammaBlockRowIdx = 0; gammaBlockRowIdx < N0; gammaBlockRowIdx++) {
2019-06-10 19:42:31 +01:00
maxMutMinusOne[gammaBlockRowIdx] = 0;
2019-08-24 14:48:38 +01:00
histIdx = DV * M;
toAdd = T_BAR - 1;
2019-06-10 19:42:31 +01:00
while ( (histIdx > 0) && (toAdd > 0)) {
if (gammaHist[gammaBlockRowIdx][histIdx] > toAdd ) {
maxMutMinusOne[gammaBlockRowIdx] += histIdx * toAdd;
toAdd = 0;
} else {
maxMutMinusOne[gammaBlockRowIdx] += histIdx * gammaHist[gammaBlockRowIdx][histIdx];
toAdd -= gammaHist[gammaBlockRowIdx][histIdx];
histIdx--;
}
}
maxMut[gammaBlockRowIdx] = histIdx + maxMutMinusOne[gammaBlockRowIdx];
}
/*seek max values across all gamma blocks */
allBlockMaxSumst = maxMut[0];
allBlockMaxSumstMinusOne = maxMutMinusOne[0];
for (size_t gammaBlockRowIdx = 0; gammaBlockRowIdx < N0; gammaBlockRowIdx++) {
2019-06-10 19:42:31 +01:00
allBlockMaxSumst = allBlockMaxSumst < maxMut[gammaBlockRowIdx] ?
maxMut[gammaBlockRowIdx] :
allBlockMaxSumst;
allBlockMaxSumstMinusOne = allBlockMaxSumstMinusOne < maxMutMinusOne[gammaBlockRowIdx] ?
maxMutMinusOne[gammaBlockRowIdx] :
allBlockMaxSumstMinusOne;
}
if (DV * M > (allBlockMaxSumstMinusOne + allBlockMaxSumst)) {
2019-08-23 11:41:58 +01:00
*secondIterThreshold = (uint8_t) (allBlockMaxSumst + 1);
2019-08-21 13:28:31 +01:00
return 1;
2019-06-10 19:42:31 +01:00
}
2019-08-21 13:28:31 +01:00
return 0;
2019-06-10 19:42:31 +01:00
}