1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
//! Oblivious transfer traits.
use crate::util::Block;
use async_trait::async_trait;
use bitvec::slice::BitSlice;
use bitvec::store::BitStore;
use bytemuck::Pod;
use rand::{CryptoRng, RngCore};
use remoc::rch::mpsc::{RecvError, SendError};
use std::fmt::Debug;
use thiserror::Error;

#[derive(Error, Debug)]
pub enum Error<Msg> {
    #[error("Error sending value")]
    Send(#[from] SendError<Msg>),
    #[error("Error receiving value")]
    Receive(#[from] RecvError),
    #[error("Received out of order message")]
    WrongOrder(Msg),
    #[error("The other party terminated the protocol")]
    UnexpectedTermination,
    #[error("The other party deviated from the protocol")]
    ProtocolDeviation,
    #[error("Error in base OT execution")]
    BaseOT(Box<dyn std::error::Error + Send>),
}

/// Sender of base random OTs.
#[async_trait]
pub trait BaseROTSender {
    type Msg;

    /// Send `count` number of random OTs via the provided channel.
    async fn send_random<RNG>(
        &mut self,
        count: usize,
        rng: &mut RNG,
        sender: &seec_channel::Sender<Self::Msg>,
        receiver: &mut seec_channel::Receiver<Self::Msg>,
    ) -> Result<Vec<[Block; 2]>, Error<Self::Msg>>
    where
        RNG: RngCore + CryptoRng + Send;
}

/// Receiver of base random OTs.
#[async_trait]
pub trait BaseROTReceiver {
    type Msg;

    /// Receive `count` number of random OTs via the provided channel.
    async fn receive_random<RNG>(
        &mut self,
        choices: &BitSlice,
        rng: &mut RNG,
        sender: &seec_channel::Sender<Self::Msg>,
        receiver: &mut seec_channel::Receiver<Self::Msg>,
    ) -> Result<Vec<Block>, Error<Self::Msg>>
    where
        RNG: RngCore + CryptoRng + Send;
}

/// OT extension sender.
#[async_trait]
pub trait ExtROTSender {
    type Msg;

    async fn send_random<RNG>(
        &mut self,
        count: usize,
        rng: &mut RNG,
        sender: &seec_channel::Sender<Self::Msg>,
        receiver: &mut seec_channel::Receiver<Self::Msg>,
    ) -> Result<Vec<[Block; 2]>, Error<Self::Msg>>
    where
        RNG: RngCore + CryptoRng + Send;

    async fn send_correlated<RNG>(
        &mut self,
        count: usize,
        correlation: impl Fn(usize, Block) -> Block + Send,
        rng: &mut RNG,
        sender: &seec_channel::Sender<Self::Msg>,
        receiver: &mut seec_channel::Receiver<Self::Msg>,
    ) -> Result<Vec<Block>, Error<Self::Msg>>
    where
        RNG: RngCore + CryptoRng + Send;

    async fn send_correlated_bytes<const LEN: usize, RNG>(
        &mut self,
        count: usize,
        correlation: impl Fn(usize, [u8; LEN]) -> [u8; LEN] + Send,
        rng: &mut RNG,
        sender: &seec_channel::Sender<Self::Msg>,
        receiver: &mut seec_channel::Receiver<Self::Msg>,
    ) -> Result<Vec<[u8; LEN]>, Error<Self::Msg>>
    where
        RNG: RngCore + CryptoRng + Send,
        [u8; LEN]: Pod;
}

/// OT extension receiver.
#[async_trait]
pub trait ExtROTReceiver {
    type Msg;

    async fn receive_random<C, RNG>(
        &mut self,
        choices: &BitSlice<C>,
        rng: &mut RNG,
        sender: &seec_channel::Sender<Self::Msg>,
        receiver: &mut seec_channel::Receiver<Self::Msg>,
    ) -> Result<Vec<Block>, Error<Self::Msg>>
    where
        RNG: RngCore + CryptoRng + Send,
        C: Pod + BitStore + Sync,
        <C as BitStore>::Unalias: Pod;

    async fn receive_correlated<C, RNG>(
        &mut self,
        choices: &BitSlice<C>,
        rng: &mut RNG,
        sender: &seec_channel::Sender<Self::Msg>,
        receiver: &mut seec_channel::Receiver<Self::Msg>,
    ) -> Result<Vec<Block>, Error<Self::Msg>>
    where
        RNG: RngCore + CryptoRng + Send,
        C: Pod + BitStore + Sync,
        <C as BitStore>::Unalias: Pod;

    async fn receive_correlated_bytes<const LEN: usize, C, RNG>(
        &mut self,
        choices: &BitSlice<C>,
        rng: &mut RNG,
        sender: &seec_channel::Sender<Self::Msg>,
        receiver: &mut seec_channel::Receiver<Self::Msg>,
    ) -> Result<Vec<[u8; LEN]>, Error<Self::Msg>>
    where
        RNG: RngCore + CryptoRng + Send,
        [u8; LEN]: Pod,
        C: Pod + BitStore + Sync,
        <C as BitStore>::Unalias: Pod;
}