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