Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/kex/test_kex.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
printf("\n"); \
}

static int kex_test_correctness(OQS_RAND *rand, OQS_KEX *(*new_method)(OQS_RAND *, const uint8_t *, const size_t), int print, unsigned long occurrences[256]) {
static int kex_test_correctness(OQS_RAND *rand, OQS_KEX * (*new_method)(OQS_RAND *, const uint8_t *, const size_t), int print, unsigned long occurrences[256]) {

OQS_KEX *kex = NULL;
int rc;
Expand Down Expand Up @@ -110,11 +110,11 @@ static int kex_test_correctness(OQS_RAND *rand, OQS_KEX *(*new_method)(OQS_RAND
OQS_KEX_alice_priv_free(kex, alice_priv);
OQS_KEX_free(kex);

return rc;
return rc;

}

static int kex_test_correctness_wrapper(OQS_RAND *rand, OQS_KEX *(*new_method)(OQS_RAND *, const uint8_t *, const size_t), int iterations) {
static int kex_test_correctness_wrapper(OQS_RAND *rand, OQS_KEX * (*new_method)(OQS_RAND *, const uint8_t *, const size_t), int iterations) {

OQS_KEX *kex = NULL;
int ret;
Expand Down Expand Up @@ -152,7 +152,7 @@ static int kex_test_correctness_wrapper(OQS_RAND *rand, OQS_KEX *(*new_method)(O
cleanup:
OQS_KEX_free(kex);

return ret;
return ret;

}

Expand Down
60 changes: 9 additions & 51 deletions src/kex_rlwe_bcns15/fft.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,6 @@ do { \
#define normalize(c,a) c = (a) + ((a) == 0xFFFFFFFF)

/* Define the basic building blocks for the FFT. */
#define DATATYPE uint32_t

#define SET_ZERO(x) (x)=0
#define add(c,a,b) modadd(c,a,b)
#define sub(c,a,b) modsub(c,a,b)
Expand Down Expand Up @@ -96,9 +94,9 @@ static uint32_t reverse(uint32_t x) {
* Exercise Exercise 4.6.4.59.
*/

static void naive(DATATYPE *z, const DATATYPE *x, const DATATYPE *y, unsigned int n) {
static void naive(uint32_t *z, const uint32_t *x, const uint32_t *y, unsigned int n) {
unsigned int i, j, k;
DATATYPE A, B;
uint32_t A, B;

for (i = 0; i < n; i++) {
SET_ZERO(B);
Expand All @@ -116,17 +114,14 @@ static void naive(DATATYPE *z, const DATATYPE *x, const DATATYPE *y, unsigned in
}
}

static void nussbaumer_fft(DATATYPE *z, const DATATYPE *x, const DATATYPE *y, struct oqs_kex_rlwe_bcns15_fft_ctx *ctx) {
DATATYPE **X1;
DATATYPE **Y1;
DATATYPE **Z1;
DATATYPE *T1;
static void nussbaumer_fft(uint32_t z[1024], const uint32_t x[1024], const uint32_t y[1024], struct oqs_kex_rlwe_bcns15_fft_ctx *ctx) {
uint32_t (*X1)[64] = ctx->x1;
uint32_t (*Y1)[64] = ctx->y1;
uint32_t (*Z1)[64] = ctx->z1;
uint32_t *T1 = ctx->t1;
unsigned int i;
int j;

X1 = (DATATYPE **) ctx->x1;
Y1 = (DATATYPE **) ctx->y1;

for (i = 0; i < 32; i++) {
for (j = 0; j < 32; j++) {
set(X1[i][j], x[32 * j + i]);
Expand All @@ -137,9 +132,6 @@ static void nussbaumer_fft(DATATYPE *z, const DATATYPE *x, const DATATYPE *y, st
}
}

Z1 = (DATATYPE **) ctx->z1;
T1 = (DATATYPE *) ctx->t1;

for (j = 4; j >= 0; j--) {
for (i = 0; i < (1U << (5 - j)); i++) {
unsigned int t, ssr = reverse(i);
Expand Down Expand Up @@ -224,36 +216,17 @@ static void nussbaumer_fft(DATATYPE *z, const DATATYPE *x, const DATATYPE *y, st
}
}

void oqs_kex_rlwe_bcns15_fft_mul(uint32_t *z, const uint32_t *x, const uint32_t *y, struct oqs_kex_rlwe_bcns15_fft_ctx *ctx) {
void oqs_kex_rlwe_bcns15_fft_mul(uint32_t z[1024], const uint32_t x[1024], const uint32_t y[1024], struct oqs_kex_rlwe_bcns15_fft_ctx *ctx) {
nussbaumer_fft(z, x, y, ctx);
}

void oqs_kex_rlwe_bcns15_fft_add(uint32_t *z, const uint32_t *x, const uint32_t *y) {
void oqs_kex_rlwe_bcns15_fft_add(uint32_t z[1024], const uint32_t x[1024], const uint32_t y[1024]) {
int i;
for (i = 0; i < 1024; i++) {
add(z[i], x[i], y[i]);
}
}

int oqs_kex_rlwe_bcns15_fft_ctx_init(struct oqs_kex_rlwe_bcns15_fft_ctx *ctx) {
ctx->x1 = (uint32_t **) malloc(64 * sizeof(uint32_t *));
ctx->y1 = (uint32_t **) malloc(64 * sizeof(uint32_t *));
ctx->z1 = (uint32_t **) malloc(64 * sizeof(uint32_t *));
ctx->t1 = (uint32_t *) malloc(64 * sizeof(uint32_t));
if (ctx->x1 == NULL || ctx->y1 == NULL || ctx->z1 == NULL || ctx->t1 == NULL) {
return 0;
}
for (int i = 0; i < 64; i++) {
ctx->x1[i] = (uint32_t *) malloc(64 * sizeof(uint32_t));
ctx->y1[i] = (uint32_t *) malloc(64 * sizeof(uint32_t));
ctx->z1[i] = (uint32_t *) malloc(64 * sizeof(uint32_t));
if (ctx->x1[i] == NULL || ctx->y1[i] == NULL || ctx->z1[i] == NULL) {
return 0;
}
}
return 1;
}

void oqs_kex_rlwe_bcns15_fft_ctx_clear(struct oqs_kex_rlwe_bcns15_fft_ctx *ctx) {
if (ctx == NULL) {
return;
Expand All @@ -265,18 +238,3 @@ void oqs_kex_rlwe_bcns15_fft_ctx_clear(struct oqs_kex_rlwe_bcns15_fft_ctx *ctx)
}
rlwe_memset_volatile(ctx->t1, 0, 64 * sizeof(uint32_t));
}

void oqs_kex_rlwe_bcns15_fft_ctx_free(struct oqs_kex_rlwe_bcns15_fft_ctx *ctx) {
if (ctx == NULL) {
return;
}
for (int i = 0; i < 64; i++) {
free(ctx->x1[i]);
free(ctx->y1[i]);
free(ctx->z1[i]);
}
free(ctx->x1);
free(ctx->y1);
free(ctx->z1);
free(ctx->t1);
}
4 changes: 0 additions & 4 deletions src/kex_rlwe_bcns15/kex_rlwe_bcns15.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@ OQS_KEX *OQS_KEX_rlwe_bcns15_new(OQS_RAND *rand, UNUSED const uint8_t *seed, UNU
if (NULL == k->ctx) {
return NULL;
}
int ok = oqs_kex_rlwe_bcns15_fft_ctx_init(k->ctx);
if (ok != 1) {
return NULL;
}

k->method_name = strdup("RLWE BCNS15");
k->estimated_classical_security = 163;
Expand Down
32 changes: 15 additions & 17 deletions src/kex_rlwe_bcns15/local.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,28 @@
#include <oqs/rand.h>

struct oqs_kex_rlwe_bcns15_fft_ctx {
uint32_t **x1;
uint32_t **y1;
uint32_t **z1;
uint32_t *t1;
uint32_t x1[64][64];
uint32_t y1[64][64];
uint32_t z1[64][64];
uint32_t t1[64];
};

void oqs_kex_rlwe_bcns15_fft_mul(uint32_t *z, const uint32_t *x, const uint32_t *y, struct oqs_kex_rlwe_bcns15_fft_ctx *ctx);
void oqs_kex_rlwe_bcns15_fft_add(uint32_t *z, const uint32_t *x, const uint32_t *y);
void oqs_kex_rlwe_bcns15_fft_mul(uint32_t z[1024], const uint32_t x[1024], const uint32_t y[1024], struct oqs_kex_rlwe_bcns15_fft_ctx *ctx);
void oqs_kex_rlwe_bcns15_fft_add(uint32_t z[1024], const uint32_t x[1024], const uint32_t y[1024]);

int oqs_kex_rlwe_bcns15_fft_ctx_init(struct oqs_kex_rlwe_bcns15_fft_ctx *ctx);
void oqs_kex_rlwe_bcns15_fft_ctx_clear(struct oqs_kex_rlwe_bcns15_fft_ctx *ctx);
void oqs_kex_rlwe_bcns15_fft_ctx_free(struct oqs_kex_rlwe_bcns15_fft_ctx *ctx);

void oqs_kex_rlwe_bcns15_sample_ct(uint32_t *s, OQS_RAND *rand);
void oqs_kex_rlwe_bcns15_round2_ct(uint64_t *out, const uint32_t *in);
void oqs_kex_rlwe_bcns15_crossround2_ct(uint64_t *out, const uint32_t *in, OQS_RAND *rand);
void oqs_kex_rlwe_bcns15_rec_ct(uint64_t *out, const uint32_t *w, const uint64_t *b);
void oqs_kex_rlwe_bcns15_sample_ct(uint32_t s[1024], OQS_RAND *rand);
void oqs_kex_rlwe_bcns15_round2_ct(uint64_t out[16], const uint32_t in[1024]);
void oqs_kex_rlwe_bcns15_crossround2_ct(uint64_t out[16], const uint32_t in[1024], OQS_RAND *rand);
void oqs_kex_rlwe_bcns15_rec_ct(uint64_t out[16], const uint32_t w[1024], const uint64_t b[16]);

void oqs_kex_rlwe_bcns15_sample(uint32_t *s, OQS_RAND *rand);
void oqs_kex_rlwe_bcns15_round2(uint64_t *out, const uint32_t *in);
void oqs_kex_rlwe_bcns15_crossround2(uint64_t *out, const uint32_t *in, OQS_RAND *rand);
void oqs_kex_rlwe_bcns15_rec(uint64_t *out, const uint32_t *w, const uint64_t *b);
void oqs_kex_rlwe_bcns15_sample(uint32_t s[1024], OQS_RAND *rand);
void oqs_kex_rlwe_bcns15_round2(uint64_t out[16], const uint32_t in[1024]);
void oqs_kex_rlwe_bcns15_crossround2(uint64_t out[16], const uint32_t in[1024], OQS_RAND *rand);
void oqs_kex_rlwe_bcns15_rec(uint64_t out[16], const uint32_t w[1024], const uint64_t b[16]);

void oqs_kex_rlwe_bcns15_a_times_s_plus_e(uint32_t *out, const uint32_t *a, const uint32_t *s, const uint32_t *e, struct oqs_kex_rlwe_bcns15_fft_ctx *fft_ctx);
void oqs_kex_rlwe_bcns15_a_times_s_plus_e(uint32_t out[1024], const uint32_t a[1024], const uint32_t s[1024], const uint32_t e[1024], struct oqs_kex_rlwe_bcns15_fft_ctx *fft_ctx);

void oqs_kex_rlwe_bcns15_generate_keypair(const uint32_t *a, uint32_t s[1024], uint32_t b[1024], struct oqs_kex_rlwe_bcns15_fft_ctx *ctx, OQS_RAND *rand);
void oqs_kex_rlwe_bcns15_compute_key_alice(const uint32_t b[1024], const uint32_t s[1024], const uint64_t c[16], uint64_t k[16], struct oqs_kex_rlwe_bcns15_fft_ctx *ctx);
Expand Down
18 changes: 9 additions & 9 deletions src/kex_rlwe_bcns15/rlwe.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ static uint32_t single_sample_ct(uint64_t *in) {
return index;
}

void oqs_kex_rlwe_bcns15_sample_ct(uint32_t *s, OQS_RAND *rand) {
void oqs_kex_rlwe_bcns15_sample_ct(uint32_t s[1024], OQS_RAND *rand) {
int i, j;
for (i = 0; i < 16; i++) {
uint64_t r = rand->rand_64(rand);
Expand All @@ -170,7 +170,7 @@ void oqs_kex_rlwe_bcns15_sample_ct(uint32_t *s, OQS_RAND *rand) {
}
}

void oqs_kex_rlwe_bcns15_round2_ct(uint64_t *out, const uint32_t *in) {
void oqs_kex_rlwe_bcns15_round2_ct(uint64_t out[16], const uint32_t in[1024]) {
int i;
memset(out, 0, 128);
for (i = 0; i < 1024; i++) {
Expand All @@ -180,7 +180,7 @@ void oqs_kex_rlwe_bcns15_round2_ct(uint64_t *out, const uint32_t *in) {
}
}

void oqs_kex_rlwe_bcns15_crossround2_ct(uint64_t *out, const uint32_t *in, OQS_RAND *rand) {
void oqs_kex_rlwe_bcns15_crossround2_ct(uint64_t out[16], const uint32_t in[1024], OQS_RAND *rand) {
int i, j;
memset(out, 0, 128);
for (i = 0; i < 64; i++) {
Expand All @@ -197,7 +197,7 @@ void oqs_kex_rlwe_bcns15_crossround2_ct(uint64_t *out, const uint32_t *in, OQS_R
}
}

void oqs_kex_rlwe_bcns15_rec_ct(uint64_t *out, const uint32_t *w, const uint64_t *b) {
void oqs_kex_rlwe_bcns15_rec_ct(uint64_t out[16], const uint32_t w[1024], const uint64_t b[16]) {
int i;
memset(out, 0, 128);
for (i = 0; i < 1024; i++) {
Expand All @@ -212,7 +212,7 @@ void oqs_kex_rlwe_bcns15_rec_ct(uint64_t *out, const uint32_t *w, const uint64_t
}
}

void oqs_kex_rlwe_bcns15_sample(uint32_t *s, OQS_RAND *rand) {
void oqs_kex_rlwe_bcns15_sample(uint32_t s[1024], OQS_RAND *rand) {
int i, j;
for (i = 0; i < 16; i++) {
uint64_t r = rand->rand_64(rand);
Expand All @@ -232,7 +232,7 @@ void oqs_kex_rlwe_bcns15_sample(uint32_t *s, OQS_RAND *rand) {
}
}

void oqs_kex_rlwe_bcns15_round2(uint64_t *out, const uint32_t *in) {
void oqs_kex_rlwe_bcns15_round2(uint64_t out[16], const uint32_t in[1024]) {
int i;

// out should have enough space for 1024-bits
Expand All @@ -246,7 +246,7 @@ void oqs_kex_rlwe_bcns15_round2(uint64_t *out, const uint32_t *in) {
}
}

void oqs_kex_rlwe_bcns15_crossround2(uint64_t *out, const uint32_t *in, OQS_RAND *rand) {
void oqs_kex_rlwe_bcns15_crossround2(uint64_t out[16], const uint32_t in[1024], OQS_RAND *rand) {
int i, j;
// out should have enough space for 1024-bits
memset(out, 0, 128);
Expand All @@ -264,7 +264,7 @@ void oqs_kex_rlwe_bcns15_crossround2(uint64_t *out, const uint32_t *in, OQS_RAND
}
}

void oqs_kex_rlwe_bcns15_rec(uint64_t *out, const uint32_t *w, const uint64_t *b) {
void oqs_kex_rlwe_bcns15_rec(uint64_t out[16], const uint32_t w[1024], const uint64_t b[16]) {
int i;

// out should have enough space for 1024 bits
Expand All @@ -286,7 +286,7 @@ void oqs_kex_rlwe_bcns15_rec(uint64_t *out, const uint32_t *w, const uint64_t *b
}
}

void oqs_kex_rlwe_bcns15_a_times_s_plus_e(uint32_t *out, const uint32_t *a, const uint32_t *s, const uint32_t *e, struct oqs_kex_rlwe_bcns15_fft_ctx *ctx) {
void oqs_kex_rlwe_bcns15_a_times_s_plus_e(uint32_t out[1024], const uint32_t a[1024], const uint32_t s[1024], const uint32_t e[1024], struct oqs_kex_rlwe_bcns15_fft_ctx *ctx) {
oqs_kex_rlwe_bcns15_fft_mul(out, a, s, ctx);
oqs_kex_rlwe_bcns15_fft_add(out, out, e);
}
Expand Down
2 changes: 1 addition & 1 deletion src/rand/rand.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ double OQS_RAND_test_statistical_distance_from_uniform(const unsigned long occur
// = 1/2 \sum_z | 1/256 - Pr[Y=z] |
double distance = 0.0;
for (int i = 0; i < 256; i++) {
distance += fabs(1.0/256.0 - (double) occurrences[i] / (double) total);
distance += fabs(1.0 / 256.0 - (double) occurrences[i] / (double) total);
}
distance /= 2.0;

Expand Down
14 changes: 7 additions & 7 deletions src/rand/test_rand.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ static int rand_test_distribution_n(OQS_RAND *rand, unsigned long occurrences[25
return 1;
}

static int rand_test_distribution_wrapper(OQS_RAND *(*new_method)(), int iterations) {
static int rand_test_distribution_wrapper(OQS_RAND * (*new_method)(), int iterations) {

OQS_RAND *rand = new_method();
if (rand == NULL) {
Expand All @@ -67,16 +67,16 @@ static int rand_test_distribution_wrapper(OQS_RAND *(*new_method)(), int iterati
occurrences[i] = 0;
}

printf("1-byte mode for %d iterations\n", 8*iterations);
rand_test_distribution_8(rand, occurrences, 8*iterations);
printf("1-byte mode for %d iterations\n", 8 * iterations);
rand_test_distribution_8(rand, occurrences, 8 * iterations);
printf(" Statistical distance from uniform: %12.10f\n", OQS_RAND_test_statistical_distance_from_uniform(occurrences));

for (int i = 0; i < 256; i++) {
occurrences[i] = 0;
}

printf("4-byte mode for %d iterations\n", 2*iterations);
rand_test_distribution_32(rand, occurrences, 2*iterations);
printf("4-byte mode for %d iterations\n", 2 * iterations);
rand_test_distribution_32(rand, occurrences, 2 * iterations);
printf(" Statistical distance from uniform: %12.10f\n", OQS_RAND_test_statistical_distance_from_uniform(occurrences));

for (int i = 0; i < 256; i++) {
Expand All @@ -91,8 +91,8 @@ static int rand_test_distribution_wrapper(OQS_RAND *(*new_method)(), int iterati
occurrences[i] = 0;
}

printf("n-byte mode for %d bytes\n", 8*iterations);
rand_test_distribution_n(rand, occurrences, 8*iterations);
printf("n-byte mode for %d bytes\n", 8 * iterations);
rand_test_distribution_n(rand, occurrences, 8 * iterations);
printf(" Statistical distance from uniform: %12.10f\n", OQS_RAND_test_statistical_distance_from_uniform(occurrences));

OQS_RAND_free(rand);
Expand Down