Skip to content

Commit 42e7c01

Browse files
committed
Added avoid duplicate emplace logic
1 parent a4b2812 commit 42e7c01

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

include/cloudkey.hpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <cereal/types/unordered_map.hpp>
88
#include <cereal/types/vector.hpp>
99
#include <tuple>
10+
#include <iostream>
1011

1112
#include "evalkeygens.hpp"
1213

@@ -101,44 +102,72 @@ struct EvalKey {
101102
template <class P>
102103
void emplacebk(const SecretKey& sk)
103104
{
105+
if (get<BootstrappingKey<P>>() != nullptr) {
106+
std::cerr << "Warning: BootstrappingKey<P> already exists. Skipping duplicate key generation." << std::endl;
107+
return;
108+
}
104109
get<BootstrappingKey<P>>() = std::make_unique_for_overwrite<BootstrappingKey<P>>();
105110
bkgen<P>(*get<BootstrappingKey<P>>(), sk);
106111
}
107112
template <class P>
108113
void emplacebkfft(const SecretKey& sk)
109114
{
115+
if (get<BootstrappingKeyFFT<P>>() != nullptr) {
116+
std::cerr << "Warning: BootstrappingKeyFFT<P> already exists. Skipping duplicate key generation." << std::endl;
117+
return;
118+
}
110119
get<BootstrappingKeyFFT<P>>() = std::make_unique_for_overwrite<BootstrappingKeyFFT<P>>();
111120
bkfftgen<P>(*get<BootstrappingKeyFFT<P>>(), sk);
112121
}
113122
template <class P>
114123
void emplacebkntt(const SecretKey& sk)
115124
{
125+
if (get<BootstrappingKeyNTT<P>>() != nullptr) {
126+
std::cerr << "Warning: BootstrappingKeyNTT<P> already exists. Skipping duplicate key generation." << std::endl;
127+
return;
128+
}
116129
get<BootstrappingKeyNTT<P>>() = std::make_unique_for_overwrite<BootstrappingKeyNTT<P>>();
117130
bknttgen<P>(*get<BootstrappingKeyNTT<P>>(), sk);
118131
}
119132
template <class P>
120133
void emplacebk2bkfft()
121134
{
135+
if (get<BootstrappingKeyFFT<P>>() != nullptr) {
136+
std::cerr << "Warning: BootstrappingKeyFFT<P> already exists. Skipping duplicate key generation." << std::endl;
137+
return;
138+
}
122139
get<BootstrappingKeyFFT<P>>() = std::make_unique_for_overwrite<BootstrappingKeyFFT<P>>();
123140
for (int i = 0; i < P::domainP::n; i++)
124141
(*get<BootstrappingKeyFFT<P>>())[i][0] = ApplyFFT2trgsw<typename P::targetP>((*get<BootstrappingKey<P>>())[i][0]);
125142
}
126143
template <class P>
127144
void emplacebk2bkntt()
128145
{
146+
if (get<BootstrappingKeyNTT<P>>() != nullptr) {
147+
std::cerr << "Warning: BootstrappingKeyNTT<P> already exists. Skipping duplicate key generation." << std::endl;
148+
return;
149+
}
129150
get<BootstrappingKeyNTT<P>>() = std::make_unique_for_overwrite<BootstrappingKeyNTT<P>>();
130151
for (int i = 0; i < P::domainP::n; i++)
131152
(*get<BootstrappingKeyNTT<P>>())[i] = ApplyNTT2trgsw<typename P::targetP>((*get<BootstrappingKey<P>>())[i][0]);
132153
}
133154
template <class P>
134155
void emplaceiksk(const SecretKey& sk)
135156
{
157+
if (get<KeySwitchingKey<P>>() != nullptr) {
158+
std::cerr << "Warning: KeySwitchingKey<P> already exists. Skipping duplicate key generation." << std::endl;
159+
return;
160+
}
136161
get<KeySwitchingKey<P>>() = std::make_unique_for_overwrite<KeySwitchingKey<P>>();
137162
ikskgen<P>(*get<KeySwitchingKey<P>>(), sk);
138163
}
139164
template <class P>
140165
void emplacesubiksk(const SecretKey& sk)
141166
{
167+
if (get<SubsetKeySwitchingKey<P>>() != nullptr) {
168+
std::cerr << "Warning: SubsetKeySwitchingKey<P> already exists. Skipping duplicate key generation." << std::endl;
169+
return;
170+
}
142171
get<SubsetKeySwitchingKey<P>>() = std::make_unique_for_overwrite<SubsetKeySwitchingKey<P>>();
143172
subikskgen<P>(*get<SubsetKeySwitchingKey<P>>(), sk);
144173
}
@@ -147,6 +176,10 @@ struct EvalKey {
147176
const Polynomial<typename P::targetP>& func,
148177
const SecretKey& sk)
149178
{
179+
if (get_map<PrivateKeySwitchingKey<P>>().find(key) != get_map<PrivateKeySwitchingKey<P>>().end()) {
180+
std::cerr << "Warning: PrivateKeySwitchingKey<P> with key '" << key << "' already exists. Skipping duplicate key generation." << std::endl;
181+
return;
182+
}
150183
get_map<PrivateKeySwitchingKey<P>>()[key] =
151184
std::unique_ptr<PrivateKeySwitchingKey<P>>(new (
152185
std::align_val_t(64)) PrivateKeySwitchingKey<P>());
@@ -157,6 +190,10 @@ struct EvalKey {
157190
const Polynomial<typename P::targetP>& func,
158191
const SecretKey& sk)
159192
{
193+
if (get_map<SubsetPrivateKeySwitchingKey<P>>().find(key) != get_map<SubsetPrivateKeySwitchingKey<P>>().end()) {
194+
std::cerr << "Warning: SubsetPrivateKeySwitchingKey<P> with key '" << key << "' already exists. Skipping duplicate key generation." << std::endl;
195+
return;
196+
}
160197
get_map<SubsetPrivateKeySwitchingKey<P>>()[key] =
161198
std::make_unique_for_overwrite<SubsetPrivateKeySwitchingKey<P>>();
162199
subprivkskgen<P>(*get_map<SubsetPrivateKeySwitchingKey<P>>()[key], func, sk);
@@ -192,13 +229,21 @@ struct EvalKey {
192229
template <class P>
193230
void emplaceahk(const SecretKey& sk)
194231
{
232+
if (get<AnnihilateKey<P>>() != nullptr) {
233+
std::cerr << "Warning: AnnihilateKey<P> already exists. Skipping duplicate key generation." << std::endl;
234+
return;
235+
}
195236
get<AnnihilateKey<P>>() = std::make_unique_for_overwrite<AnnihilateKey<P>>();
196237
annihilatekeygen<P>(*get<AnnihilateKey<P>>(), sk);
197238
}
198239

199240
template <class P>
200241
void emplacecbsk(const SecretKey& sk)
201242
{
243+
if (get<CBswitchingKey<P>>() != nullptr) {
244+
std::cerr << "Warning: CBswitchingKey<P> already exists. Skipping duplicate key generation." << std::endl;
245+
return;
246+
}
202247
get<CBswitchingKey<P>>() = std::make_unique_for_overwrite<CBswitchingKey<P>>();
203248
for (int i = 0; i < P::k; i++) {
204249
Polynomial<P> partkey;

thirdparties/AES

Submodule AES updated from 94acec5 to dcd9559

0 commit comments

Comments
 (0)