1
1
mirror of https://github.com/henrydcase/pqc.git synced 2024-11-27 01:41:40 +00:00
pqcrypto/crypto_kem/ledakemlt52/leaktime/dfr_test.c

88 lines
2.9 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_LEDAKEMLT52_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 */
int gamma[N0][N0][P] = {{{0}}};
int maxMut[N0], maxMutMinusOne[N0];
int allBlockMaxSumst, allBlockMaxSumstMinusOne;
2019-06-10 19:42:31 +01:00
unsigned int gammaHist[N0][DV * M + 1] = {{0}};
for (int i = 0; i < N0; i++) {
for (int j = 0; j < DV * M; j++) {
if (LSparse[i][j] != 0) {
2019-08-21 13:28:31 +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_LEDAKEMLT52_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-06-10 19:42:31 +01:00
for (int i = 0; i < N0; i++ ) {
for (int j = 0; j < N0; j++ ) {
2019-08-21 13:28:31 +01:00
for (int k = 0; k < (DV * M); k++) {
for (int 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-06-10 19:42:31 +01:00
for (int i = 0; i < N0; i++ ) {
for (int j = 0; j < N0; j++ ) {
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 */
for (int i = 0; i < N0; i++ ) {
for (int j = 0; j < N0; j++ ) {
for (int k = 0; k < P; k++) {
gammaHist[i][gamma[i][j][k]]++;
}
}
}
for (int gammaBlockRowIdx = 0; gammaBlockRowIdx < N0; gammaBlockRowIdx++) {
2019-08-21 13:28:31 +01:00
unsigned int toAdd = T_BAR - 1;
2019-06-10 19:42:31 +01:00
maxMutMinusOne[gammaBlockRowIdx] = 0;
2019-08-21 13:28:31 +01:00
unsigned int histIdx = DV * M;
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 (int gammaBlockRowIdx = 0; gammaBlockRowIdx < N0 ; gammaBlockRowIdx++) {
allBlockMaxSumst = allBlockMaxSumst < maxMut[gammaBlockRowIdx] ?
maxMut[gammaBlockRowIdx] :
allBlockMaxSumst;
allBlockMaxSumstMinusOne = allBlockMaxSumstMinusOne < maxMutMinusOne[gammaBlockRowIdx] ?
maxMutMinusOne[gammaBlockRowIdx] :
allBlockMaxSumstMinusOne;
}
if (DV * M > (allBlockMaxSumstMinusOne + allBlockMaxSumst)) {
2019-08-21 13:28:31 +01:00
*secondIterThreshold = allBlockMaxSumst + 1;
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
}