stoffelcrypto 0.1.0

Asynchronous HoneyBadgerMPC protocols, preprocessing, and arithmetic for Stoffel.
Documentation
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
use crate::{
    common::{lagrange_interpolate, rbc::RbcError, share::ShareError},
    honeybadger::{
        batch_recon::BatchReconError,
        fpmul::f256::{Gf256, Gf256Error},
        mul::MulError,
        robust_interpolate::{robust_interpolate::RobustShare, InterpolateError},
        SessionId,
    },
};
use ark_ff::{BigInteger, FftField, PrimeField};
use ark_poly::{univariate::DensePolynomial, EvaluationDomain, GeneralEvaluationDomain};
use ark_serialize::SerializationError;
use bincode::ErrorKind;
use num_bigint::BigUint;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use stoffelnet::network_utils::NetworkError;
use thiserror::Error;
use tokio::sync::oneshot::{channel, Receiver, Sender};

pub mod f256;
pub mod fpmul;
pub mod prandbitd;
pub mod rand_bit;
pub mod truncpr;
//--------------------------------------------Rand-bit--------------------------------------------
#[derive(Error, Debug)]
pub enum RandBitError {
    #[error("incompatible treshold ({0:}) and number of parties {1:}")]
    IncompatibleNumberOfParties(usize, usize),
    #[error("the square multiplication was not completed successfuly")]
    SquareMult(#[from] MulError),
    #[error("the square is zero")]
    ZeroSquare,
    #[error("the square root does not exist")]
    SquareRoot,
    #[error("the inverse does not exist")]
    Inverse,
    #[error("number of random shares is not a multiple of (t+1)")]
    Incompatible,
    #[error("Duplicate input: {0}")]
    Duplicate(String),
    #[error("waiting for more openings")]
    WaitForOk,
    #[error("error in batch reconstruction: {0:?}")]
    BatchRecError(#[from] BatchReconError),
    #[error("error during deserialization: {0:?}")]
    SerializationError(#[from] SerializationError),
    #[error("error operating with the shares: {0:?}")]
    ShareError(#[from] ShareError),
    #[error("error sending the result: {0:?}")]
    SendError(SessionId),
    #[error("error receiving the result: {0:?}")]
    ReceiveError(SessionId),
    #[error("storage limit exceeded: {0}")]
    LimitError(String),
    #[error("no such session ID exists: {0:?}")]
    NoSuchSessionId(SessionId),
    #[error("unknown calling protocol in session ID {0:?}")]
    SessionIdError(SessionId),
    #[error("cannot create {0:?} random bits at once")]
    ShareLimitError(usize),
    #[error("result already received: {0:?}")]
    ResultAlreadyReceived(SessionId),
    #[error("multiplication {0:?} did not complete in time")]
    Timeout(SessionId),
    #[error("received abort signal")]
    Abort,
    #[error("Session id {0:?} does not exist in store")]
    ClearStoreError(SessionId),
}

#[derive(Copy, Clone, Debug, PartialEq)]
pub enum ProtocolState {
    Initialized,
    NotInitialized,
    Finished,
}

#[derive(Debug)]
pub struct RandBitStorage<F>
where
    F: FftField,
{
    /// State of the protocol.
    pub protocol_state: ProtocolState,
    /// Output of the protocol. If the protocol is not finished yet, `protocol_output` will be
    /// [`None`].
    pub protocol_output: Option<Vec<RobustShare<F>>>,
    /// Share of `a`
    pub a_share: Option<Vec<RobustShare<F>>>,
    pub output_open: HashMap<u8, Vec<F>>,
    pub output_sender: Option<Sender<Vec<RobustShare<F>>>>,
    pub output_receiver: Option<Receiver<Vec<RobustShare<F>>>>,
}

impl<F> RandBitStorage<F>
where
    F: FftField,
{
    pub fn empty() -> Self {
        let (output_sender, output_receiver) = channel();
        Self {
            protocol_state: ProtocolState::NotInitialized,
            protocol_output: None,
            a_share: None,
            output_open: HashMap::new(),
            output_sender: Some(output_sender),
            output_receiver: Some(output_receiver),
        }
    }
}

//--------------------------------------------Prandbitd--------------------------------------------
#[derive(Debug, Error)]
pub enum PRandError {
    /// The parameters for the precision are too big
    #[error("the parameters for k and l surpassed the field capacity")]
    SurpassedFieldCapacity,
    /// The error occurs when communicating using the network.
    #[error("there was an error in the network: {0:?}")]
    NetworkError(#[from] NetworkError),
    #[error("error while serializing an arkworks object: {0:?}")]
    ArkSerialization(#[from] SerializationError),
    #[error("error while serializing an arkworks object: {0:?}")]
    ArkDeserialization(SerializationError),
    #[error("error while serializing the object into bytes: {0:?}")]
    SerializationError(#[from] Box<ErrorKind>),
    /// The protocol received an abort signal.
    #[error("received abort signal")]
    Abort,
    #[error("Duplicate input: {0}")]
    Duplicate(String),
    #[error("number of random shares is not a multiple of (t+1)")]
    Incompatible,
    #[error("Not set:{0}")]
    NotSet(String),
    #[error("ShareError: {0}")]
    ShareError(#[from] ShareError),
    #[error("unknown calling protocol in session ID {0:?}")]
    SessionIdError(SessionId),
    #[error("error sending the result: {0:?}")]
    SendError(SessionId),
    #[error("error receiving the result: {0:?}")]
    ReceiveError(SessionId),
    #[error("F2_8 Error: {0}")]
    F2_8Error(#[from] Gf256Error),
    #[error("InterpolateError: {0}")]
    InterpolateError(#[from] InterpolateError),
    #[error("error in batch reconstruction: {0:?}")]
    BatchRecError(#[from] BatchReconError),
    #[error("no such session ID exists: {0:?}")]
    NoSuchSessionId(SessionId),
    #[error("result already received: {0:?}")]
    ResultAlreadyReceived(SessionId),
    #[error("multiplication {0:?} did not complete in time")]
    Timeout(SessionId),
    #[error("Session id {0:?} does not exist in store")]
    ClearStoreError(SessionId),
    #[error("Store Limit")]
    LimitError,
    #[error("Invalid message: {0}")]
    InvalidMessage(String),
}

/// Message sent in the Random Double Sharing protocol.
#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct PRandBitDMessage {
    /// ID of the sender of the message.
    pub sender_id: usize,
    pub session_id: SessionId,
    pub tset: Vec<usize>,
    pub r_t: Vec<BigUint>,
    pub payload: Vec<u8>,
}

impl PRandBitDMessage {
    /// Creates a new PRandBitDMessage.
    pub fn new(
        sender_id: usize,
        session_id: SessionId,
        tset: Vec<usize>,
        r_t: Vec<BigUint>,
        payload: Vec<u8>,
    ) -> Self {
        Self {
            sender_id,
            session_id,
            tset,
            r_t,
            payload,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PrandState {
    Initialized,
    BitFinished,
    IntFinished,
}

#[derive(Debug)]
pub struct PRandBitDStore<F: PrimeField, G: PrimeField> {
    /// For every maximal unqualified set T that excludes this player,
    /// we store the full mask r_T = sum_i r_T^i
    pub batch_size: Option<usize>,
    /// Messages that arrived before batch_size/r_t_bound were set; reprocessed once initialized.
    pub pending_riss_messages: Vec<PRandBitDMessage>,
    pub output_open: HashMap<u8, Vec<F>>,
    pub riss_shares: HashMap<Vec<usize>, HashMap<usize, Vec<BigUint>>>, // tset -> {sender -> val}
    pub r_t: HashMap<Vec<usize>, Vec<BigUint>>,
    pub no_of_tsets: Option<usize>,
    pub share_r_q: Option<Vec<RobustShare<F>>>, //smaller field
    pub share_r_p: Option<Vec<RobustShare<G>>>, // PrandInt output
    pub share_b_q: Option<Vec<RobustShare<F>>>, //smaller field
    pub share_r_2: Option<Vec<Gf256>>,
    pub share_b_2: Vec<Gf256>,          //PrandBitD output
    pub share_b_p: Vec<RobustShare<G>>, //PrandBitD/PrandBitL output
    pub state: PrandState,
    pub output_bit_sender: Option<Sender<Vec<(RobustShare<G>, Gf256)>>>,
    pub output_int_sender: Option<Sender<Vec<RobustShare<G>>>>,
    pub output_bit_receiver: Option<Receiver<Vec<(RobustShare<G>, Gf256)>>>,
    pub output_int_receiver: Option<Receiver<Vec<RobustShare<G>>>>,
    pub open_started: bool,
    pub r_t_bound: Option<BigUint>,
}

impl<F: PrimeField, G: PrimeField> PRandBitDStore<F, G> {
    pub fn empty() -> Self {
        let (output_bit_sender, output_bit_receiver) = channel();
        let (output_int_sender, output_int_receiver) = channel();
        Self {
            batch_size: None,
            pending_riss_messages: Vec::new(),
            output_open: HashMap::new(),
            riss_shares: HashMap::new(),
            r_t: HashMap::new(),
            no_of_tsets: None,
            share_r_q: None,
            share_r_p: None,
            share_b_q: None,
            share_r_2: None,
            share_b_2: Vec::new(),
            share_b_p: Vec::new(),
            state: PrandState::Initialized,
            output_bit_sender: Some(output_bit_sender),
            output_int_sender: Some(output_int_sender),
            output_bit_receiver: Some(output_bit_receiver),
            output_int_receiver: Some(output_int_receiver),
            open_started: false,
            r_t_bound: None,
        }
    }
}

pub fn build_all_f_polys<H: PrimeField>(
    n: usize,
    tsets: Vec<Vec<usize>>,
) -> Result<HashMap<Vec<usize>, DensePolynomial<H>>, ShareError> {
    let domain =
        GeneralEvaluationDomain::<H>::new(n).ok_or_else(|| ShareError::NoSuitableDomain(n))?;
    tsets
        .into_iter()
        .map(|tset| {
            // Construct interpolation points
            let xs = std::iter::once(H::zero())
                .chain(tset.iter().map(|j| domain.element(*j)))
                .collect::<Vec<_>>();
            let ys = std::iter::once(H::one())
                .chain(std::iter::repeat(H::zero()).take(tset.len()))
                .collect::<Vec<_>>();
            // Interpolate polynomial
            let poly = lagrange_interpolate(&xs, &ys)?;
            Ok((tset, poly))
        })
        .collect()
}

//--------------------------------------------TruncPr--------------------------------------------
#[derive(Debug, Error)]
pub enum TruncPrError {
    /// The error occurs when communicating using the network.
    #[error("there was an error in the network: {0:?}")]
    NetworkError(#[from] NetworkError),
    #[error("error while serializing an arkworks object: {0:?}")]
    ArkSerialization(#[from] SerializationError),
    #[error("error while serializing an arkworks object: {0:?}")]
    ArkDeserialization(SerializationError),
    #[error("error while serializing the object into bytes: {0:?}")]
    SerializationError(#[from] Box<ErrorKind>),
    /// The protocol received an abort signal.
    #[error("received abort signal")]
    Abort,
    #[error("Duplicate input: {0}")]
    Duplicate(usize),
    #[error("Rbc error: {0}")]
    RbcError(#[from] RbcError),
    #[error("ShareError: {0}")]
    ShareError(#[from] ShareError),
    #[error("error sending the result: {0:?}")]
    SendError(SessionId),
    #[error("error receiving the result: {0:?}")]
    ReceiveError(SessionId),
    #[error("InterpolateError: {0}")]
    InterpolateError(#[from] InterpolateError),
    #[error("malformed session ID {0:?}")]
    SessionIdError(SessionId),
    #[error("no such session ID exists: {0:?}")]
    NoSuchSessionId(SessionId),
    #[error("result already received: {0:?}")]
    ResultAlreadyReceived(SessionId),
    #[error("multiplication {0:?} did not complete in time")]
    Timeout(SessionId),
    #[error("Session id {0:?} does not exist in store")]
    ClearStoreError(SessionId),
    #[error("Store Limit")]
    LimitError,
}

#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct TruncPrMessage {
    pub sender_id: usize,
    pub session_id: SessionId,
    pub payload: Vec<u8>,
}

impl TruncPrMessage {
    pub fn new(sender_id: usize, session_id: SessionId, share_bytes: Vec<u8>) -> Self {
        Self {
            sender_id,
            session_id,
            payload: share_bytes,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TruncState {
    Initialized,
    Finished,
}

#[derive(Debug)]
pub struct TruncPrStore<F: PrimeField> {
    pub m: usize,
    pub k: usize,
    pub r_dash: Option<RobustShare<F>>, // [r'] = sum 2^i [r_i]
    pub share_a: Option<RobustShare<F>>,
    pub open_buf: HashMap<usize, RobustShare<F>>, // sender_id -> share of (b + r)
    pub share_d: Option<RobustShare<F>>,          // [d]
    pub state: TruncState,
    pub output_sender: Option<Sender<RobustShare<F>>>,
    pub output_receiver: Option<Receiver<RobustShare<F>>>,
}

impl<F: PrimeField> TruncPrStore<F> {
    pub fn empty() -> Self {
        let (output_sender, output_receiver) = channel();
        Self {
            m: 0,
            k: 0,
            r_dash: None,
            share_a: None,
            open_buf: HashMap::new(),
            share_d: None,
            state: TruncState::Initialized,
            output_sender: Some(output_sender),
            output_receiver: Some(output_receiver),
        }
    }
}

// ---------- helpers ----------

pub fn pow2_f<F: PrimeField>(e: usize) -> F {
    F::from(2u64).pow(&[e as u64])
}

pub fn mod_pow_2_from_field<F: PrimeField>(x: F, m: usize) -> F {
    let bigint = x.into_bigint();
    let mut bytes = bigint.to_bytes_le();

    let full_bytes = m / 8;
    let extra_bits = m % 8;

    let usable_bytes = if extra_bits == 0 {
        full_bytes
    } else {
        full_bytes + 1
    };

    // Truncate extra bytes
    if bytes.len() > usable_bytes {
        bytes.truncate(usable_bytes);
    }

    if extra_bits > 0 && !bytes.is_empty() {
        let mask = (1u8 << extra_bits) - 1;
        bytes[full_bytes] &= mask;
    }

    // Interpret as an integer modulo q and return it as a field element.
    F::from_le_bytes_mod_order(&bytes)
}