Skip to content

Commit 6a0edd9

Browse files
author
Wei Dai
authored
Merge pull request #379 from jlhcrawford/expand-benchmarks
Expand benchmark coverage
2 parents 7cb7aad + b5078fa commit 6a0edd9

6 files changed

Lines changed: 228 additions & 0 deletions

File tree

native/bench/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ if(SEAL_BUILD_BENCH)
5757
PRIVATE
5858
${CMAKE_CURRENT_LIST_DIR}/bench.cpp
5959
${CMAKE_CURRENT_LIST_DIR}/keygen.cpp
60+
${CMAKE_CURRENT_LIST_DIR}/ntt.cpp
6061
${CMAKE_CURRENT_LIST_DIR}/bfv.cpp
6162
${CMAKE_CURRENT_LIST_DIR}/ckks.cpp
6263
)

native/bench/bench.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ namespace sealbench
6363
SEAL_BENCHMARK_REGISTER(BFV, n, log_q, DecodeBatch, bm_bfv_decode_batch, bm_env_bfv);
6464
SEAL_BENCHMARK_REGISTER(BFV, n, log_q, EvaluateAddCt, bm_bfv_add_ct, bm_env_bfv);
6565
SEAL_BENCHMARK_REGISTER(BFV, n, log_q, EvaluateAddPt, bm_bfv_add_pt, bm_env_bfv);
66+
SEAL_BENCHMARK_REGISTER(BFV, n, log_q, EvaluateNegate, bm_bfv_negate, bm_env_bfv);
67+
SEAL_BENCHMARK_REGISTER(BFV, n, log_q, EvaluateSubCt, bm_bfv_sub_ct, bm_env_bfv);
68+
SEAL_BENCHMARK_REGISTER(BFV, n, log_q, EvaluateSubPt, bm_bfv_sub_pt, bm_env_bfv);
6669
SEAL_BENCHMARK_REGISTER(BFV, n, log_q, EvaluateMulCt, bm_bfv_mul_ct, bm_env_bfv);
6770
SEAL_BENCHMARK_REGISTER(BFV, n, log_q, EvaluateMulPt, bm_bfv_mul_pt, bm_env_bfv);
6871
SEAL_BENCHMARK_REGISTER(BFV, n, log_q, EvaluateSquare, bm_bfv_square, bm_env_bfv);
@@ -84,6 +87,9 @@ namespace sealbench
8487
SEAL_BENCHMARK_REGISTER(CKKS, n, log_q, DecodeDouble, bm_ckks_decode_double, bm_env_ckks);
8588
SEAL_BENCHMARK_REGISTER(CKKS, n, log_q, EvaluateAddCt, bm_ckks_add_ct, bm_env_ckks);
8689
SEAL_BENCHMARK_REGISTER(CKKS, n, log_q, EvaluateAddPt, bm_ckks_add_pt, bm_env_ckks);
90+
SEAL_BENCHMARK_REGISTER(CKKS, n, log_q, EvaluateNegate, bm_ckks_negate, bm_env_ckks);
91+
SEAL_BENCHMARK_REGISTER(CKKS, n, log_q, EvaluateSubCt, bm_ckks_sub_ct, bm_env_ckks);
92+
SEAL_BENCHMARK_REGISTER(CKKS, n, log_q, EvaluateSubPt, bm_ckks_sub_pt, bm_env_ckks);
8793
SEAL_BENCHMARK_REGISTER(CKKS, n, log_q, EvaluateMulCt, bm_ckks_mul_ct, bm_env_ckks);
8894
SEAL_BENCHMARK_REGISTER(CKKS, n, log_q, EvaluateMulPt, bm_ckks_mul_pt, bm_env_ckks);
8995
SEAL_BENCHMARK_REGISTER(CKKS, n, log_q, EvaluateSquare, bm_ckks_square, bm_env_ckks);
@@ -96,6 +102,12 @@ namespace sealbench
96102
SEAL_BENCHMARK_REGISTER(CKKS, n, log_q, EvaluateRelinInplace, bm_ckks_relin_inplace, bm_env_ckks);
97103
SEAL_BENCHMARK_REGISTER(CKKS, n, log_q, EvaluateRotate, bm_ckks_rotate, bm_env_ckks);
98104
}
105+
SEAL_BENCHMARK_REGISTER(NTT, n, log_q, ForwardNTT, bm_forward_ntt, bm_env_bfv);
106+
SEAL_BENCHMARK_REGISTER(NTT, n, log_q, InverseNTT, bm_inverse_ntt, bm_env_bfv);
107+
SEAL_BENCHMARK_REGISTER(NTT, n, log_q, ForwardNTTLowLevel, bm_forward_ntt_low_level, bm_env_bfv);
108+
SEAL_BENCHMARK_REGISTER(NTT, n, log_q, InverseNTTLowLevel, bm_inverse_ntt_low_level, bm_env_bfv);
109+
SEAL_BENCHMARK_REGISTER(NTT, n, log_q, ForwardNTTLowLevelLazy, bm_forward_ntt_low_level_lazy, bm_env_bfv);
110+
SEAL_BENCHMARK_REGISTER(NTT, n, log_q, InverseNTTLowLevelLazy, bm_inverse_ntt_low_level_lazy, bm_env_bfv);
99111
}
100112

101113
} // namespace sealbench

native/bench/bench.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,14 @@ namespace sealbench
336336
std::vector<seal::Ciphertext> ct_;
337337
}; // namespace BMEnv
338338

339+
// NTT benchmark cases
340+
void bm_forward_ntt(benchmark::State &state, std::shared_ptr<BMEnv> bm_env);
341+
void bm_inverse_ntt(benchmark::State &state, std::shared_ptr<BMEnv> bm_env);
342+
void bm_forward_ntt_low_level(benchmark::State &state, std::shared_ptr<BMEnv> bm_env);
343+
void bm_inverse_ntt_low_level(benchmark::State &state, std::shared_ptr<BMEnv> bm_env);
344+
void bm_forward_ntt_low_level_lazy(benchmark::State &state, std::shared_ptr<BMEnv> bm_env);
345+
void bm_inverse_ntt_low_level_lazy(benchmark::State &state, std::shared_ptr<BMEnv> bm_env);
346+
339347
// KeyGen benchmark cases
340348
void bm_keygen_secret(benchmark::State &state, std::shared_ptr<BMEnv> bm_env);
341349
void bm_keygen_public(benchmark::State &state, std::shared_ptr<BMEnv> bm_env);
@@ -350,6 +358,9 @@ namespace sealbench
350358
void bm_bfv_decode_batch(benchmark::State &state, std::shared_ptr<BMEnv> bm_env);
351359
void bm_bfv_add_ct(benchmark::State &state, std::shared_ptr<BMEnv> bm_env);
352360
void bm_bfv_add_pt(benchmark::State &state, std::shared_ptr<BMEnv> bm_env);
361+
void bm_bfv_negate(benchmark::State &state, std::shared_ptr<BMEnv> bm_env);
362+
void bm_bfv_sub_ct(benchmark::State &state, std::shared_ptr<BMEnv> bm_env);
363+
void bm_bfv_sub_pt(benchmark::State &state, std::shared_ptr<BMEnv> bm_env);
353364
void bm_bfv_mul_ct(benchmark::State &state, std::shared_ptr<BMEnv> bm_env);
354365
void bm_bfv_mul_pt(benchmark::State &state, std::shared_ptr<BMEnv> bm_env);
355366
void bm_bfv_square(benchmark::State &state, std::shared_ptr<BMEnv> bm_env);
@@ -366,6 +377,9 @@ namespace sealbench
366377
void bm_ckks_decode_double(benchmark::State &state, std::shared_ptr<BMEnv> bm_env);
367378
void bm_ckks_add_ct(benchmark::State &state, std::shared_ptr<BMEnv> bm_env);
368379
void bm_ckks_add_pt(benchmark::State &state, std::shared_ptr<BMEnv> bm_env);
380+
void bm_ckks_negate(benchmark::State &state, std::shared_ptr<BMEnv> bm_env);
381+
void bm_ckks_sub_ct(benchmark::State &state, std::shared_ptr<BMEnv> bm_env);
382+
void bm_ckks_sub_pt(benchmark::State &state, std::shared_ptr<BMEnv> bm_env);
369383
void bm_ckks_mul_ct(benchmark::State &state, std::shared_ptr<BMEnv> bm_env);
370384
void bm_ckks_mul_pt(benchmark::State &state, std::shared_ptr<BMEnv> bm_env);
371385
void bm_ckks_square(benchmark::State &state, std::shared_ptr<BMEnv> bm_env);

native/bench/bfv.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,48 @@ namespace sealbench
115115
}
116116
}
117117

118+
void bm_bfv_negate(State &state, shared_ptr<BMEnv> bm_env)
119+
{
120+
vector<Ciphertext> &ct = bm_env->ct();
121+
for (auto _ : state)
122+
{
123+
state.PauseTiming();
124+
bm_env->randomize_ct_bfv(ct[0]);
125+
126+
state.ResumeTiming();
127+
bm_env->evaluator()->negate(ct[0], ct[2]);
128+
}
129+
}
130+
131+
void bm_bfv_sub_ct(State &state, shared_ptr<BMEnv> bm_env)
132+
{
133+
vector<Ciphertext> &ct = bm_env->ct();
134+
for (auto _ : state)
135+
{
136+
state.PauseTiming();
137+
bm_env->randomize_ct_bfv(ct[0]);
138+
bm_env->randomize_ct_bfv(ct[1]);
139+
140+
state.ResumeTiming();
141+
bm_env->evaluator()->sub(ct[0], ct[1], ct[2]);
142+
}
143+
}
144+
145+
void bm_bfv_sub_pt(State &state, shared_ptr<BMEnv> bm_env)
146+
{
147+
vector<Ciphertext> &ct = bm_env->ct();
148+
Plaintext &pt = bm_env->pt()[0];
149+
for (auto _ : state)
150+
{
151+
state.PauseTiming();
152+
bm_env->randomize_ct_bfv(ct[0]);
153+
bm_env->randomize_pt_bfv(pt);
154+
155+
state.ResumeTiming();
156+
bm_env->evaluator()->sub_plain(ct[0], pt, ct[2]);
157+
}
158+
}
159+
118160
void bm_bfv_mul_ct(State &state, shared_ptr<BMEnv> bm_env)
119161
{
120162
vector<Ciphertext> &ct = bm_env->ct();

native/bench/ckks.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,56 @@ namespace sealbench
123123
}
124124
}
125125

126+
void bm_ckks_negate(State &state, shared_ptr<BMEnv> bm_env)
127+
{
128+
vector<Ciphertext> &ct = bm_env->ct();
129+
double scale = bm_env->safe_scale();
130+
for (auto _ : state)
131+
{
132+
state.PauseTiming();
133+
bm_env->randomize_ct_ckks(ct[0]);
134+
ct[0].scale() = scale;
135+
136+
state.ResumeTiming();
137+
bm_env->evaluator()->negate(ct[0], ct[2]);
138+
}
139+
}
140+
141+
void bm_ckks_sub_ct(State &state, shared_ptr<BMEnv> bm_env)
142+
{
143+
vector<Ciphertext> &ct = bm_env->ct();
144+
double scale = bm_env->safe_scale();
145+
for (auto _ : state)
146+
{
147+
state.PauseTiming();
148+
bm_env->randomize_ct_ckks(ct[0]);
149+
ct[0].scale() = scale;
150+
bm_env->randomize_ct_ckks(ct[1]);
151+
ct[1].scale() = scale;
152+
153+
state.ResumeTiming();
154+
bm_env->evaluator()->sub(ct[0], ct[1], ct[2]);
155+
}
156+
}
157+
158+
void bm_ckks_sub_pt(State &state, shared_ptr<BMEnv> bm_env)
159+
{
160+
vector<Ciphertext> &ct = bm_env->ct();
161+
Plaintext &pt = bm_env->pt()[0];
162+
double scale = bm_env->safe_scale();
163+
for (auto _ : state)
164+
{
165+
state.PauseTiming();
166+
bm_env->randomize_ct_ckks(ct[0]);
167+
ct[0].scale() = scale;
168+
bm_env->randomize_pt_ckks(pt);
169+
pt.scale() = scale;
170+
171+
state.ResumeTiming();
172+
bm_env->evaluator()->sub_plain(ct[0], pt, ct[2]);
173+
}
174+
}
175+
126176
void bm_ckks_mul_ct(State &state, shared_ptr<BMEnv> bm_env)
127177
{
128178
vector<Ciphertext> &ct = bm_env->ct();

native/bench/ntt.cpp

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
3+
4+
#include "seal/seal.h"
5+
#include "seal/util/rlwe.h"
6+
#include "bench.h"
7+
8+
using namespace benchmark;
9+
using namespace sealbench;
10+
using namespace seal;
11+
using namespace std;
12+
13+
/**
14+
This file defines benchmarks for NTT-related HE primitives.
15+
*/
16+
17+
namespace sealbench
18+
{
19+
void bm_forward_ntt(State &state, shared_ptr<BMEnv> bm_env)
20+
{
21+
vector<Ciphertext> &ct = bm_env->ct();
22+
for (auto _ : state)
23+
{
24+
state.PauseTiming();
25+
bm_env->randomize_ct_bfv(ct[0]);
26+
27+
state.ResumeTiming();
28+
bm_env->evaluator()->transform_to_ntt(ct[0], ct[2]);
29+
}
30+
}
31+
32+
void bm_inverse_ntt(State &state, shared_ptr<BMEnv> bm_env)
33+
{
34+
vector<Ciphertext> &ct = bm_env->ct();
35+
for (auto _ : state)
36+
{
37+
state.PauseTiming();
38+
bm_env->randomize_ct_bfv(ct[0]);
39+
bm_env->evaluator()->transform_to_ntt_inplace(ct[0]);
40+
41+
state.ResumeTiming();
42+
bm_env->evaluator()->transform_from_ntt(ct[0], ct[2]);
43+
}
44+
}
45+
46+
void bm_forward_ntt_low_level(State &state, shared_ptr<BMEnv> bm_env)
47+
{
48+
parms_id_type parms_id = bm_env->context().first_parms_id();
49+
auto context_data = bm_env->context().get_context_data(parms_id);
50+
const auto &small_ntt_tables = context_data->small_ntt_tables();
51+
vector<Ciphertext> &ct = bm_env->ct();
52+
for (auto _ : state)
53+
{
54+
state.PauseTiming();
55+
bm_env->randomize_ct_bfv(ct[0]);
56+
57+
state.ResumeTiming();
58+
ntt_negacyclic_harvey(ct[0].data(), small_ntt_tables[0]);
59+
}
60+
}
61+
62+
void bm_inverse_ntt_low_level(State &state, shared_ptr<BMEnv> bm_env)
63+
{
64+
parms_id_type parms_id = bm_env->context().first_parms_id();
65+
auto context_data = bm_env->context().get_context_data(parms_id);
66+
const auto &small_ntt_tables = context_data->small_ntt_tables();
67+
vector<Ciphertext> &ct = bm_env->ct();
68+
for (auto _ : state)
69+
{
70+
state.PauseTiming();
71+
bm_env->randomize_ct_bfv(ct[0]);
72+
73+
state.ResumeTiming();
74+
inverse_ntt_negacyclic_harvey(ct[0].data(), small_ntt_tables[0]);
75+
}
76+
}
77+
78+
void bm_forward_ntt_low_level_lazy(State &state, shared_ptr<BMEnv> bm_env)
79+
{
80+
parms_id_type parms_id = bm_env->context().first_parms_id();
81+
auto context_data = bm_env->context().get_context_data(parms_id);
82+
const auto &small_ntt_tables = context_data->small_ntt_tables();
83+
vector<Ciphertext> &ct = bm_env->ct();
84+
for (auto _ : state)
85+
{
86+
state.PauseTiming();
87+
bm_env->randomize_ct_bfv(ct[0]);
88+
89+
state.ResumeTiming();
90+
ntt_negacyclic_harvey_lazy(ct[0].data(), small_ntt_tables[0]);
91+
}
92+
}
93+
94+
void bm_inverse_ntt_low_level_lazy(State &state, shared_ptr<BMEnv> bm_env)
95+
{
96+
parms_id_type parms_id = bm_env->context().first_parms_id();
97+
auto context_data = bm_env->context().get_context_data(parms_id);
98+
const auto &small_ntt_tables = context_data->small_ntt_tables();
99+
vector<Ciphertext> &ct = bm_env->ct();
100+
for (auto _ : state)
101+
{
102+
state.PauseTiming();
103+
bm_env->randomize_ct_bfv(ct[0]);
104+
105+
state.ResumeTiming();
106+
inverse_ntt_negacyclic_harvey_lazy(ct[0].data(), small_ntt_tables[0]);
107+
}
108+
}
109+
} // namespace sealbench

0 commit comments

Comments
 (0)