33232a0343
* Sebastian's HQC merge request * Clean up changes to common infrastructure * Fix Bitmask macro It assumed that ``unsigned long`` was 64 bit * Remove maxlen from nistseedexpander It's a complicated thing to handle because the value is larger than size_t supports on 32-bit platforms * Initialize buffers to help linter * Add Nistseedexpander test * Resolve UB in gf2x.c Some of the shifts could be larger than WORD_SIZE_BITS, ie. larger than the width of uint64_t. This apparently on Intel gets interpreted as the shift mod 64, but on ARM something else happened. * Fix Windows complaints * rename log, exp which appear to be existing functions on MS * Solve endianness problems * remove all spaces before ';' * Fix duplicate consistency * Fix duplicate consistency * Fix complaints by MSVC about narrowing int * Add nistseedexpander.obj to COMMON_OBJECTS_NOPATH * astyle format util.[ch] * add util.h to makefile * Sort includes in util.h * Fix more Windows MSVC complaints Co-authored-by: Sebastian Verschoor <sebastian@zeroknowledge.me> Co-authored-by: Thom Wiggers <thom@thomwiggers.nl>
101 lines
2.9 KiB
C
101 lines
2.9 KiB
C
/**
|
|
* @file repetition.c
|
|
* @brief Implementation of repetition codes
|
|
*/
|
|
|
|
#include "parameters.h"
|
|
#include "repetition.h"
|
|
#include <stddef.h>
|
|
#include <stdint.h>
|
|
|
|
static void array_to_rep_codeword(uint8_t *o, const uint8_t *v);
|
|
|
|
|
|
/**
|
|
* @brief Encoding each bit in the message m using the repetition code
|
|
*
|
|
* For reasons of clarity and comprehensibility, we do the encoding by storing the encoded bits in a String (each bit in an a uint8_t),
|
|
* then we parse the obtained string to an compact array using the function array_to_rep_codeword().
|
|
*
|
|
* @param[out] em Pointer to an array that is the code word
|
|
* @param[in] m Pointer to an array that is the message
|
|
*/
|
|
void PQCLEAN_HQC2561CCA2_LEAKTIME_repetition_code_encode(uint8_t *em, const uint8_t *m) {
|
|
uint8_t tmp[PARAM_N1N2] = {0};
|
|
uint8_t bit = 0;
|
|
uint32_t index;
|
|
|
|
for (size_t i = 0; i < (VEC_N1_SIZE_BYTES - 1); ++i) {
|
|
for (uint8_t j = 0; j < 8; ++j) {
|
|
bit = (m[i] >> j) & 0x01;
|
|
index = (8 * (uint32_t)i + j) * PARAM_N2;
|
|
for (uint8_t k = 0; k < PARAM_N2; ++k) {
|
|
tmp[index + k] = bit;
|
|
}
|
|
}
|
|
}
|
|
|
|
for (uint8_t j = 0; j < (PARAM_N1 % 8); ++j) {
|
|
bit = (m[VEC_N1_SIZE_BYTES - 1] >> j) & 0x01;
|
|
index = (8 * (VEC_N1_SIZE_BYTES - 1) + j) * PARAM_N2;
|
|
for (uint8_t k = 0; k < PARAM_N2; ++k) {
|
|
tmp[index + k] = bit;
|
|
}
|
|
}
|
|
|
|
array_to_rep_codeword(em, tmp);
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* @brief Decoding the code words to a message using the repetition code
|
|
*
|
|
* We use a majority decoding. In fact we have that PARAM_N2 = 2 * PARAM_T + 1, thus,
|
|
* if the Hamming weight of the vector is greater than PARAM_T, the code word is decoded
|
|
* to 1 and 0 otherwise.
|
|
*
|
|
* @param[out] m Pointer to an array that is the message
|
|
* @param[in] em Pointer to an array that is the code word
|
|
*/
|
|
void PQCLEAN_HQC2561CCA2_LEAKTIME_repetition_code_decode(uint8_t *m, const uint8_t *em) {
|
|
size_t t = 0; // m index
|
|
uint8_t k = PARAM_N2; // block counter
|
|
uint8_t ones = 0; // number of 1 in the current block
|
|
|
|
for (size_t i = 0; i < VEC_N1N2_SIZE_BYTES; ++i) {
|
|
for (uint8_t j = 0; j < 8; ++j) {
|
|
ones += (em[i] >> j) & 0x01;
|
|
|
|
if (--k) {
|
|
continue;
|
|
}
|
|
|
|
m[t / 8] |= (ones > PARAM_T) << t % 8;
|
|
++t;
|
|
k = PARAM_N2;
|
|
ones = 0;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* @brief Parse an array to an compact array
|
|
*
|
|
* @param[out] o Pointer to an array
|
|
* @param[in] v Pointer to an array
|
|
*/
|
|
static void array_to_rep_codeword(uint8_t *o, const uint8_t *v) {
|
|
for (size_t i = 0; i < (VEC_N1N2_SIZE_BYTES - 1); ++i) {
|
|
for (uint8_t j = 0; j < 8; ++j) {
|
|
o[i] |= v[j + 8 * i] << j;
|
|
}
|
|
}
|
|
|
|
for (uint8_t j = 0; j < PARAM_N1N2 % 8; ++j) {
|
|
o[VEC_N1N2_SIZE_BYTES - 1] |= (v[j + 8 * (VEC_N1N2_SIZE_BYTES - 1)]) << j;
|
|
}
|
|
}
|