Skip to content

Commit d372bf7

Browse files
Merge pull request #1339 from ShirChoiEX:OpenfheInplaceOperations
PiperOrigin-RevId: 724369141
2 parents bd8d72e + e44d36b commit d372bf7

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

lib/Dialect/Openfhe/IR/OpenfheOps.td

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,21 @@ class Openfhe_BinaryOp<string mnemonic, list<Trait> traits = []>
5858
let results = (outs NewLWECiphertext:$output);
5959
}
6060

61+
class Openfhe_BinaryInPlaceOp<string mnemonic, list<Trait> traits = []>
62+
: Openfhe_Op<mnemonic, traits # [
63+
AllTypesMatch<["lhs", "rhs"]>,
64+
]> {
65+
66+
let summary = "In-place binary operation for OpenFHE";
67+
68+
let arguments = (ins
69+
Openfhe_CryptoContext:$cryptoContext,
70+
NewLWECiphertext:$lhs,
71+
NewLWECiphertext:$rhs
72+
);
73+
}
74+
75+
6176
def GenParamsOp : Openfhe_Op<"gen_params"> {
6277
let description = [{
6378
Generates the parameters for the OpenFHE scheme.
@@ -165,6 +180,17 @@ def SubOp : Openfhe_BinaryOp<"sub",
165180
let summary = "OpenFHE sub operation of two ciphertexts.";
166181
}
167182

183+
// In-Place Addition
184+
def AddInPlaceOp : Openfhe_BinaryInPlaceOp<"add_inplace"> {
185+
let summary = "Performs in-place homomorphic addition, modifying lhs.";
186+
}
187+
188+
// In-Place Subtraction
189+
def SubInPlaceOp : Openfhe_BinaryInPlaceOp<"sub_inplace"> {
190+
let summary = "Performs in-place homomorphic subtraction, modifying lhs.";
191+
}
192+
193+
168194
def AddPlainOp : Openfhe_Op<"add_plain",[
169195
Pure,
170196
AllTypesMatch<["ciphertext", "output"]>

tests/Dialect/Openfhe/IR/ops.mlir

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,20 @@ module {
7070
%out = openfhe.add %cc, %c1, %c2: (!cc, !ct, !ct) -> !ct
7171
return
7272
}
73+
// CHECK-LABEL: func @test_inplace_add
74+
func.func @test_inplace_add(%cc: !cc, %pt : !pt, %pk : !pk) {
75+
%c1 = openfhe.encrypt %cc, %pt, %pk : (!cc, !pt, !pk) -> !ct
76+
%c2 = openfhe.encrypt %cc, %pt, %pk : (!cc, !pt, !pk) -> !ct
77+
openfhe.add_inplace %cc, %c1, %c2: (!cc, !ct, !ct) -> ()
78+
return
79+
}
80+
// CHECK-LABEL: func @test_inplace_sub
81+
func.func @test_inplace_sub(%cc: !cc, %pt : !pt, %pk : !pk) {
82+
%c1 = openfhe.encrypt %cc, %pt, %pk : (!cc, !pt, !pk) -> !ct
83+
%c2 = openfhe.encrypt %cc, %pt, %pk : (!cc, !pt, !pk) -> !ct
84+
openfhe.sub_inplace %cc, %c1, %c2: (!cc, !ct, !ct) -> ()
85+
return
86+
}
7387

7488
// CHECK-LABEL: func @test_sub
7589
func.func @test_sub(%cc : !cc, %pt : !pt, %pk: !pk) {

0 commit comments

Comments
 (0)