From 8f73863e001eea0efea72b2596190d4b2d547d93 Mon Sep 17 00:00:00 2001 From: Quentin Bourgerie Date: Wed, 16 Jun 2021 12:04:41 +0200 Subject: [PATCH] fix(compiler): Fixing MidLFHE.h_add verifier and adding tests --- .../lib/Dialect/MidLFHE/IR/MidLFHEOps.cpp | 20 +++++++++++-------- .../MidLFHE/op_h_add_err_dimension.mlir | 7 +++++++ .../MidLFHE/op_h_add_err_log2StdDev.mlir | 7 +++++++ .../tests/Dialect/MidLFHE/op_h_add_err_p.mlir | 7 +++++++ .../MidLFHE/op_h_add_err_padding1.mlir | 7 +++++++ .../MidLFHE/op_h_add_err_padding2.mlir | 7 +++++++ .../MidLFHE/op_h_add_err_padding3.mlir | 7 +++++++ .../MidLFHE/op_h_add_err_phantomBits.mlir | 7 +++++++ .../MidLFHE/op_h_add_err_polynomialSize.mlir | 7 +++++++ .../MidLFHE/op_h_add_err_scalingFactor.mlir | 7 +++++++ 10 files changed, 75 insertions(+), 8 deletions(-) create mode 100644 compiler/tests/Dialect/MidLFHE/op_h_add_err_dimension.mlir create mode 100644 compiler/tests/Dialect/MidLFHE/op_h_add_err_log2StdDev.mlir create mode 100644 compiler/tests/Dialect/MidLFHE/op_h_add_err_p.mlir create mode 100644 compiler/tests/Dialect/MidLFHE/op_h_add_err_padding1.mlir create mode 100644 compiler/tests/Dialect/MidLFHE/op_h_add_err_padding2.mlir create mode 100644 compiler/tests/Dialect/MidLFHE/op_h_add_err_padding3.mlir create mode 100644 compiler/tests/Dialect/MidLFHE/op_h_add_err_phantomBits.mlir create mode 100644 compiler/tests/Dialect/MidLFHE/op_h_add_err_polynomialSize.mlir create mode 100644 compiler/tests/Dialect/MidLFHE/op_h_add_err_scalingFactor.mlir diff --git a/compiler/lib/Dialect/MidLFHE/IR/MidLFHEOps.cpp b/compiler/lib/Dialect/MidLFHE/IR/MidLFHEOps.cpp index 7d126e910..6f726ecdb 100644 --- a/compiler/lib/Dialect/MidLFHE/IR/MidLFHEOps.cpp +++ b/compiler/lib/Dialect/MidLFHE/IR/MidLFHEOps.cpp @@ -101,6 +101,10 @@ bool verifyHAddResultPadding(::mlir::OpState &op, GLWECipherTextType &inA, if (inA.getPaddingBits() == -1 && inB.getPaddingBits() == -1) { return true; } + if (inA.getPaddingBits() != inB.getPaddingBits()) { + emitOpErrorForIncompatibleGLWEParameter(op, "padding"); + return false; + } return verifyAddResultPadding(op, inA, out); } @@ -137,25 +141,25 @@ bool verifyHAddSameGLWEParameter(::mlir::OpState &op, GLWECipherTextType &inA, emitOpErrorForIncompatibleGLWEParameter(op, "dimension"); return false; } - if (inA.getPolynomialSize() != inB.getPolynomialSize() && + if (inA.getPolynomialSize() != inB.getPolynomialSize() || inA.getPolynomialSize() != out.getPolynomialSize()) { emitOpErrorForIncompatibleGLWEParameter(op, "polynomialSize"); return false; } - if (inA.getBits() != inB.getBits() && inA.getBits() != out.getBits()) { + if (inA.getBits() != inB.getBits() || inA.getBits() != out.getBits()) { emitOpErrorForIncompatibleGLWEParameter(op, "bits"); return false; } - if (inA.getP() != inB.getP() && inA.getP() != out.getP()) { + if (inA.getP() != inB.getP() || inA.getP() != out.getP()) { emitOpErrorForIncompatibleGLWEParameter(op, "p"); return false; } - if (inA.getPhantomBits() != inB.getPhantomBits() && + if (inA.getPhantomBits() != inB.getPhantomBits() || inA.getPhantomBits() != out.getPhantomBits()) { emitOpErrorForIncompatibleGLWEParameter(op, "phantomBits"); return false; } - if (inA.getScalingFactor() && inB.getScalingFactor() && + if (inA.getScalingFactor() != inB.getScalingFactor() || inA.getScalingFactor() != out.getScalingFactor()) { emitOpErrorForIncompatibleGLWEParameter(op, "scalingFactor"); return false; @@ -167,15 +171,15 @@ bool verifyHAddSameGLWEParameter(::mlir::OpState &op, GLWECipherTextType &inA, GLWECipherTextType inA = op.a().getType().cast(); GLWECipherTextType inB = op.b().getType().cast(); GLWECipherTextType out = op.getResult().getType().cast(); + if (!verifyHAddSameGLWEParameter(op, inA, inB, out)) { + return ::mlir::failure(); + } if (!verifyHAddResultPadding(op, inA, inB, out)) { return ::mlir::failure(); } if (!verifyHAddResultLog2StdDev(op, inA, inB, out)) { return ::mlir::failure(); } - if (!verifyHAddSameGLWEParameter(op, inA, inB, out)) { - return ::mlir::failure(); - } return ::mlir::success(); } diff --git a/compiler/tests/Dialect/MidLFHE/op_h_add_err_dimension.mlir b/compiler/tests/Dialect/MidLFHE/op_h_add_err_dimension.mlir new file mode 100644 index 000000000..ce7001b2d --- /dev/null +++ b/compiler/tests/Dialect/MidLFHE/op_h_add_err_dimension.mlir @@ -0,0 +1,7 @@ +// RUN: not zamacompiler %s 2>&1| FileCheck %s + +// CHECK-LABEL: error: should have the same GLWE dimension parameter +func @add(%arg0: !MidLFHE.glwe<{1024,12,64}{0,7,0,50,-25}>, %arg1: !MidLFHE.glwe<{2048,12,64}{0,7,0,50,-25}>) -> !MidLFHE.glwe<{1024,12,64}{0,7,0,50,-24}> { + %0 = "MidLFHE.h_add"(%arg0, %arg1): (!MidLFHE.glwe<{1024,12,64}{0,7,0,50,-25}>, !MidLFHE.glwe<{2048,12,64}{0,7,0,50,-25}>) -> (!MidLFHE.glwe<{1024,12,64}{0,7,0,50,-24}>) + return %0: !MidLFHE.glwe<{1024,12,64}{0,7,0,50,-24}> +} diff --git a/compiler/tests/Dialect/MidLFHE/op_h_add_err_log2StdDev.mlir b/compiler/tests/Dialect/MidLFHE/op_h_add_err_log2StdDev.mlir new file mode 100644 index 000000000..1018ab9a9 --- /dev/null +++ b/compiler/tests/Dialect/MidLFHE/op_h_add_err_log2StdDev.mlir @@ -0,0 +1,7 @@ +// RUN: not zamacompiler %s 2>&1| FileCheck %s + +// CHECK-LABEL: error: 'MidLFHE.h_add' op has unexpected log2StdDev parameter of its GLWE result, expected:-22 +func @add_plain(%arg0: !MidLFHE.glwe<{1024,12,64}{0,7,0,57,-25}>, %arg1: !MidLFHE.glwe<{1024,12,64}{0,7,0,57,-23}>) -> !MidLFHE.glwe<{1024,12,64}{0,7,0,57,-29}> { + %1 = "MidLFHE.h_add"(%arg0, %arg1): (!MidLFHE.glwe<{1024,12,64}{0,7,0,57,-25}>, !MidLFHE.glwe<{1024,12,64}{0,7,0,57,-23}>) -> (!MidLFHE.glwe<{1024,12,64}{0,7,0,57,-29}>) + return %1: !MidLFHE.glwe<{1024,12,64}{0,7,0,57,-29}> +} \ No newline at end of file diff --git a/compiler/tests/Dialect/MidLFHE/op_h_add_err_p.mlir b/compiler/tests/Dialect/MidLFHE/op_h_add_err_p.mlir new file mode 100644 index 000000000..9ec49f0a6 --- /dev/null +++ b/compiler/tests/Dialect/MidLFHE/op_h_add_err_p.mlir @@ -0,0 +1,7 @@ +// RUN: not zamacompiler %s 2>&1| FileCheck %s + +// CHECK-LABEL: error: should have the same GLWE p parameter +func @add(%arg0: !MidLFHE.glwe<{1024,12,64}{0,7,0,50,-25}>, %arg1: !MidLFHE.glwe<{1024,12,64}{0,8,0,50,-25}>) -> !MidLFHE.glwe<{1024,12,64}{0,7,0,50,-24}> { + %0 = "MidLFHE.h_add"(%arg0, %arg1): (!MidLFHE.glwe<{1024,12,64}{0,7,0,50,-25}>, !MidLFHE.glwe<{1024,12,64}{0,8,0,50,-25}>) -> (!MidLFHE.glwe<{1024,12,64}{0,7,0,50,-24}>) + return %0: !MidLFHE.glwe<{1024,12,64}{0,7,0,50,-24}> +} diff --git a/compiler/tests/Dialect/MidLFHE/op_h_add_err_padding1.mlir b/compiler/tests/Dialect/MidLFHE/op_h_add_err_padding1.mlir new file mode 100644 index 000000000..4a07f3720 --- /dev/null +++ b/compiler/tests/Dialect/MidLFHE/op_h_add_err_padding1.mlir @@ -0,0 +1,7 @@ +// RUN: not zamacompiler %s 2>&1| FileCheck %s + +// CHECK-LABEL: error: should have the same GLWE padding parameter +func @add(%arg0: !MidLFHE.glwe<{1024,12,64}{0,7,0,50,-25}>, %arg1: !MidLFHE.glwe<{1024,12,64}{1,7,0,50,-25}>) -> !MidLFHE.glwe<{1024,12,64}{0,7,0,50,-24}> { + %0 = "MidLFHE.h_add"(%arg0, %arg1): (!MidLFHE.glwe<{1024,12,64}{0,7,0,50,-25}>, !MidLFHE.glwe<{1024,12,64}{1,7,0,50,-25}>) -> (!MidLFHE.glwe<{1024,12,64}{0,7,0,50,-24}>) + return %0: !MidLFHE.glwe<{1024,12,64}{0,7,0,50,-24}> +} diff --git a/compiler/tests/Dialect/MidLFHE/op_h_add_err_padding2.mlir b/compiler/tests/Dialect/MidLFHE/op_h_add_err_padding2.mlir new file mode 100644 index 000000000..cbf0b47e7 --- /dev/null +++ b/compiler/tests/Dialect/MidLFHE/op_h_add_err_padding2.mlir @@ -0,0 +1,7 @@ +// RUN: not zamacompiler %s 2>&1| FileCheck %s + +// CHECK-LABEL: error: the result should have one less padding bit than the input +func @add(%arg0: !MidLFHE.glwe<{1024,12,64}{2,7,0,50,-25}>, %arg1: !MidLFHE.glwe<{1024,12,64}{2,7,0,50,-25}>) -> !MidLFHE.glwe<{1024,12,64}{0,7,0,50,-24}> { + %0 = "MidLFHE.h_add"(%arg0, %arg1): (!MidLFHE.glwe<{1024,12,64}{2,7,0,50,-25}>, !MidLFHE.glwe<{1024,12,64}{2,7,0,50,-25}>) -> (!MidLFHE.glwe<{1024,12,64}{0,7,0,50,-24}>) + return %0: !MidLFHE.glwe<{1024,12,64}{0,7,0,50,-24}> +} diff --git a/compiler/tests/Dialect/MidLFHE/op_h_add_err_padding3.mlir b/compiler/tests/Dialect/MidLFHE/op_h_add_err_padding3.mlir new file mode 100644 index 000000000..f4d9772e9 --- /dev/null +++ b/compiler/tests/Dialect/MidLFHE/op_h_add_err_padding3.mlir @@ -0,0 +1,7 @@ +// RUN: not zamacompiler %s 2>&1| FileCheck %s + +// CHECK-LABEL: error: the result shoud have 0 paddingBits has input has 0 paddingBits +func @add(%arg0: !MidLFHE.glwe<{1024,12,64}{0,7,0,50,-25}>, %arg1: !MidLFHE.glwe<{1024,12,64}{0,7,0,50,-25}>) -> !MidLFHE.glwe<{1024,12,64}{1,7,0,50,-24}> { + %0 = "MidLFHE.h_add"(%arg0, %arg1): (!MidLFHE.glwe<{1024,12,64}{0,7,0,50,-25}>, !MidLFHE.glwe<{1024,12,64}{0,7,0,50,-25}>) -> (!MidLFHE.glwe<{1024,12,64}{1,7,0,50,-24}>) + return %0: !MidLFHE.glwe<{1024,12,64}{1,7,0,50,-24}> +} diff --git a/compiler/tests/Dialect/MidLFHE/op_h_add_err_phantomBits.mlir b/compiler/tests/Dialect/MidLFHE/op_h_add_err_phantomBits.mlir new file mode 100644 index 000000000..227aba61e --- /dev/null +++ b/compiler/tests/Dialect/MidLFHE/op_h_add_err_phantomBits.mlir @@ -0,0 +1,7 @@ +// RUN: not zamacompiler %s 2>&1| FileCheck %s + +// CHECK-LABEL: should have the same GLWE phantomBits parameter +func @add(%arg0: !MidLFHE.glwe<{1024,12,64}{0,7,0,50,-25}>, %arg1: !MidLFHE.glwe<{1024,12,64}{0,7,1,50,-25}>) -> !MidLFHE.glwe<{1024,12,64}{0,7,0,50,-24}> { + %0 = "MidLFHE.h_add"(%arg0, %arg1): (!MidLFHE.glwe<{1024,12,64}{0,7,0,50,-25}>, !MidLFHE.glwe<{1024,12,64}{0,7,1,50,-25}>) -> (!MidLFHE.glwe<{1024,12,64}{0,7,0,50,-24}>) + return %0: !MidLFHE.glwe<{1024,12,64}{0,7,0,50,-24}> +} diff --git a/compiler/tests/Dialect/MidLFHE/op_h_add_err_polynomialSize.mlir b/compiler/tests/Dialect/MidLFHE/op_h_add_err_polynomialSize.mlir new file mode 100644 index 000000000..202dd2fe9 --- /dev/null +++ b/compiler/tests/Dialect/MidLFHE/op_h_add_err_polynomialSize.mlir @@ -0,0 +1,7 @@ +// RUN: not zamacompiler %s 2>&1| FileCheck %s + +// CHECK-LABEL: should have the same GLWE polynomialSize parameter +func @add(%arg0: !MidLFHE.glwe<{1024,12,64}{0,7,0,50,-25}>, %arg1: !MidLFHE.glwe<{1024,10,64}{0,7,0,50,-25}>) -> !MidLFHE.glwe<{1024,12,64}{0,7,0,50,-24}> { + %0 = "MidLFHE.h_add"(%arg0, %arg1): (!MidLFHE.glwe<{1024,12,64}{0,7,0,50,-25}>, !MidLFHE.glwe<{1024,10,64}{0,7,0,50,-25}>) -> (!MidLFHE.glwe<{1024,12,64}{0,7,0,50,-24}>) + return %0: !MidLFHE.glwe<{1024,12,64}{0,7,0,50,-24}> +} diff --git a/compiler/tests/Dialect/MidLFHE/op_h_add_err_scalingFactor.mlir b/compiler/tests/Dialect/MidLFHE/op_h_add_err_scalingFactor.mlir new file mode 100644 index 000000000..10b9418cf --- /dev/null +++ b/compiler/tests/Dialect/MidLFHE/op_h_add_err_scalingFactor.mlir @@ -0,0 +1,7 @@ +// RUN: not zamacompiler %s 2>&1| FileCheck %s + +// CHECK-LABEL: should have the same GLWE scalingFactor parameter +func @add(%arg0: !MidLFHE.glwe<{1024,12,64}{0,7,0,50,-25}>, %arg1: !MidLFHE.glwe<{1024,12,64}{0,7,0,50,-25}>) -> !MidLFHE.glwe<{1024,12,64}{0,7,0,49,-25}> { + %1 = "MidLFHE.h_add"(%arg0, %arg1): (!MidLFHE.glwe<{1024,12,64}{0,7,0,50,-25}>, !MidLFHE.glwe<{1024,12,64}{0,7,0,50,-25}>) -> (!MidLFHE.glwe<{1024,12,64}{0,7,0,49,-25}>) + return %1: !MidLFHE.glwe<{1024,12,64}{0,7,0,49,-25}> +} \ No newline at end of file