Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Faster benchmarks #386

Closed
wants to merge 1 commit into from
Closed
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
11 changes: 5 additions & 6 deletions native/bench/bench.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ namespace sealbench
(string("n=") + to_string(n) + string(" / log(q)=") + to_string(log_q) + string(" / " #category " / " #name)) \
.c_str(), \
[=](State &st) { func(st, __VA_ARGS__); }) \
->Unit(benchmark::kMicrosecond) \
->Iterations(10);
->Unit(benchmark::kMicrosecond)

void register_bm_family(
const pair<size_t, vector<Modulus>> &parms, unordered_map<EncryptionParameters, shared_ptr<BMEnv>> &bm_env_map)
Expand Down Expand Up @@ -68,11 +67,11 @@ namespace sealbench
SEAL_BENCHMARK_REGISTER(BFV, n, log_q, EvaluateSquare, bm_bfv_square, bm_env_bfv);
if (bm_env_bfv->context().first_context_data()->parms().coeff_modulus().size() > 1)
{
SEAL_BENCHMARK_REGISTER(BFV, n, log_q, EvaluateModSwitchInplace, bm_bfv_modswitch_inplace, bm_env_bfv);
SEAL_BENCHMARK_REGISTER(BFV, n, log_q, EvaluateModSwitch, bm_bfv_modswitch, bm_env_bfv);
}
if (bm_env_bfv->context().using_keyswitching())
{
SEAL_BENCHMARK_REGISTER(BFV, n, log_q, EvaluateRelinInplace, bm_bfv_relin_inplace, bm_env_bfv);
SEAL_BENCHMARK_REGISTER(BFV, n, log_q, EvaluateRelin, bm_bfv_relin, bm_env_bfv);
SEAL_BENCHMARK_REGISTER(BFV, n, log_q, EvaluateRotateRows, bm_bfv_rotate_rows, bm_env_bfv);
SEAL_BENCHMARK_REGISTER(BFV, n, log_q, EvaluateRotateCols, bm_bfv_rotate_cols, bm_env_bfv);
}
Expand All @@ -89,11 +88,11 @@ namespace sealbench
SEAL_BENCHMARK_REGISTER(CKKS, n, log_q, EvaluateSquare, bm_ckks_square, bm_env_ckks);
if (bm_env_bfv->context().first_context_data()->parms().coeff_modulus().size() > 1)
{
SEAL_BENCHMARK_REGISTER(CKKS, n, log_q, EvaluateRescaleInplace, bm_ckks_rescale_inplace, bm_env_ckks);
SEAL_BENCHMARK_REGISTER(CKKS, n, log_q, EvaluateRescale, bm_ckks_rescale, bm_env_ckks);
}
if (bm_env_bfv->context().using_keyswitching())
{
SEAL_BENCHMARK_REGISTER(CKKS, n, log_q, EvaluateRelinInplace, bm_ckks_relin_inplace, bm_env_ckks);
SEAL_BENCHMARK_REGISTER(CKKS, n, log_q, EvaluateRelin, bm_ckks_relin, bm_env_ckks);
SEAL_BENCHMARK_REGISTER(CKKS, n, log_q, EvaluateRotate, bm_ckks_rotate, bm_env_ckks);
}
}
Expand Down
8 changes: 4 additions & 4 deletions native/bench/bench.h
Original file line number Diff line number Diff line change
Expand Up @@ -353,8 +353,8 @@ namespace sealbench
void bm_bfv_mul_ct(benchmark::State &state, std::shared_ptr<BMEnv> bm_env);
void bm_bfv_mul_pt(benchmark::State &state, std::shared_ptr<BMEnv> bm_env);
void bm_bfv_square(benchmark::State &state, std::shared_ptr<BMEnv> bm_env);
void bm_bfv_modswitch_inplace(benchmark::State &state, std::shared_ptr<BMEnv> bm_env);
void bm_bfv_relin_inplace(benchmark::State &state, std::shared_ptr<BMEnv> bm_env);
void bm_bfv_modswitch(benchmark::State &state, std::shared_ptr<BMEnv> bm_env);
void bm_bfv_relin(benchmark::State &state, std::shared_ptr<BMEnv> bm_env);
void bm_bfv_rotate_rows(benchmark::State &state, std::shared_ptr<BMEnv> bm_env);
void bm_bfv_rotate_cols(benchmark::State &state, std::shared_ptr<BMEnv> bm_env);

Expand All @@ -369,7 +369,7 @@ namespace sealbench
void bm_ckks_mul_ct(benchmark::State &state, std::shared_ptr<BMEnv> bm_env);
void bm_ckks_mul_pt(benchmark::State &state, std::shared_ptr<BMEnv> bm_env);
void bm_ckks_square(benchmark::State &state, std::shared_ptr<BMEnv> bm_env);
void bm_ckks_rescale_inplace(benchmark::State &state, std::shared_ptr<BMEnv> bm_env);
void bm_ckks_relin_inplace(benchmark::State &state, std::shared_ptr<BMEnv> bm_env);
void bm_ckks_rescale(benchmark::State &state, std::shared_ptr<BMEnv> bm_env);
void bm_ckks_relin(benchmark::State &state, std::shared_ptr<BMEnv> bm_env);
void bm_ckks_rotate(benchmark::State &state, std::shared_ptr<BMEnv> bm_env);
} // namespace sealbench
129 changes: 44 additions & 85 deletions native/bench/bfv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,195 +18,154 @@ namespace sealbench
{
void bm_bfv_encrypt_secret(State &state, shared_ptr<BMEnv> bm_env)
{
vector<Ciphertext> &ct = bm_env->ct();
Plaintext &pt = bm_env->pt()[0];
vector<Ciphertext> ct = bm_env->ct();
Plaintext pt = bm_env->pt()[0];
bm_env->randomize_pt_bfv(pt);
for (auto _ : state)
{
state.PauseTiming();
bm_env->randomize_pt_bfv(pt);

state.ResumeTiming();
bm_env->encryptor()->encrypt_symmetric(pt, ct[2]);
}
}

void bm_bfv_encrypt_public(State &state, shared_ptr<BMEnv> bm_env)
{
vector<Ciphertext> &ct = bm_env->ct();
Plaintext &pt = bm_env->pt()[0];
vector<Ciphertext> ct = bm_env->ct();
Plaintext pt = bm_env->pt()[0];
bm_env->randomize_pt_bfv(pt);
for (auto _ : state)
{
state.PauseTiming();
bm_env->randomize_pt_bfv(pt);

state.ResumeTiming();
bm_env->encryptor()->encrypt(pt, ct[2]);
}
}

void bm_bfv_decrypt(State &state, shared_ptr<BMEnv> bm_env)
{
vector<Ciphertext> &ct = bm_env->ct();
Plaintext &pt = bm_env->pt()[0];
vector<Ciphertext> ct = bm_env->ct();
Plaintext pt = bm_env->pt()[0];
bm_env->randomize_ct_bfv(ct[0]);
for (auto _ : state)
{
state.PauseTiming();
bm_env->randomize_ct_bfv(ct[0]);

state.ResumeTiming();
bm_env->decryptor()->decrypt(ct[0], pt);
}
}

void bm_bfv_encode_batch(State &state, shared_ptr<BMEnv> bm_env)
{
vector<uint64_t> &msg = bm_env->msg_uint64();
Plaintext &pt = bm_env->pt()[0];
Plaintext pt = bm_env->pt()[0];
bm_env->randomize_message_uint64(msg);
for (auto _ : state)
{
state.PauseTiming();
bm_env->randomize_message_uint64(msg);

state.ResumeTiming();
bm_env->batch_encoder()->encode(msg, pt);
}
}

void bm_bfv_decode_batch(State &state, shared_ptr<BMEnv> bm_env)
{
vector<uint64_t> &msg = bm_env->msg_uint64();
Plaintext &pt = bm_env->pt()[0];
Plaintext pt = bm_env->pt()[0];
bm_env->randomize_pt_bfv(pt);
for (auto _ : state)
{
state.PauseTiming();
bm_env->randomize_pt_bfv(pt);

state.ResumeTiming();
bm_env->batch_encoder()->decode(pt, msg);
}
}

void bm_bfv_add_ct(State &state, shared_ptr<BMEnv> bm_env)
{
vector<Ciphertext> &ct = bm_env->ct();
vector<Ciphertext> ct = bm_env->ct();
bm_env->randomize_ct_bfv(ct[0]);
bm_env->randomize_ct_bfv(ct[1]);
Ciphertext res;
for (auto _ : state)
{
state.PauseTiming();
bm_env->randomize_ct_bfv(ct[0]);
bm_env->randomize_ct_bfv(ct[1]);
state.ResumeTiming();
Ciphertext res;
bm_env->evaluator()->add(ct[0], ct[1], res);
}
}

void bm_bfv_add_pt(State &state, shared_ptr<BMEnv> bm_env)
{
vector<Ciphertext> &ct = bm_env->ct();
Plaintext &pt = bm_env->pt()[0];
vector<Ciphertext> ct = bm_env->ct();
Plaintext pt = bm_env->pt()[0];
bm_env->randomize_ct_bfv(ct[0]);
bm_env->randomize_pt_bfv(pt);
for (auto _ : state)
{
state.PauseTiming();
bm_env->randomize_ct_bfv(ct[0]);
bm_env->randomize_pt_bfv(pt);

state.ResumeTiming();
bm_env->evaluator()->add_plain(ct[0], pt, ct[2]);
}
}

void bm_bfv_mul_ct(State &state, shared_ptr<BMEnv> bm_env)
{
vector<Ciphertext> &ct = bm_env->ct();
vector<Ciphertext> ct = bm_env->ct();
bm_env->randomize_ct_bfv(ct[0]);
bm_env->randomize_ct_bfv(ct[1]);
for (auto _ : state)
{
state.PauseTiming();
bm_env->randomize_ct_bfv(ct[0]);
bm_env->randomize_ct_bfv(ct[1]);

state.ResumeTiming();
bm_env->evaluator()->multiply(ct[0], ct[1], ct[2]);
}
}

void bm_bfv_mul_pt(State &state, shared_ptr<BMEnv> bm_env)
{
vector<Ciphertext> &ct = bm_env->ct();
Plaintext &pt = bm_env->pt()[0];
vector<Ciphertext> ct = bm_env->ct();
Plaintext pt = bm_env->pt()[0];
bm_env->randomize_ct_bfv(ct[0]);
bm_env->randomize_pt_bfv(pt);
for (auto _ : state)
{
state.PauseTiming();
bm_env->randomize_ct_bfv(ct[0]);
bm_env->randomize_pt_bfv(pt);

state.ResumeTiming();
bm_env->evaluator()->multiply_plain(ct[0], pt, ct[2]);
}
}

void bm_bfv_square(State &state, shared_ptr<BMEnv> bm_env)
{
vector<Ciphertext> &ct = bm_env->ct();
vector<Ciphertext> ct = bm_env->ct();
bm_env->randomize_ct_bfv(ct[0]);
bm_env->randomize_ct_bfv(ct[1]);
for (auto _ : state)
{
state.PauseTiming();
bm_env->randomize_ct_bfv(ct[0]);
bm_env->randomize_ct_bfv(ct[1]);

state.ResumeTiming();
bm_env->evaluator()->square(ct[0], ct[2]);
}
}

void bm_bfv_modswitch_inplace(State &state, shared_ptr<BMEnv> bm_env)
void bm_bfv_modswitch(State &state, shared_ptr<BMEnv> bm_env)
{
vector<Ciphertext> &ct = bm_env->ct();
vector<Ciphertext> ct = bm_env->ct();
bm_env->randomize_ct_bfv(ct[0]);
for (auto _ : state)
{
state.PauseTiming();
bm_env->randomize_ct_bfv(ct[0]);

state.ResumeTiming();
bm_env->evaluator()->mod_switch_to_next_inplace(ct[0]);
bm_env->evaluator()->mod_switch_to_next(ct[0], ct[1]);
}
}

void bm_bfv_relin_inplace(State &state, shared_ptr<BMEnv> bm_env)
void bm_bfv_relin(State &state, shared_ptr<BMEnv> bm_env)
{
Ciphertext ct;
vector<Ciphertext> ct = bm_env->ct();
bm_env->randomize_ct_bfv(ct[0]);
ct[0].resize(bm_env->context(), size_t(3));
for (auto _ : state)
{
state.PauseTiming();
ct.resize(bm_env->context(), size_t(3));
bm_env->randomize_ct_bfv(ct);

state.ResumeTiming();
bm_env->evaluator()->relinearize_inplace(ct, bm_env->rlk());
bm_env->evaluator()->relinearize(ct[0], bm_env->rlk(), ct[1]);
}
}

void bm_bfv_rotate_rows(State &state, shared_ptr<BMEnv> bm_env)
{
vector<Ciphertext> &ct = bm_env->ct();
vector<Ciphertext> ct = bm_env->ct();
bm_env->randomize_ct_bfv(ct[0]);
for (auto _ : state)
{
state.PauseTiming();
bm_env->randomize_ct_bfv(ct[0]);

state.ResumeTiming();
bm_env->evaluator()->rotate_rows(ct[0], 1, bm_env->glk(), ct[2]);
}
}

void bm_bfv_rotate_cols(State &state, shared_ptr<BMEnv> bm_env)
{
vector<Ciphertext> &ct = bm_env->ct();
vector<Ciphertext> ct = bm_env->ct();
bm_env->randomize_ct_bfv(ct[0]);
for (auto _ : state)
{
state.PauseTiming();
bm_env->randomize_ct_bfv(ct[0]);

state.ResumeTiming();
bm_env->evaluator()->rotate_columns(ct[0], bm_env->glk(), ct[2]);
}
}
Expand Down
Loading