Skip to content

Commit c441e8a

Browse files
committed
poc: add tfhe-rs with bazel deps
Signed-off-by: Asra Ali <[email protected]> fix sbox timeout Signed-off-by: Asra Ali <[email protected]> remove test files Signed-off-by: Asra Ali <[email protected]> address comments Signed-off-by: Asra Ali <[email protected]> remove unneeded deps Signed-off-by: Asra Ali <[email protected]>
1 parent 16752d2 commit c441e8a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+1328
-3660
lines changed

.github/workflows/build_and_test.yml

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,3 @@ jobs:
7474
- name: "Run `bazel test`"
7575
run: |
7676
bazel test --noincompatible_strict_action_env --//:enable_openmp=0 -c fastbuild //...
77-
78-
# Tests specifically for the tfhe-rs codegen
79-
- name: rustup toolchain install
80-
uses: dtolnay/rust-toolchain@439cf607258077187679211f12aa6f19af4a0af7 # [email protected]
81-
with:
82-
toolchain: stable
83-
84-
- name: Test rust codegen targets
85-
run: |
86-
bash .github/workflows/run_rust_tests.sh

.github/workflows/run_rust_tests.sh

Lines changed: 0 additions & 23 deletions
This file was deleted.

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ repos:
3939
hooks:
4040
- id: codespell
4141
args: ["-L", "crate, fpt"]
42-
42+
exclude: "^(.*[.]lock)"
4343

4444
# Changes tabs to spaces
4545
- repo: https://github.com/Lucas-C/pre-commit-hooks

MODULE.bazel

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,33 @@ git_override(
110110
commit = "c84a140c93352cdabbfb547c531be34515b12228",
111111
remote = "https://github.com/google/re2",
112112
)
113+
114+
bazel_dep(name = "rules_rust", version = "0.60.0")
115+
116+
crate = use_extension("@rules_rust//crate_universe:extensions.bzl", "crate")
117+
crate.spec(
118+
features = ["derive"],
119+
package = "clap",
120+
version = "4.1.8",
121+
)
122+
crate.spec(
123+
package = "rayon",
124+
version = "1.6.1",
125+
)
126+
crate.spec(
127+
features = ["derive"],
128+
package = "serde",
129+
version = "1.0.152",
130+
)
131+
crate.spec(
132+
features = [
133+
"boolean",
134+
"shortint",
135+
"integer",
136+
"x86_64-unix",
137+
],
138+
package = "tfhe",
139+
version = "0.5.3",
140+
)
141+
crate.from_specs()
142+
use_repo(crate, "crates")

MODULE.bazel.lock

Lines changed: 817 additions & 3033 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/Examples/openfhe/README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,4 @@ These tests exercise OpenFHE codegen for the
44
[OpenFHE](https://github.com/openfheorg/openfhe-development) backend library,
55
including compiling the generated C++ source and running the resulting binary.
66

7-
OpenFHE is added as a project-level dependency (unlike the `tfhe-rs` end-to-end
8-
tests) and built from source.
7+
OpenFHE is added as a project-level dependency and built from source.

tests/Examples/tfhe_rust/BUILD

Lines changed: 92 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,96 @@
1-
# See README.md for setup required to run these tests
2-
3-
load("//bazel:lit.bzl", "glob_lit_tests")
1+
load("@heir//tests/Examples/tfhe_rust:test.bzl", "tfhe_rs_end_to_end_test")
42

53
package(default_applicable_licenses = ["@heir//:license"])
64

7-
glob_lit_tests(
8-
name = "all_tests",
9-
data = [
10-
"Cargo.toml",
11-
"src/main.rs",
12-
"src/main_add_one.rs",
13-
"src/main_fully_connected.rs",
14-
"src/main_multi_output.rs",
15-
"src/main_sbox.rs",
16-
"@heir//tests:test_utilities",
17-
],
18-
default_tags = [
19-
"manual",
20-
"notap",
21-
],
22-
driver = "@heir//tests:run_lit.sh",
23-
size_override = {
24-
"test_add.mlir": "large",
25-
"test_add_one.mlir": "large",
26-
"test_bitand.mlir": "large",
27-
"test_simple_lut.mlir": "large",
28-
"test_fully_connected.mlir": "large",
29-
"test_sbox.mlir": "large",
30-
"test_multi_output.mlir": "large",
31-
},
32-
test_file_exts = ["mlir"],
5+
tfhe_rs_end_to_end_test(
6+
name = "add_one_test",
7+
size = "large",
8+
heir_opt_flags = [
9+
"--forward-store-to-load",
10+
"--cggi-to-tfhe-rust",
11+
"--canonicalize",
12+
"--cse",
13+
],
14+
heir_translate_flags = [
15+
"--emit-tfhe-rust",
16+
"--use-levels",
17+
],
18+
mlir_src = "add_one.mlir",
19+
test_src = "src/main_add_one.rs",
20+
)
21+
22+
tfhe_rs_end_to_end_test(
23+
name = "add_test",
24+
size = "large",
25+
heir_translate_flags = ["--emit-tfhe-rust"],
26+
mlir_src = "add.mlir",
27+
test_src = "src/main_add.rs",
28+
)
29+
30+
tfhe_rs_end_to_end_test(
31+
name = "bitand_test",
32+
size = "large",
33+
heir_translate_flags = [
34+
"--emit-tfhe-rust",
35+
"--use-levels",
36+
],
37+
mlir_src = "bitand.mlir",
38+
test_src = "src/main_bitand.rs",
39+
)
40+
41+
tfhe_rs_end_to_end_test(
42+
name = "simple_lut_test",
43+
size = "large",
44+
heir_translate_flags = ["--emit-tfhe-rust"],
45+
mlir_src = "simple_lut.mlir",
46+
test_src = "src/main_simple_lut.rs",
47+
)
48+
49+
tfhe_rs_end_to_end_test(
50+
name = "fully_connected_test",
51+
size = "large",
52+
heir_opt_flags = [
53+
"--mlir-to-cggi",
54+
"--scheme-to-tfhe-rs",
55+
],
56+
heir_translate_flags = [
57+
"--emit-tfhe-rust",
58+
"--use-levels",
59+
],
60+
mlir_src = "fully_connected.mlir",
61+
tags = ["yosys"],
62+
test_src = "src/main_fully_connected.rs",
63+
)
64+
65+
tfhe_rs_end_to_end_test(
66+
name = "sbox_test",
67+
# Skip in debug mode
68+
size = "enormous",
69+
heir_opt_flags = [
70+
"--mlir-to-cggi",
71+
"--scheme-to-tfhe-rs",
72+
],
73+
heir_translate_flags = [
74+
"--emit-tfhe-rust",
75+
"--use-levels",
76+
],
77+
mlir_src = "sbox.mlir",
78+
tags = ["yosys"],
79+
test_src = "src/main_sbox.rs",
80+
)
81+
82+
tfhe_rs_end_to_end_test(
83+
name = "multi_output_test",
84+
size = "large",
85+
heir_opt_flags = [
86+
"--mlir-to-cggi",
87+
"--scheme-to-tfhe-rs",
88+
],
89+
heir_translate_flags = [
90+
"--emit-tfhe-rust",
91+
"--use-levels",
92+
],
93+
mlir_src = "multi_output.mlir",
94+
tags = ["yosys"],
95+
test_src = "src/main_multi_output.rs",
3396
)

tests/Examples/tfhe_rust/Cargo.toml

Lines changed: 0 additions & 30 deletions
This file was deleted.

tests/Examples/tfhe_rust/README.md

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,7 @@ These tests exercise Rust codegen for the
44
[tfhe-rs](https://github.com/zama-ai/tfhe-rs) backend library, including
55
compiling the generated Rust source and running the resulting binary.
66

7-
To avoid introducing these large dependencies into the entire project, these
8-
tests are manual, and require the system they're running on to have
9-
[Cargo](https://doc.rust-lang.org/cargo/index.html) installed. During the test,
10-
cargo will fetch and build the required dependencies, and `Cargo.toml` in this
11-
directory effectively pins the version of `tfhe` supported.
12-
13-
Use the following command to run the tests in this directory, where the default
14-
Cargo home `$HOME/.cargo` may need to be replaced by your custom `$CARGO_HOME`,
15-
if you overrode the default option when installing Cargo.
16-
17-
```bash
18-
bazel query "filter('.mlir.test$', //tests/Examples/tfhe_rust/...)" \
19-
| xargs bazel test --noincompatible_strict_action_env --sandbox_writable_path=$HOME/.cargo "$@"
20-
```
21-
22-
The `manual` tag is added to the targets in this directory to ensure that they
23-
are not run when someone runs a glob test like `bazel test //...`.
24-
25-
If you don't do this correctly, you will see an error like this:
26-
27-
```
28-
# .---command stderr------------
29-
# | Updating crates.io index
30-
# | Downloading crates ...
31-
# | Downloaded memoffset v0.9.0
32-
# | error: failed to download replaced source registry `crates-io`
33-
# |
34-
# | Caused by:
35-
# | failed to open `/home/you/.cargo/registry/cache/index.crates.io-6f17d22bba15001f/memoffset-0.9.0.crate`
36-
# |
37-
# | Caused by:
38-
# | Read-only file system (os error 30)
39-
# `-----------------------------
40-
```
7+
The tests are generated using a custom Bazel macro found in
8+
[test.bzl](tests/Examples/tfhe_rust/test.bzl). `tfhe-rs` is added a
9+
project-level dependency, and it's version is pinned in
10+
[MODULE.bazel](MODULE.bazel).

tests/Examples/tfhe_rust/test_add.mlir renamed to tests/Examples/tfhe_rust/add.mlir

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
// This test ensures the testing harness is working properly with minimal codegen.
22

3-
// RUN: heir-translate %s --emit-tfhe-rust --use-levels > %S/src/fn_under_test.rs
4-
// RUN: cargo run --release --manifest-path %S/Cargo.toml --bin main -- 2 3 --message_bits=3 | FileCheck %s
5-
63
!sks = !tfhe_rust.server_key
74
!lut = !tfhe_rust.lookup_table
85
!eui3 = !tfhe_rust.eui3
96

10-
// CHECK: 5
117
func.func @fn_under_test(%sks : !sks, %a: !eui3, %b: !eui3) -> !eui3 {
128
%res = tfhe_rust.add %sks, %a, %b: (!sks, !eui3, !eui3) -> !eui3
139
return %res : !eui3

tests/Examples/tfhe_rust/test_add_one.mlir renamed to tests/Examples/tfhe_rust/add_one.mlir

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
// RUN: heir-opt --forward-store-to-load --cggi-to-tfhe-rust --canonicalize --cse %s | heir-translate --emit-tfhe-rust > %S/src/fn_under_test.rs
2-
// RUN: cargo run --release --manifest-path %S/Cargo.toml --bin main_add_one -- 2 --message_bits=3 | FileCheck %s
3-
41
#encoding = #lwe.unspecified_bit_field_encoding<cleartext_bitwidth = 3>
52
!ct_ty = !lwe.lwe_ciphertext<encoding = #encoding>
63
!pt_ty = !lwe.lwe_plaintext<encoding = #encoding>
74

8-
// CHECK: 00000011
95
module {
106
func.func @fn_under_test(%arg0: memref<8x!ct_ty>) -> memref<8x!ct_ty> {
117
%true = arith.constant true

tests/Examples/tfhe_rust/test_bitand.mlir renamed to tests/Examples/tfhe_rust/bitand.mlir

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
// This test ensures the testing harness is working properly with minimal codegen.
22

3-
// RUN: heir-translate %s --emit-tfhe-rust > %S/src/fn_under_test.rs
4-
// RUN: cargo run --release --manifest-path %S/Cargo.toml --bin main -- 2 3 --message_bits=3 | FileCheck %s
5-
63
!sks = !tfhe_rust.server_key
74
!eui3 = !tfhe_rust.eui3
85

9-
// CHECK: 0
106
func.func @fn_under_test(%sks : !sks, %a: !eui3, %b: !eui3) -> !eui3 {
117
%res = tfhe_rust.bitand %sks, %a, %b: (!sks, !eui3, !eui3) -> !eui3
128
return %res : !eui3

tests/Examples/tfhe_rust/test_fully_connected.mlir renamed to tests/Examples/tfhe_rust/fully_connected.mlir

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
// RUN: heir-opt --mlir-to-cggi --scheme-to-tfhe-rs %s | heir-translate --emit-tfhe-rust > %S/src/fn_under_test.rs
2-
// RUN: cargo run --release --manifest-path %S/Cargo.toml --bin main_fully_connected -- 2 --message_bits=3 | FileCheck %s
3-
41
// This takes takes the input x and outputs 2 \cdot x + 1.
5-
// CHECK: 00000101
62
#map = affine_map<(d0, d1) -> (0)>
73
#map1 = affine_map<(d0, d1) -> (d0, d1)>
84
module {
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module {
2+
func.func @multi_output(%arg0: i8 {secret.secret}) -> (i8, i8) {
3+
%c2_i8 = arith.constant 2 : i8
4+
%c3_i8 = arith.constant 3 : i8
5+
%0 = arith.addi %arg0, %c3_i8 : i8
6+
%1 = arith.muli %arg0, %c2_i8 : i8
7+
return %0, %1 : i8, i8
8+
}
9+
}

tests/Examples/tfhe_rust/sbox.mlir

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Single (mini) sbox table lookup for AES encryption.
2+
3+
#map = affine_map<(d0) -> (d0)>
4+
module {
5+
func.func @sub_bytes(%arg0: i8 {secret.secret}) -> i8 {
6+
%cst = arith.constant dense<"0x637C777BF26B6FC5"> : tensor<8xi8>
7+
%2 = arith.index_cast %arg0 : i8 to index
8+
%extracted = tensor.extract %cst[%2] : tensor<8xi8>
9+
return %extracted : i8
10+
}
11+
}

tests/Examples/tfhe_rust/test_simple_lut.mlir renamed to tests/Examples/tfhe_rust/simple_lut.mlir

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
// This test ensures the testing harness is working properly with minimal codegen.
22

3-
// RUN: heir-translate %s --emit-tfhe-rust > %S/src/fn_under_test.rs
4-
// RUN: cargo run --release --manifest-path %S/Cargo.toml --bin main -- 1 0 --message_bits=3 | FileCheck %s
5-
63
!sks = !tfhe_rust.server_key
74
!lut = !tfhe_rust.lookup_table
85
!eui3 = !tfhe_rust.eui3
96

10-
// We're computing, effectively (0b00000111 >> (1 << 1)) & 1, i.e., 0b111 >> 2
11-
// CHECK: 1
127
func.func @fn_under_test(%sks : !sks, %a: !eui3, %b: !eui3) -> !eui3 {
138
%lut = tfhe_rust.generate_lookup_table %sks {truthTable = 7 : ui8} : (!sks) -> !lut
149
%0 = tfhe_rust.scalar_left_shift %sks, %a {shiftAmount = 1 : index} : (!sks, !eui3) -> !eui3

0 commit comments

Comments
 (0)