Skip to content

Commit 5cf3ffe

Browse files
committed
Add correlated OT
1 parent 94d1fd6 commit 5cf3ffe

File tree

7 files changed

+434
-54
lines changed

7 files changed

+434
-54
lines changed

.cargo/config.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
1+
[target.x86_64-unknown-linux-gnu]
2+
rustflags = ["-C", "target-cpu=native"]
3+
14
[build]
25
rustflags = "-C target-cpu=native"

cryprot-core/src/buf.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ use crate::alloc::{HugePageMemory, allocate_zeroed_vec};
1414
pub trait Buf<T>:
1515
Default + Debug + Deref<Target = [T]> + DerefMut + Send + Sync + 'static + private::Sealed
1616
{
17+
/// The 'kind' of this Buf implementation. This is e.g. `Vec<E>` for `Vec<T>
18+
/// as Buf<T>`.
19+
///
20+
/// This associated type can be used to create Bufs of the same kind, but
21+
/// with a different inner type.
22+
type BufKind<E>: Buf<E>
23+
where
24+
E: Zeroable + Clone + Default + Debug + Send + Sync + 'static;
1725
/// Create a new `Buf` of length `len` with all elements set to zero.
1826
///
1927
/// Implementations of this directly allocate zeroed memory and do not write
@@ -41,6 +49,11 @@ pub trait Buf<T>:
4149
}
4250

4351
impl<T: Zeroable + Clone + Default + Debug + Send + Sync + 'static> Buf<T> for Vec<T> {
52+
type BufKind<E>
53+
= Vec<E>
54+
where
55+
E: Zeroable + Clone + Default + Debug + Send + Sync + 'static;
56+
4457
fn zeroed(len: usize) -> Self {
4558
allocate_zeroed_vec(len)
4659
}
@@ -66,6 +79,11 @@ impl<T: Zeroable + Clone + Default + Debug + Send + Sync + 'static> Buf<T> for V
6679
}
6780

6881
impl<T: Zeroable + Clone + Default + Debug + Send + Sync + 'static> Buf<T> for HugePageMemory<T> {
82+
type BufKind<E>
83+
= HugePageMemory<E>
84+
where
85+
E: Zeroable + Clone + Default + Debug + Send + Sync + 'static;
86+
6987
fn zeroed(len: usize) -> Self {
7088
HugePageMemory::zeroed(len)
7189
}

cryprot-ot/benches/bench.rs

Lines changed: 80 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use criterion::{BatchSize, Criterion, criterion_group, criterion_main};
77
use cryprot_core::{Block, alloc::HugePageMemory};
88
use cryprot_net::testing::{init_bench_tracing, local_conn};
99
use cryprot_ot::{
10-
RotReceiver, RotSender,
10+
CotReceiver, CotSender, RotReceiver, RotSender,
1111
base::SimplestOt,
1212
extension::{
1313
MaliciousOtExtensionReceiver, MaliciousOtExtensionSender, SemiHonestOtExtensionReceiver,
@@ -99,7 +99,7 @@ fn criterion_benchmark(c: &mut Criterion) {
9999
}),
100100
tokio::spawn(async move {
101101
receiver
102-
.receive_into(&choices, &mut receiver_ots)
102+
.receive_into(&mut receiver_ots, &choices)
103103
.await
104104
.unwrap();
105105
receiver_ots
@@ -113,64 +113,53 @@ fn criterion_benchmark(c: &mut Criterion) {
113113
})
114114
});
115115

116-
g.bench_function(format!("2 parallel 2**{p} extension OTs"), |b| {
116+
g.bench_function(format!("2**{p} correlated extension OTs"), |b| {
117117
b.to_async(&rt).iter_custom(|iters| {
118118
let mut c11 = c1.sub_connection();
119119
let mut c22 = c2.sub_connection();
120+
120121
async move {
121122
let mut duration = Duration::ZERO;
123+
let mut sender_ots = HugePageMemory::zeroed(count);
124+
let mut receiver_ots = HugePageMemory::zeroed(count);
122125
for _ in 0..iters {
123-
let (
124-
mut sender1,
125-
mut receiver1,
126-
mut sender2,
127-
mut receiver2,
128-
choices1,
129-
choices2,
130-
) = {
126+
// setup not included in duration
127+
let (mut sender, mut receiver, choices) = {
131128
let mut rng1 = StdRng::seed_from_u64(42);
132-
let mut rng2 = StdRng::seed_from_u64(42 * 42);
133-
let choices1 = random_choices(count, &mut rng1);
134-
let choices2 = random_choices(count, &mut rng2);
135-
let mut sender1 = SemiHonestOtExtensionSender::new_with_rng(
136-
c11.sub_connection(),
137-
rng1.clone(),
138-
);
139-
let mut receiver1 = SemiHonestOtExtensionReceiver::new_with_rng(
140-
c22.sub_connection(),
141-
rng2.clone(),
142-
);
143-
144-
let mut sender2 =
129+
let rng2 = StdRng::seed_from_u64(42 * 42);
130+
let choices = random_choices(count, &mut rng1);
131+
let mut sender =
145132
SemiHonestOtExtensionSender::new_with_rng(c11.sub_connection(), rng1);
146-
let mut receiver2 =
133+
let mut receiver =
147134
SemiHonestOtExtensionReceiver::new_with_rng(c22.sub_connection(), rng2);
148-
149-
tokio::try_join!(
150-
sender1.do_base_ots(),
151-
receiver1.do_base_ots(),
152-
sender2.do_base_ots(),
153-
receiver2.do_base_ots()
154-
)
155-
.unwrap();
156-
(sender1, receiver1, sender2, receiver2, choices1, choices2)
135+
tokio::try_join!(sender.do_base_ots(), receiver.do_base_ots()).unwrap();
136+
(sender, receiver, choices)
157137
};
158138
let now = Instant::now();
159-
let jh1 = tokio::spawn(async move { sender1.send(count).await });
160-
let jh2 = tokio::spawn(async move { receiver1.receive(&choices1).await });
161-
let jh3 = tokio::spawn(async move { sender2.send(count).await });
162-
let jh4 = tokio::spawn(async move { receiver2.receive(&choices2).await });
163-
let (ot1, ot2, ot3, ot4) = tokio::try_join!(jh1, jh2, jh3, jh4).unwrap();
139+
(sender_ots, receiver_ots) = tokio::try_join!(
140+
tokio::spawn(async move {
141+
sender
142+
.correlated_send_into(&mut sender_ots, |_| Block::ONES)
143+
.await
144+
.unwrap();
145+
sender_ots
146+
}),
147+
tokio::spawn(async move {
148+
receiver
149+
.correlated_receive_into(&mut receiver_ots, &choices)
150+
.await
151+
.unwrap();
152+
receiver_ots
153+
})
154+
)
155+
.unwrap();
164156
duration += now.elapsed();
165-
ot1.unwrap();
166-
ot2.unwrap();
167-
ot3.unwrap();
168-
ot4.unwrap();
169157
}
170158
duration
171159
}
172160
})
173161
});
162+
174163
g.finish();
175164

176165
let mut g = c.benchmark_group("malicious OT extension");
@@ -206,7 +195,54 @@ fn criterion_benchmark(c: &mut Criterion) {
206195
}),
207196
tokio::spawn(async move {
208197
receiver
209-
.receive_into(&choices, &mut receiver_ots)
198+
.receive_into(&mut receiver_ots, &choices)
199+
.await
200+
.unwrap();
201+
receiver_ots
202+
})
203+
)
204+
.unwrap();
205+
duration += now.elapsed();
206+
}
207+
duration
208+
}
209+
})
210+
});
211+
212+
g.bench_function(format!("2**{p} correlated extension OTs"), |b| {
213+
b.to_async(&rt).iter_custom(|iters| {
214+
let mut c11 = c1.sub_connection();
215+
let mut c22 = c2.sub_connection();
216+
217+
async move {
218+
let mut duration = Duration::ZERO;
219+
let mut sender_ots = HugePageMemory::zeroed(count);
220+
let mut receiver_ots = HugePageMemory::zeroed(count);
221+
for _ in 0..iters {
222+
// setup not included in duration
223+
let (mut sender, mut receiver, choices) = {
224+
let mut rng1 = StdRng::seed_from_u64(42);
225+
let rng2 = StdRng::seed_from_u64(42 * 42);
226+
let choices = random_choices(count, &mut rng1);
227+
let mut sender =
228+
MaliciousOtExtensionSender::new_with_rng(c11.sub_connection(), rng1);
229+
let mut receiver =
230+
MaliciousOtExtensionReceiver::new_with_rng(c22.sub_connection(), rng2);
231+
tokio::try_join!(sender.do_base_ots(), receiver.do_base_ots()).unwrap();
232+
(sender, receiver, choices)
233+
};
234+
let now = Instant::now();
235+
(sender_ots, receiver_ots) = tokio::try_join!(
236+
tokio::spawn(async move {
237+
sender
238+
.correlated_send_into(&mut sender_ots, |_| Block::ONES)
239+
.await
240+
.unwrap();
241+
sender_ots
242+
}),
243+
tokio::spawn(async move {
244+
receiver
245+
.correlated_receive_into(&mut receiver_ots, &choices)
210246
.await
211247
.unwrap();
212248
receiver_ots

0 commit comments

Comments
 (0)