25 TutorialEltwiseAddBenchmarkDescription::TutorialEltwiseAddBenchmarkDescription()
45 m_descriptor.cat_params.offline.data_count[0] = 2;
46 m_descriptor.cat_params.offline.data_count[1] = 5;
50 m_descriptor.cipher_param_mask = 1 << 1;
59 m_descriptor.security = TUTORIAL_HE_SECURITY_128;
63 m_descriptor.other = 0;
72 default_workload_params.
n() = 400;
73 this->addDefaultParameters(default_workload_params);
78 TutorialEltwiseAddBenchmarkDescription::~TutorialEltwiseAddBenchmarkDescription()
87 ss << BenchmarkDescription::getBenchmarkDescription(p_w_params);
88 ss <<
", Encryption parameters" << std::endl
89 <<
", , HE Library, PALISADE v1.11.3" << std::endl
90 <<
", , Poly modulus degree, " << PolyModulusDegree << std::endl
91 <<
", , Coefficient Moduli, 60, 60";
96 <<
", Workload parameters" << std::endl
97 <<
", , n, " << w_params.n();
111 TutorialEngine &ex_engine =
dynamic_cast<TutorialEngine &
>(engine);
112 return new TutorialEltwiseAddBenchmark(ex_engine, m_descriptor, *p_params);
129 TutorialEltwiseAddBenchmark::Workload::PalisadeBFVContext::PalisadeBFVContext(
int poly_modulus_degree)
131 assert(TutorialEltwiseAddBenchmarkDescription::NumCoefficientModuli == 1);
132 std::size_t plaintext_modulus = 65537;
133 lbcrypto::SecurityLevel sec_level = lbcrypto::HEStd_128_classic;
135 std::size_t num_coeff_moduli = 2;
136 std::size_t max_depth = 2;
137 std::size_t coeff_moduli_bits = 40;
139 lbcrypto::CryptoContext<lbcrypto::DCRTPoly> crypto_context =
140 lbcrypto::CryptoContextFactory<lbcrypto::DCRTPoly>::genCryptoContextBFVrns(
141 plaintext_modulus, sec_level, sigma, 0, num_coeff_moduli,
142 0, OPTIMIZED, max_depth, 0, coeff_moduli_bits, poly_modulus_degree);
143 crypto_context->Enable(ENCRYPTION);
144 crypto_context->Enable(SHE);
146 lbcrypto::LPKeyPair<lbcrypto::DCRTPoly> local_key = crypto_context->KeyGen();
147 lbcrypto::LPKeyPair<lbcrypto::DCRTPoly> *p_key =
new lbcrypto::LPKeyPair<lbcrypto::DCRTPoly>(local_key.publicKey, local_key.secretKey);
148 local_key = lbcrypto::LPKeyPair<lbcrypto::DCRTPoly>();
149 m_keys = std::unique_ptr<lbcrypto::LPKeyPair<lbcrypto::DCRTPoly>>(p_key);
151 m_p_palisade_context = std::make_shared<lbcrypto::CryptoContext<lbcrypto::DCRTPoly>>(crypto_context);
152 m_slot_count = poly_modulus_degree;
157 if (vector_size <= 0)
158 throw std::invalid_argument(
"vector_size");
159 m_vector_size = vector_size;
160 m_p_context = std::make_shared<PalisadeBFVContext>(TutorialEltwiseAddBenchmarkDescription::PolyModulusDegree);
163 std::vector<lbcrypto::Plaintext> TutorialEltwiseAddBenchmark::Workload::encodeVector(
const std::vector<std::vector<std::int64_t>> &vec)
165 std::vector<lbcrypto::Plaintext> retval(vec.size());
167 for (std::size_t i = 0; i < vec.size(); ++i)
169 assert(vec[i].size() <= context().getSlotCount());
170 retval[i] = context().context()->MakePackedPlaintext(vec[i]);
175 std::vector<lbcrypto::Ciphertext<lbcrypto::DCRTPoly>> TutorialEltwiseAddBenchmark::Workload::encryptVector(
const std::vector<lbcrypto::Plaintext> &encoded_vec)
177 std::vector<lbcrypto::Ciphertext<lbcrypto::DCRTPoly>> retval(encoded_vec.size());
178 for (std::size_t i = 0; i < encoded_vec.size(); i++)
179 retval[i] = context().context()->Encrypt(context().publicKey(), encoded_vec[i]);
183 std::vector<lbcrypto::Ciphertext<lbcrypto::DCRTPoly>> TutorialEltwiseAddBenchmark::Workload::eltwiseadd(
const std::vector<lbcrypto::Plaintext> &A,
184 const std::vector<lbcrypto::Ciphertext<lbcrypto::DCRTPoly>> &B)
186 std::vector<lbcrypto::Ciphertext<lbcrypto::DCRTPoly>> retval(A.size() * B.size());
193 for (std::size_t A_i = 0; A_i < A.size(); ++A_i)
194 for (std::size_t B_i = 0; B_i < B.size(); ++B_i)
196 lbcrypto::Ciphertext<lbcrypto::DCRTPoly> &retval_item = retval[A_i * B.size() + B_i];
197 retval_item = context().context()->EvalAdd(B[B_i], A[A_i]);
203 std::vector<lbcrypto::Plaintext> TutorialEltwiseAddBenchmark::Workload::decryptResult(
const std::vector<lbcrypto::Ciphertext<lbcrypto::DCRTPoly>> &encrypted_result)
205 std::vector<lbcrypto::Plaintext> retval(encrypted_result.size());
206 for (std::size_t i = 0; i < encrypted_result.size(); i++)
207 retval[i] = context().decrypt(encrypted_result[i]);
211 std::vector<std::vector<int64_t>> TutorialEltwiseAddBenchmark::Workload::decodeResult(
const std::vector<lbcrypto::Plaintext> &encoded_result)
213 std::vector<std::vector<int64_t>> retval(encoded_result.size());
214 for (std::size_t i = 0; i < encoded_result.size(); ++i)
216 retval[i] = encoded_result[i]->GetPackedValue();
217 retval[i].resize(m_vector_size);
231 hebench::cpp::BaseBenchmark(engine, bench_desc, bench_params)
236 if (bench_params.
count < TutorialEltwiseAddBenchmarkDescription::NumWorkloadParams)
238 +
std::to_string(TutorialEltwiseAddBenchmarkDescription::NumWorkloadParams)
244 if (w_params.n() <= 0
245 || w_params.n() - 1 > TutorialEltwiseAddBenchmarkDescription::PolyModulusDegree / 2)
247 +
std::to_string(TutorialEltwiseAddBenchmarkDescription::PolyModulusDegree / 2)),
253 m_p_workload = std::make_shared<Workload>(w_params.n());
258 TutorialEltwiseAddBenchmark::~TutorialEltwiseAddBenchmark()
260 m_p_workload.reset();
276 std::stringstream ss;
277 ss <<
"Unexpected number of input samples for operation parameter " << param_pack.
param_position
278 <<
". Expected " << this->getDescriptor().cat_params.offline.data_count[param_pack.
param_position]
285 std::vector<std::vector<std::int64_t>> clear_param(param_pack.
buffer_count);
287 for (std::size_t sample_i = 0; sample_i < clear_param.size(); ++sample_i)
290 const std::int64_t *start_pt =
reinterpret_cast<const std::int64_t *
>(native_sample.
p);
291 const std::int64_t *end_pt = start_pt + native_sample.
size /
sizeof(std::int64_t);
292 clear_param[sample_i] = std::vector<std::int64_t>(start_pt, end_pt);
297 std::vector<lbcrypto::Plaintext> encoded = m_p_workload->encodeVector(clear_param);
300 InternalParam<lbcrypto::Plaintext> retval;
301 retval.samples = std::move(encoded);
303 retval.tag = InternalParamInfo::tagPlaintext;
305 return this->getEngine().template createHandle<decltype(retval)>(
306 sizeof(lbcrypto::Plaintext) * retval.samples.size(),
320 const InternalParam<lbcrypto::Plaintext> &encoded_data =
321 this->getEngine().retrieveFromHandle<InternalParam<lbcrypto::Plaintext>>(h_encoded_data, InternalParamInfo::tagPlaintext);
325 std::vector<std::vector<std::int64_t>> clear_result = m_p_workload->decodeResult(encoded_data.samples);
331 std::uint64_t min_sample_count = std::min(native_datapack.
buffer_count, clear_result.size());
332 for (std::uint64_t sample_i = 0; sample_i < min_sample_count; ++sample_i)
337 const std::vector<std::int64_t> &decoded = clear_result[sample_i];
338 std::uint64_t min_size = std::min(decoded.size(), native_sample.
size /
sizeof(std::int64_t));
339 std::copy_n(decoded.begin(), min_size,
340 reinterpret_cast<std::int64_t *
>(native_sample.
p));
350 const InternalParam<lbcrypto::Plaintext> &encoded_parameter =
351 this->getEngine().template retrieveFromHandle<InternalParam<lbcrypto::Plaintext>>(h_encoded_parameters,
352 InternalParamInfo::tagPlaintext);
356 std::vector<lbcrypto::Ciphertext<lbcrypto::DCRTPoly>> encrypted = m_p_workload->encryptVector(encoded_parameter.samples);
360 InternalParam<lbcrypto::Ciphertext<lbcrypto::DCRTPoly>> retval;
361 retval.samples = std::move(encrypted);
362 retval.param_position = encoded_parameter.param_position;
363 retval.tag = InternalParamInfo::tagCiphertext;
365 return this->getEngine().template createHandle<decltype(retval)>(
366 sizeof(lbcrypto::Ciphertext<lbcrypto::DCRTPoly>) * retval.samples.size(),
377 const InternalParam<lbcrypto::Ciphertext<lbcrypto::DCRTPoly>> &encrypted_data =
378 this->getEngine().retrieveFromHandle<InternalParam<lbcrypto::Ciphertext<lbcrypto::DCRTPoly>>>(h_encrypted_data, InternalParamInfo::tagCiphertext);
381 assert(encrypted_data.param_position == 0);
384 std::vector<lbcrypto::Plaintext> encoded_data_samples = m_p_workload->decryptResult(encrypted_data.samples);
388 InternalParam<lbcrypto::Plaintext> retval;
389 retval.samples = std::move(encoded_data_samples);
390 retval.param_position = encrypted_data.param_position;
391 retval.tag = InternalParamInfo::tagPlaintext;
393 return this->getEngine().template createHandle<decltype(retval)>(
394 sizeof(lbcrypto::Plaintext) * retval.samples.size(),
404 assert(count == TutorialEltwiseAddBenchmarkDescription::ParametersCount);
407 std::pair<std::vector<lbcrypto::Plaintext>, std::vector<lbcrypto::Ciphertext<lbcrypto::DCRTPoly>>> params;
412 for (std::size_t handle_i = 0; handle_i < count; ++handle_i)
414 const InternalParamInfo ¶m_info =
415 this->getEngine().retrieveFromHandle<InternalParamInfo>(p_local_data[handle_i]);
416 assert(param_info.param_position < TutorialEltwiseAddBenchmarkDescription::ParametersCount);
418 switch (param_info.param_position)
422 if (!params.first.empty())
425 const InternalParam<lbcrypto::Plaintext> &internal_param =
426 this->getEngine().retrieveFromHandle<InternalParam<lbcrypto::Plaintext>>(p_local_data[handle_i],
427 InternalParamInfo::tagPlaintext);
429 params.first = internal_param.samples;
434 if (!params.second.empty())
438 const InternalParam<lbcrypto::Ciphertext<lbcrypto::DCRTPoly>> &internal_param =
439 this->getEngine().retrieveFromHandle<InternalParam<lbcrypto::Ciphertext<lbcrypto::DCRTPoly>>>(p_local_data[handle_i],
440 InternalParamInfo::tagCiphertext);
442 params.second = internal_param.samples;
450 return this->getEngine().template createHandle<decltype(params)>(
452 InternalParamInfo::tagPlaintext | InternalParamInfo::tagCiphertext,
469 p_local_data[0] = this->getEngine().duplicateHandle(h_remote_data,
470 InternalParamInfo::tagCiphertext);
481 const std::pair<std::vector<lbcrypto::Plaintext>, std::vector<lbcrypto::Ciphertext<lbcrypto::DCRTPoly>>> ¶ms =
482 this->getEngine().retrieveFromHandle<std::pair<std::vector<lbcrypto::Plaintext>, std::vector<lbcrypto::Ciphertext<lbcrypto::DCRTPoly>>>>(
483 h_remote_packed, InternalParamInfo::tagCiphertext | InternalParamInfo::tagPlaintext);
486 const std::vector<lbcrypto::Plaintext> &A = params.first;
487 const std::vector<lbcrypto::Ciphertext<lbcrypto::DCRTPoly>> &B = params.second;
491 std::array<std::size_t, TutorialEltwiseAddBenchmarkDescription::ParametersCount> param_size;
492 param_size[0] = A.size();
493 param_size[1] = B.size();
494 std::uint64_t results_count = 1;
495 for (std::size_t param_i = 0; param_i < TutorialEltwiseAddBenchmarkDescription::ParametersCount; ++param_i)
497 if (p_param_indexers[param_i].value_index >= param_size[param_i])
499 std::stringstream ss;
500 ss <<
"Invalid parameter indexer for operation parameter " << param_i <<
". Expected index in range [0, "
501 << param_size[param_i] <<
"), but " << p_param_indexers[param_i].
value_index <<
" received.";
505 else if (p_param_indexers[param_i].value_index + p_param_indexers[param_i].batch_size > param_size[param_i])
507 std::stringstream ss;
508 ss <<
"Invalid parameter indexer for operation parameter " << param_i <<
". Expected batch size in range [1, "
509 << param_size[param_i] - p_param_indexers[param_i].
value_index <<
"], but " << p_param_indexers[param_i].
batch_size <<
" received.";
513 results_count *= p_param_indexers[param_i].
batch_size;
518 std::vector<lbcrypto::Ciphertext<lbcrypto::DCRTPoly>> result = m_p_workload->eltwiseadd(A, B);
519 assert(result.size() == results_count);
524 InternalParam<lbcrypto::Ciphertext<lbcrypto::DCRTPoly>> retval;
525 retval.samples = std::move(result);
526 retval.param_position = 0;
527 retval.tag = InternalParamInfo::tagCiphertext;
531 return this->getEngine().template createHandle<decltype(retval)>(
532 sizeof(lbcrypto::Ciphertext<lbcrypto::DCRTPoly>) * retval.samples.size(),
Top level opaque benchmark class.
Base class that encapsulates common behavior of backend engines.
std::uint64_t n() const
Number elements in a vector.
#define HEBERROR_MSG_CLASS(message)
@ Int64
64 bits signed integers.
ErrorCode encrypt(Handle h_benchmark, Handle h_plaintext, Handle *h_ciphertext)
Encrypts a plain text into a cipher text.
ErrorCode operate(Handle h_benchmark, Handle h_remote_packed_params, const ParameterIndexer *p_param_indexers, uint64_t indexers_count, Handle *h_remote_output)
Performs the workload operation of the benchmark.
std::uint64_t count
Number of workload parameters.
DataPack * p_data_packs
Collection of data packs.
ErrorCode encode(Handle h_benchmark, const DataPackCollection *p_parameters, Handle *h_plaintext)
Given a pack of parameters in raw, native data format, encodes them into plain text suitable for back...
std::uint64_t value_index
Index of parameter value inside the data pack.
ErrorCode decrypt(Handle h_benchmark, Handle h_ciphertext, Handle *h_plaintext)
Decrypts a cipher text into corresponding plain text.
ErrorCode store(Handle h_benchmark, Handle h_remote, Handle *h_local_packed_params, std::uint64_t local_count)
Retrieves the specified data from the backend.
std::uint64_t pack_count
Number of data packs in the collection.
ErrorCode createBenchmark(Handle h_engine, Handle h_bench_desc, const WorkloadParams *p_params, Handle *h_benchmark)
Instantiates a benchmark on the backend.
std::uint64_t batch_size
Number of values to use, starting from index.
std::uint64_t size
Size of underlying data.
std::uint64_t param_position
The 0-based position of this parameter in the corresponding function call.
void * p
Pointer to underlying data.
ErrorCode load(Handle h_benchmark, const Handle *h_local_packed_params, std::uint64_t local_count, Handle *h_remote)
Loads the specified data from the local host into the remote backend to use as parameter during a cal...
std::uint64_t buffer_count
Number of data buffers in p_buffers.
ErrorCode decode(Handle h_benchmark, Handle h_plaintext, DataPackCollection *p_native)
Decodes plaintext data into the appropriate raw, native format.
NativeDataBuffer * p_buffers
Array of data buffers for parameter.
Workload
Defines all possible workloads.
Defines a benchmark test.
Defines a data package for an operation.
Defines a collection of data packs.
Specifies the parameters for a workload.
Structure to contain flexible data.
std::string to_string(const std::string_view &s)
#define HEBENCH_ECODE_INVALID_ARGS
Indicates invalid arguments to function call.
#define HEBENCH_ECODE_CRITICAL_ERROR
Specifies a critical error.
#define HEBENCH_HE_SCHEME_BFV