HEBench
tutorial_eltwiseadd_benchmark_seal.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 "seal/seal.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<seal::Plaintext> encodeVector(const std::vector<std::vector<std::int64_t>> &vec);
100  std::vector<seal::Plaintext> encodeVector(const std::vector<gsl::span<const std::int64_t>> &vec);
101  std::vector<seal::Ciphertext> encryptVector(const std::vector<seal::Plaintext> &encoded_vec);
102  std::vector<seal::Ciphertext> eltwiseadd(const std::vector<seal::Plaintext> &A,
103  const std::vector<seal::Ciphertext> &B);
104  std::vector<seal::Plaintext> decryptResult(const std::vector<seal::Ciphertext> &encrypted_result);
105  std::vector<std::vector<int64_t>> decodeResult(const std::vector<seal::Plaintext> &encoded_result);
106 
107  private:
108  class SealBFVContext
109  {
110  public:
111  SealBFVContext(int poly_modulus_degree);
112 
113  seal::BatchEncoder &encoder() { return *m_p_batch_encoder; }
114  seal::Evaluator &evaluator() { return *m_p_evaluator; }
115  seal::Decryptor &decryptor() { return *m_p_decryptor; }
116  const seal::Encryptor &encryptor() const { return *m_p_encryptor; }
117  const seal::PublicKey &public_key() const { return m_public_key; }
118  const seal::SecretKey &secret_key() const { return m_secret_key; }
119  seal::SEALContext &context() { return *m_p_seal_context; }
120 
121  private:
122  std::shared_ptr<seal::SEALContext> m_p_seal_context;
123  std::unique_ptr<seal::KeyGenerator> m_p_keygen;
124  seal::PublicKey m_public_key;
125  seal::SecretKey m_secret_key;
126  std::unique_ptr<seal::Encryptor> m_p_encryptor;
127  std::unique_ptr<seal::Evaluator> m_p_evaluator;
128  std::unique_ptr<seal::Decryptor> m_p_decryptor;
129  std::unique_ptr<seal::BatchEncoder> m_p_batch_encoder;
130  };
131 
132  std::size_t m_vector_size;
133  std::shared_ptr<SealBFVContext> m_p_context;
134 
135  SealBFVContext &context() { return *m_p_context; }
136  };
138 
139  std::shared_ptr<Workload> m_p_workload;
140 };
141 
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)
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