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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
use crate::avss_mpc::input::{AvssInputError, AvssInputMessage};
use crate::avss_mpc::{deser_bounded_feldman_vec, AvssSessionId, AvssWrappedMessage, ProtocolType};
use crate::common::share::avss::verify_feldman;
use crate::common::share::feldman::FeldmanShamirShare;
use crate::common::{ProtocolSessionId, SecretSharingScheme, RBC};
use ark_ec::CurveGroup;
use ark_ff::FftField;
use ark_serialize::CanonicalSerialize;
use std::collections::HashMap;
use std::sync::Arc;
use stoffelnet::network_utils::{ClientId, Network, PartyId};
use tokio::{
    sync::{
        watch::{channel, Receiver, Sender},
        Mutex,
    },
    time::{timeout, Duration},
};
use tracing::info;

const MAX_INPUT_ELEMENTS: u64 = 65_536;

/// In the beginning of an MPC calculation, each node has to obtain a share of all clients' inputs.
/// This follows the same pattern as HoneyBadger's input protocol but uses FeldmanShamirShare
/// with Feldman commitment verification:
///   1. each server sends its random FeldmanShamirShare to the respective client,
///   2. once `t+1` verified shares received, the client reconstructs the random values per input
///      and broadcasts the input plus the random value to all nodes via RBC,
///   3. each server receives that masked value and subtracts their respective random share to
///      obtain a share of the input
///
/// The sending of random shares to clients does not use session IDs, since it only occurs once
/// before the computation has started. The RBC call to send masked inputs uses the session ID
///   `[caller=Input, exec=0, sub=client ID, round=0, instance=instance ID]`

#[derive(PartialEq, Clone, Debug)]
enum InputType {
    Empty,
    RandomShares,
    MaskedInputs,
    InputShares,
}

#[derive(Clone, Debug)]
pub struct AvssInputServer<F: FftField, R: RBC, G: CurveGroup<ScalarField = F>> {
    pub id: usize,
    pub n: usize,
    pub t: usize,
    pub rbc: R,
    pub rbc_output: Arc<Mutex<tokio::sync::mpsc::Receiver<AvssSessionId>>>,
    status_sender: Sender<HashMap<ClientId, (InputType, Vec<FeldmanShamirShare<F, G>>)>>,
    status_receiver: Receiver<HashMap<ClientId, (InputType, Vec<FeldmanShamirShare<F, G>>)>>,
}

/// Calculate input shares by subtracting random shares from masked inputs.
/// Given masked_value `m = input + r` (a public field element) and random share `r_i`
/// (a FeldmanShamirShare of `r`), computes `m - r_i` which is a share of `input`.
///
/// For FeldmanShamirShare: the share value becomes `m - r_i.value`, and the commitments
/// become `[m*G - C_0, -C_1, ..., -C_t]` since the new polynomial is `m - f_r(x)`.
fn calculate_input_shares<F: FftField, G: CurveGroup<ScalarField = F>>(
    masked_inputs: &[F],
    random_shares: &[FeldmanShamirShare<F, G>],
) -> Vec<FeldmanShamirShare<F, G>> {
    masked_inputs
        .iter()
        .zip(random_shares)
        .map(|(masked_value, random_share)| {
            let neg_share = (random_share.clone() * (-F::one())).unwrap();
            (neg_share + *masked_value).unwrap()
        })
        .collect()
}

impl<F: FftField, R: RBC<Id = AvssSessionId>, G: CurveGroup<ScalarField = F>>
    AvssInputServer<F, R, G>
{
    pub fn new(
        id: usize,
        n: usize,
        t: usize,
        input_ids: Vec<ClientId>,
    ) -> Result<Self, AvssInputError> {
        let (rbc_sender, rbc_receiver) = tokio::sync::mpsc::channel(200);
        let rbc = R::new(
            id,
            n,
            t,
            t + 1,
            rbc_sender,
            Arc::new(AvssWrappedMessage::rbc_wrap),
        )?;
        let (status_sender, status_receiver) = channel(
            input_ids
                .into_iter()
                .map(|id| (id, (InputType::Empty, vec![])))
                .collect(),
        );

        Ok(Self {
            id,
            n,
            t,
            rbc,
            rbc_output: Arc::new(Mutex::new(rbc_receiver)),
            status_sender,
            status_receiver,
        })
    }

    pub async fn drain_rbc_output(&mut self) -> Result<(), AvssInputError> {
        loop {
            let id = {
                let mut rx = self.rbc_output.lock().await;
                match rx.try_recv() {
                    Ok(id) => id,
                    Err(tokio::sync::mpsc::error::TryRecvError::Empty) => break,
                    Err(tokio::sync::mpsc::error::TryRecvError::Disconnected) => {
                        return Err(AvssInputError::Abort);
                    }
                }
            };

            let output = self.rbc.get_store(id).await?;
            match self.input_handler(id.sub_id().into(), output).await {
                Ok(()) => {}
                Err(e) => {
                    return Err(e);
                }
            }
        }
        Ok(())
    }

    /// Called by each server to send its share of `r_i` to the client.
    pub async fn init<N: Network>(
        &mut self,
        client_id: usize,
        shares: Vec<FeldmanShamirShare<F, G>>,
        input_len: usize,
        net: Arc<N>,
    ) -> Result<(), AvssInputError> {
        if shares.len() != input_len {
            return Err(AvssInputError::InvalidInput(
                "Incorrect number of shares".to_string(),
            ));
        }

        let mut send_over_network = false;
        let mut already_rand_shares = false;
        let mut unknown_client = false;
        let mut invalid_length = false;

        self.status_sender.send_if_modified(|status| {
            match status.get(&client_id) {
                Some((InputType::RandomShares | InputType::InputShares, _)) => {
                    already_rand_shares = true;
                    false
                }
                Some((InputType::MaskedInputs, masked_inputs_shares)) => {
                    // masked_inputs_shares contain F values stored as FeldmanShamirShares
                    // but actually they are plain F values. We stored them as shares for type
                    // compatibility. Extract the F values.
                    let masked_values: Vec<F> = masked_inputs_shares
                        .iter()
                        .map(|s| s.feldmanshare.share[0])
                        .collect();
                    if masked_values.len() != shares.len() {
                        invalid_length = true;
                        return false;
                    }
                    let input_shares = calculate_input_shares(&masked_values, &shares);

                    status.insert(client_id, (InputType::InputShares, input_shares));
                    info!("Calculated inputs for client {}", client_id);

                    true
                }
                Some((InputType::Empty, _)) => {
                    // update status before sending via network!
                    status.insert(client_id, (InputType::RandomShares, shares.clone()));
                    info!("Stored local mask shares for client {}", client_id);

                    send_over_network = true;
                    true
                }
                None => {
                    unknown_client = true;
                    false
                }
            }
        });

        if unknown_client {
            return Err(AvssInputError::InvalidInput(
                "Unknown client {client_id}".to_string(),
            ));
        }
        if invalid_length {
            return Err(AvssInputError::InvalidInput(
                "masked_inputs length does not match shares length".to_string(),
            ));
        }
        if already_rand_shares {
            return Err(AvssInputError::Duplicate(format!(
                "random shares already obtained for client {}",
                client_id
            )));
        }
        if send_over_network {
            let mut payload = Vec::new();
            shares.serialize_compressed(&mut payload)?;
            let msg = AvssInputMessage::new(self.id, payload);
            let wrapped = AvssWrappedMessage::Input(msg);
            let bytes = bincode::serialize(&wrapped)?;
            net.send_to_client(client_id, &bytes).await?;
            info!("Server {} sent MaskShare to client {}", self.id, client_id);
        }

        Ok(())
    }

    /// Called by each server: receives masked m_i, subtracts r_i to get share of m_i.
    pub async fn input_handler(
        &mut self,
        sender_id: PartyId,
        payload: Vec<u8>,
    ) -> Result<(), AvssInputError> {
        info!(
            "Server {} received MaskedInput from client {}",
            self.id, sender_id
        );

        if payload.len() < 8 {
            return Err(AvssInputError::InvalidInput(
                "Payload too short".to_string(),
            ));
        }
        let declared_len = u64::from_le_bytes(payload[..8].try_into().unwrap());
        if declared_len > MAX_INPUT_ELEMENTS {
            return Err(AvssInputError::InvalidInput(
                "Declared input length exceeds maximum".to_string(),
            ));
        }
        let masked_inputs: Vec<F> =
            ark_serialize::CanonicalDeserialize::deserialize_compressed(payload.as_slice())?;

        let mut unknown_client = false;
        let mut already_masked_inputs = false;
        let mut invalid_length = false;

        self.status_sender
            .send_if_modified(|status| match status.get(&sender_id) {
                Some((InputType::MaskedInputs | InputType::InputShares, _)) => {
                    already_masked_inputs = true;
                    false
                }
                Some((InputType::RandomShares, random_shares)) => {
                    if masked_inputs.len() != random_shares.len() {
                        invalid_length = true;
                        return false;
                    }
                    let input_shares = calculate_input_shares(&masked_inputs, random_shares);

                    status.insert(sender_id, (InputType::InputShares, input_shares));
                    info!(
                        "Server {} stored input shares from client {}",
                        self.id, sender_id
                    );

                    true
                }
                Some((InputType::Empty, _)) => {
                    // Store masked inputs as FeldmanShamirShare with dummy degree/commitments
                    // so they fit in the HashMap. We use a sentinel representation.
                    let masked_as_shares: Vec<FeldmanShamirShare<F, G>> = masked_inputs
                        .iter()
                        .map(|m| {
                            FeldmanShamirShare::new(
                                *m,
                                0,                         // dummy id
                                0,                         // dummy degree
                                vec![G::generator() * *m], // dummy commitment
                            )
                            .unwrap()
                        })
                        .collect();
                    status.insert(sender_id, (InputType::MaskedInputs, masked_as_shares));
                    info!(
                        "Server {} stored masked inputs from client {}",
                        self.id, sender_id
                    );

                    true
                }
                None => {
                    unknown_client = true;
                    false
                }
            });

        if invalid_length {
            return Err(AvssInputError::InvalidInput(format!(
                "masked_inputs length {} does not match expected {}",
                masked_inputs.len(),
                sender_id
            )));
        }
        if already_masked_inputs {
            return Err(AvssInputError::Duplicate(format!(
                "Server {} already received masked inputs from {}",
                self.id, sender_id
            )));
        }
        if unknown_client {
            return Err(AvssInputError::InvalidInput(
                "Unknown client {client_id}".to_string(),
            ));
        }

        Ok(())
    }

    pub async fn wait_for_all_inputs(
        &mut self,
        duration: Duration,
    ) -> Result<HashMap<ClientId, Vec<FeldmanShamirShare<F, G>>>, AvssInputError> {
        let status_future = self.status_receiver.wait_for(|statuses| {
            statuses
                .iter()
                .map(|(_, (status, _))| status)
                .all(|status| *status == InputType::InputShares)
        });

        match timeout(duration, status_future).await {
            Err(elapsed_err) => Err(AvssInputError::Timeout(elapsed_err)),
            Ok(Err(recv_err)) => Err(AvssInputError::WaitingError(recv_err)),
            Ok(Ok(statuses)) => {
                info!("Server {} has inputs from all clients", self.id);
                let input_shares = statuses
                    .iter()
                    .map(|(id, (_, shares))| (*id, shares.clone()))
                    .collect();

                Ok(input_shares)
            }
        }
    }
}

struct AvssInputClientData<F: FftField, R: RBC, G: CurveGroup<ScalarField = F>> {
    pub rbc: R,
    pub inputs: Vec<F>,
    pub rbc_done: bool,
    pub received_shares: HashMap<usize, Vec<FeldmanShamirShare<F, G>>>,
}

pub struct AvssInputClient<F: FftField, R: RBC, G: CurveGroup<ScalarField = F>> {
    pub client_id: usize,
    pub n: usize,
    pub t: usize,
    pub instance_id: u32,
    client_data: Arc<Mutex<AvssInputClientData<F, R, G>>>,
}

// implement manually because derive(Clone) requires R: Clone, which is not needed at all
impl<F: FftField, R: RBC, G: CurveGroup<ScalarField = F>> Clone for AvssInputClient<F, R, G> {
    fn clone(&self) -> Self {
        Self {
            client_id: self.client_id,
            n: self.n,
            t: self.t,
            instance_id: self.instance_id,
            client_data: Arc::clone(&self.client_data),
        }
    }
}

impl<F: FftField, R: RBC<Id = AvssSessionId>, G: CurveGroup<ScalarField = F>>
    AvssInputClient<F, R, G>
{
    pub fn new(
        id: usize,
        n: usize,
        t: usize,
        instance_id: u32,
        inputs: Vec<F>,
    ) -> Result<Self, AvssInputError> {
        let (rbc_sender, _) = tokio::sync::mpsc::channel(200);
        let rbc = R::new(
            id,
            n,
            t,
            t + 1,
            rbc_sender,
            Arc::new(AvssWrappedMessage::rbc_wrap),
        )?;
        Ok(Self {
            client_id: id,
            n,
            t,
            instance_id,
            client_data: Arc::new(Mutex::new(AvssInputClientData::<F, R, G> {
                rbc,
                inputs,
                received_shares: HashMap::new(),
                rbc_done: false,
            })),
        })
    }

    pub async fn init_handler<N: Network + Send + Sync>(
        &self,
        msg: AvssInputMessage,
        net: Arc<N>,
    ) -> Result<(), AvssInputError> {
        if msg.payload.len() < 8 {
            return Err(AvssInputError::InvalidInput(
                "Payload too short".to_string(),
            ));
        }
        let declared_len = u64::from_le_bytes(msg.payload[..8].try_into().unwrap()) as usize;
        let input_len = self.client_data.lock().await.inputs.len();
        if declared_len != input_len {
            return Err(AvssInputError::InvalidInput(
                "Mismatch in input and share length".to_string(),
            ));
        }

        let shares: Vec<FeldmanShamirShare<F, G>> = {
            let mut r = msg.payload.as_slice();
            deser_bounded_feldman_vec::<F, G>(&mut r, input_len, self.t + 1)?
        };
        let mut d = self.client_data.lock().await;

        if shares.len() != input_len {
            return Err(AvssInputError::InvalidInput(
                "Mismatch in input and share length".to_string(),
            ));
        }

        // Verify Feldman commitments on received shares
        for share in &shares {
            if share.feldmanshare.degree != self.t {
                return Err(AvssInputError::InvalidInput(format!(
                    "Invalid share degree from server {}",
                    msg.sender_id
                )));
            }
            if !verify_feldman(share.clone()) {
                return Err(AvssInputError::VerificationFailed(format!(
                    "Feldman verification failed for share from server {}",
                    msg.sender_id
                )));
            }
        }

        // happens if less than `n` messages were sufficient for reconstruction
        if d.rbc_done {
            return Ok(());
        }

        if d.received_shares.contains_key(&msg.sender_id) {
            return Err(AvssInputError::Duplicate(format!(
                "Already random shares received from {}",
                msg.sender_id
            )));
        }
        if d.received_shares.len() == self.n {
            return Err(AvssInputError::InvalidInput(format!(
                "Cannot receive from more than {} parties",
                self.n
            )));
        }
        d.received_shares.insert(msg.sender_id, shares.clone());
        info!(
            "Client {} received MaskShare from server {}",
            self.client_id, msg.sender_id
        );

        if d.received_shares.len() >= self.t + 1 {
            let mut masks = vec![];
            let mut output = vec![];
            let mut consistent_groups: Vec<Option<Vec<FeldmanShamirShare<F, G>>>> =
                vec![None; input_len];

            for i in 0..input_len {
                let mut by_commitment: HashMap<Vec<u8>, Vec<FeldmanShamirShare<F, G>>> =
                    HashMap::new();
                for (_, shares) in d.received_shares.iter() {
                    let share = &shares[i];
                    let mut key = Vec::new();
                    share.commitments.serialize_compressed(&mut key)?;
                    by_commitment.entry(key).or_default().push(share.clone());
                }
                match by_commitment.into_values().find(|g| g.len() >= self.t + 1) {
                    Some(group) => consistent_groups[i] = Some(group),
                    None => return Ok(()), // not enough consistent shares yet, wait for more
                }
            }

            info!("Received enough consistent shares to reconstruct");
            for group in consistent_groups.into_iter().flatten() {
                let secret = FeldmanShamirShare::<F, G>::recover_secret(&group, self.n, self.t)?;
                masks.push(secret.1);
            }

            for (i, r) in masks.iter().enumerate() {
                output.push(d.inputs[i] + r);
            }

            let mut payload = Vec::new();
            output.serialize_compressed(&mut payload)?;
            // Broadcast to servers
            let sessionid = AvssSessionId::new(
                ProtocolType::Input,
                AvssSessionId::pack_slot(
                    0, // subprotocol ID not needed because only called once
                    self.client_id as u8,
                    0,
                ),
                self.instance_id,
            );

            d.rbc.init(payload, sessionid, net).await?;
            d.rbc_done = true;
            info!(
                "Client {} initialized broadcasting of masked input to all servers",
                self.client_id
            );
        }

        Ok(())
    }

    /// Process any message (used for both client and server roles).
    pub async fn process<N: Network + Send + Sync>(
        &mut self,
        msg: AvssInputMessage,
        net: Arc<N>,
    ) -> Result<(), AvssInputError> {
        self.init_handler(msg, net).await
    }
}