Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion lib/Dialect/LWE/Conversions/LWEToPolynomial/LWEToPolynomial.td
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,20 @@ def LWEToPolynomial : Pass<"lwe-to-polynomial"> {
let summary = "Lower `lwe` to `polynomial` dialect.";

let description = [{
This pass lowers the `lwe` dialect to `polynomial` dialect.
This pass lowers the `lwe` dialect to `polynomial` dialect, implementing
polynomial arithmetic behind each ciphertext operation.

Types are lowered as follows:

- An RLWE ciphertext made of `k` polynomials, e.g., `(c_0, c_1)`, becomes
a `tensor<k x !polynomial.polynomial<...>>` over the ciphertext ring.
Tensors of ciphertexts become higher-dimensional tensors of polynomials.
- Plaintexts and ring elements become a single polynomial over their ring.
- Secret keys become a tensor of one polynomial `s`, and public keys a
tensor of two polynomials `(b, a) = (-a * s + t * e, a)`, where `t` is
the plaintext modulus.

(* example filepath=tests/Dialect/LWE/Conversions/lwe_to_polynomial/doctest.mlir *)
}];

let dependentDialects = [
Expand Down
37 changes: 37 additions & 0 deletions tests/Dialect/LWE/Conversions/lwe_to_polynomial/doctest.mlir
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// RUN: heir-opt --lwe-to-polynomial %s | FileCheck %s

!Zq = !mod_arith.int<1095233372161 : i64>
#key = #lwe.key<>
#ring_Q = #polynomial.ring<coefficientType = !rns.rns<!Zq>, polynomialModulus = <1 + x**8>>
#encoding = #lwe.full_crt_packing_encoding<scaling_factor = 0>
!ct = !lwe.lwe_ciphertext<encoding = #encoding, ciphertext_space = <ring = #ring_Q, encryption_type = lsb>, key = #key>
!ct_D3 = !lwe.lwe_ciphertext<encoding = #encoding, ciphertext_space = <ring = #ring_Q, encryption_type = lsb, size = 3>, key = #key>

// A ciphertext made of N polynomials is lowered to a tensor of N polynomials,
// and ciphertext arithmetic to polynomial arithmetic on its components.

// CHECK: func.func @add_and_mul(
// CHECK-SAME: %[[X:.*]]: tensor<2x![[POLY:.*]]>, %[[Y:.*]]: tensor<2x![[POLY]]>
// CHECK-SAME: -> tensor<3x![[POLY]]>
func.func @add_and_mul(%x: !ct, %y: !ct) -> !ct_D3 {
// CHECK: %[[SUM:.*]] = polynomial.add %[[X]], %[[Y]] : tensor<2x![[POLY]]>
%sum = lwe.radd %x, %y : (!ct, !ct) -> !ct

// (s0, s1) * (y0, y1) = (s0 * y0, s0 * y1 + s1 * y0, s1 * y1)
// CHECK-DAG: %[[C0:.*]] = arith.constant 0 : index
// CHECK-DAG: %[[C1:.*]] = arith.constant 1 : index
// CHECK-DAG: %[[S0:.*]] = tensor.extract %[[SUM]][%[[C0]]]
// CHECK-DAG: %[[S1:.*]] = tensor.extract %[[SUM]][%[[C1]]]
// CHECK-DAG: %[[Y0:.*]] = tensor.extract %[[Y]][%[[C0]]]
// CHECK-DAG: %[[Y1:.*]] = tensor.extract %[[Y]][%[[C1]]]
// CHECK-DAG: %[[Z0:.*]] = polynomial.mul %[[S0]], %[[Y0]]
// CHECK-DAG: %[[S0Y1:.*]] = polynomial.mul %[[S0]], %[[Y1]]
// CHECK-DAG: %[[S1Y0:.*]] = polynomial.mul %[[S1]], %[[Y0]]
// CHECK-DAG: %[[Z1:.*]] = polynomial.add %[[S0Y1]], %[[S1Y0]]
// CHECK-DAG: %[[Z2:.*]] = polynomial.mul %[[S1]], %[[Y1]]
// CHECK: %[[Z:.*]] = tensor.from_elements %[[Z0]], %[[Z1]], %[[Z2]]
%prod = lwe.rmul %sum, %y : (!ct, !ct) -> !ct_D3

// CHECK: return %[[Z]]
return %prod : !ct_D3
}
Loading