tfhe 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +45 -69
- package/package.json +1 -1
- package/tfhe.d.ts +1519 -214
- package/tfhe.js +4976 -400
- package/tfhe_bg.wasm +0 -0
package/README.md
CHANGED
|
@@ -31,7 +31,9 @@ implementation. The goal is to have a stable, simple, high-performance, and
|
|
|
31
31
|
production-ready library for all the advanced features of TFHE.
|
|
32
32
|
|
|
33
33
|
## Getting Started
|
|
34
|
+
The steps to run a first example are described below.
|
|
34
35
|
|
|
36
|
+
### Cargo.toml configuration
|
|
35
37
|
To use the latest version of `TFHE-rs` in your project, you first need to add it as a dependency in your `Cargo.toml`:
|
|
36
38
|
|
|
37
39
|
+ For x86_64-based machines running Unix-like OSes:
|
|
@@ -45,7 +47,7 @@ tfhe = { version = "*", features = ["boolean", "shortint", "integer", "x86_64-un
|
|
|
45
47
|
```toml
|
|
46
48
|
tfhe = { version = "*", features = ["boolean", "shortint", "integer", "aarch64-unix"] }
|
|
47
49
|
```
|
|
48
|
-
Note: users with ARM devices must
|
|
50
|
+
Note: users with ARM devices must compile `TFHE-rs` using a stable toolchain with version >= 1.72.
|
|
49
51
|
|
|
50
52
|
|
|
51
53
|
+ For x86_64-based machines with the [`rdseed instruction`](https://en.wikipedia.org/wiki/RDRAND)
|
|
@@ -57,95 +59,69 @@ tfhe = { version = "*", features = ["boolean", "shortint", "integer", "x86_64"]
|
|
|
57
59
|
|
|
58
60
|
Note: aarch64-based machines are not yet supported for Windows as it's currently missing an entropy source to be able to seed the [CSPRNGs](https://en.wikipedia.org/wiki/Cryptographically_secure_pseudorandom_number_generator) used in TFHE-rs
|
|
59
61
|
|
|
60
|
-
Note that when running code that uses `tfhe-rs`, it is highly recommended
|
|
61
|
-
to run in release mode with cargo's `--release` flag to have the best performances possible,
|
|
62
|
-
eg: `cargo run --release`.
|
|
63
62
|
|
|
64
|
-
|
|
63
|
+
## A simple example
|
|
65
64
|
|
|
66
|
-
|
|
67
|
-
use tfhe::boolean::prelude::*;
|
|
65
|
+
Here is a full example:
|
|
68
66
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
67
|
+
``` rust
|
|
68
|
+
use tfhe::prelude::*;
|
|
69
|
+
use tfhe::{generate_keys, set_server_key, ConfigBuilder, FheUint32, FheUint8};
|
|
72
70
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
let
|
|
71
|
+
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
72
|
+
// Basic configuration to use homomorphic integers
|
|
73
|
+
let config = ConfigBuilder::all_disabled()
|
|
74
|
+
.enable_default_integers()
|
|
75
|
+
.build();
|
|
76
76
|
|
|
77
|
-
//
|
|
78
|
-
|
|
79
|
-
let ct_3 = server_key.not(&ct_2);
|
|
80
|
-
let ct_4 = server_key.and(&ct_1, &ct_2);
|
|
81
|
-
let ct_5 = server_key.nand(&ct_3, &ct_4);
|
|
82
|
-
let ct_6 = server_key.mux(&ct_5, &ct_3, &ct_4);
|
|
77
|
+
// Key generation
|
|
78
|
+
let (client_key, server_keys) = generate_keys(config);
|
|
83
79
|
|
|
84
|
-
|
|
85
|
-
let
|
|
86
|
-
|
|
87
|
-
}
|
|
88
|
-
```
|
|
80
|
+
let clear_a = 1344u32;
|
|
81
|
+
let clear_b = 5u32;
|
|
82
|
+
let clear_c = 7u8;
|
|
89
83
|
|
|
90
|
-
|
|
84
|
+
// Encrypting the input data using the (private) client_key
|
|
85
|
+
// FheUint32: Encrypted equivalent to u32
|
|
86
|
+
let mut encrypted_a = FheUint32::try_encrypt(clear_a, &client_key)?;
|
|
87
|
+
let encrypted_b = FheUint32::try_encrypt(clear_b, &client_key)?;
|
|
91
88
|
|
|
92
|
-
|
|
93
|
-
|
|
89
|
+
// FheUint8: Encrypted equivalent to u8
|
|
90
|
+
let encrypted_c = FheUint8::try_encrypt(clear_c, &client_key)?;
|
|
94
91
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
// with 2 bits of message and 2 bits of carry
|
|
98
|
-
let (client_key, server_key) = gen_keys(PARAM_MESSAGE_2_CARRY_2_KS_PBS);
|
|
92
|
+
// On the server side:
|
|
93
|
+
set_server_key(server_keys);
|
|
99
94
|
|
|
100
|
-
|
|
101
|
-
let
|
|
95
|
+
// Clear equivalent computations: 1344 * 5 = 6720
|
|
96
|
+
let encrypted_res_mul = &encrypted_a * &encrypted_b;
|
|
102
97
|
|
|
103
|
-
//
|
|
104
|
-
|
|
105
|
-
let ct_2 = client_key.encrypt(msg2);
|
|
98
|
+
// Clear equivalent computations: 1344 >> 5 = 42
|
|
99
|
+
encrypted_a = &encrypted_res_mul >> &encrypted_b;
|
|
106
100
|
|
|
107
|
-
//
|
|
108
|
-
let
|
|
101
|
+
// Clear equivalent computations: let casted_a = a as u8;
|
|
102
|
+
let casted_a: FheUint8 = encrypted_a.cast_into();
|
|
109
103
|
|
|
110
|
-
//
|
|
111
|
-
|
|
112
|
-
let f = |x:u64| x.count_ones() as u64;
|
|
104
|
+
// Clear equivalent computations: min(42, 7) = 7
|
|
105
|
+
let encrypted_res_min = &casted_a.min(&encrypted_c);
|
|
113
106
|
|
|
114
|
-
//
|
|
115
|
-
|
|
107
|
+
// Operation between clear and encrypted data:
|
|
108
|
+
// Clear equivalent computations: 7 & 1 = 1
|
|
109
|
+
let encrypted_res = encrypted_res_min & 1_u8;
|
|
116
110
|
|
|
117
|
-
//
|
|
118
|
-
let
|
|
111
|
+
// Decrypting on the client side:
|
|
112
|
+
let clear_res: u8 = encrypted_res.decrypt(&client_key);
|
|
113
|
+
assert_eq!(clear_res, 1_u8);
|
|
119
114
|
|
|
120
|
-
|
|
121
|
-
let output = client_key.decrypt(&ct_res);
|
|
122
|
-
assert_eq!(output, f(msg1 + msg2));
|
|
115
|
+
Ok(())
|
|
123
116
|
}
|
|
124
117
|
```
|
|
125
118
|
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
```rust
|
|
129
|
-
use tfhe::integer::gen_keys_radix;
|
|
130
|
-
use tfhe::shortint::parameters::PARAM_MESSAGE_2_CARRY_2_KS_PBS;
|
|
131
|
-
|
|
132
|
-
fn main() {
|
|
133
|
-
// We create keys to create 16 bits integers
|
|
134
|
-
// using 8 blocks of 2 bits
|
|
135
|
-
let (cks, sks) = gen_keys_radix(PARAM_MESSAGE_2_CARRY_2_KS_PBS, 8);
|
|
119
|
+
To run this code, use the following command:
|
|
120
|
+
<p align="center"> <code> cargo run --release </code> </p>
|
|
136
121
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
let mut a = cks.encrypt(clear_a as u64);
|
|
141
|
-
let mut b = cks.encrypt(clear_b as u64);
|
|
142
|
-
|
|
143
|
-
let encrypted_max = sks.smart_max_parallelized(&mut a, &mut b);
|
|
144
|
-
let decrypted_max: u64 = cks.decrypt(&encrypted_max);
|
|
122
|
+
Note that when running code that uses `tfhe-rs`, it is highly recommended
|
|
123
|
+
to run in release mode with cargo's `--release` flag to have the best performances possible,
|
|
145
124
|
|
|
146
|
-
assert_eq!(decrypted_max as u16, clear_a.max(clear_b))
|
|
147
|
-
}
|
|
148
|
-
```
|
|
149
125
|
|
|
150
126
|
## Contributing
|
|
151
127
|
|
package/package.json
CHANGED