Skip to content

Commit fd3708a

Browse files
added bandwidth optimized decryption
1 parent da12e99 commit fd3708a

3 files changed

Lines changed: 331 additions & 7 deletions

File tree

benches/bte_bench.rs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ struct FoBenchContext {
3131
cts: Vec<fo::FoCiphertext<E>>,
3232
combined_pd: <E as Pairing>::G1,
3333
hints: fo::DecryptionHints<E>,
34+
bandwidth_hints: fo::BandwidthDecryptionHints<E>,
3435
}
3536

3637
fn make_context(batch_size: usize, num_parties: usize, threshold: usize) -> BenchContext {
@@ -87,7 +88,10 @@ fn make_fo_context(batch_size: usize, num_parties: usize, threshold: usize) -> F
8788
.map(|sk| fo::partial_decrypt(sk, &cts))
8889
.collect();
8990
let combined_pd = fo::combine::<E>(&pds);
90-
let (_, hints) = fo::helper_decrypt(&dk, &combined_pd, &cts);
91+
let cross = fo::predecrypt_fft(&dk, &cts);
92+
let (_, hints) = fo::helper_finalize(&dk, &combined_pd, &cts, &cross);
93+
let (_, bandwidth_hints) =
94+
fo::helper_finalize_bandwidth_optimized(&dk, &combined_pd, &cts, &cross);
9195

9296
FoBenchContext {
9397
ek,
@@ -96,6 +100,7 @@ fn make_fo_context(batch_size: usize, num_parties: usize, threshold: usize) -> F
96100
cts,
97101
combined_pd,
98102
hints,
103+
bandwidth_hints,
99104
}
100105
}
101106

@@ -221,6 +226,27 @@ fn bench_fo_batch_verify(c: &mut Criterion) {
221226
group.finish();
222227
}
223228

229+
fn bench_fo_batch_verify_bandwidth_optimized(c: &mut Criterion) {
230+
let mut group = c.benchmark_group("fo_batch_verify_bandwidth_optimized");
231+
group.sample_size(10);
232+
233+
for &b in &[8, 32, 128, 512, 2048] {
234+
let ctx = make_fo_context(b, 100, 50);
235+
group.bench_with_input(BenchmarkId::from_parameter(b), &b, |bench, _| {
236+
let mut rng = test_rng();
237+
bench.iter(|| {
238+
fo::batch_verify_bandwidth_optimized(
239+
&ctx.ek,
240+
&ctx.cts,
241+
&ctx.bandwidth_hints,
242+
&mut rng,
243+
)
244+
});
245+
});
246+
}
247+
group.finish();
248+
}
249+
224250
criterion_group!(
225251
name = benches;
226252
config = Criterion::default().measurement_time(Duration::from_secs(5));
@@ -233,5 +259,6 @@ criterion_group!(
233259
bench_fo_encrypt,
234260
bench_fo_helper_decrypt,
235261
bench_fo_batch_verify,
262+
bench_fo_batch_verify_bandwidth_optimized,
236263
);
237264
criterion_main!(benches);

examples/fo_e2e.rs

Lines changed: 73 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use ark_bls12_381::Bls12_381;
22
use ark_std::test_rng;
33
use simple_batched_threshold_encryption::bte::{
44
crs::setup,
5+
fo,
56
fo::{
67
batch_verify, combine, encrypt, helper_decrypt, helper_finalize, partial_decrypt,
78
predecrypt_fft, verify_partial_decryption,
@@ -18,16 +19,28 @@ enum Mode {
1819
Pipelined,
1920
}
2021

22+
#[derive(Clone, Copy, Debug)]
23+
enum HintMode {
24+
VerificationOptimized,
25+
BandwidthOptimized,
26+
}
27+
2128
#[derive(Clone, Debug)]
2229
struct Config {
2330
batch_size: usize,
2431
num_parties: usize,
2532
threshold: usize,
2633
iterations: usize,
2734
mode: Mode,
35+
hint_mode: HintMode,
2836
msg_len: usize,
2937
}
3038

39+
enum Hints {
40+
Verification(fo::DecryptionHints<E>),
41+
Bandwidth(fo::BandwidthDecryptionHints<E>),
42+
}
43+
3144
#[derive(Default)]
3245
struct Timing {
3346
setup: Duration,
@@ -99,14 +112,32 @@ fn main() {
99112

100113
// -- Helper: expensive decrypt + hint generation ------------------
101114
let (predecrypt_time, helper_finalize_time, helper_total_time, helper_msgs, hints) =
102-
match config.mode {
103-
Mode::Direct => {
115+
match (config.mode, config.hint_mode) {
116+
(Mode::Direct, HintMode::VerificationOptimized) => {
104117
let start = Instant::now();
105118
let (msgs, hints) = helper_decrypt(&dk, &pd, &cts);
106119
let total = start.elapsed();
107-
(Duration::ZERO, Duration::ZERO, total, msgs, hints)
120+
(
121+
Duration::ZERO,
122+
Duration::ZERO,
123+
total,
124+
msgs,
125+
Hints::Verification(hints),
126+
)
108127
}
109-
Mode::Pipelined => {
128+
(Mode::Direct, HintMode::BandwidthOptimized) => {
129+
let start = Instant::now();
130+
let (msgs, hints) = fo::helper_decrypt_bandwidth_optimized(&dk, &pd, &cts);
131+
let total = start.elapsed();
132+
(
133+
Duration::ZERO,
134+
Duration::ZERO,
135+
total,
136+
msgs,
137+
Hints::Bandwidth(hints),
138+
)
139+
}
140+
(Mode::Pipelined, HintMode::VerificationOptimized) => {
110141
let start = Instant::now();
111142
let cross = predecrypt_fft(&dk, &cts);
112143
let pre = start.elapsed();
@@ -115,7 +146,19 @@ fn main() {
115146
let (msgs, hints) = helper_finalize(&dk, &pd, &cts, &cross);
116147
let fin = start.elapsed();
117148

118-
(pre, fin, pre + fin, msgs, hints)
149+
(pre, fin, pre + fin, msgs, Hints::Verification(hints))
150+
}
151+
(Mode::Pipelined, HintMode::BandwidthOptimized) => {
152+
let start = Instant::now();
153+
let cross = predecrypt_fft(&dk, &cts);
154+
let pre = start.elapsed();
155+
156+
let start = Instant::now();
157+
let (msgs, hints) =
158+
fo::helper_finalize_bandwidth_optimized(&dk, &pd, &cts, &cross);
159+
let fin = start.elapsed();
160+
161+
(pre, fin, pre + fin, msgs, Hints::Bandwidth(hints))
119162
}
120163
};
121164

@@ -128,7 +171,12 @@ fn main() {
128171

129172
// -- Verifier: batch verify (no pairings!) ------------------------
130173
let start = Instant::now();
131-
let verified = batch_verify(&ek, &cts, &hints, &mut rng);
174+
let verified = match &hints {
175+
Hints::Verification(hints) => batch_verify(&ek, &cts, hints, &mut rng),
176+
Hints::Bandwidth(hints) => {
177+
fo::batch_verify_bandwidth_optimized(&ek, &cts, hints, &mut rng)
178+
}
179+
};
132180
let batch_verify_time = start.elapsed();
133181

134182
let verified_msgs = verified.expect("batch verification failed");
@@ -180,6 +228,7 @@ fn parse_args() -> Config {
180228
threshold: 5,
181229
iterations: 1,
182230
mode: Mode::Pipelined,
231+
hint_mode: HintMode::VerificationOptimized,
183232
msg_len: 256,
184233
};
185234

@@ -201,6 +250,9 @@ fn parse_args() -> Config {
201250
"--mode" | "-m" => {
202251
config.mode = parse_mode_arg(args.next());
203252
}
253+
"--hint-mode" => {
254+
config.hint_mode = parse_hint_mode_arg(args.next());
255+
}
204256
"--msg-len" | "-l" => {
205257
config.msg_len = parse_usize_arg("--msg-len", args.next());
206258
}
@@ -239,6 +291,19 @@ fn parse_mode_arg(value: Option<String>) -> Mode {
239291
}
240292
}
241293

294+
fn parse_hint_mode_arg(value: Option<String>) -> HintMode {
295+
match value
296+
.unwrap_or_else(|| panic!("missing value for --hint-mode"))
297+
.as_str()
298+
{
299+
"verification" => HintMode::VerificationOptimized,
300+
"bandwidth" => HintMode::BandwidthOptimized,
301+
other => panic!(
302+
"invalid hint mode: {other} (expected 'verification' or 'bandwidth')"
303+
),
304+
}
305+
}
306+
242307
fn print_help_and_exit() -> ! {
243308
println!("Usage: cargo run --release --example fo_e2e -- [options]");
244309
println!();
@@ -248,6 +313,7 @@ fn print_help_and_exit() -> ! {
248313
println!(" --threshold, -t Threshold (default: 5)");
249314
println!(" --iters, -i Iterations (default: 1)");
250315
println!(" --mode, -m Helper mode: direct | pipelined (default: pipelined)");
316+
println!(" --hint-mode Hint mode: verification | bandwidth (default: verification)");
251317
println!(" --msg-len, -l Per-message byte length (default: 256)");
252318
std::process::exit(0);
253319
}
@@ -279,6 +345,7 @@ fn print_run_header(config: &Config) {
279345
println!("msg length : {} bytes", config.msg_len);
280346
println!("iterations : {}", config.iterations);
281347
println!("mode : {:?}", config.mode);
348+
println!("hint mode : {:?}", config.hint_mode);
282349
println!();
283350
}
284351

0 commit comments

Comments
 (0)