Skip to content

Commit

Permalink
fix(optimizer): fix leveled noise propagation
Browse files Browse the repository at this point in the history
  • Loading branch information
aPere3 committed Jul 15, 2024
1 parent 4d7fe18 commit 4ab336f
Show file tree
Hide file tree
Showing 17 changed files with 306 additions and 283 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ include "concretelang/Dialect/FHE/Interfaces/FHEInterfaces.td"
class FHE_Op<string mnemonic, list<Trait> traits = []> :
Op<FHE_Dialect, mnemonic, traits>;

def FHE_ZeroEintOp : FHE_Op<"zero", [Pure, ConstantNoise]> {
def FHE_ZeroEintOp : FHE_Op<"zero", [Pure, ZeroNoise]> {
let summary = "Returns a trivial encrypted integer of 0";

let description = [{
Expand All @@ -34,7 +34,7 @@ def FHE_ZeroEintOp : FHE_Op<"zero", [Pure, ConstantNoise]> {
let results = (outs FHE_AnyEncryptedInteger:$out);
}

def FHE_ZeroTensorOp : FHE_Op<"zero_tensor", [Pure, ConstantNoise]> {
def FHE_ZeroTensorOp : FHE_Op<"zero_tensor", [Pure, ZeroNoise]> {
let summary = "Creates a new tensor with all elements initialized to an encrypted zero.";

let description = [{
Expand All @@ -52,7 +52,7 @@ def FHE_ZeroTensorOp : FHE_Op<"zero_tensor", [Pure, ConstantNoise]> {
let results = (outs Type<And<[TensorOf<[FHE_AnyEncryptedInteger]>.predicate, HasStaticShapePred]>>:$tensor);
}

def FHE_AddEintIntOp : FHE_Op<"add_eint_int", [Pure, BinaryEintInt, DeclareOpInterfaceMethods<Binary>]> {
def FHE_AddEintIntOp : FHE_Op<"add_eint_int", [Pure, BinaryEintInt, AdditiveNoise, DeclareOpInterfaceMethods<Binary>]> {
let summary = "Adds an encrypted integer and a clear integer";

let description = [{
Expand Down Expand Up @@ -85,7 +85,7 @@ def FHE_AddEintIntOp : FHE_Op<"add_eint_int", [Pure, BinaryEintInt, DeclareOpInt
let hasFolder = 1;
}

def FHE_AddEintOp : FHE_Op<"add_eint", [Pure, BinaryEint, DeclareOpInterfaceMethods<BinaryEint>]> {
def FHE_AddEintOp : FHE_Op<"add_eint", [Pure, BinaryEint, AdditiveNoise, DeclareOpInterfaceMethods<BinaryEint>]> {
let summary = "Adds two encrypted integers";

let description = [{
Expand Down Expand Up @@ -117,7 +117,7 @@ def FHE_AddEintOp : FHE_Op<"add_eint", [Pure, BinaryEint, DeclareOpInterfaceMeth
let hasVerifier = 1;
}

def FHE_SubIntEintOp : FHE_Op<"sub_int_eint", [Pure, BinaryIntEint]> {
def FHE_SubIntEintOp : FHE_Op<"sub_int_eint", [Pure, BinaryIntEint, AdditiveNoise]> {
let summary = "Subtract an encrypted integer from a clear integer";

let description = [{
Expand Down Expand Up @@ -149,7 +149,7 @@ def FHE_SubIntEintOp : FHE_Op<"sub_int_eint", [Pure, BinaryIntEint]> {
let hasVerifier = 1;
}

def FHE_SubEintIntOp : FHE_Op<"sub_eint_int", [Pure, BinaryEintInt, DeclareOpInterfaceMethods<Binary>]> {
def FHE_SubEintIntOp : FHE_Op<"sub_eint_int", [Pure, BinaryEintInt, AdditiveNoise, DeclareOpInterfaceMethods<Binary>]> {
let summary = "Subtract a clear integer from an encrypted integer";

let description = [{
Expand Down Expand Up @@ -182,7 +182,7 @@ def FHE_SubEintIntOp : FHE_Op<"sub_eint_int", [Pure, BinaryEintInt, DeclareOpInt
let hasFolder = 1;
}

def FHE_SubEintOp : FHE_Op<"sub_eint", [Pure, BinaryEint, DeclareOpInterfaceMethods<BinaryEint>]> {
def FHE_SubEintOp : FHE_Op<"sub_eint", [Pure, BinaryEint, AdditiveNoise, DeclareOpInterfaceMethods<BinaryEint>]> {
let summary = "Subtract an encrypted integer from an encrypted integer";

let description = [{
Expand Down Expand Up @@ -214,7 +214,7 @@ def FHE_SubEintOp : FHE_Op<"sub_eint", [Pure, BinaryEint, DeclareOpInterfaceMeth
let hasVerifier = 1;
}

def FHE_NegEintOp : FHE_Op<"neg_eint", [Pure, UnaryEint, DeclareOpInterfaceMethods<UnaryEint>]> {
def FHE_NegEintOp : FHE_Op<"neg_eint", [Pure, UnaryEint, AdditiveNoise, DeclareOpInterfaceMethods<UnaryEint>]> {

let summary = "Negates an encrypted integer";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,22 @@ def ConstantNoise : OpInterface<"ConstantNoise"> {
let cppNamespace = "mlir::concretelang::FHE";
}

def ZeroNoise : OpInterface<"ZeroNoise"> {
let description = [{
An operation outputs a ciphertext with zero noise.
}];

let cppNamespace = "mlir::concretelang::FHE";
}

def AdditiveNoise : OpInterface<"AdditiveNoise"> {
let description = [{
An n-ary operation whose output noise is the unweighted sum of all input noises.
}];

let cppNamespace = "mlir::concretelang::FHE";
}

def UnaryEint : OpInterface<"UnaryEint"> {
let description = [{
A unary operation on scalars, with the operand encrypted.
Expand All @@ -63,7 +79,7 @@ def UnaryEint : OpInterface<"UnaryEint"> {
if (auto operandTy = dyn_cast<mlir::RankedTensorType>($_op->getOpOperand(0).get().getType())) {
return operandTy.getElementType();
} else return $_op->getOpOperand(0).get().getType();
}]>
}]>
];
}

Expand Down Expand Up @@ -124,8 +140,8 @@ def Binary : OpInterface<"Binary"> {
if (auto cstOp = llvm::dyn_cast_or_null<mlir::arith::ConstantOp>($_op->
getOpOperand(opNum).get().getDefiningOp()))
return cstOp->template getAttrOfType<mlir::DenseIntElementsAttr>("value").template getValues<llvm::APInt>();
else return {};
}]>,
else return {};
}]>,
];
}

Expand Down
Loading

0 comments on commit 4ab336f

Please sign in to comment.