ComputeFHE 1.0
General-Purpose Privacy-Preserving Computation Library for TFHE
Loading...
Searching...
No Matches
Einteger.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2026 Faris Serdar Taşel <fst@cankaya.edu.tr>
3 * SPDX-FileCopyrightText: 2026 Efe Çiftci <efeciftci@cankaya.edu.tr>
4 *
5 * SPDX-License-Identifier: MIT
6 */
7
13#pragma once
16#include <iostream>
17using namespace std;
18
19namespace computefhe {
27 class Einteger : public Serializable {
28 private:
29 static bool div_cache(const FixedPoint &a, const FixedPoint &b);
30 static bool div_cache(const FixedPoint &a, uint64_t b);
31 static bool div_cache(uint64_t a, const FixedPoint &b);
32
33 protected:
34 FixedPoint data;
35 size_t size;
36 bool sign;
37
38 static FixedPoint cached_divident;
39 static FixedPoint cached_divisor;
40 static FixedPoint cached_quotient;
41 static FixedPoint cached_remainder;
42
43 static int64_t sign_extend(uint64_t d, size_t n_digits);
44 void _sync_var();
45 void _desync_var();
46 static bool promote(const Einteger &a, const Einteger &b,
47 FixedPoint &a_out, FixedPoint &b_out);
48
49 public:
55
62 static FixedPoint promote(const Einteger &a, size_t s);
63
68 Einteger(int64_t d);
69
76 Einteger(size_t n_digits, bool is_signed);
77
84 Einteger(int64_t d, size_t n_digits);
85
92 Einteger(uint64_t d, size_t n_digits);
93
99 Einteger(const FixedPoint &fp, bool is_signed);
100
105 Einteger(const Einteger &other);
106
110 virtual ~Einteger();
111
116 const FixedPoint &getData() const;
117
123 size_t getSize() const;
124
129 bool isSigned() const;
130
131 // Comparison operators
138 virtual const Einteger operator==(const Einteger &) const;
145 virtual const Einteger operator!=(const Einteger &) const;
152 virtual const Einteger operator>(const Einteger &) const;
160 virtual const Einteger operator>=(const Einteger &) const;
167 virtual const Einteger operator<(const Einteger &) const;
174 virtual const Einteger operator<=(const Einteger &) const;
180 virtual const Einteger operator==(uint64_t) const;
186 virtual const Einteger operator!=(uint64_t) const;
192 virtual const Einteger operator>(uint64_t) const;
199 virtual const Einteger operator>=(uint64_t) const;
205 virtual const Einteger operator<(uint64_t) const;
212 virtual const Einteger operator<=(uint64_t) const;
213
214 // Arithmetic operators
220 virtual const Einteger operator+(const Einteger &) const;
226 virtual const Einteger operator+=(const Einteger &);
232 virtual const Einteger operator-(const Einteger &) const;
238 virtual const Einteger operator-=(const Einteger &);
244 virtual const Einteger operator*(const Einteger &) const;
250 virtual const Einteger operator*=(const Einteger &);
256 virtual const Einteger operator/(const Einteger &) const;
262 virtual const Einteger operator/=(const Einteger &);
268 virtual const Einteger operator%(const Einteger &) const;
274 virtual const Einteger operator%=(const Einteger &);
280 virtual const Einteger operator+(uint64_t) const;
286 virtual const Einteger operator+=(uint64_t);
292 virtual const Einteger operator-(uint64_t) const;
298 virtual const Einteger operator-=(uint64_t);
304 virtual const Einteger operator*(uint64_t) const;
311 virtual const Einteger operator*=(uint64_t);
317 virtual const Einteger operator/(uint64_t) const;
323 virtual const Einteger operator/=(uint64_t);
329 virtual const Einteger operator%(uint64_t) const;
335 virtual const Einteger operator%=(uint64_t);
340 const Einteger operator-() const;
341
342 // Logic operators
348 virtual const Einteger operator&(const Einteger &) const;
354 virtual const Einteger operator&=(const Einteger &);
360 virtual const Einteger operator|(const Einteger &) const;
366 virtual const Einteger operator|=(const Einteger &);
372 virtual const Einteger operator^(const Einteger &) const;
378 virtual const Einteger operator^=(const Einteger &);
384 virtual const Einteger operator&(uint64_t) const;
390 virtual const Einteger operator&=(uint64_t);
396 virtual const Einteger operator|(uint64_t) const;
402 virtual const Einteger operator|=(uint64_t);
408 virtual const Einteger operator^(uint64_t) const;
414 virtual const Einteger operator^=(uint64_t);
420 virtual const Einteger operator!() const;
425 virtual const Einteger operator~() const;
431 virtual const Einteger operator&&(const Einteger &) const;
437 virtual const Einteger operator&&(uint64_t) const;
443 virtual const Einteger operator||(const Einteger &) const;
449 virtual const Einteger operator||(uint64_t) const;
450
451 // TODO: logical and/or for bool-type
452
453 // Increment & Decrement operators
478
479 // Shift operators
485 const Einteger operator<<(int) const;
497 const Einteger operator>>(int) const;
504
505 // Assignment operators
517 Einteger &operator=(uint64_t);
518
519 // Type conversion
524 virtual explicit operator bool() const;
529 virtual explicit operator int8_t() const;
534 virtual explicit operator uint8_t() const;
539 virtual explicit operator int16_t() const;
544 virtual explicit operator uint16_t() const;
549 virtual explicit operator int32_t() const;
554 virtual explicit operator uint32_t() const;
559 virtual explicit operator int64_t() const;
564 virtual explicit operator uint64_t() const;
569 virtual explicit operator double() const;
570
571 // Friend functions
579 friend ostream &operator<<(ostream &out, const Einteger &obj);
587 friend const Einteger operator==(uint64_t a, const Einteger &b);
595 friend const Einteger operator!=(uint64_t a, const Einteger &b);
603 friend const Einteger operator>(uint64_t a, const Einteger &b);
611 friend const Einteger operator>=(uint64_t a, const Einteger &b);
619 friend const Einteger operator<(uint64_t a, const Einteger &b);
627 friend const Einteger operator<=(uint64_t a, const Einteger &b);
635 friend const Einteger operator+(uint64_t a, const Einteger &b);
643 friend const Einteger operator-(uint64_t a, const Einteger &b);
651 friend const Einteger operator*(uint64_t a, const Einteger &b);
659 friend const Einteger operator/(uint64_t a, const Einteger &b);
666 friend const Einteger operator%(uint64_t a, const Einteger &b);
674 friend const Einteger operator&(uint64_t a, const Einteger &b);
682 friend const Einteger operator|(uint64_t a, const Einteger &b);
690 friend const Einteger operator^(uint64_t a, const Einteger &b);
698 friend const Einteger operator&&(uint64_t a, const Einteger &b);
706 friend const Einteger operator||(uint64_t a, const Einteger &b);
707
708 // Serialization support
709 template <class Archive>
710 void save(Archive &ar, std::uint32_t const version) const {
711 ar(cereal::make_nvp("data", data));
712 ar(cereal::make_nvp("size", size));
713 ar(cereal::make_nvp("sign", sign));
714 }
715
716 template <class Archive>
717 void load(Archive &ar, std::uint32_t const version) {
718 if (version > SerializedVersion()) {
719 OPENFHE_THROW("serialized object version " +
720 std::to_string(version) +
721 " is from a later version of the library");
722 }
723 ar(cereal::make_nvp("data", data));
724 ar(cereal::make_nvp("size", size));
725 ar(cereal::make_nvp("sign", sign));
726 }
727
728 std::string SerializedObjectName() const override { return "Einteger"; }
729
730 static uint32_t SerializedVersion() { return 1; }
731
732 // TODO: friend shift operators for integral types
733 // TODO: friend logical and/or for bool-type
734 };
735
736 ostream &operator<<(ostream &out, const Einteger &obj);
737 const Einteger operator==(uint64_t a, const Einteger &b);
738 const Einteger operator!=(uint64_t a, const Einteger &b);
739 const Einteger operator>(uint64_t a, const Einteger &b);
740 const Einteger operator>=(uint64_t a, const Einteger &b);
741 const Einteger operator<(uint64_t a, const Einteger &b);
742 const Einteger operator<=(uint64_t a, const Einteger &b);
743
744 const Einteger operator+(uint64_t a, const Einteger &b);
745 const Einteger operator-(uint64_t a, const Einteger &b);
746 const Einteger operator*(uint64_t a, const Einteger &b);
747 const Einteger operator/(uint64_t a, const Einteger &b);
748 const Einteger operator%(uint64_t a, const Einteger &b);
749
750 const Einteger operator&(uint64_t a, const Einteger &b);
751 const Einteger operator|(uint64_t a, const Einteger &b);
752 const Einteger operator^(uint64_t a, const Einteger &b);
753 const Einteger operator&&(uint64_t a, const Einteger &b);
754 const Einteger operator||(uint64_t a, const Einteger &b);
755
756 template <typename T, size_t BITS, bool SIGNED>
768 class EInt : public Einteger {
769 public:
770 EInt(T d = 0) : Einteger((uint64_t)d, BITS) { this->sign = SIGNED; }
771 EInt(const Einteger &other) : Einteger(promote(other, BITS), SIGNED) {}
772 };
773
791} // namespace computefhe
Defines core enumerations and helper functions for ComputeFHE configuration.
const Einteger operator==(double a, const Efixedpoint &b)
const Einteger operator>(double a, const Efixedpoint &b)
ostream & operator<<(ostream &out, const Efixedpoint &obj)
const Efixedpoint operator+(double a, const Efixedpoint &b)
const Einteger operator>=(double a, const Efixedpoint &b)
const Efixedpoint operator*(double a, const Efixedpoint &b)
const Einteger operator!=(double a, const Efixedpoint &b)
const Einteger operator<(double a, const Efixedpoint &b)
const Efixedpoint operator/(double a, const Efixedpoint &b)
const Einteger operator<=(double a, const Efixedpoint &b)
const Efixedpoint operator-(double a, const Efixedpoint &b)
const Einteger operator|(uint64_t a, const Einteger &b)
const Einteger operator^(uint64_t a, const Einteger &b)
const Einteger operator&(uint64_t a, const Einteger &b)
const Einteger operator&&(uint64_t a, const Einteger &b)
const Einteger operator%(uint64_t a, const Einteger &b)
const Einteger operator||(uint64_t a, const Einteger &b)
Defines the fundamental bit-level and bit-vector data structures for FHE operations.
Template class for fixed-size encrypted integers.
Definition Einteger.h:768
Represents an encrypted integer.
Definition Einteger.h:27
virtual const Einteger operator^=(uint64_t)
Encrypted bitwise XOR assignment with a plaintext uint64_t.
virtual const Einteger operator>(uint64_t) const
Encrypted greater-than comparison with a plaintext uint64_t.
virtual const Einteger operator|=(const Einteger &)
Encrypted bitwise OR assignment with another Einteger.
virtual const Einteger operator/(const Einteger &) const
Encrypted division with another Einteger.
virtual const Einteger operator||(const Einteger &) const
Encrypted logical OR with another Einteger.
virtual const Einteger operator*=(const Einteger &)
Encrypted multiplication assignment with another Einteger.
virtual const Einteger operator/=(const Einteger &)
Encrypted division assignment with another Einteger.
virtual const Einteger operator|=(uint64_t)
Encrypted bitwise OR assignment with a plaintext uint64_t.
const Einteger operator>>(int) const
Encrypted right shift.
virtual const Einteger operator/=(uint64_t)
Encrypted division assignment with a plaintext uint64_t.
virtual const Einteger operator|(uint64_t) const
Encrypted bitwise OR with a plaintext uint64_t.
virtual const Einteger operator|(const Einteger &) const
Encrypted bitwise OR with another Einteger.
friend const Einteger operator>(uint64_t a, const Einteger &b)
Friend operator for plaintext uint64_t greater-than comparison with Einteger.
virtual const Einteger operator*(const Einteger &) const
Encrypted multiplication with another Einteger.
friend const Einteger operator>=(uint64_t a, const Einteger &b)
Friend operator for plaintext uint64_t greater-than-or-equal comparison with Einteger.
Einteger & operator=(uint64_t)
Assignment operator from a plaintext uint64_t.
const Einteger operator--()
Pre-decrement operator. Decrements the encrypted integer by one.
friend const Einteger operator&(uint64_t a, const Einteger &b)
Friend operator for plaintext uint64_t bitwise AND with Einteger.
friend const Einteger operator/(uint64_t a, const Einteger &b)
Friend operator for plaintext uint64_t division with Einteger.
virtual const Einteger operator-=(const Einteger &)
Encrypted subtraction assignment with another Einteger.
friend const Einteger operator==(uint64_t a, const Einteger &b)
Friend operator for plaintext uint64_t equality comparison with Einteger.
virtual const Einteger operator+(const Einteger &) const
Encrypted addition with another Einteger.
virtual const Einteger operator%=(const Einteger &)
Encrypted modulo assignment with another Einteger.
virtual const Einteger operator>=(const Einteger &) const
Encrypted greater-than-or-equal comparison with another Einteger.
virtual const Einteger operator<(const Einteger &) const
Encrypted less-than comparison with another Einteger.
bool isSigned() const
Checks if the encrypted integer is signed.
const Einteger operator++(int)
Post-increment operator. Increments the encrypted integer by one.
virtual const Einteger operator!() const
Encrypted logical NOT.
virtual const Einteger operator+=(const Einteger &)
Encrypted addition assignment with another Einteger.
virtual const Einteger operator/(uint64_t) const
Encrypted division with a plaintext uint64_t.
virtual const Einteger operator!=(const Einteger &) const
Encrypted inequality comparison with another Einteger.
virtual const Einteger operator+=(uint64_t)
Encrypted addition assignment with a plaintext uint64_t.
virtual const Einteger operator^(const Einteger &) const
Encrypted bitwise XOR with another Einteger.
virtual const Einteger operator&=(const Einteger &)
Encrypted bitwise AND assignment with another Einteger.
friend const Einteger operator+(uint64_t a, const Einteger &b)
Friend operator for plaintext uint64_t addition with Einteger.
virtual const Einteger operator<=(const Einteger &) const
Encrypted less-than-or-equal comparison with another Einteger.
virtual const Einteger operator%=(uint64_t)
Encrypted modulo assignment with a plaintext uint64_t.
virtual const Einteger operator+(uint64_t) const
Encrypted addition with a plaintext uint64_t.
size_t getSize() const
Gets the number of bits used to represent the encrypted integer.
virtual const Einteger operator^=(const Einteger &)
Encrypted bitwise XOR assignment with another Einteger.
virtual const Einteger operator==(const Einteger &) const
Encrypted equality comparison with another Einteger.
virtual const Einteger operator-(uint64_t) const
Encrypted subtraction with a plaintext uint64_t.
virtual const Einteger operator!=(uint64_t) const
Encrypted inequality comparison with a plaintext uint64_t.
friend const Einteger operator&&(uint64_t a, const Einteger &b)
Friend operator for plaintext uint64_t logical AND with Einteger.
const Einteger operator++()
Pre-increment operator. Increments the encrypted integer by one.
virtual const Einteger operator||(uint64_t) const
Encrypted logical OR with a plaintext uint64_t.
Einteger(const Einteger &other)
Copy constructor.
Einteger()
Default constructor. Initializes an empty or zero-valued encrypted integer.
virtual const Einteger operator-(const Einteger &) const
Encrypted subtraction with another Einteger.
virtual ~Einteger()
Destructor.
Einteger(const FixedPoint &fp, bool is_signed)
Constructs an Einteger from an existing FixedPoint object.
const Einteger operator<<=(int)
Encrypted left shift assignment.
virtual const Einteger operator&=(uint64_t)
Encrypted bitwise AND assignment with a plaintext uint64_t.
friend const Einteger operator|(uint64_t a, const Einteger &b)
Friend operator for plaintext uint64_t bitwise OR with Einteger.
const Einteger operator--(int)
Post-decrement operator. Decrements the encrypted integer by one.
virtual const Einteger operator*=(uint64_t)
Encrypted multiplication assignment with a plaintext uint64_t.
friend const Einteger operator<=(uint64_t a, const Einteger &b)
Friend operator for plaintext uint64_t less-than-or-equal comparison with Einteger.
Einteger(int64_t d, size_t n_digits)
Constructs an Einteger from a plaintext int64_t with a specified number of bits.
virtual const Einteger operator>(const Einteger &) const
Encrypted greater-than comparison with another Einteger.
Einteger & operator=(const Einteger &)
Assignment operator from another Einteger.
virtual const Einteger operator==(uint64_t) const
Encrypted equality comparison with a plaintext uint64_t.
const Einteger operator>>=(int)
Encrypted right shift assignment.
virtual const Einteger operator%(uint64_t) const
Encrypted modulo with a plaintext uint64_t.
static FixedPoint promote(const Einteger &a, size_t s)
Promotes or truncates an Einteger to a specific bit size.
friend const Einteger operator*(uint64_t a, const Einteger &b)
Friend operator for plaintext uint64_t multiplication with Einteger.
friend ostream & operator<<(ostream &out, const Einteger &obj)
Overloads the stream insertion operator by decrypting Einteger.
friend const Einteger operator<(uint64_t a, const Einteger &b)
Friend operator for plaintext uint64_t less-than comparison with Einteger.
const FixedPoint & getData() const
Gets the underlying FixedPoint data.
virtual const Einteger operator~() const
Encrypted bitwise NOT (one's complement).
virtual const Einteger operator^(uint64_t) const
Encrypted bitwise XOR with a plaintext uint64_t.
virtual const Einteger operator&(uint64_t) const
Encrypted bitwise AND with a plaintext uint64_t.
virtual const Einteger operator>=(uint64_t) const
Encrypted greater-than-or-equal comparison with a plaintext uint64_t.
Einteger(int64_t d)
Constructs an Einteger from a plaintext int64_t.
virtual const Einteger operator<(uint64_t) const
Encrypted less-than comparison with a plaintext uint64_t.
Einteger(size_t n_digits, bool is_signed)
Constructs an Einteger with a specified number of bits and signedness.
virtual const Einteger operator<=(uint64_t) const
Encrypted less-than-or-equal comparison with a plaintext uint64_t.
Einteger(uint64_t d, size_t n_digits)
Constructs an Einteger from a plaintext uint64_t with a specified number of bits.
friend const Einteger operator-(uint64_t a, const Einteger &b)
Friend operator for plaintext uint64_t subtraction with Einteger.
friend const Einteger operator||(uint64_t a, const Einteger &b)
Friend operator for plaintext uint64_t logical OR with Einteger.
const Einteger operator-() const
Encrypted unary negation (two's complement).
friend const Einteger operator^(uint64_t a, const Einteger &b)
Friend operator for plaintext uint64_t bitwise XOR with Einteger.
const Einteger operator<<(int) const
Encrypted left shift.
virtual const Einteger operator%(const Einteger &) const
Encrypted modulo with another Einteger.
virtual const Einteger operator&(const Einteger &) const
Encrypted bitwise AND with another Einteger.
virtual const Einteger operator-=(uint64_t)
Encrypted subtraction assignment with a plaintext uint64_t.
virtual const Einteger operator&&(uint64_t) const
Encrypted logical AND with a plaintext uint64_t.
virtual const Einteger operator*(uint64_t) const
Encrypted multiplication with a plaintext uint64_t.
friend const Einteger operator!=(uint64_t a, const Einteger &b)
Friend operator for plaintext uint64_t inequality comparison with Einteger.
virtual const Einteger operator&&(const Einteger &) const
Encrypted logical AND with another Einteger.
friend const Einteger operator%(uint64_t a, const Einteger &b)
Friend operator for plaintext uint64_t modulo with Einteger.
A bit-vector representation of an encrypted or plaintext word.
Definition FixedPoint.h:118