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.
Files changed (5) hide show
  1. package/README.md +45 -69
  2. package/package.json +1 -1
  3. package/tfhe.d.ts +1519 -214
  4. package/tfhe.js +4976 -400
  5. 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 use `TFHE-rs` by compiling using the `nightly` toolchain.
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
- Here is a full example evaluating a Boolean circuit:
63
+ ## A simple example
65
64
 
66
- ```rust
67
- use tfhe::boolean::prelude::*;
65
+ Here is a full example:
68
66
 
69
- fn main() {
70
- // We generate a set of client/server keys, using the default parameters:
71
- let (client_key, server_key) = gen_keys();
67
+ ``` rust
68
+ use tfhe::prelude::*;
69
+ use tfhe::{generate_keys, set_server_key, ConfigBuilder, FheUint32, FheUint8};
72
70
 
73
- // We use the client secret key to encrypt two messages:
74
- let ct_1 = client_key.encrypt(true);
75
- let ct_2 = client_key.encrypt(false);
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
- // We use the server public key to execute a boolean circuit:
78
- // if ((NOT ct_2) NAND (ct_1 AND ct_2)) then (NOT ct_2) else (ct_1 AND ct_2)
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
- // We use the client key to decrypt the output of the circuit:
85
- let output = client_key.decrypt(&ct_6);
86
- assert_eq!(output, true);
87
- }
88
- ```
80
+ let clear_a = 1344u32;
81
+ let clear_b = 5u32;
82
+ let clear_c = 7u8;
89
83
 
90
- Another example of how the library can be used with shortints:
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
- ```rust
93
- use tfhe::shortint::prelude::*;
89
+ // FheUint8: Encrypted equivalent to u8
90
+ let encrypted_c = FheUint8::try_encrypt(clear_c, &client_key)?;
94
91
 
95
- fn main() {
96
- // Generate a set of client/server keys
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
- let msg1 = 3;
101
- let msg2 = 2;
95
+ // Clear equivalent computations: 1344 * 5 = 6720
96
+ let encrypted_res_mul = &encrypted_a * &encrypted_b;
102
97
 
103
- // Encrypt two messages using the (private) client key:
104
- let ct_1 = client_key.encrypt(msg1);
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
- // Homomorphically compute an addition
108
- let ct_add = server_key.unchecked_add(&ct_1, &ct_2);
101
+ // Clear equivalent computations: let casted_a = a as u8;
102
+ let casted_a: FheUint8 = encrypted_a.cast_into();
109
103
 
110
- // Define the Hamming weight function
111
- // f: x -> sum of the bits of x
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
- // Generate the lookup table for the function
115
- let acc = server_key.generate_lookup_table(f);
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
- // Compute the function over the ciphertext using the PBS
118
- let ct_res = server_key.apply_lookup_table(&ct_add, &acc);
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
- // Decrypt the ciphertext using the (private) client key
121
- let output = client_key.decrypt(&ct_res);
122
- assert_eq!(output, f(msg1 + msg2));
115
+ Ok(())
123
116
  }
124
117
  ```
125
118
 
126
- An example using integer:
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
- let clear_a = 2382u16;
138
- let clear_b = 29374u16;
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
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "tfhe",
3
3
  "description": "TFHE-rs is a fully homomorphic encryption (FHE) library that implements Zama's variant of TFHE.",
4
- "version": "0.3.0",
4
+ "version": "0.4.0",
5
5
  "license": "BSD-3-Clause-Clear",
6
6
  "repository": {
7
7
  "type": "git",