| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <inttypes.h> |
| 7 |
#include <stddef.h> |
| 8 |
#include <stdio.h> |
| 9 |
#include <stdlib.h> |
| 10 |
#include <string.h> |
| 11 |
#include "../../mldsa/src/poly.h" |
| 12 |
#include "../../mldsa/src/polyvec.h" |
| 13 |
#include "../../mldsa/src/randombytes.h" |
| 14 |
#include "hal.h" |
| 15 |
|
| 16 |
#define NWARMUP 50 |
| 17 |
#define NITERATIONS 300 |
| 18 |
#define NTESTS 20 |
| 19 |
|
| 20 |
#define CHECK(x) \ |
| 21 |
do \ |
| 22 |
{ \ |
| 23 |
int rc; \ |
| 24 |
rc = (x); \ |
| 25 |
if (!rc) \ |
| 26 |
{ \ |
| 27 |
fprintf(stderr, "ERROR (%s,%d)\n", __FILE__, __LINE__); \ |
| 28 |
return 1; \ |
| 29 |
} \ |
| 30 |
} while (0) |
| 31 |
|
| 32 |
static int cmp_uint64_t(const void *a, const void *b) |
| 33 |
{ |
| 34 |
return (int)((*((const uint64_t *)a)) - (*((const uint64_t *)b))); |
| 35 |
} |
| 36 |
|
| 37 |
#define BENCH(txt, code) \ |
| 38 |
for (i = 0; i < NTESTS; i++) \ |
| 39 |
{ \ |
| 40 |
CHECK(mld_randombytes((uint8_t *)data0, sizeof(data0)) == 0); \ |
| 41 |
CHECK(mld_randombytes((uint8_t *)&polyvecl_a, sizeof(polyvecl_a)) == 0); \ |
| 42 |
CHECK(mld_randombytes((uint8_t *)&polyvecl_b, sizeof(polyvecl_b)) == 0); \ |
| 43 |
CHECK(mld_randombytes((uint8_t *)&polymat, sizeof(polymat)) == 0); \ |
| 44 |
for (j = 0; j < NWARMUP; j++) \ |
| 45 |
{ \ |
| 46 |
code; \ |
| 47 |
} \ |
| 48 |
\ |
| 49 |
t0 = get_cyclecounter(); \ |
| 50 |
for (j = 0; j < NITERATIONS; j++) \ |
| 51 |
{ \ |
| 52 |
code; \ |
| 53 |
} \ |
| 54 |
t1 = get_cyclecounter(); \ |
| 55 |
(cyc)[i] = t1 - t0; \ |
| 56 |
} \ |
| 57 |
qsort((cyc), NTESTS, sizeof(uint64_t), cmp_uint64_t); \ |
| 58 |
printf(txt " cycles=%" PRIu64 "\n", (cyc)[NTESTS >> 1] / NITERATIONS); |
| 59 |
|
| 60 |
static int bench(void) |
| 61 |
{ |
| 62 |
MLD_ALIGN int32_t data0[256]; |
| 63 |
MLD_ALIGN mld_poly poly_out; |
| 64 |
MLD_ALIGN mld_polyvecl polyvecl_a, polyvecl_b; |
| 65 |
MLD_ALIGN mld_polyveck polyveck_out; |
| 66 |
MLD_ALIGN mld_polymat polymat; |
| 67 |
uint64_t cyc[NTESTS]; |
| 68 |
unsigned i, j; |
| 69 |
uint64_t t0, t1; |
| 70 |
|
| 71 |
|
| 72 |
BENCH("poly_ntt", mld_poly_ntt((mld_poly *)data0)) |
| 73 |
BENCH("poly_invntt_tomont", mld_poly_invntt_tomont((mld_poly *)data0)) |
| 74 |
|
| 75 |
|
| 76 |
BENCH("polyvecl_pointwise_acc_montgomery", |
| 77 |
mld_polyvecl_pointwise_acc_montgomery(&poly_out, &polyvecl_a, |
| 78 |
&polyvecl_b)) |
| 79 |
BENCH("polyvec_matrix_pointwise_montgomery", |
| 80 |
mld_polyvec_matrix_pointwise_montgomery(&polyveck_out, &polymat, |
| 81 |
&polyvecl_b)) |
| 82 |
|
| 83 |
return 0; |
| 84 |
} |
| 85 |
|
| 86 |
int main(void) |
| 87 |
{ |
| 88 |
enable_cyclecounter(); |
| 89 |
bench(); |
| 90 |
disable_cyclecounter(); |
| 91 |
|
| 92 |
return 0; |
| 93 |
} |