HEBench
tutorial_eltwiseadd_benchmark_palisade.h
Go to the documentation of this file.
1 
2 // Copyright (C) 2021 Intel Corporation
3 // SPDX-License-Identifier: Apache-2.0
4 
5 #pragma once
6 
8 
9 #include <array>
10 
12 #include "palisade.h"
13 
15 class TutorialEltwiseAddBenchmarkDescription : public hebench::cpp::BenchmarkDescription
17 {
18 public:
19  HEBERROR_DECLARE_CLASS_NAME(TutorialEltwiseAddBenchmarkDescription)
20 
21 public:
22  // This workload (EltwiseAdd) requires only 1 parameter
23  static constexpr std::uint64_t NumWorkloadParams = 1;
24 
25  // Operation specifics
26  static constexpr std::uint64_t ParametersCount = 2; // number of parameters for this operation
27  static constexpr std::uint64_t ResultComponentsCount = 1; // number of components of result for this operation
28 
29  // HE specific parameters
30  static constexpr std::size_t PolyModulusDegree = 8192;
31  static constexpr std::size_t NumCoefficientModuli = 1;
32 
33  TutorialEltwiseAddBenchmarkDescription();
34  ~TutorialEltwiseAddBenchmarkDescription() override;
35 
36  std::string getBenchmarkDescription(const hebench::APIBridge::WorkloadParams *p_w_params) const override;
38  const hebench::APIBridge::WorkloadParams *p_params) override;
39  void destroyBenchmark(hebench::cpp::BaseBenchmark *p_bench) override;
40 };
41 
43 class TutorialEltwiseAddBenchmark : public hebench::cpp::BaseBenchmark
45 {
46 public:
47  HEBERROR_DECLARE_CLASS_NAME(TutorialEltwiseAddBenchmark)
48 
49 public:
50  static constexpr std::int64_t tag = 0x1;
51 
52  TutorialEltwiseAddBenchmark(hebench::cpp::BaseEngine &engine,
54  const hebench::APIBridge::WorkloadParams &bench_params);
55  ~TutorialEltwiseAddBenchmark() override;
56 
58  void decode(hebench::APIBridge::Handle encoded_data, hebench::APIBridge::DataPackCollection *p_native) override;
61 
62  hebench::APIBridge::Handle load(const hebench::APIBridge::Handle *p_local_data, std::uint64_t count) override;
63  void store(hebench::APIBridge::Handle remote_data,
64  hebench::APIBridge::Handle *p_local_data, std::uint64_t count) override;
65 
67  const hebench::APIBridge::ParameterIndexer *p_param_indexers) override;
68 
69  std::int64_t classTag() const override { return BaseBenchmark::classTag() | TutorialEltwiseAddBenchmark::tag; }
70 
71 private:
73  struct InternalParamInfo
74  {
75  public:
76  static constexpr std::int64_t tagPlaintext = 0x10;
77  static constexpr std::int64_t tagCiphertext = 0x20;
78 
79  std::uint64_t param_position;
80  std::int64_t tag;
81  };
82  // used to bundle a collection of samples for an operation parameter
83  template <class T>
84  struct InternalParam : public InternalParamInfo
85  {
86  public:
87  std::vector<T> samples;
88  };
90 
93  class Workload
95  {
96  public:
97  Workload(std::size_t vector_size);
98 
99  std::vector<lbcrypto::Plaintext> encodeVector(const std::vector<std::vector<std::int64_t>> &vec);
100  std::vector<lbcrypto::Ciphertext<lbcrypto::DCRTPoly>> encryptVector(const std::vector<lbcrypto::Plaintext> &encoded_vec);
101  std::vector<lbcrypto::Ciphertext<lbcrypto::DCRTPoly>> eltwiseadd(const std::vector<lbcrypto::Plaintext> &A,
102  const std::vector<lbcrypto::Ciphertext<lbcrypto::DCRTPoly>> &B);
103  std::vector<lbcrypto::Plaintext> decryptResult(const std::vector<lbcrypto::Ciphertext<lbcrypto::DCRTPoly>> &encrypted_result);
104  std::vector<std::vector<int64_t>> decodeResult(const std::vector<lbcrypto::Plaintext> &encoded_result);
105 
106  private:
107  class PalisadeBFVContext
108  {
109  public:
110  PalisadeBFVContext(int poly_modulus_degree);
111 
112  auto publicKey() const { return m_keys->publicKey; }
113  std::size_t getSlotCount() const { return m_slot_count; }
114  lbcrypto::CryptoContext<lbcrypto::DCRTPoly> &context() { return *m_p_palisade_context; }
115  void decrypt(const lbcrypto::Ciphertext<lbcrypto::DCRTPoly> &cipher, lbcrypto::Plaintext &plain)
116  {
117  context()->Decrypt(m_keys->secretKey, cipher, &plain);
118  }
119 
120  lbcrypto::Plaintext decrypt(const lbcrypto::Ciphertext<lbcrypto::DCRTPoly> &cipher)
121  {
122  lbcrypto::Plaintext retval;
123  decrypt(cipher, retval);
124  return retval;
125  }
126 
127  private:
128  std::shared_ptr<lbcrypto::CryptoContext<lbcrypto::DCRTPoly>> m_p_palisade_context;
129  std::unique_ptr<lbcrypto::LPKeyPair<lbcrypto::DCRTPoly>> m_keys;
130  std::size_t m_slot_count;
131  };
132 
133  std::size_t m_vector_size;
134  std::shared_ptr<PalisadeBFVContext> m_p_context;
135 
136  PalisadeBFVContext &context() { return *m_p_context; }
137  };
139 
140  std::shared_ptr<Workload> m_p_workload;
141 };
142 
Top level opaque benchmark class.
Definition: benchmark.hpp:143
virtual hebench::APIBridge::Handle load(const hebench::APIBridge::Handle *p_local_data, std::uint64_t count)=0
virtual hebench::APIBridge::Handle decrypt(hebench::APIBridge::Handle encrypted_data)=0
virtual void store(hebench::APIBridge::Handle remote_data, hebench::APIBridge::Handle *p_local_data, std::uint64_t count)=0
virtual hebench::APIBridge::Handle encode(const hebench::APIBridge::DataPackCollection *p_parameters)=0
std::int64_t classTag() const override
Retrieves the tag of the class to which this object belongs.
Definition: benchmark.hpp:199
virtual hebench::APIBridge::Handle encrypt(hebench::APIBridge::Handle encoded_data)=0
virtual void decode(hebench::APIBridge::Handle encoded_data, hebench::APIBridge::DataPackCollection *p_native)=0
virtual hebench::APIBridge::Handle operate(hebench::APIBridge::Handle h_remote_packed, const hebench::APIBridge::ParameterIndexer *p_param_indexers, std::uint64_t indexers_count)=0
Base class that encapsulates common behavior of backend engines.
Definition: engine.hpp:70
Base class for objects representing a specific benchmark based on an HEBench benchmark descriptor.
Definition: benchmark.hpp:36
virtual void destroyBenchmark(BaseBenchmark *p_bench)=0
Destroys and frees resources held by a BaseBenchmark (or derived) object previously created by create...
virtual BaseBenchmark * createBenchmark(BaseEngine &engine, const hebench::APIBridge::WorkloadParams *p_params)=0
Instantiates the represented benchmark.
virtual std::string getBenchmarkDescription(const hebench::APIBridge::WorkloadParams *p_w_params) const
Retrieves human-readable description specific to the represented benchmark.
Definition: benchmark.cpp:17
#define HEBERROR_DECLARE_CLASS_NAME(class_name)
ErrorCode decrypt(Handle h_benchmark, Handle h_ciphertext, Handle *h_plaintext)
Decrypts a cipher text into corresponding plain text.
Workload
Defines all possible workloads.
Definition: types.h:83
Defines a benchmark test.
Definition: types.h:527
Defines a collection of data packs.
Definition: types.h:624
Specifies the parameters for a workload.
Definition: types.h:363
Structure to contain flexible data.
Definition: types.h:552