mirror of
https://github.com/zama-ai/concrete.git
synced 2026-04-17 03:00:54 -04:00
enhance(compiler): MidLFHE has now only one GLWE type that take all cryptographic parameters
This commit is contained in:
@@ -19,8 +19,8 @@ class MidLFHE_Op<string mnemonic, list<OpTrait> traits = []> :
|
||||
Op<MidLFHE_Dialect, mnemonic, traits>;
|
||||
|
||||
def AddPlainOp : MidLFHE_Op<"add_plain"> {
|
||||
let arguments = (ins CipherTextType:$a, AnyInteger:$b);
|
||||
let results = (outs CipherTextType);
|
||||
let arguments = (ins GLWECipherTextType:$a, AnyInteger:$b);
|
||||
let results = (outs GLWECipherTextType);
|
||||
|
||||
let builders = [
|
||||
OpBuilder<(ins "Value":$a, "Value":$b), [{
|
||||
@@ -30,8 +30,8 @@ def AddPlainOp : MidLFHE_Op<"add_plain"> {
|
||||
}
|
||||
|
||||
def MulPlainOp : MidLFHE_Op<"mul_plain"> {
|
||||
let arguments = (ins CipherTextType:$a, AnyInteger:$b);
|
||||
let results = (outs CipherTextType);
|
||||
let arguments = (ins GLWECipherTextType:$a, AnyInteger:$b);
|
||||
let results = (outs GLWECipherTextType);
|
||||
|
||||
let builders = [
|
||||
OpBuilder<(ins "Value":$a, "Value":$b), [{
|
||||
@@ -40,29 +40,6 @@ def MulPlainOp : MidLFHE_Op<"mul_plain"> {
|
||||
];
|
||||
}
|
||||
|
||||
def PBSRegion : Region<
|
||||
CPred<"::mlir::zamalang::predPBSRegion($_self)">,
|
||||
"pbs region needs one block with one any integer argument">;
|
||||
|
||||
def PBSOp : MidLFHE_Op<"pbs"> {
|
||||
let arguments = (ins CipherTextType:$x, I32Attr:$big_n, I32Attr:$log_noise, I32Attr:$base_log, I32Attr:$level);
|
||||
let results = (outs CipherTextType:$result);
|
||||
let regions = (region PBSRegion:$region);
|
||||
}
|
||||
|
||||
def ReturnOp : MidLFHE_Op<"pbs_return", [NoSideEffect, ReturnLike, Terminator]> {
|
||||
let summary = "terminator of pbs block";
|
||||
let arguments = (ins AnyInteger);
|
||||
let builders = [OpBuilder<(ins), [{ /* nothing to do */ }]>];
|
||||
}
|
||||
|
||||
|
||||
def KeySwitchOp : MidLFHE_Op<"keyswitch"> {
|
||||
let arguments = (ins KeySwitchingKeyType:$ks, CipherTextType:$ct, I32Attr:$base_log, I32Attr:$level);
|
||||
let results = (outs CipherTextType);
|
||||
}
|
||||
|
||||
|
||||
def HAddOp : MidLFHE_Op<"h_add"> {
|
||||
let arguments = (ins GLWECipherTextType:$a, GLWECipherTextType:$b);
|
||||
let results = (outs GLWECipherTextType);
|
||||
@@ -87,4 +64,20 @@ def HMulOp : MidLFHE_Op<"h_mul"> {
|
||||
}
|
||||
|
||||
|
||||
def PBSRegion : Region<
|
||||
CPred<"::mlir::zamalang::predPBSRegion($_self)">,
|
||||
"pbs region needs one block with one any integer argument">;
|
||||
|
||||
def PBSOp : MidLFHE_Op<"pbs"> {
|
||||
let arguments = (ins GLWECipherTextType:$x, I32Attr:$big_n, I32Attr:$log_noise, I32Attr:$base_log, I32Attr:$level);
|
||||
let results = (outs GLWECipherTextType:$result);
|
||||
let regions = (region PBSRegion:$region);
|
||||
}
|
||||
|
||||
def ReturnOp : MidLFHE_Op<"pbs_return", [NoSideEffect, ReturnLike, Terminator]> {
|
||||
let summary = "terminator of pbs block";
|
||||
let arguments = (ins AnyInteger);
|
||||
let builders = [OpBuilder<(ins), [{ /* nothing to do */ }]>];
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -7,35 +7,6 @@ include "zamalang/Dialect/MidLFHE/IR/MidLFHEDialect.td"
|
||||
|
||||
class MidLFHE_Type<string name> : TypeDef<MidLFHE_Dialect, name> { }
|
||||
|
||||
def LWECipherTextType : MidLFHE_Type<"LWECipherText"> {
|
||||
let mnemonic = "lwe";
|
||||
|
||||
let summary = "An LWE cipher text";
|
||||
|
||||
let description = [{
|
||||
An LWE cipher text
|
||||
}];
|
||||
|
||||
let parameters = (ins "unsigned":$size);
|
||||
|
||||
// We define the printer inline.
|
||||
let printer = [{
|
||||
$_printer << "lwe<" << getImpl()->size << ">";
|
||||
}];
|
||||
|
||||
// The parser is defined here also.
|
||||
let parser = [{
|
||||
if ($_parser.parseLess())
|
||||
return Type();
|
||||
int size;
|
||||
if ($_parser.parseInteger(size))
|
||||
return Type();
|
||||
if ($_parser.parseGreater())
|
||||
return Type();
|
||||
return get($_ctxt, size);
|
||||
}];
|
||||
}
|
||||
|
||||
def GLWECipherTextType : MidLFHE_Type<"GLWECipherText"> {
|
||||
let mnemonic = "glwe";
|
||||
|
||||
@@ -45,121 +16,114 @@ def GLWECipherTextType : MidLFHE_Type<"GLWECipherText"> {
|
||||
An GLWE cipher text
|
||||
}];
|
||||
|
||||
let parameters = (ins "unsigned":$size, "unsigned":$N);
|
||||
let parameters = (ins
|
||||
// The size of the mask
|
||||
"signed":$dimension,
|
||||
// Size of the polynome
|
||||
"signed":$polynomialSize,
|
||||
// Number of bits of the ciphertext
|
||||
"signed":$bits,
|
||||
// Number of bits for padding
|
||||
"signed":$paddingBits,
|
||||
// The precision of the encrypted value
|
||||
"signed":$p,
|
||||
// Number of 'phantom' bits on the right of the message
|
||||
"signed":$phantomRight,
|
||||
// The scaling factor
|
||||
"signed":$scalingFactor,
|
||||
// Log2 of the standard derivation
|
||||
"signed":$log2StdDev
|
||||
);
|
||||
|
||||
// We define the printer inline.
|
||||
let printer = [{
|
||||
$_printer << "glwe<" << getImpl()->size << "," << getImpl()->N << ">";
|
||||
$_printer << "glwe"
|
||||
<< "<{";
|
||||
if (getImpl()->dimension == -1) $_printer << "_";
|
||||
else $_printer << getImpl()->dimension;
|
||||
$_printer << ",";
|
||||
if (getImpl()->polynomialSize == -1) $_printer << "_";
|
||||
else $_printer << getImpl()->polynomialSize;
|
||||
$_printer << ",";
|
||||
if (getImpl()->bits == -1) $_printer << "_";
|
||||
else $_printer << getImpl()->bits;
|
||||
$_printer << "}";
|
||||
$_printer << "{";
|
||||
if (getImpl()->paddingBits == -1) $_printer << "_";
|
||||
else $_printer << getImpl()->paddingBits;
|
||||
$_printer << ",";
|
||||
if (getImpl()->p == -1) $_printer << "_";
|
||||
else $_printer << getImpl()->p;
|
||||
$_printer << ",";
|
||||
if (getImpl()->phantomRight == -1) $_printer << "_";
|
||||
else $_printer << getImpl()->phantomRight;
|
||||
$_printer << ",";
|
||||
if (getImpl()->scalingFactor == -1) $_printer << "_";
|
||||
else $_printer << getImpl()->scalingFactor;
|
||||
$_printer << ",";
|
||||
if (getImpl()->log2StdDev == -1) $_printer << "_";
|
||||
else $_printer << getImpl()->log2StdDev;
|
||||
$_printer << "}>";
|
||||
}];
|
||||
|
||||
// The parser is defined here also.
|
||||
let parser = [{
|
||||
if ($_parser.parseLess())
|
||||
return Type();
|
||||
int size;
|
||||
if ($_parser.parseInteger(size))
|
||||
return Type();
|
||||
if ($_parser.parseComma())
|
||||
return Type();
|
||||
int N;
|
||||
if ($_parser.parseInteger(N))
|
||||
return Type();
|
||||
if ($_parser.parseGreater())
|
||||
return Type();
|
||||
return get($_ctxt, size, N);
|
||||
if ($_parser.parseLess())
|
||||
return Type();
|
||||
|
||||
// First parameters block
|
||||
if ($_parser.parseLBrace())
|
||||
return Type();
|
||||
int dimension = -1;
|
||||
if ($_parser.parseOptionalKeyword("_") && $_parser.parseInteger(dimension))
|
||||
return Type();
|
||||
if ($_parser.parseComma())
|
||||
return Type();
|
||||
int polynomialSize = -1;
|
||||
if ($_parser.parseOptionalKeyword("_") && $_parser.parseInteger(polynomialSize))
|
||||
return Type();
|
||||
if ($_parser.parseComma())
|
||||
return Type();
|
||||
int bits = -1;
|
||||
if ($_parser.parseOptionalKeyword("_") && $_parser.parseInteger(bits))
|
||||
return Type();
|
||||
if ($_parser.parseRBrace())
|
||||
return Type();
|
||||
|
||||
// Next parameters block
|
||||
if ($_parser.parseLBrace())
|
||||
return Type();
|
||||
int paddingBits = -1;
|
||||
if ($_parser.parseOptionalKeyword("_") && $_parser.parseInteger(paddingBits))
|
||||
return Type();
|
||||
if ($_parser.parseComma())
|
||||
return Type();
|
||||
int p = -1;
|
||||
if ($_parser.parseInteger(p))
|
||||
return Type();
|
||||
if ($_parser.parseComma())
|
||||
return Type();
|
||||
int phantomRight = -1;
|
||||
if ($_parser.parseOptionalKeyword("_") && $_parser.parseInteger(phantomRight))
|
||||
return Type();
|
||||
if ($_parser.parseComma())
|
||||
return Type();
|
||||
int scalingFactor = -1;
|
||||
if ($_parser.parseOptionalKeyword("_") && $_parser.parseInteger(scalingFactor))
|
||||
return Type();
|
||||
if ($_parser.parseComma())
|
||||
return Type();
|
||||
int log2StdDev = -1;
|
||||
if ($_parser.parseOptionalKeyword("_") && $_parser.parseInteger(log2StdDev))
|
||||
return Type();
|
||||
if ($_parser.parseRBrace())
|
||||
return Type();
|
||||
|
||||
if ($_parser.parseGreater())
|
||||
return Type();
|
||||
|
||||
return get($_ctxt, dimension, polynomialSize, bits, paddingBits, p, phantomRight, scalingFactor, log2StdDev);
|
||||
}];
|
||||
}
|
||||
|
||||
def GGSWCipherTextType : MidLFHE_Type<"GGSWCipherText"> {
|
||||
let mnemonic = "ggsw";
|
||||
|
||||
let summary = "An GGSW cipher text";
|
||||
|
||||
let description = [{
|
||||
An GGSW cipher text
|
||||
}];
|
||||
|
||||
let parameters = (ins "unsigned":$size, "unsigned":$N, "unsigned": $level, "unsigned":$base_log);
|
||||
|
||||
// We define the printer inline.
|
||||
let printer = [{
|
||||
$_printer << "ggsw<" << getImpl()->size << "," << getImpl()->N << "," << getImpl()->level << "," << getImpl()->base_log << ">";
|
||||
}];
|
||||
|
||||
// The parser is defined here also.
|
||||
let parser = [{
|
||||
if ($_parser.parseLess())
|
||||
return Type();
|
||||
int size;
|
||||
if ($_parser.parseInteger(size))
|
||||
return Type();
|
||||
if ($_parser.parseComma())
|
||||
return Type();
|
||||
int N;
|
||||
if ($_parser.parseInteger(N))
|
||||
return Type();
|
||||
if ($_parser.parseComma())
|
||||
return Type();
|
||||
int level;
|
||||
if ($_parser.parseInteger(level))
|
||||
return Type();
|
||||
if ($_parser.parseComma())
|
||||
return Type();
|
||||
int base_log;
|
||||
if ($_parser.parseInteger(base_log))
|
||||
return Type();
|
||||
if ($_parser.parseGreater())
|
||||
return Type();
|
||||
return get($_ctxt, size, N, level, base_log);
|
||||
}];
|
||||
}
|
||||
|
||||
def AnyCipherTextType : MidLFHE_Type<"AnyCipherText"> {
|
||||
let mnemonic = "ciphertext";
|
||||
|
||||
let summary = "Any cipher text";
|
||||
|
||||
let description = [{
|
||||
Any cipher text
|
||||
}];
|
||||
|
||||
// We define the printer inline.
|
||||
let printer = [{
|
||||
$_printer << "ciphertext";
|
||||
}];
|
||||
|
||||
// The parser is defined here also.
|
||||
let parser = [{
|
||||
return get($_ctxt);
|
||||
}];
|
||||
}
|
||||
|
||||
def KeySwitchingKeyType : MidLFHE_Type<"KeySwitchingKey"> {
|
||||
let mnemonic = "ksk";
|
||||
|
||||
let summary = "A KeySwitching key";
|
||||
|
||||
let description = [{
|
||||
A KeySwitching key
|
||||
}];
|
||||
|
||||
// We define the printer inline.
|
||||
let printer = [{
|
||||
$_printer << "ksk";
|
||||
}];
|
||||
|
||||
// The parser is defined here also.
|
||||
let parser = [{
|
||||
return get($_ctxt);
|
||||
}];
|
||||
}
|
||||
|
||||
|
||||
def CipherTextType: TypeConstraint<Or<[
|
||||
LWECipherTextType.predicate,
|
||||
GLWECipherTextType.predicate,
|
||||
GGSWCipherTextType.predicate,
|
||||
AnyCipherTextType.predicate,
|
||||
]>, "ciphertext-like">;
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user