1
1
mirror of https://github.com/henrydcase/pqc.git synced 2024-12-02 12:21:23 +00:00
pqcrypto/crypto_kem/ntruhrss701/clean/sample.c

47 lines
1.5 KiB
C
Raw Normal View History

2019-04-15 01:26:42 +01:00
#include "sample.h"
void PQCLEAN_NTRUHRSS701_CLEAN_sample_fg(poly *f, poly *g, const unsigned char uniformbytes[NTRU_SAMPLE_FG_BYTES]) {
PQCLEAN_NTRUHRSS701_CLEAN_sample_iid_plus(f, uniformbytes);
PQCLEAN_NTRUHRSS701_CLEAN_sample_iid_plus(g, uniformbytes + NTRU_SAMPLE_IID_BYTES);
2019-04-15 01:26:42 +01:00
}
void PQCLEAN_NTRUHRSS701_CLEAN_sample_rm(poly *r, poly *m, const unsigned char uniformbytes[NTRU_SAMPLE_RM_BYTES]) {
PQCLEAN_NTRUHRSS701_CLEAN_sample_iid(r, uniformbytes);
PQCLEAN_NTRUHRSS701_CLEAN_sample_iid(m, uniformbytes + NTRU_SAMPLE_IID_BYTES);
}
2019-04-15 01:26:42 +01:00
void PQCLEAN_NTRUHRSS701_CLEAN_sample_iid_plus(poly *r, const unsigned char uniformbytes[NTRU_SAMPLE_IID_BYTES]) {
/* Sample r using PQCLEAN_NTRUHRSS701_CLEAN_sample_iid then conditionally flip */
2019-04-15 01:26:42 +01:00
/* signs of even index coefficients so that <x*r, r> >= 0. */
int i;
uint16_t s = 0;
PQCLEAN_NTRUHRSS701_CLEAN_sample_iid(r, uniformbytes);
/* Map {0,1,2} -> {0, 1, 2^16 - 1} */
for (i = 0; i < NTRU_N - 1; i++) {
r->coeffs[i] = r->coeffs[i] | (-(r->coeffs[i] >> 1));
}
/* s = <x*r, r>. (r[n-1] = 0) */
for (i = 0; i < NTRU_N - 1; i++) {
2020-03-09 22:02:44 +00:00
s += (uint16_t)((uint32_t)r->coeffs[i + 1] * (uint32_t)r->coeffs[i]);
2019-04-15 01:26:42 +01:00
}
/* Extract sign of s (sign(0) = 1) */
s = 1 | (-(s >> 15));
for (i = 0; i < NTRU_N; i += 2) {
2020-03-09 22:02:44 +00:00
r->coeffs[i] = (uint16_t)((uint32_t)s * (uint32_t)r->coeffs[i]);
2019-04-15 01:26:42 +01:00
}
/* Map {0,1,2^16-1} -> {0, 1, 2} */
for (i = 0; i < NTRU_N; i++) {
r->coeffs[i] = 3 & (r->coeffs[i] ^ (r->coeffs[i] >> 15));
}
}