Skip to content

Commit baa933e

Browse files
committed
feat: add 2^-128 parameters
1 parent 59efca3 commit baa933e

File tree

401 files changed

+10457
-5926
lines changed

Some content is hidden

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

401 files changed

+10457
-5926
lines changed

Makefile

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,18 @@ clippy_rustdoc: install_rs_check_toolchain
363363
fi && \
364364
CLIPPYFLAGS="-D warnings" RUSTDOCFLAGS="--no-run --nocapture --test-builder ./scripts/clippy_driver.sh -Z unstable-options" \
365365
cargo "$(CARGO_RS_CHECK_TOOLCHAIN)" test --doc \
366-
--features=boolean,shortint,integer,zk-pok,pbs-stats,strings \
366+
--features=boolean,shortint,integer,zk-pok,pbs-stats,strings,experimental \
367+
-p $(TFHE_SPEC)
368+
369+
.PHONY: clippy_rustdoc_gpu # Run clippy lints on doctests enabling the boolean, shortint, integer and zk-pok
370+
clippy_rustdoc_gpu: install_rs_check_toolchain
371+
if [[ "$(OS)" != "Linux" ]]; then \
372+
echo "WARNING: skipped clippy_rustdoc_gpu, unsupported OS $(OS)"; \
373+
exit 0; \
374+
fi && \
375+
CLIPPYFLAGS="-D warnings" RUSTDOCFLAGS="--no-run --nocapture --test-builder ./scripts/clippy_driver.sh -Z unstable-options" \
376+
cargo "$(CARGO_RS_CHECK_TOOLCHAIN)" test --doc \
377+
--features=boolean,shortint,integer,zk-pok,pbs-stats,strings,experimental,gpu \
367378
-p $(TFHE_SPEC)
368379

369380
.PHONY: clippy_c_api # Run clippy lints enabling the boolean, shortint and the C API
@@ -956,6 +967,10 @@ check_intra_md_links: install_mlc
956967
check_md_links: install_mlc
957968
mlc --match-file-extension tfhe/docs
958969

970+
.PHONY: check_parameter_export_ok # Checks exported "current" shortint parameter module is correct
971+
check_parameter_export_ok:
972+
python3 ./scripts/check_current_param_export.py
973+
959974
.PHONY: check_compile_tests # Build tests in debug without running them
960975
check_compile_tests: install_rs_build_toolchain
961976
RUSTFLAGS="$(RUSTFLAGS)" cargo $(CARGO_RS_BUILD_TOOLCHAIN) test --no-run \
@@ -1313,15 +1328,17 @@ sha256_bool: install_rs_check_toolchain
13131328
--example sha256_bool --features=boolean
13141329

13151330
.PHONY: pcc # pcc stands for pre commit checks (except GPU)
1316-
pcc: no_tfhe_typo no_dbg_log check_fmt check_typos lint_doc check_md_docs_are_tested check_intra_md_links \
1317-
clippy_all check_compile_tests test_tfhe_lints tfhe_lints
1331+
pcc: no_tfhe_typo no_dbg_log check_parameter_export_ok check_fmt check_typos lint_doc \
1332+
check_md_docs_are_tested check_intra_md_links clippy_all check_compile_tests test_tfhe_lints \
1333+
tfhe_lints
13181334

13191335
.PHONY: pcc_gpu # pcc stands for pre commit checks for GPU compilation
1320-
pcc_gpu: clippy_gpu clippy_cuda_backend check_compile_tests_benches_gpu check_rust_bindings_did_not_change
1336+
pcc_gpu: check_rust_bindings_did_not_change clippy_rustdoc_gpu \
1337+
clippy_gpu clippy_cuda_backend check_compile_tests_benches_gpu
13211338

13221339
.PHONY: fpcc # pcc stands for pre commit checks, the f stands for fast
1323-
fpcc: no_tfhe_typo no_dbg_log check_fmt check_typos lint_doc check_md_docs_are_tested clippy_fast \
1324-
check_compile_tests
1340+
fpcc: no_tfhe_typo no_dbg_log check_parameter_export_ok check_fmt check_typos lint_doc \
1341+
check_md_docs_are_tested clippy_fast check_compile_tests
13251342

13261343
.PHONY: conformance # Automatically fix problems that can be fixed
13271344
conformance: fix_newline fmt fmt_js

apps/trivium/README.md

Lines changed: 123 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -18,102 +18,102 @@ use tfhe::prelude::*;
1818
use tfhe_trivium::TriviumStream;
1919

2020
fn get_hexadecimal_string_from_lsb_first_stream(a: Vec<bool>) -> String {
21-
assert!(a.len() % 8 == 0);
22-
let mut hexadecimal: String = "".to_string();
23-
for test in a.chunks(8) {
24-
// Encoding is bytes in LSB order
25-
match test[4..8] {
26-
[false, false, false, false] => hexadecimal.push('0'),
27-
[true, false, false, false] => hexadecimal.push('1'),
28-
[false, true, false, false] => hexadecimal.push('2'),
29-
[true, true, false, false] => hexadecimal.push('3'),
30-
31-
[false, false, true, false] => hexadecimal.push('4'),
32-
[true, false, true, false] => hexadecimal.push('5'),
33-
[false, true, true, false] => hexadecimal.push('6'),
34-
[true, true, true, false] => hexadecimal.push('7'),
35-
36-
[false, false, false, true] => hexadecimal.push('8'),
37-
[true, false, false, true] => hexadecimal.push('9'),
38-
[false, true, false, true] => hexadecimal.push('A'),
39-
[true, true, false, true] => hexadecimal.push('B'),
40-
41-
[false, false, true, true] => hexadecimal.push('C'),
42-
[true, false, true, true] => hexadecimal.push('D'),
43-
[false, true, true, true] => hexadecimal.push('E'),
44-
[true, true, true, true] => hexadecimal.push('F'),
45-
_ => ()
46-
};
47-
match test[0..4] {
48-
[false, false, false, false] => hexadecimal.push('0'),
49-
[true, false, false, false] => hexadecimal.push('1'),
50-
[false, true, false, false] => hexadecimal.push('2'),
51-
[true, true, false, false] => hexadecimal.push('3'),
52-
53-
[false, false, true, false] => hexadecimal.push('4'),
54-
[true, false, true, false] => hexadecimal.push('5'),
55-
[false, true, true, false] => hexadecimal.push('6'),
56-
[true, true, true, false] => hexadecimal.push('7'),
57-
58-
[false, false, false, true] => hexadecimal.push('8'),
59-
[true, false, false, true] => hexadecimal.push('9'),
60-
[false, true, false, true] => hexadecimal.push('A'),
61-
[true, true, false, true] => hexadecimal.push('B'),
62-
63-
[false, false, true, true] => hexadecimal.push('C'),
64-
[true, false, true, true] => hexadecimal.push('D'),
65-
[false, true, true, true] => hexadecimal.push('E'),
66-
[true, true, true, true] => hexadecimal.push('F'),
67-
_ => ()
68-
};
69-
}
70-
return hexadecimal;
21+
assert!(a.len() % 8 == 0);
22+
let mut hexadecimal: String = "".to_string();
23+
for test in a.chunks(8) {
24+
// Encoding is bytes in LSB order
25+
match test[4..8] {
26+
[false, false, false, false] => hexadecimal.push('0'),
27+
[true, false, false, false] => hexadecimal.push('1'),
28+
[false, true, false, false] => hexadecimal.push('2'),
29+
[true, true, false, false] => hexadecimal.push('3'),
30+
31+
[false, false, true, false] => hexadecimal.push('4'),
32+
[true, false, true, false] => hexadecimal.push('5'),
33+
[false, true, true, false] => hexadecimal.push('6'),
34+
[true, true, true, false] => hexadecimal.push('7'),
35+
36+
[false, false, false, true] => hexadecimal.push('8'),
37+
[true, false, false, true] => hexadecimal.push('9'),
38+
[false, true, false, true] => hexadecimal.push('A'),
39+
[true, true, false, true] => hexadecimal.push('B'),
40+
41+
[false, false, true, true] => hexadecimal.push('C'),
42+
[true, false, true, true] => hexadecimal.push('D'),
43+
[false, true, true, true] => hexadecimal.push('E'),
44+
[true, true, true, true] => hexadecimal.push('F'),
45+
_ => ()
46+
};
47+
match test[0..4] {
48+
[false, false, false, false] => hexadecimal.push('0'),
49+
[true, false, false, false] => hexadecimal.push('1'),
50+
[false, true, false, false] => hexadecimal.push('2'),
51+
[true, true, false, false] => hexadecimal.push('3'),
52+
53+
[false, false, true, false] => hexadecimal.push('4'),
54+
[true, false, true, false] => hexadecimal.push('5'),
55+
[false, true, true, false] => hexadecimal.push('6'),
56+
[true, true, true, false] => hexadecimal.push('7'),
57+
58+
[false, false, false, true] => hexadecimal.push('8'),
59+
[true, false, false, true] => hexadecimal.push('9'),
60+
[false, true, false, true] => hexadecimal.push('A'),
61+
[true, true, false, true] => hexadecimal.push('B'),
62+
63+
[false, false, true, true] => hexadecimal.push('C'),
64+
[true, false, true, true] => hexadecimal.push('D'),
65+
[false, true, true, true] => hexadecimal.push('E'),
66+
[true, true, true, true] => hexadecimal.push('F'),
67+
_ => ()
68+
};
69+
}
70+
return hexadecimal;
7171
}
7272

7373
fn main() {
74-
let config = ConfigBuilder::default().build();
75-
let (client_key, server_key) = generate_keys(config);
74+
let config = ConfigBuilder::default().build();
75+
let (client_key, server_key) = generate_keys(config);
7676

77-
let key_string = "0053A6F94C9FF24598EB".to_string();
78-
let mut key = [false; 80];
77+
let key_string = "0053A6F94C9FF24598EB".to_string();
78+
let mut key = [false; 80];
7979

80-
for i in (0..key_string.len()).step_by(2) {
81-
let mut val: u8 = u8::from_str_radix(&key_string[i..i+2], 16).unwrap();
82-
for j in 0..8 {
83-
key[8*(i>>1) + j] = val % 2 == 1;
84-
val >>= 1;
85-
}
86-
}
80+
for i in (0..key_string.len()).step_by(2) {
81+
let mut val: u8 = u8::from_str_radix(&key_string[i..i+2], 16).unwrap();
82+
for j in 0..8 {
83+
key[8*(i>>1) + j] = val % 2 == 1;
84+
val >>= 1;
85+
}
86+
}
8787

88-
let iv_string = "0D74DB42A91077DE45AC".to_string();
89-
let mut iv = [false; 80];
88+
let iv_string = "0D74DB42A91077DE45AC".to_string();
89+
let mut iv = [false; 80];
9090

91-
for i in (0..iv_string.len()).step_by(2) {
92-
let mut val: u8 = u8::from_str_radix(&iv_string[i..i+2], 16).unwrap();
93-
for j in 0..8 {
94-
iv[8*(i>>1) + j] = val % 2 == 1;
95-
val >>= 1;
96-
}
97-
}
91+
for i in (0..iv_string.len()).step_by(2) {
92+
let mut val: u8 = u8::from_str_radix(&iv_string[i..i+2], 16).unwrap();
93+
for j in 0..8 {
94+
iv[8*(i>>1) + j] = val % 2 == 1;
95+
val >>= 1;
96+
}
97+
}
9898

99-
let output_0_63 = "F4CD954A717F26A7D6930830C4E7CF0819F80E03F25F342C64ADC66ABA7F8A8E6EAA49F23632AE3CD41A7BD290A0132F81C6D4043B6E397D7388F3A03B5FE358".to_string();
99+
let output_0_63 = "F4CD954A717F26A7D6930830C4E7CF0819F80E03F25F342C64ADC66ABA7F8A8E6EAA49F23632AE3CD41A7BD290A0132F81C6D4043B6E397D7388F3A03B5FE358".to_string();
100100

101-
let cipher_key = key.map(|x| FheBool::encrypt(x, &client_key));
102-
let cipher_iv = iv.map(|x| FheBool::encrypt(x, &client_key));
101+
let cipher_key = key.map(|x| FheBool::encrypt(x, &client_key));
102+
let cipher_iv = iv.map(|x| FheBool::encrypt(x, &client_key));
103103

104104

105-
let mut trivium = TriviumStream::<FheBool>::new(cipher_key, cipher_iv, &server_key);
105+
let mut trivium = TriviumStream::<FheBool>::new(cipher_key, cipher_iv, &server_key);
106106

107-
let mut vec = Vec::<bool>::with_capacity(64*8);
108-
while vec.len() < 64*8 {
109-
let cipher_outputs = trivium.next_64();
110-
for c in cipher_outputs {
111-
vec.push(c.decrypt(&client_key))
112-
}
113-
}
107+
let mut vec = Vec::<bool>::with_capacity(64*8);
108+
while vec.len() < 64*8 {
109+
let cipher_outputs = trivium.next_64();
110+
for c in cipher_outputs {
111+
vec.push(c.decrypt(&client_key))
112+
}
113+
}
114114

115-
let hexadecimal = get_hexadecimal_string_from_lsb_first_stream(vec);
116-
assert_eq!(output_0_63, hexadecimal[0..64*2]);
115+
let hexadecimal = get_hexadecimal_string_from_lsb_first_stream(vec);
116+
assert_eq!(output_0_63, hexadecimal[0..64*2]);
117117
}
118118
```
119119

@@ -129,75 +129,76 @@ Other sizes than 64 bit are expected to be available in the future.
129129

130130
# FHE shortint Trivium implementation
131131

132-
The same implementation is also available for generic Ciphertexts representing bits (meant to be used with parameters `V0_11_PARAM_MESSAGE_1_CARRY_1_KS_PBS_GAUSSIAN_2M64`).
132+
The same implementation is also available for generic Ciphertexts representing bits (meant to be used with parameters `V1_0_PARAM_MESSAGE_1_CARRY_1_KS_PBS_GAUSSIAN_2M128`).
133133
It uses a lower level API of tfhe-rs, so the syntax is a little bit different. It also implements the `TransCiphering` trait. For optimization purposes, it does not internally run
134134
on the same cryptographic parameters as the high level API of tfhe-rs. As such, it requires the usage of a casting key, to switch from one parameter space to another, which makes
135135
its setup a little more intricate.
136136

137137
Example code:
138138
```rust
139139
use tfhe::shortint::prelude::*;
140-
use tfhe::shortint::parameters::{
141-
V0_11_PARAM_MESSAGE_1_CARRY_1_KS_PBS_GAUSSIAN_2M64,
142-
V0_11_PARAM_MESSAGE_2_CARRY_2_PBS_KS_GAUSSIAN_2M64,
140+
use tfhe::shortint::parameters::v1_0::{
141+
V1_0_PARAM_MESSAGE_1_CARRY_1_KS_PBS_GAUSSIAN_2M128,
142+
V1_0_PARAM_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M128,
143+
V1_0_PARAM_KEYSWITCH_1_1_KS_PBS_TO_2_2_KS_PBS_GAUSSIAN_2M128,
143144
};
144145
use tfhe::{ConfigBuilder, generate_keys, FheUint64};
145146
use tfhe::prelude::*;
146147
use tfhe_trivium::TriviumStreamShortint;
147148

148149
fn test_shortint() {
149-
let config = ConfigBuilder::default()
150-
.use_custom_parameters(V0_11_PARAM_MESSAGE_2_CARRY_2_PBS_KS_GAUSSIAN_2M64)
150+
let config = ConfigBuilder::default()
151+
.use_custom_parameters(V1_0_PARAM_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M128)
151152
.build();
152-
let (hl_client_key, hl_server_key) = generate_keys(config);
153+
let (hl_client_key, hl_server_key) = generate_keys(config);
153154
let underlying_ck: tfhe::shortint::ClientKey = (*hl_client_key.as_ref()).clone().into();
154155
let underlying_sk: tfhe::shortint::ServerKey = (*hl_server_key.as_ref()).clone().into();
155156

156-
let (client_key, server_key): (ClientKey, ServerKey) = gen_keys(V0_11_PARAM_MESSAGE_1_CARRY_1_KS_PBS_GAUSSIAN_2M64);
157+
let (client_key, server_key): (ClientKey, ServerKey) = gen_keys(V1_0_PARAM_MESSAGE_1_CARRY_1_KS_PBS_GAUSSIAN_2M128);
157158
let ksk = KeySwitchingKey::new(
158159
(&client_key, Some(&server_key)),
159160
(&underlying_ck, &underlying_sk),
160-
V0_11_PARAM_KEYSWITCH_1_1_KS_PBS_TO_2_2_KS_PBS,
161+
V1_0_PARAM_KEYSWITCH_1_1_KS_PBS_TO_2_2_KS_PBS_GAUSSIAN_2M128_2M128,
161162
);
162163

163-
let key_string = "0053A6F94C9FF24598EB".to_string();
164-
let mut key = [0; 80];
164+
let key_string = "0053A6F94C9FF24598EB".to_string();
165+
let mut key = [0; 80];
165166

166-
for i in (0..key_string.len()).step_by(2) {
167-
let mut val = u64::from_str_radix(&key_string[i..i+2], 16).unwrap();
168-
for j in 0..8 {
169-
key[8*(i>>1) + j] = val % 2;
170-
val >>= 1;
171-
}
172-
}
167+
for i in (0..key_string.len()).step_by(2) {
168+
let mut val = u64::from_str_radix(&key_string[i..i+2], 16).unwrap();
169+
for j in 0..8 {
170+
key[8*(i>>1) + j] = val % 2;
171+
val >>= 1;
172+
}
173+
}
173174

174-
let iv_string = "0D74DB42A91077DE45AC".to_string();
175-
let mut iv = [0; 80];
175+
let iv_string = "0D74DB42A91077DE45AC".to_string();
176+
let mut iv = [0; 80];
176177

177-
for i in (0..iv_string.len()).step_by(2) {
178-
let mut val = u64::from_str_radix(&iv_string[i..i+2], 16).unwrap();
179-
for j in 0..8 {
180-
iv[8*(i>>1) + j] = val % 2;
181-
val >>= 1;
182-
}
183-
}
184-
let output_0_63 = "F4CD954A717F26A7D6930830C4E7CF0819F80E03F25F342C64ADC66ABA7F8A8E6EAA49F23632AE3CD41A7BD290A0132F81C6D4043B6E397D7388F3A03B5FE358".to_string();
178+
for i in (0..iv_string.len()).step_by(2) {
179+
let mut val = u64::from_str_radix(&iv_string[i..i+2], 16).unwrap();
180+
for j in 0..8 {
181+
iv[8*(i>>1) + j] = val % 2;
182+
val >>= 1;
183+
}
184+
}
185+
let output_0_63 = "F4CD954A717F26A7D6930830C4E7CF0819F80E03F25F342C64ADC66ABA7F8A8E6EAA49F23632AE3CD41A7BD290A0132F81C6D4043B6E397D7388F3A03B5FE358".to_string();
185186

186-
let cipher_key = key.map(|x| client_key.encrypt(x));
187-
let cipher_iv = iv.map(|x| client_key.encrypt(x));
187+
let cipher_key = key.map(|x| client_key.encrypt(x));
188+
let cipher_iv = iv.map(|x| client_key.encrypt(x));
188189

189-
let mut ciphered_message = vec![FheUint64::try_encrypt(0u64, &hl_client_key).unwrap(); 9];
190+
let mut ciphered_message = vec![FheUint64::try_encrypt(0u64, &hl_client_key).unwrap(); 9];
190191

191-
let mut trivium = TriviumStreamShortint::new(cipher_key, cipher_iv, &server_key, &ksk);
192+
let mut trivium = TriviumStreamShortint::new(cipher_key, cipher_iv, &server_key, &ksk);
192193

193-
let mut vec = Vec::<u64>::with_capacity(8);
194-
while vec.len() < 8 {
195-
let trans_ciphered_message = trivium.trans_encrypt_64(ciphered_message.pop().unwrap(), &hl_server_key);
196-
vec.push(trans_ciphered_message.decrypt(&hl_client_key));
197-
}
194+
let mut vec = Vec::<u64>::with_capacity(8);
195+
while vec.len() < 8 {
196+
let trans_ciphered_message = trivium.trans_encrypt_64(ciphered_message.pop().unwrap(), &hl_server_key);
197+
vec.push(trans_ciphered_message.decrypt(&hl_client_key));
198+
}
198199

199-
let hexadecimal = get_hexagonal_string_from_u64(vec);
200-
assert_eq!(output_0_63, hexadecimal[0..64*2]);
200+
let hexadecimal = get_hexagonal_string_from_u64(vec);
201+
assert_eq!(output_0_63, hexadecimal[0..64*2]);
201202
}
202203
```
203204

0 commit comments

Comments
 (0)