HEaaN
Crypto Lab's great homomorphic encryption library
HomEvaluator.hpp
Go to the documentation of this file.
1 // //
3 // Copyright (C) 2021-2023 Crypto Lab Inc. //
4 // //
5 // - This file is part of HEaaN homomorphic encryption library. //
6 // - HEaaN cannot be copied and/or distributed without the express permission //
7 // of Crypto Lab Inc. //
8 // //
10 
11 #pragma once
12 
13 #include <vector>
14 
15 #include "HEaaN/HEaaNExport.hpp"
16 #include "HEaaN/KeyPack.hpp"
17 #include "HEaaN/Real.hpp"
18 
19 namespace HEaaN {
20 
21 class Message;
22 class Plaintext;
23 class Ciphertext;
24 class HomEvaluatorImpl;
25 
30  friend class BootstrapperImpl;
31 
32 public:
33  explicit HomEvaluator(const Context &context,
34  const std::string &key_dir_path);
35  explicit HomEvaluator(const Context &context, const KeyPack &pack);
36 
40  void negate(const Message &msg, Message &msg_out) const;
44  void negate(const Plaintext &ptxt, Plaintext &ptxt_out) const;
48  void negate(const Ciphertext &ctxt, Ciphertext &ctxt_out) const;
49 
55  void add(const Message &msg1, const Complex &cnst_complex,
56  Message &msg_out) const;
63  void add(const Message &msg1, const Message &msg2, Message &msg_out) const;
71  void add(const Plaintext &ptxt1, const Complex &cnst_complex,
72  Plaintext &ptxt_out) const;
79  void add(const Plaintext &ptxt1, const Plaintext &ptxt2,
80  Plaintext &ptxt_out) const;
88  void add(const Ciphertext &ctxt1, const Complex &cnst_complex,
89  Ciphertext &ctxt_out) const;
96  void add(const Ciphertext &ctxt1, const Message &msg2,
97  Ciphertext &ctxt_out) const;
105  void add(const Ciphertext &ctxt1, const Plaintext &ptxt2,
106  Ciphertext &ctxt_out) const;
116  void add(const Ciphertext &ctxt1, const Ciphertext &ctxt2,
117  Ciphertext &ctxt_out) const;
118 
124  void sub(const Message &msg1, const Complex &cnst_complex,
125  Message &msg_out) const;
132  void sub(const Message &msg1, const Message &msg2, Message &msg_out) const;
140  void sub(const Plaintext &ptxt1, const Complex &cnst_complex,
141  Plaintext &ptxt_out) const;
148  void sub(const Plaintext &ptxt1, const Plaintext &ptxt2,
149  Plaintext &ptxt_out) const;
157  void sub(const Ciphertext &ctxt1, const Complex &cnst_complex,
158  Ciphertext &ctxt_out) const;
166  void sub(const Ciphertext &ctxt1, const Message &msg2,
167  Ciphertext &ctxt_out) const;
175  void sub(const Ciphertext &ctxt1, const Plaintext &ptxt2,
176  Ciphertext &ctxt_out) const;
186  void sub(const Ciphertext &ctxt1, const Ciphertext &ctxt2,
187  Ciphertext &ctxt_out) const;
188 
194  void mult(const Message &msg1, const Complex &cnst_complex,
195  Message &msg_out) const;
202  void mult(const Message &msg1, const Message &msg2, Message &msg_out) const;
210  void mult(const Plaintext &ptxt1, const Plaintext &ptxt2,
211  Plaintext &ptxt_out) const;
219  void mult(const Plaintext &ptxt1, const Complex &cnst_complex,
220  Plaintext &ptxt_out) const;
234  void mult(const Ciphertext &ctxt1, const Complex &cnst_complex,
235  Ciphertext &ctxt_out) const;
244  void mult(const Ciphertext &ctxt1, const Message &msg2,
245  Ciphertext &ctxt_out) const;
255  void mult(const Ciphertext &ctxt1, const Plaintext &ptxt2,
256  Ciphertext &ctxt_out) const;
266  void mult(const Ciphertext &ctxt1, const Ciphertext &ctxt2,
267  Ciphertext &ctxt_out) const;
268 
272  void multImagUnit(const Message &msg, Message &msg_out) const;
276  void multImagUnit(const Plaintext &ptxt, Plaintext &ptxt_out) const;
280  void multImagUnit(const Ciphertext &ctxt, Ciphertext &ctxt_out) const;
281 
286  void multInteger(const Plaintext &ptxt, u64 cnst_integer,
287  Plaintext &ptxt_out) const;
288 
293  void multInteger(const Ciphertext &ctxt, u64 cnst_integer,
294  Ciphertext &ctxt_out) const;
295 
299  void square(const Message &msg, Message &msg_out) const;
300 
304  void square(const Plaintext &ptxt, Plaintext &ptxt_out) const;
305 
309  void square(const Ciphertext &ctxt, Ciphertext &ctxt_out) const;
310 
316  void leftRotate(const Message &msg, u64 rot, Message &msg_out) const;
322  void leftRotate(const Plaintext &ptxt, u64 rot, Plaintext &ptxt_out) const;
323 
329  void leftRotate(const Ciphertext &ctxt, u64 rot,
330  Ciphertext &ctxt_out) const;
331 
337  void rightRotate(const Message &msg, u64 rot, Message &msg_out) const;
343  void rightRotate(const Plaintext &ptxt, u64 rot, Plaintext &ptxt_out) const;
349  void rightRotate(const Ciphertext &ctxt, u64 rot,
350  Ciphertext &ctxt_out) const;
351 
357  void rotSum(const std::vector<Ciphertext> &ctxt,
358  const std::vector<u64> &rot_idx, Ciphertext &ctxt_out) const;
359 
368  void leftRotateReduce(const Message &msg, const u64 &idx_interval,
369  const u64 &num_summation, Message &msg_out) const;
378  void rightRotateReduce(const Message &msg, const u64 &idx_interval,
379  const u64 &num_summation, Message &msg_out) const;
388  void leftRotateReduce(const Ciphertext &ctxt, const u64 &idx_interval,
389  const u64 &num_summation, Ciphertext &ctxt_out) const;
398  void rightRotateReduce(const Ciphertext &ctxt, const u64 &idx_interval,
399  const u64 &num_summation,
400  Ciphertext &ctxt_out) const;
404  void conjugate(const Message &msg, Message &msg_out) const;
408  void conjugate(const Plaintext &ptxt, Plaintext &ptxt_out) const;
412  void conjugate(const Ciphertext &ctxt, Ciphertext &ctxt_out) const;
416  void killImag(const Ciphertext &ctxt, Ciphertext &ctxt_out) const;
417 
423  void multWithoutRescale(const Ciphertext &ctxt1,
424  const Complex &cnst_complex,
425  Ciphertext &ctxt_out) const;
435  void multWithoutRescale(const Ciphertext &ctxt1, const Plaintext &ptxt2,
436  Ciphertext &ctxt_out) const;
446  void multWithoutRescale(const Ciphertext &ctxt1, const Ciphertext &ctxt2,
447  Ciphertext &ctxt_out) const;
448 
461  void tensor(const Ciphertext &ctxt1, const Ciphertext &ctxt2,
462  Ciphertext &ctxt_out) const;
467  void relinearize(const Ciphertext &ctxt, Ciphertext &ctxt_out) const;
468 
474  void rescale(Plaintext &ptxt) const;
475 
481  void rescale(Ciphertext &ctxt) const;
482 
493  void inverseRescale(Plaintext &ptxt) const;
494 
506  void inverseRescale(Ciphertext &ctxt) const;
507 
514  void levelDown(const Ciphertext &ctxt, u64 target_level,
515  Ciphertext &ctxt_out) const;
521  void levelDownOne(const Ciphertext &ctxt, Ciphertext &ctxt_out) const;
522 
528  void relevel(const Plaintext &ptxt, const u64 target_level,
529  Plaintext &ptxt_out) const;
530 
533  const Context &getContext() const { return context_; }
534 
535 private:
539  std::shared_ptr<HomEvaluatorImpl> impl_;
540 };
541 } // namespace HEaaN
HEaaN::KeyPack
Class managing public keys.
Definition: KeyPack.hpp:29
HEaaN::Complex
std::complex< Real > Complex
Default type for representing a complex number.
Definition: Real.hpp:22
HEaaN::Message
Definition: Message.hpp:30
HEaaN::HomEvaluator::impl_
std::shared_ptr< HomEvaluatorImpl > impl_
Internal implementation object.
Definition: HomEvaluator.hpp:539
HEaaN::HomEvaluator
A class consisting of basic operation of Ciphertext and Message.
Definition: HomEvaluator.hpp:29
HEaaN::u64
std::uint64_t u64
Definition: Integers.hpp:17
HEaaN::Plaintext
A class of plaintexts each of which is simply a polynomial.
Definition: Plaintext.hpp:26
HEaaN::Ciphertext
A class of ciphertexts each of which contains a vector of polynomials of length > 1.
Definition: Ciphertext.hpp:27
HEaaN::Context
std::shared_ptr< ContextContent > Context
Definition: Context.hpp:27
HEaaN::HomEvaluator::getContext
const Context & getContext() const
Get the internal Context object.
Definition: HomEvaluator.hpp:533
HEaaN::HomEvaluator::context_
const Context context_
A context with which HomEvaluator is associated.
Definition: HomEvaluator.hpp:537
HEaaNExport.hpp
HEAAN_API
#define HEAAN_API
Definition: HEaaNExport.hpp:26
HEaaN
Definition: Bootstrapper.hpp:16
Real.hpp
KeyPack.hpp