Skip to content

Commit

Permalink
Inplace binary operation class for the OpenFHE IR + sum and sub inpla…
Browse files Browse the repository at this point in the history
…ce operations
  • Loading branch information
ShirChoiEX committed Jan 29, 2025
1 parent 772dc4d commit 48daf13
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
26 changes: 26 additions & 0 deletions lib/Dialect/Openfhe/IR/OpenfheOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,21 @@ class Openfhe_BinaryOp<string mnemonic, list<Trait> traits = []>
let results = (outs NewLWECiphertext:$output);
}

class Openfhe_BinaryInPlaceOp<string mnemonic, list<Trait> traits = []>
: Openfhe_Op<mnemonic, traits # [
AllTypesMatch<["lhs", "rhs"]>,
]> {

let summary = "In-place binary operation for OpenFHE";

let arguments = (ins
Openfhe_CryptoContext:$cryptoContext,
NewLWECiphertext:$lhs,
NewLWECiphertext:$rhs
);
}


def GenParamsOp : Openfhe_Op<"gen_params"> {
let description = [{
Generates the parameters for the OpenFHE scheme.
Expand Down Expand Up @@ -156,6 +171,17 @@ def DecryptOp : Openfhe_Op<"decrypt", [Pure]> {
def AddOp : Openfhe_BinaryOp<"add"> { let summary = "OpenFHE add operation of two ciphertexts."; }
def SubOp : Openfhe_BinaryOp<"sub"> { let summary = "OpenFHE sub operation of two ciphertexts."; }

// In-Place Addition
def AddInPlaceOp : Openfhe_BinaryInPlaceOp<"add_inplace"> {
let summary = "Performs in-place homomorphic addition, modifying lhs.";
}

// In-Place Subtraction
def SubInPlaceOp : Openfhe_BinaryInPlaceOp<"sub_inplace"> {
let summary = "Performs in-place homomorphic subtraction, modifying lhs.";
}


def AddPlainOp : Openfhe_Op<"add_plain",[
Pure,
AllTypesMatch<["ciphertext", "output"]>
Expand Down
16 changes: 15 additions & 1 deletion tests/Dialect/Openfhe/IR/ops.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,20 @@ module {
%out = openfhe.add %cc, %c1, %c2: (!cc, !ct, !ct) -> !ct
return
}
// CHECK-LABEL: func @test_inplace_add
func.func @test_inplace_add(%cc: !cc, %pt : !pt, %pk : !pk) {
%c1 = openfhe.encrypt %cc, %pt, %pk : (!cc, !pt, !pk) -> !ct
%c2 = openfhe.encrypt %cc, %pt, %pk : (!cc, !pt, !pk) -> !ct
openfhe.add_inplace %cc, %c1, %c2: (!cc, !ct, !ct) -> ()
return
}
// CHECK-LABEL: func @test_inplace_sub
func.func @test_inplace_sub(%cc: !cc, %pt : !pt, %pk : !pk) {
%c1 = openfhe.encrypt %cc, %pt, %pk : (!cc, !pt, !pk) -> !ct
%c2 = openfhe.encrypt %cc, %pt, %pk : (!cc, !pt, !pk) -> !ct
openfhe.sub_inplace %cc, %c1, %c2: (!cc, !ct, !ct) -> ()
return
}

// CHECK-LABEL: func @test_sub
func.func @test_sub(%cc : !cc, %pt : !pt, %pk: !pk) {
Expand Down Expand Up @@ -175,4 +189,4 @@ module {
openfhe.setup_bootstrap %cc {levelBudgetEncode = 3, levelBudgetDecode = 3}: (!cc) -> ()
return
}
}
}

0 comments on commit 48daf13

Please sign in to comment.