pqc/test/bench/kyber.cc

129 lines
3.7 KiB
C++
Raw Permalink Normal View History

2021-04-22 21:41:40 +01:00
#include <array>
#include <stdint.h>
#include <utility>
#include <benchmark/benchmark.h>
#include <benchmark/../../src/statistics.h>
#include <benchmark/../../src/cycleclock.h>
2021-04-22 22:34:07 +01:00
#include "kem/kyber/kyber512/avx2/polyvec.h"
extern "C" {
#include "kem/kyber/kyber512/avx2/indcpa.h"
2021-04-22 23:05:15 +01:00
#include "kem/kyber/kyber512/avx2/kem.h"
2021-04-25 22:23:54 +01:00
#include "kem/kyber/kyber512/avx2/rejsample.h"
2021-05-06 12:09:01 +01:00
#include "kem/kyber/kyber512/avx2/ntt.h"
2021-04-22 22:34:07 +01:00
}
2021-04-22 21:41:40 +01:00
auto cpucycle = [](benchmark::State &st, int64_t cycles) {
st.counters["CPU cycles: mean"] = benchmark::Counter(
cycles, benchmark::Counter::kAvgIterations | benchmark::Counter::kResultNoFormat);
};
static void BenchKyberMatK2(benchmark::State &st) {
int64_t t, total = 0;
2021-04-22 22:34:07 +01:00
polyvec a[KYBER_K];
2021-06-21 09:20:48 +01:00
uint8_t seed[32] = {0};
2021-04-22 21:41:40 +01:00
for (auto _ : st) {
t = benchmark::cycleclock::Now();
2021-04-22 22:34:07 +01:00
PQCLEAN_KYBER512_AVX2_gen_matrix(a, seed, 0);
2021-04-22 21:41:40 +01:00
total += benchmark::cycleclock::Now() - t;
2021-04-22 22:34:07 +01:00
benchmark::DoNotOptimize(a);
2021-04-22 21:41:40 +01:00
}
cpucycle(st, total);
}
2021-04-25 22:23:54 +01:00
static void BenchKyberRejSampling(benchmark::State &st) {
int64_t t, total = 0;
int16_t a[256] = {0};
uint8_t buf[168*3] = {0};
2021-04-25 22:23:54 +01:00
for (auto _ : st) {
t = benchmark::cycleclock::Now();
2021-04-25 22:34:30 +01:00
PQCLEAN_KYBER512_AVX2_rej_uniform_avx(a, buf);
2021-04-25 22:23:54 +01:00
total += benchmark::cycleclock::Now() - t;
benchmark::DoNotOptimize(a);
}
cpucycle(st, total);
}
2021-04-22 23:05:15 +01:00
static void BenchKyberKeygen(benchmark::State &st) {
int64_t t, total = 0;
uint8_t sk[1632];
uint8_t pk[800];
for (auto _ : st) {
t = benchmark::cycleclock::Now();
PQCLEAN_KYBER512_AVX2_crypto_kem_keypair(pk, sk);
total += benchmark::cycleclock::Now() - t;
benchmark::DoNotOptimize(pk);
benchmark::DoNotOptimize(sk);
}
cpucycle(st, total);
}
static void BenchKyberEncaps(benchmark::State &st) {
int64_t t, total = 0;
uint8_t sk[1632];
uint8_t pk[800];
uint8_t ct[768];
uint8_t ss[32];
PQCLEAN_KYBER512_AVX2_crypto_kem_keypair(pk, sk);
for (auto _ : st) {
t = benchmark::cycleclock::Now();
PQCLEAN_KYBER512_AVX2_crypto_kem_enc(ss, ct, pk);
total += benchmark::cycleclock::Now() - t;
benchmark::DoNotOptimize(pk);
}
cpucycle(st, total);
}
static void BenchKyberDecaps(benchmark::State &st) {
int64_t t, total = 0;
uint8_t sk[1632];
uint8_t pk[800];
uint8_t ct[768];
uint8_t ss[32];
PQCLEAN_KYBER512_AVX2_crypto_kem_keypair(pk, sk);
PQCLEAN_KYBER512_AVX2_crypto_kem_enc(ss, ct, pk);
for (auto _ : st) {
t = benchmark::cycleclock::Now();
PQCLEAN_KYBER512_AVX2_crypto_kem_dec(ss, ct, sk);
total += benchmark::cycleclock::Now() - t;
benchmark::DoNotOptimize(sk);
}
cpucycle(st, total);
}
2021-05-06 12:09:01 +01:00
2021-05-09 18:11:18 +01:00
static void BenchKyberBaseMulAVX(benchmark::State &st) {
2021-05-06 12:09:01 +01:00
int64_t t, total = 0;
__m256i r[32],a[32],b[32],data[32];
for (auto _ : st) {
t = benchmark::cycleclock::Now();
PQCLEAN_KYBER512_AVX2_basemul_avx(r,a,b,data);
total += benchmark::cycleclock::Now() - t;
benchmark::DoNotOptimize(r);
}
cpucycle(st, total);
}
2021-05-09 18:11:18 +01:00
static void BenchKyberNttAVX(benchmark::State &st) {
int64_t t, total = 0;
__m256i r[32],data[32];
for (auto _ : st) {
t = benchmark::cycleclock::Now();
PQCLEAN_KYBER512_AVX2_ntt_avx(r, data);
total += benchmark::cycleclock::Now() - t;
benchmark::DoNotOptimize(r);
}
cpucycle(st, total);
}
2021-04-22 21:41:40 +01:00
BENCHMARK(BenchKyberMatK2);
2021-04-25 22:23:54 +01:00
BENCHMARK(BenchKyberRejSampling);
2021-04-22 23:05:15 +01:00
BENCHMARK(BenchKyberKeygen);
2021-05-09 18:11:18 +01:00
BENCHMARK(BenchKyberBaseMulAVX);
BENCHMARK(BenchKyberNttAVX);
// TODO: not sure why but memcheck fails in INDCPA encryption
BENCHMARK(BenchKyberEncaps);
BENCHMARK(BenchKyberDecaps);