ComputeFHE 1.0
General-Purpose Privacy-Preserving Computation Library for TFHE
Loading...
Searching...
No Matches
BaseALU.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
14#include <computefhe/Einteger.h>
16
17using namespace lbcrypto;
18
19namespace computefhe {
20 class ComputeFHE;
21
30 class BaseALU {
31 protected:
32 BinaryDigit carry;
33 ComputeFHE *cfhe_base;
34
35 public:
42
46 virtual ~BaseALU();
47
50
52 virtual void SetCarry(BinaryDigit value);
53
55 virtual void SetCarry();
56
58 virtual void ResetCarry();
59
64 virtual BinaryDigit FHE_False();
65 virtual BinaryDigit FHE_True();
66 virtual BinaryDigit FHE_AND(const BinaryDigit &a, const BinaryDigit &b);
67 virtual BinaryDigit FHE_NAND(const BinaryDigit &a,
68 const BinaryDigit &b);
69 virtual BinaryDigit FHE_OR(const BinaryDigit &a, const BinaryDigit &b);
70 virtual BinaryDigit FHE_NOR(const BinaryDigit &a, const BinaryDigit &b);
71 virtual BinaryDigit FHE_XOR(const BinaryDigit &a, const BinaryDigit &b);
72 virtual BinaryDigit FHE_XNOR(const BinaryDigit &a,
73 const BinaryDigit &b);
74 virtual BinaryDigit FHE_NOT(const BinaryDigit &a);
75 virtual BinaryDigit FHE_MUX(const BinaryDigit &s, const BinaryDigit &a,
76 const BinaryDigit &b);
78
83
88 virtual BinaryDigit Gate_AND(const BinaryDigit &a,
89 const BinaryDigit &b);
90 virtual BinaryDigit Gate_NAND(const BinaryDigit &a,
91 const BinaryDigit &b);
92 virtual BinaryDigit Gate_OR(const BinaryDigit &a, const BinaryDigit &b);
93 virtual BinaryDigit Gate_NOR(const BinaryDigit &a,
94 const BinaryDigit &b);
95 virtual BinaryDigit Gate_XOR(const BinaryDigit &a,
96 const BinaryDigit &b);
97 virtual BinaryDigit Gate_XNOR(const BinaryDigit &a,
98 const BinaryDigit &b);
99 virtual BinaryDigit Gate_NOT(const BinaryDigit &a);
100 virtual BinaryDigit Gate_MUX(const BinaryDigit &s, const BinaryDigit &a,
101 const BinaryDigit &b);
103
109
111 virtual BinaryDigit Gate_MAJ(const BinaryDigit &a, const BinaryDigit &b,
112 const BinaryDigit &c) = 0;
115 const BinaryDigit &b,
116 const BinaryDigit &c) = 0;
120 const BinaryDigit &a,
121 const BinaryDigit &b,
122 BinaryDigit *carry_out = nullptr) = 0;
125 const BinaryDigit &e0,
126 const BinaryDigit &s0) = 0;
128
133
136 virtual FixedPoint Mux(const BinaryDigit &s, const FixedPoint &a,
137 const FixedPoint &b) = 0;
138
141 virtual FixedPoint ToggleMSB(const FixedPoint &a) = 0;
142
144 virtual FixedPoint ShiftLeft(const FixedPoint &a, size_t shift) = 0;
147 virtual FixedPoint ShiftRight(const FixedPoint &a, size_t shift,
148 bool is_arithmetic = false) = 0;
149
151 virtual void Swap_if(const BinaryDigit &cond, BinaryDigit &a,
152 BinaryDigit &b) = 0;
155 virtual void Swap_if(const BinaryDigit &cond, FixedPoint &a,
156 FixedPoint &b) = 0;
157
159 virtual void HalfAdder(const BinaryDigit &a, const BinaryDigit &b,
160 BinaryDigit &sum, BinaryDigit &carry_out) = 0;
162 virtual void HalfSubtractor(const BinaryDigit &a, const BinaryDigit &b,
163 BinaryDigit &sum,
164 BinaryDigit &carry_out) = 0;
166 virtual void FullAdder(const BinaryDigit &a, const BinaryDigit &b,
167 const BinaryDigit &c, BinaryDigit &sum,
168 BinaryDigit &carry_out) = 0;
170
175
177 virtual FixedPoint Add(const FixedPoint &a, const FixedPoint &b) = 0;
179 virtual FixedPoint AddC(const FixedPoint &a, const FixedPoint &b) = 0;
181 virtual FixedPoint AddNC(const FixedPoint &a, const FixedPoint &b) = 0;
183 virtual FixedPoint AddCNC(const FixedPoint &a, const FixedPoint &b) = 0;
184
186 virtual FixedPoint Sub(const FixedPoint &a, const FixedPoint &b) = 0;
188 virtual FixedPoint SubC(const FixedPoint &a, const FixedPoint &b) = 0;
190 virtual FixedPoint SubNC(const FixedPoint &a, const FixedPoint &b) = 0;
193 virtual FixedPoint SubCNC(const FixedPoint &a, const FixedPoint &b) = 0;
194
196 virtual FixedPoint Neg(const FixedPoint &a) = 0;
198 virtual FixedPoint Not(const FixedPoint &a) = 0;
200
205
207 const FixedPoint &b) = 0;
209 virtual BinaryDigit CmpEq(const FixedPoint &a, const FixedPoint &b) = 0;
212 const FixedPoint &b) = 0;
215 const FixedPoint &b) = 0;
218 const FixedPoint &b) = 0;
221 const FixedPoint &b) = 0;
224 const FixedPoint &b) = 0;
226 virtual BinaryDigit CmpGT(const FixedPoint &a, const FixedPoint &b) = 0;
229 const FixedPoint &b) = 0;
231 virtual BinaryDigit CmpLT(const FixedPoint &a, const FixedPoint &b) = 0;
233
238
240 virtual FixedPoint FullMul(const FixedPoint &a,
241 const FixedPoint &b) = 0;
243 virtual FixedPoint Mul(const FixedPoint &a, const FixedPoint &b) = 0;
246 virtual void DivU(const FixedPoint &a, const FixedPoint &b,
247 FixedPoint &q, FixedPoint &r) = 0;
249
255
257 virtual FixedPoint PAdd(const FixedPoint &a, const FixedPoint &pb) = 0;
259 virtual FixedPoint PAddC(const FixedPoint &a, const FixedPoint &pb) = 0;
261 virtual FixedPoint PAddNC(const FixedPoint &a,
262 const FixedPoint &pb) = 0;
265 virtual FixedPoint PAddCNC(const FixedPoint &a,
266 const FixedPoint &pb) = 0;
267
269 virtual FixedPoint PSub(const FixedPoint &pa, const FixedPoint &b) = 0;
271 virtual FixedPoint PSubC(const FixedPoint &pa, const FixedPoint &b) = 0;
273 virtual FixedPoint PSubNC(const FixedPoint &pa,
274 const FixedPoint &b) = 0;
277 virtual FixedPoint PSubCNC(const FixedPoint &pa,
278 const FixedPoint &b) = 0;
279
281 virtual FixedPoint CPSub(const FixedPoint &a, const FixedPoint &pb) = 0;
283 virtual FixedPoint CPSubC(const FixedPoint &a,
284 const FixedPoint &pb) = 0;
287 virtual FixedPoint CPSubNC(const FixedPoint &a,
288 const FixedPoint &pb) = 0;
291 const FixedPoint &pb) = 0;
292
295 const FixedPoint &pb) = 0;
298 const FixedPoint &pb) = 0;
302 const FixedPoint &pb) = 0;
304 virtual FixedPoint PMul(const FixedPoint &a, const FixedPoint &pb) = 0;
307 const FixedPoint &pb) = 0;
309 };
310} // namespace computefhe
Defines the encrypted integer representation and operations.
Defines the fundamental bit-level and bit-vector data structures for FHE operations.
Abstract base class for FHE-based Arithmetic Logic Units.
Definition BaseALU.h:30
virtual BinaryDigit CmpLT(const FixedPoint &a, const FixedPoint &b)=0
Signed Less-Than.
virtual FixedPoint PSubC(const FixedPoint &pa, const FixedPoint &b)=0
Plaintext subtraction with carry.
virtual FixedPoint PAdd(const FixedPoint &a, const FixedPoint &pb)=0
Plaintext addition: result = a + pb.
virtual void ResetCarry()
Resets the internal carry to an encrypted 'False' value.
virtual FixedPoint PSubCNC(const FixedPoint &pa, const FixedPoint &b)=0
Plaintext subtraction with carry, but no update to internal state.
BaseALU(ComputeFHE *cfhe)
Construct a new BaseALU object.
virtual FixedPoint FullMul(const FixedPoint &a, const FixedPoint &b)=0
Performs full multiplication resulting in potentially larger bit-width.
virtual void Swap_if(const BinaryDigit &cond, BinaryDigit &a, BinaryDigit &b)=0
Conditional swap of two bits if 'cond' is true.
virtual FixedPoint PAddCNC(const FixedPoint &a, const FixedPoint &pb)=0
Plaintext addition with carry, but no update to internal state.
virtual BinaryDigit CmpEq(const FixedPoint &a, const FixedPoint &b)=0
Equal.
virtual FixedPoint Mux(const BinaryDigit &s, const FixedPoint &a, const FixedPoint &b)=0
Multiplexer for FixedPoint values: returns 'a' if 's' is true, else 'b'.
virtual FixedPoint PMul(const FixedPoint &a, const FixedPoint &pb)=0
Standard multiplication by a plaintext constant.
virtual FixedPoint SubC(const FixedPoint &a, const FixedPoint &b)=0
Subtraction with Borrow (!Carry): result = a - b - !carry.
virtual BinaryDigit CmpLTEq_U(const FixedPoint &a, const FixedPoint &b)=0
Unsigned Less-Than or Equal.
virtual BinaryDigit CmpGTEq_U(const FixedPoint &a, const FixedPoint &b)=0
Unsigned Greater-Than or Equal.
virtual BinaryDigit CmpLTEq(const FixedPoint &a, const FixedPoint &b)=0
Signed Less-Than or Equal.
virtual FixedPoint PMulFast(const FixedPoint &a, const FixedPoint &pb)=0
Optimized standard multiplication by a plaintext constant.
virtual FixedPoint Not(const FixedPoint &a)=0
Bitwise NOT.
virtual FixedPoint PFullMul(const FixedPoint &a, const FixedPoint &pb)=0
Full multiplication by a plaintext constant.
virtual FixedPoint PSubNC(const FixedPoint &pa, const FixedPoint &b)=0
Plaintext subtraction without updating internal carry.
virtual void FullAdder(const BinaryDigit &a, const BinaryDigit &b, const BinaryDigit &c, BinaryDigit &sum, BinaryDigit &carry_out)=0
1-bit full adder with explicit carry-in.
virtual FixedPoint Add(const FixedPoint &a, const FixedPoint &b)=0
Addition: result = a + b.
virtual FixedPoint PSub(const FixedPoint &pa, const FixedPoint &b)=0
Plaintext subtraction: result = pa - b.
virtual BinaryDigit CmpGT(const FixedPoint &a, const FixedPoint &b)=0
Signed Greater-Than.
virtual void HalfAdder(const BinaryDigit &a, const BinaryDigit &b, BinaryDigit &sum, BinaryDigit &carry_out)=0
1-bit half adder.
virtual FixedPoint Mul(const FixedPoint &a, const FixedPoint &b)=0
Standard multiplication (truncated to input width).
virtual FixedPoint ShiftRight(const FixedPoint &a, size_t shift, bool is_arithmetic=false)=0
Shift right (supports both logical and arithmetic/signed shifts).
virtual FixedPoint PFullMulFast(const FixedPoint &a, const FixedPoint &pb)=0
Optimized full multiplication by a plaintext constant.
virtual void DivU(const FixedPoint &a, const FixedPoint &b, FixedPoint &q, FixedPoint &r)=0
Unsigned division, calculating quotient (q) and remainder (r).
virtual BinaryDigit CmpLT_U(const FixedPoint &a, const FixedPoint &b)=0
Unsigned Less-Than.
virtual FixedPoint CPSubC(const FixedPoint &a, const FixedPoint &pb)=0
Ciphertext-Plaintext subtraction with carry.
virtual FixedPoint ShiftLeft(const FixedPoint &a, size_t shift)=0
Logical shift left.
virtual FixedPoint AddC(const FixedPoint &a, const FixedPoint &b)=0
Addition with Carry: result = a + b + carry.
virtual FixedPoint AddNC(const FixedPoint &a, const FixedPoint &b)=0
Addition without updating internal carry.
virtual BinaryDigit Gate_DigitSum(const BinaryDigit &e1, const BinaryDigit &e0, const BinaryDigit &s0)=0
Performs bitwise digit summation logic.
virtual BinaryDigit CmpNotEq(const FixedPoint &a, const FixedPoint &b)=0
Not-Equal.
virtual void HalfSubtractor(const BinaryDigit &a, const BinaryDigit &b, BinaryDigit &sum, BinaryDigit &carry_out)=0
1-bit half subtractor.
virtual BinaryDigit CmpGT_U(const FixedPoint &a, const FixedPoint &b)=0
Unsigned Greater-Than.
virtual BinaryDigit CmpGTEq(const FixedPoint &a, const FixedPoint &b)=0
Signed Greater-Than or Equal.
virtual BinaryDigit Constant1()
Returns encrypted constant 1 (true).
virtual FixedPoint CPSub(const FixedPoint &a, const FixedPoint &pb)=0
Ciphertext-Plaintext subtraction: result = a - pb.
virtual void Swap_if(const BinaryDigit &cond, FixedPoint &a, FixedPoint &b)=0
Conditional swap of two FixedPoint values if 'cond' is true.
virtual BinaryDigit Gate_MAJ(const BinaryDigit &a, const BinaryDigit &b, const BinaryDigit &c)=0
Majority gate: returns true if at least two inputs are true.
virtual FixedPoint Sub(const FixedPoint &a, const FixedPoint &b)=0
Subtraction: result = a - b.
virtual FixedPoint CPSubCNC(const FixedPoint &a, const FixedPoint &pb)=0
Ciphertext-Plaintext subtraction with carry, no update.
virtual FixedPoint AddCNC(const FixedPoint &a, const FixedPoint &b)=0
Addition with Carry, but without updating internal carry.
virtual BinaryDigit GetCarry()
Returns the current internal carry state.
virtual FixedPoint SubCNC(const FixedPoint &a, const FixedPoint &b)=0
Subtraction with Carry, but without updating internal carry.
virtual FixedPoint PBoothsMul(const FixedPoint &a, const FixedPoint &pb)=0
Multiplication using Booth's algorithm with a plaintext constant.
virtual BinaryDigit Constant0()
Returns encrypted constant 0 (false).
virtual FixedPoint PAddC(const FixedPoint &a, const FixedPoint &pb)=0
Plaintext addition with carry.
virtual FixedPoint PAddNC(const FixedPoint &a, const FixedPoint &pb)=0
Plaintext addition without updating internal carry.
virtual void SetCarry(BinaryDigit value)
Manually sets the internal carry to a specific value.
virtual FixedPoint Neg(const FixedPoint &a)=0
Arithmetic negation: result = -a.
virtual void SetCarry()
Sets the internal carry to an encrypted 'True' value.
virtual ~BaseALU()
Destroy the BaseALU object.
virtual FixedPoint CPSubNC(const FixedPoint &a, const FixedPoint &pb)=0
Ciphertext-Plaintext subtraction without updating internal carry.
virtual BinaryDigit Gate_XOR3(const BinaryDigit &a, const BinaryDigit &b, const BinaryDigit &c)=0
3-input XOR gate.
virtual FixedPoint SubNC(const FixedPoint &a, const FixedPoint &b)=0
Subtraction without updating internal carry.
virtual BinaryDigit Gate_MulAdd(const BinaryDigit &m, const BinaryDigit &a, const BinaryDigit &b, BinaryDigit *carry_out=nullptr)=0
Multiplies two digits and adds a third, optionally returning carry out.
virtual FixedPoint ToggleMSB(const FixedPoint &a)=0
Toggles the Most Significant Bit (useful for sign manipulation).
The core manager class for the ComputeFHE library.
Definition ComputeFHE.h:68
A proxy-object representing a single bit that can behave as either a ciphertext or a plaintext.
Definition FixedPoint.h:35
A bit-vector representation of an encrypted or plaintext word.
Definition FixedPoint.h:118