fix: Fixing integer extension for plaintext encoding (close #847)

This commit is contained in:
Quentin Bourgerie
2022-12-20 16:17:47 +01:00
parent b1bc6730f9
commit 63334e138f
9 changed files with 28 additions and 8 deletions

View File

@@ -164,7 +164,7 @@ struct CrtOpPattern : public mlir::OpConversionPattern<T> {
mlir::Value writePlaintextCrtEncoding(mlir::Location location,
mlir::Value rawPlaintext,
mlir::PatternRewriter &rewriter) const {
mlir::Value castedPlaintext = rewriter.create<mlir::arith::ExtUIOp>(
mlir::Value castedPlaintext = rewriter.create<mlir::arith::ExtSIOp>(
location, rewriter.getI64Type(), rawPlaintext);
return rewriter.create<TFHE::EncodePlaintextWithCrtOp>(
location,

View File

@@ -122,7 +122,7 @@ struct ScalarOpPattern : public mlir::OpConversionPattern<T> {
int64_t encryptedWidth,
mlir::ConversionPatternRewriter &rewriter) const {
int64_t intShift = 64 - 1 - encryptedWidth;
mlir::Value castedInt = rewriter.create<mlir::arith::ExtUIOp>(
mlir::Value castedInt = rewriter.create<mlir::arith::ExtSIOp>(
location, rewriter.getIntegerType(64), rawPlaintext);
mlir::Value constantShiftOp = rewriter.create<mlir::arith::ConstantOp>(
location, rewriter.getI64IntegerAttr(intShift));

View File

@@ -2,7 +2,7 @@
// CHECK-LABEL: func.func @add_eint_int(%arg0: tensor<5x!TFHE.glwe<{_,_,_}{7}>>) -> tensor<5x!TFHE.glwe<{_,_,_}{7}>> {
// CHECK-NEXT: %c1_i8 = arith.constant 1 : i8
// CHECK-NEXT: %0 = arith.extui %c1_i8 : i8 to i64
// CHECK-NEXT: %0 = arith.extsi %c1_i8 : i8 to i64
// CHECK-NEXT: %1 = "TFHE.encode_plaintext_with_crt"(%0) {mods = [2, 3, 5, 7, 11], modsProd = 2310 : i64} : (i64) -> tensor<5xi64>
// CHECK-NEXT: %2 = bufferization.alloc_tensor() : tensor<5x!TFHE.glwe<{_,_,_}{7}>>
// CHECK-NEXT: %c0 = arith.constant 0 : index

View File

@@ -17,7 +17,7 @@
//CHECK-NEXT: %6 = tensor.extract %arg2[%arg5] : tensor<4xi3>
//CHECK-NEXT: %c0_0 = arith.constant 0 : index
//CHECK-NEXT: %7 = tensor.extract_slice %0[%arg3, %arg5, %arg7, %arg9, %c0_0] [1, 1, 1, 1, 5] [1, 1, 1, 1, 1] : tensor<100x4x15x15x5x!TFHE.glwe<{_,_,_}{2}>> to tensor<5x!TFHE.glwe<{_,_,_}{2}>>
//CHECK-NEXT: %8 = arith.extui %6 : i3 to i64
//CHECK-NEXT: %8 = arith.extsi %6 : i3 to i64
//CHECK-NEXT: %9 = "TFHE.encode_plaintext_with_crt"(%8) {mods = [2, 3, 5, 7, 11], modsProd = 2310 : i64} : (i64) -> tensor<5xi64>
//CHECK-NEXT: %10 = bufferization.alloc_tensor() : tensor<5x!TFHE.glwe<{_,_,_}{2}>>
//CHECK-NEXT: %c0_1 = arith.constant 0 : index

View File

@@ -2,7 +2,7 @@
// CHECK-LABEL: func.func @sub_int_eint(%arg0: tensor<5x!TFHE.glwe<{_,_,_}{7}>>) -> tensor<5x!TFHE.glwe<{_,_,_}{7}>> {
// CHECK-NEXT: %c1_i8 = arith.constant 1 : i8
// CHECK-NEXT: %0 = arith.extui %c1_i8 : i8 to i64
// CHECK-NEXT: %0 = arith.extsi %c1_i8 : i8 to i64
// CHECK-NEXT: %1 = "TFHE.encode_plaintext_with_crt"(%0) {mods = [2, 3, 5, 7, 11], modsProd = 2310 : i64} : (i64) -> tensor<5xi64>
// CHECK-NEXT: %2 = bufferization.alloc_tensor() : tensor<5x!TFHE.glwe<{_,_,_}{7}>>
// CHECK-NEXT: %c0 = arith.constant 0 : index

View File

@@ -3,7 +3,7 @@
// CHECK-LABEL: func.func @add_eint_int(%arg0: !TFHE.glwe<{_,_,_}{7}>) -> !TFHE.glwe<{_,_,_}{7}>
func.func @add_eint_int(%arg0: !FHE.eint<7>) -> !FHE.eint<7> {
// CHECK-NEXT: %c1_i8 = arith.constant 1 : i8
// CHECK-NEXT: %0 = arith.extui %c1_i8 : i8 to i64
// CHECK-NEXT: %0 = arith.extsi %c1_i8 : i8 to i64
// CHECK-NEXT: %c56_i64 = arith.constant 56 : i64
// CHECK-NEXT: %1 = arith.shli %0, %c56_i64 : i64
// CHECK-NEXT: %2 = "TFHE.add_glwe_int"(%arg0, %1) : (!TFHE.glwe<{_,_,_}{7}>, i64) -> !TFHE.glwe<{_,_,_}{7}>

View File

@@ -15,7 +15,7 @@
// CHECK-NEXT: %5 = scf.for %arg9 = %c0 to %c15 step %c1 iter_args(%arg10 = %arg8) -> (tensor<100x4x15x15x!TFHE.glwe<{_,_,_}{2}>>) {
// CHECK-NEXT: %6 = tensor.extract %arg2[%arg5] : tensor<4xi3>
// CHECK-NEXT: %7 = tensor.extract %0[%arg3, %arg5, %arg7, %arg9] : tensor<100x4x15x15x!TFHE.glwe<{_,_,_}{2}>>
// CHECK-NEXT: %8 = arith.extui %6 : i3 to i64
// CHECK-NEXT: %8 = arith.extsi %6 : i3 to i64
// CHECK-NEXT: %c61_i64 = arith.constant 61 : i64
// CHECK-NEXT: %9 = arith.shli %8, %c61_i64 : i64
// CHECK-NEXT: %10 = "TFHE.add_glwe_int"(%7, %9) : (!TFHE.glwe<{_,_,_}{2}>, i64) -> !TFHE.glwe<{_,_,_}{2}>

View File

@@ -3,7 +3,7 @@
// CHECK-LABEL: func.func @sub_int_eint(%arg0: !TFHE.glwe<{_,_,_}{7}>) -> !TFHE.glwe<{_,_,_}{7}>
func.func @sub_int_eint(%arg0: !FHE.eint<7>) -> !FHE.eint<7> {
// CHECK-NEXT: %c1_i8 = arith.constant 1 : i8
// CHECK-NEXT: %0 = arith.extui %c1_i8 : i8 to i64
// CHECK-NEXT: %0 = arith.extsi %c1_i8 : i8 to i64
// CHECK-NEXT: %c56_i64 = arith.constant 56 : i64
// CHECK-NEXT: %1 = arith.shli %0, %c56_i64 : i64
// CHECK-NEXT: %2 = "TFHE.sub_int_glwe"(%1, %arg0) : (i64, !TFHE.glwe<{_,_,_}{7}>) -> !TFHE.glwe<{_,_,_}{7}>

View File

@@ -184,6 +184,26 @@ tests:
- tensor: [9,4,7,7,10,9,9,4,7,7,10,9]
shape: [4,3]
---
# Minimized bug 847 (CRT)
# https://github.com/zama-ai/concrete-compiler-internal/issues/847
description: bug_847_crt
program: |
func.func @main(%arg0: !FHE.eint<16>) -> !FHE.eint<16> {
%c32768_i17 = arith.constant 32768 : i17
%0 = "FHE.sub_eint_int"(%arg0, %c32768_i17) : (!FHE.eint<16>, i17) -> !FHE.eint<16>
return %0 : !FHE.eint<16>
}
encoding: crt
tests:
- inputs:
- scalar: 32769
outputs:
- scalar: 1
- inputs:
- scalar: 32770
outputs:
- scalar: 2
---
description: boolean_and
program: |
func.func @main(%arg0: !FHE.ebool, %arg1: !FHE.ebool) -> !FHE.ebool {