mpc-ristretto 0.1.0

A library for performing secure multiparty computation using the Ristretto group.
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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
//! Groups the definitions and trait implementations for a scalar value within an MPC network
#![allow(unused_doc_comments)]
use std::{
    borrow::Borrow,
    convert::TryInto,
    iter::{Product, Sum},
    ops::{Add, AddAssign, Index, Mul, MulAssign, Neg, Sub, SubAssign},
};

use clear_on_drop::clear::Clear;
use curve25519_dalek::{ristretto::RistrettoPoint, scalar::Scalar};
use rand_core::{CryptoRng, OsRng, RngCore};
use subtle::ConstantTimeEq;
use tokio::runtime::Handle;
use zeroize::Zeroize;

use crate::{
    beaver::SharedValueSource,
    commitment::PedersenCommitment,
    error::{MpcError, MpcNetworkError},
    macros::{self},
    network::MpcNetwork,
    BeaverSource, SharedNetwork, Visibility, Visible,
};

/// Represents a scalar value allocated in an MPC network
#[derive(Debug)]
pub struct MpcScalar<N: MpcNetwork + Send, S: SharedValueSource<Scalar>> {
    /// the underlying value of the scalar allocated in the network
    pub value: Scalar,
    /// The visibility flag; what amount of information parties have
    pub(crate) visibility: Visibility,
    /// The underlying network that the MPC operates on
    pub(crate) network: SharedNetwork<N>,
    /// The source for shared values; MAC keys, beaver triples, etc
    pub(crate) beaver_source: BeaverSource<S>,
}

impl<N: MpcNetwork + Send, S: SharedValueSource<Scalar>> Clone for MpcScalar<N, S> {
    fn clone(&self) -> Self {
        Self {
            value: self.value,
            visibility: self.visibility,
            network: self.network.clone(),
            beaver_source: self.beaver_source.clone(),
        }
    }
}

/**
 * Static and helper methods
 */

/// Converts a scalar to u64
pub fn scalar_to_u64(a: &Scalar) -> u64 {
    u64::from_le_bytes(a.to_bytes()[..8].try_into().unwrap())
}

/**
 * Wrapper type implementations
 */
impl<N: MpcNetwork + Send, S: SharedValueSource<Scalar>> MpcScalar<N, S> {
    /**
     * Helper methods
     */
    #[inline]
    pub(crate) fn is_private(&self) -> bool {
        self.visibility == Visibility::Private
    }

    #[inline]
    pub(crate) fn is_shared(&self) -> bool {
        self.visibility == Visibility::Shared
    }

    #[inline]
    pub(crate) fn is_public(&self) -> bool {
        self.visibility == Visibility::Public
    }

    #[inline]
    pub fn value(&self) -> Scalar {
        self.value
    }

    #[inline]
    pub fn to_scalar(&self) -> Scalar {
        self.value()
    }

    #[inline]
    pub(crate) fn network(&self) -> SharedNetwork<N> {
        self.network.clone()
    }

    #[inline]
    pub(crate) fn beaver_source(&self) -> BeaverSource<S> {
        self.beaver_source.clone()
    }

    /**
     * Casting methods
     */

    /// Create a public network scalar from a u64
    pub fn from_public_u64(
        a: u64,
        network: SharedNetwork<N>,
        beaver_source: BeaverSource<S>,
    ) -> Self {
        Self::from_u64_with_visibility(a, Visibility::Public, network, beaver_source)
    }

    /// Create a private network scalar from a given u64
    pub fn from_private_u64(
        a: u64,
        network: SharedNetwork<N>,
        beaver_source: BeaverSource<S>,
    ) -> Self {
        Self::from_u64_with_visibility(a, Visibility::Private, network, beaver_source)
    }

    /// Create a scalar from a given u64 and visibility
    pub(crate) fn from_u64_with_visibility(
        a: u64,
        visibility: Visibility,
        network: SharedNetwork<N>,
        beaver_source: BeaverSource<S>,
    ) -> Self {
        Self {
            network,
            visibility,
            beaver_source,
            value: Scalar::from(a),
        }
    }

    /// Allocate a public network value from an underlying scalar
    pub fn from_public_scalar(
        value: Scalar,
        network: SharedNetwork<N>,
        beaver_source: BeaverSource<S>,
    ) -> Self {
        Self::from_scalar_with_visibility(value, Visibility::Public, network, beaver_source)
    }

    /// Allocate a private network value from an underlying scalar
    pub fn from_private_scalar(
        value: Scalar,
        network: SharedNetwork<N>,
        beaver_source: BeaverSource<S>,
    ) -> Self {
        Self::from_scalar_with_visibility(value, Visibility::Private, network, beaver_source)
    }

    /// Allocate an existing scalar in the network with given visibility
    pub(crate) fn from_scalar_with_visibility(
        value: Scalar,
        visibility: Visibility,
        network: SharedNetwork<N>,
        beaver_source: BeaverSource<S>,
    ) -> Self {
        Self {
            network,
            visibility,
            value,
            beaver_source,
        }
    }

    /// Generate a random scalar
    /// Random will always be SharedWithOwner(self); two parties cannot reliably generate the same random value
    pub fn random<R: RngCore + CryptoRng>(
        rng: &mut R,
        network: SharedNetwork<N>,
        beaver_source: BeaverSource<S>,
    ) -> Self {
        Self {
            network,
            visibility: Visibility::Private,
            beaver_source,
            value: Scalar::random(rng),
        }
    }

    /// Default-esque implementation
    pub fn default(network: SharedNetwork<N>, beaver_source: BeaverSource<S>) -> Self {
        Self::zero(network, beaver_source)
    }

    // Build a scalar from bytes
    macros::impl_delegated_wrapper!(
        Scalar,
        from_bytes_mod_order,
        from_bytes_mod_order_with_visibility,
        bytes,
        [u8; 32]
    );
    macros::impl_delegated_wrapper!(
        Scalar,
        from_bytes_mod_order_wide,
        from_bytes_mod_order_wide_with_visibility,
        input,
        &[u8; 64]
    );

    pub fn from_canonical_bytes(
        bytes: [u8; 32],
        network: SharedNetwork<N>,
        beaver_source: BeaverSource<S>,
    ) -> Option<MpcScalar<N, S>> {
        Self::from_canonical_bytes_with_visibility(
            bytes,
            Visibility::Public,
            network,
            beaver_source,
        )
    }

    pub fn from_canonical_bytes_with_visibility(
        bytes: [u8; 32],
        visibility: Visibility,
        network: SharedNetwork<N>,
        beaver_source: BeaverSource<S>,
    ) -> Option<MpcScalar<N, S>> {
        Some(MpcScalar {
            visibility,
            network,
            beaver_source,
            value: Scalar::from_canonical_bytes(bytes)?,
        })
    }

    macros::impl_delegated_wrapper!(
        Scalar,
        from_bits,
        from_bits_with_visibility,
        bytes,
        [u8; 32]
    );

    // Convert a scalar to bytes
    macros::impl_delegated!(to_bytes, self, [u8; 32]);
    macros::impl_delegated!(as_bytes, self, &[u8; 32]);
    // Check whether the scalar is canonically represented mod l
    macros::impl_delegated!(is_canonical, self, bool);
    // Generate the additive identity
    macros::impl_delegated_wrapper!(Scalar, zero);
    // Generate the multiplicative identity
    macros::impl_delegated_wrapper!(Scalar, one);
}

/**
 * Secret sharing implementation
 */
impl<N: MpcNetwork + Send, S: SharedValueSource<Scalar>> MpcScalar<N, S> {
    /// From a privately held value, construct an additive secret share and distribute this
    /// to the counterparty. The local party samples a random value R which is given to the peer
    /// The local party then holds a - R where a is the underlying value.
    /// This method is called by both parties, only one of which transmits, the peer will simply
    /// await the sent share
    pub fn share_secret(&self, party_id: u64) -> Result<MpcScalar<N, S>, MpcNetworkError> {
        let my_party_id = self.network.as_ref().borrow().party_id();

        if my_party_id == party_id {
            // Sender party
            // Sample a random additive complement
            let mut rng = OsRng {};
            let random_share = Scalar::random(&mut rng);

            // Broadcast the counterparty's share
            Handle::current().block_on(
                self.network
                    .as_ref()
                    .borrow_mut()
                    .send_single_scalar(random_share),
            )?;

            // Do not subtract directly as the random scalar is not directly allocated in the network
            // subtracting directly ties it to the subtraction implementation in a fragile way
            Ok(MpcScalar {
                value: self.value - random_share,
                visibility: Visibility::Shared,
                network: self.network.clone(),
                beaver_source: self.beaver_source.clone(),
            })
        } else {
            Self::receive_value(self.network.clone(), self.beaver_source.clone())
        }
    }

    /// Share a batch of privately held secrets by constructing additive shares
    pub fn batch_share_secrets(
        party_id: u64,
        secrets: &[MpcScalar<N, S>],
    ) -> Result<Vec<MpcScalar<N, S>>, MpcNetworkError> {
        assert!(
            secrets.iter().all(|secret| secret.is_private()),
            "Values to be shared must be in private state"
        );

        if secrets.is_empty() {
            return Ok(Vec::new());
        }

        let network = secrets[0].network();
        let beaver_source = secrets[0].beaver_source();
        let my_party_id = network.as_ref().borrow().party_id();

        if my_party_id == party_id {
            // Sender party
            let mut rng = OsRng {};
            let random_shares: Vec<Scalar> = (0..secrets.len())
                .map(|_| Scalar::random(&mut rng))
                .collect();

            // Broadcast the random shares to the peer
            Handle::current()
                .block_on(network.as_ref().borrow_mut().send_scalars(&random_shares))?;

            Ok(secrets
                .iter()
                .zip(random_shares.iter())
                .map(|(secret, blinding)| MpcScalar {
                    value: secret.value() - blinding,
                    visibility: Visibility::Shared,
                    network: network.clone(),
                    beaver_source: beaver_source.clone(),
                })
                .collect())
        } else {
            Self::batch_receive_values(secrets.len(), network, beaver_source)
        }
    }

    /// Local party receives a secret share of a value; as opposed to using share_secret, no existing value is needed
    pub fn receive_value(
        network: SharedNetwork<N>,
        beaver_source: BeaverSource<S>,
    ) -> Result<MpcScalar<N, S>, MpcNetworkError> {
        let value =
            Handle::current().block_on(network.as_ref().borrow_mut().receive_single_scalar())?;

        Ok(MpcScalar {
            value,
            visibility: Visibility::Shared,
            network,
            beaver_source,
        })
    }

    /// Local party receives a batch of shared values
    pub fn batch_receive_values(
        num_expected: usize,
        network: SharedNetwork<N>,
        beaver_source: BeaverSource<S>,
    ) -> Result<Vec<MpcScalar<N, S>>, MpcNetworkError> {
        let values = Handle::current()
            .block_on(network.as_ref().borrow_mut().receive_scalars(num_expected))?;

        Ok(values
            .iter()
            .map(|value| MpcScalar {
                value: *value,
                visibility: Visibility::Shared,
                network: network.clone(),
                beaver_source: beaver_source.clone(),
            })
            .collect())
    }

    /// From a shared value, both parties open their shares and construct the plaintext value.
    /// Note that the parties no longer hold valid additive secret shares of the value, this is used
    /// at the end of a computation
    pub fn open(&self) -> Result<MpcScalar<N, S>, MpcNetworkError> {
        assert!(!self.is_private(), "Private values may not be opened...");
        if self.is_public() {
            return Ok(self.clone());
        }

        // Send my scalar and expect one back
        let received_scalar = Handle::current().block_on(
            self.network
                .as_ref()
                .borrow_mut()
                .broadcast_single_scalar(self.value),
        )?;

        // Reconstruct the plaintext from the peer's share
        Ok(MpcScalar::from_public_scalar(
            self.value + received_scalar,
            self.network.clone(),
            self.beaver_source.clone(),
        ))
    }

    /// Open a batch of shared values
    pub fn batch_open(values: &[MpcScalar<N, S>]) -> Result<Vec<MpcScalar<N, S>>, MpcNetworkError> {
        assert!(
            values.iter().all(|value| !value.is_private()),
            "Private values may not be opened..."
        );

        if values.is_empty() {
            return Ok(Vec::new());
        }

        let network = values[0].network();
        let beaver_source = values[0].beaver_source();

        // Both parties share their values
        let received_scalars = Handle::current().block_on(
            network.as_ref().borrow_mut().broadcast_scalars(
                &values
                    .iter()
                    .map(|value| value.value())
                    .collect::<Vec<Scalar>>(),
            ),
        )?;

        Ok(values
            .iter()
            .zip(received_scalars.iter())
            .map(|(my_share, peer_share)| {
                if my_share.is_public() {
                    return my_share.clone();
                }

                MpcScalar::from_public_scalar(
                    my_share.value() + peer_share,
                    network.clone(),
                    beaver_source.clone(),
                )
            })
            .collect())
    }

    /// From a shared value:
    ///     1. Commit to the value and exchange commitments
    ///     2. Open those commitments to the underlying value
    ///     3. Verify that the peer's opening matches their commitment
    pub fn commit_and_open(&self) -> Result<MpcScalar<N, S>, MpcError> {
        assert!(!self.is_private(), "Private values may not be opened...");
        if self.is_public() {
            return Ok(self.clone());
        }

        // Compute a Pedersen commitment to the value
        let commitment = PedersenCommitment::commit(self.to_scalar());
        let peer_commitment = Handle::current()
            .block_on(
                self.network()
                    .as_ref()
                    .borrow_mut()
                    .broadcast_single_point(commitment.get_commitment()),
            )
            .map_err(MpcError::NetworkError)?;

        // Open the commitment to the underlying value
        let received_scalars = Handle::current()
            .block_on(
                self.network()
                    .as_ref()
                    .borrow_mut()
                    .broadcast_scalars(&[commitment.get_blinding(), commitment.get_value()]),
            )
            .map_err(MpcError::NetworkError)?;

        let (peer_blinding, peer_value) = (received_scalars[0], received_scalars[1]);

        // Verify the commitment and return the opened value
        if !PedersenCommitment::verify_from_values(peer_commitment, peer_blinding, peer_value) {
            return Err(MpcError::AuthenticationError);
        }

        Ok(Self {
            value: self.value() + peer_value,
            visibility: Visibility::Public,
            network: self.network(),
            beaver_source: self.beaver_source(),
        })
    }

    /// Commit to and open a batch of secret shared values
    pub fn batch_commit_and_open(
        values: &[MpcScalar<N, S>],
    ) -> Result<Vec<MpcScalar<N, S>>, MpcError> {
        assert!(
            values.iter().all(|value| !value.is_private()),
            "Private values may not be opened...",
        );

        if values.is_empty() {
            return Ok(Vec::new());
        }

        let network = values[0].network();
        let beaver_source = values[0].beaver_source();

        // Generate commitments to the values and share them with the peer
        let commitments: Vec<PedersenCommitment> = values
            .iter()
            .map(|value| PedersenCommitment::commit(value.to_scalar()))
            .collect();
        let peer_commitments = Handle::current()
            .block_on(
                network.as_ref().borrow_mut().broadcast_points(
                    &commitments
                        .iter()
                        .map(|comm| comm.get_commitment())
                        .collect::<Vec<RistrettoPoint>>(),
                ),
            )
            .map_err(MpcError::NetworkError)?;

        // Open both the underlying values and the blinding factors
        let mut commitment_data: Vec<Scalar> = Vec::new();
        commitments.iter().for_each(|comm| {
            commitment_data.push(comm.get_blinding());
            commitment_data.push(comm.get_value());
        });

        let received_values = Handle::current()
            .block_on(
                network
                    .as_ref()
                    .borrow_mut()
                    .broadcast_scalars(&commitment_data),
            )
            .map_err(MpcError::NetworkError)?;

        // Verify the peer's commitments
        let mut peer_values: Vec<Scalar> = Vec::new();
        received_values
            .chunks(2 /* chunk_size */) // Fetch each pair of blinding, value
            .zip(peer_commitments.into_iter())
            .try_for_each(|(revealed_values, comm)| {
                // Destructure the received payload and append to the peer values vector
                let (blinding, value) = (revealed_values[0], revealed_values[1]);
                peer_values.push(value);

                // Verify the Pedersen commitment, report an authentication error if opening fails
                if !PedersenCommitment::verify_from_values(comm, blinding, value) {
                    return Err(MpcError::AuthenticationError);
                }

                Ok(())
            })?;

        // If the commitments open properly then add shares together to recover cleartext
        Ok(values
            .iter()
            .zip(peer_values)
            .map(|(my_value, peer_value)| {
                if my_value.is_public() {
                    return my_value.clone();
                }

                MpcScalar {
                    value: my_value.value() + peer_value,
                    visibility: Visibility::Public,
                    network: network.clone(),
                    beaver_source: beaver_source.clone(),
                }
            })
            .collect())
    }

    /// Retrieves the next Beaver triplet from the Beaver source and allocates the values within the network
    fn next_beaver_triplet(&self) -> (MpcScalar<N, S>, MpcScalar<N, S>, MpcScalar<N, S>) {
        let (a, b, c) = self.beaver_source.as_ref().borrow_mut().next_triplet();

        (
            MpcScalar::from_scalar_with_visibility(
                a,
                Visibility::Shared,
                self.network.clone(),
                self.beaver_source.clone(),
            ),
            MpcScalar::from_scalar_with_visibility(
                b,
                Visibility::Shared,
                self.network.clone(),
                self.beaver_source.clone(),
            ),
            MpcScalar::from_scalar_with_visibility(
                c,
                Visibility::Shared,
                self.network.clone(),
                self.beaver_source.clone(),
            ),
        )
    }

    /// Retrieves the next Beaver triplet batch from the Beaver source and allocates the value in the network
    #[allow(clippy::type_complexity)]
    fn next_beaver_triplet_batch(
        &self,
        num_triplets: usize,
    ) -> Vec<(MpcScalar<N, S>, MpcScalar<N, S>, MpcScalar<N, S>)> {
        let triplet_batch = self
            .beaver_source
            .as_ref()
            .borrow_mut()
            .next_triplet_batch(num_triplets);

        // Allocate values as shared in the network
        triplet_batch
            .iter()
            .map(|(a, b, c)| {
                (
                    MpcScalar::from_scalar_with_visibility(
                        *a,
                        Visibility::Shared,
                        self.network.clone(),
                        self.beaver_source.clone(),
                    ),
                    MpcScalar::from_scalar_with_visibility(
                        *b,
                        Visibility::Shared,
                        self.network.clone(),
                        self.beaver_source.clone(),
                    ),
                    MpcScalar::from_scalar_with_visibility(
                        *c,
                        Visibility::Shared,
                        self.network.clone(),
                        self.beaver_source.clone(),
                    ),
                )
            })
            .collect::<Vec<_>>()
    }
}

/**
 * Generic trait implementations
 */
impl<N: MpcNetwork + Send, S: SharedValueSource<Scalar>> Visible for MpcScalar<N, S> {
    fn visibility(&self) -> Visibility {
        self.visibility
    }
}

impl<N: MpcNetwork + Send, S: SharedValueSource<Scalar>> PartialEq for MpcScalar<N, S> {
    fn eq(&self, other: &Self) -> bool {
        self.value.eq(&other.value)
    }
}

impl<N: MpcNetwork + Send, S: SharedValueSource<Scalar>> ConstantTimeEq for MpcScalar<N, S> {
    fn ct_eq(&self, other: &Self) -> subtle::Choice {
        self.value.ct_eq(&other.value)
    }
}

impl<N: MpcNetwork + Send, S: SharedValueSource<Scalar>> Index<usize> for MpcScalar<N, S> {
    type Output = u8;

    fn index(&self, index: usize) -> &Self::Output {
        self.value.index(index)
    }
}

impl<N: MpcNetwork + Send, S: SharedValueSource<Scalar>> Clear for &mut MpcScalar<N, S> {
    #[allow(clippy::needless_borrow)]
    fn clear(&mut self) {
        (&mut self.value).clear();
    }
}

/**
 * Mul and variants for: borrowed, non-borrowed, and Scalar types
 */

/// Implementation of mul with the beaver trick
/// This implementation panics in the case of a network error.
/// Ideally this is done in a thread where the panic can be handled by the parent.
impl<'a, N: MpcNetwork + Send, S: SharedValueSource<Scalar>> Mul<&'a MpcScalar<N, S>>
    for &'a MpcScalar<N, S>
{
    type Output = MpcScalar<N, S>;

    /// Multiplies two (possibly shared) values. The only case in which we need a Beaver trick
    /// is when both lhs and rhs are Shared. If only one is shared, multiplying by a public value
    /// directly leads to an additive sharing. If both are public, we do not need an additive share.
    /// TODO(@joey): What is the correct behavior when one or both of lhs and rhs are private
    ///
    /// See https://securecomputation.org/docs/pragmaticmpc.pdf (Section 3.4) for the identities this
    /// implementation makes use of.
    fn mul(self, rhs: &'a MpcScalar<N, S>) -> Self::Output {
        if self.is_shared() && rhs.is_shared() {
            let (a, b, c) = self.next_beaver_triplet();

            // Open the values d = [lhs - a] and e = [rhs - b]
            let opened_values = MpcScalar::batch_open(&[(self - &a), (rhs - &b)]).unwrap();
            let lhs_minus_a = &opened_values[0];
            let rhs_minus_b = &opened_values[1];

            // Identity: [a * b] = de + d[b] + e[a] + [c]
            // All multiplications here are between a public and shared value or
            // two public values, so the recursion will not hit this case
            let mut res = lhs_minus_a * &b + rhs_minus_b * &a + c;

            // Split into additive shares, the king holds de + res
            if self.network.as_ref().borrow().am_king() {
                res += lhs_minus_a * rhs_minus_b;
            }

            res
        } else {
            // Directly multiply
            MpcScalar {
                visibility: Visibility::min_visibility_two(self, rhs),
                network: self.network.clone(),
                beaver_source: self.beaver_source.clone(),
                value: self.value * rhs.value,
            }
        }
    }
}

// Multiplication with a scalar value is equivalent to a public multiplication, no Beaver
// trick needed
macros::impl_operator_variants!(MpcScalar<N, S>, Mul, mul, *, MpcScalar<N, S>);
macros::impl_wrapper_type!(MpcScalar<N, S>, Scalar, MpcScalar::from_public_scalar, Mul, mul, *, authenticated=false);
macros::impl_arithmetic_assign!(MpcScalar<N, S>, MulAssign, mul_assign, *, MpcScalar<N, S>);
macros::impl_arithmetic_assign!(MpcScalar<N, S>, MulAssign, mul_assign, *, Scalar);

/**
 * Batch multiply allowing for batches of communication
 */

impl<N: MpcNetwork + Send, S: SharedValueSource<Scalar>> MpcScalar<N, S> {
    /// Returns the result [a_1 * b_1, ..., a_n * b_n]
    ///
    /// This method is not meant to be used directly, instead, it should be called
    /// through the MPC fabric which will inject `am_king` and `beaver_source`
    pub fn batch_mul(
        a: &[MpcScalar<N, S>],
        b: &[MpcScalar<N, S>],
    ) -> Result<Vec<MpcScalar<N, S>>, MpcNetworkError> {
        assert_eq!(
            a.len(),
            b.len(),
            "input arrays to batch_mul must be of equal length"
        );

        if a.is_empty() {
            return Ok(Vec::new());
        }

        let n = a.len();
        let mut res = Vec::with_capacity(n);

        // If one (or both) of a and b is public, it can be multiplied locally
        // so we first separate out these values to avoid unnecessary computation/communication
        let mut beaver_mul_pairs = Vec::new();
        for i in 0..a.len() {
            if !a[i].is_public() && !b[i].is_public() {
                beaver_mul_pairs.push((&a[i], &b[i]))
            }
        }

        // For each of the multiplications that requires a beaver-style mul; sample a multiplication triplet
        let num_beaver_muls = beaver_mul_pairs.len();
        let mut beaver_triplets = a[0].next_beaver_triplet_batch(num_beaver_muls);

        // Tile a payload buffer with the beaver openings then share
        let mut beaver_subs = Vec::with_capacity(2 * n);
        beaver_mul_pairs
            .iter()
            .zip(beaver_triplets.iter())
            .for_each(|((a_val, b_val), (beaver_a, beaver_b, _))| {
                beaver_subs.push(*a_val - beaver_a);
                beaver_subs.push(*b_val - beaver_b);
            });

        // Open the tiled beaver subtractions
        let mut opened_beaver_subs = if num_beaver_muls == 0 {
            Vec::new()
        } else {
            MpcScalar::batch_open(&beaver_subs)?
        };
        for i in 0..n {
            if a[i].is_public() || b[i].is_public() {
                res.push(&a[i] * &b[i])
            } else {
                // Fetch the next opening of a beaver sub
                let (lhs_minus_a, rhs_minus_b) =
                    (opened_beaver_subs.remove(0), opened_beaver_subs.remove(0));

                let (beaver_a, beaver_b, beaver_c) = beaver_triplets.remove(0);

                // Perform the multiplication and place it in the result
                // Identity: [a * b] = de + d[b] + e[a] + [c]
                // All multiplications here are between a public and shared value or
                // two public values, so the recursion will not hit this case
                let result = &lhs_minus_a * &beaver_b
                    + &rhs_minus_b * &beaver_a
                    + lhs_minus_a * rhs_minus_b
                    + &beaver_c;

                res.push(result);
            }
        }

        Ok(res)
    }
}

/**
 * Add and variants for: borrowed, non-borrowed, and scalar types
 */
impl<'a, N: MpcNetwork + Send, S: SharedValueSource<Scalar>> Add<&'a MpcScalar<N, S>>
    for &'a MpcScalar<N, S>
{
    type Output = MpcScalar<N, S>;

    fn add(self, rhs: &'a MpcScalar<N, S>) -> Self::Output {
        // If public + shared swap the arguments for simplicity
        if self.is_public() && rhs.is_shared() {
            return rhs + self;
        }

        // If both values are public; both parties add the values together to obtain
        // a public result.
        // If both values are shared; both parties add the shared values together to
        // obtain a shared result.
        // If only one value is public, the king adds the public valid to her share
        // I.e. if the parties hold an additive sharing of a = a_1 + a_2 and with to
        // add public b; the king now holds a_1 + b and the peer holds a_2. Effectively
        // they construct an implicit secret sharing of b where b_1 = b and b_2 = 0
        let am_king = self.network.as_ref().borrow().am_king();

        let res = {
            if self.is_public() && rhs.is_public() ||  // Both public
                self.is_shared() && rhs.is_shared() ||  // Both shared
                am_king
            // One public, but local peer is king
            {
                self.value() + rhs.value()
            } else {
                self.value()
            }
        };

        MpcScalar {
            value: res,
            visibility: Visibility::min_visibility_two(self, rhs),
            network: self.network.clone(),
            beaver_source: self.beaver_source.clone(),
        }
    }
}

macros::impl_operator_variants!(MpcScalar<N, S>, Add, add, +, MpcScalar<N, S>);
macros::impl_wrapper_type!(MpcScalar<N, S>, Scalar, MpcScalar::from_public_scalar, Add, add, +, authenticated=false);
macros::impl_arithmetic_assign!(MpcScalar<N, S>, AddAssign, add_assign, +, MpcScalar<N, S>);
macros::impl_arithmetic_assign!(MpcScalar<N, S>, AddAssign, add_assign, +, Scalar);

/**
 * Sub and variants for: borrowed, non-borrowed, and scalar types
 */
impl<'a, N: MpcNetwork + Send, S: SharedValueSource<Scalar>> Sub<&'a MpcScalar<N, S>>
    for &'a MpcScalar<N, S>
{
    type Output = MpcScalar<N, S>;

    #[allow(clippy::suspicious_arithmetic_impl)]
    fn sub(self, rhs: &'a MpcScalar<N, S>) -> Self::Output {
        self + rhs.neg()
    }
}

macros::impl_operator_variants!(MpcScalar<N, S>, Sub, sub, -, MpcScalar<N, S>);
macros::impl_wrapper_type!(MpcScalar<N, S>, Scalar, MpcScalar::from_public_scalar, Sub, sub, -, authenticated=false);
macros::impl_arithmetic_assign!(MpcScalar<N, S>, SubAssign, sub_assign, -, MpcScalar<N, S>);
macros::impl_arithmetic_assign!(MpcScalar<N, S>, SubAssign, sub_assign, -, Scalar);

impl<N: MpcNetwork + Send, S: SharedValueSource<Scalar>> Neg for MpcScalar<N, S> {
    type Output = MpcScalar<N, S>;

    fn neg(self) -> Self::Output {
        (&self).neg()
    }
}

impl<'a, N: MpcNetwork + Send, S: SharedValueSource<Scalar>> Neg for &'a MpcScalar<N, S> {
    type Output = MpcScalar<N, S>;

    fn neg(self) -> Self::Output {
        MpcScalar {
            visibility: self.visibility,
            network: self.network.clone(),
            beaver_source: self.beaver_source.clone(),
            value: self.value.neg(),
        }
    }
}

/**
 * Iterator traits
 */

/// TODO: Optimize this to use tree-structured round parallelism
impl<N, S, T> Product<T> for MpcScalar<N, S>
where
    N: MpcNetwork + Send,
    S: SharedValueSource<Scalar>,
    T: Borrow<MpcScalar<N, S>>,
{
    fn product<I: Iterator<Item = T>>(iter: I) -> Self {
        let mut peekable = iter.peekable();
        let first_elem = peekable.peek().unwrap();
        let network: SharedNetwork<N> = first_elem.borrow().network.clone();
        let beaver_source: BeaverSource<S> = first_elem.borrow().beaver_source.clone();

        peekable.fold(MpcScalar::one(network, beaver_source), |acc, item| {
            acc * item.borrow()
        })
    }
}

impl<N, S, T> Sum<T> for MpcScalar<N, S>
where
    N: MpcNetwork + Send,
    S: SharedValueSource<Scalar>,
    T: Borrow<MpcScalar<N, S>>,
{
    fn sum<I: Iterator<Item = T>>(iter: I) -> Self {
        // This operation is invalid on an empty iterator, unwrap is expected
        let mut peekable = iter.peekable();
        let first_elem = peekable.peek().unwrap();
        let network = first_elem.borrow().network.clone();
        let beaver_source: BeaverSource<S> = first_elem.borrow().beaver_source.clone();

        peekable.fold(
            MpcScalar::from_u64_with_visibility(0, Visibility::Shared, network, beaver_source),
            |acc, item| acc + item.borrow(),
        )
    }
}

impl<N: MpcNetwork + Send, S: SharedValueSource<Scalar>> MpcScalar<N, S> {
    /// Takes a linear combination of the input scalars
    pub fn linear_combination(
        scalars: &[MpcScalar<N, S>],
        coeffs: &[MpcScalar<N, S>],
    ) -> Result<MpcScalar<N, S>, MpcNetworkError> {
        Ok(MpcScalar::batch_mul(scalars, coeffs)?.iter().sum())
    }
}

impl<N: MpcNetwork + Send, S: SharedValueSource<Scalar>> Zeroize for MpcScalar<N, S> {
    fn zeroize(&mut self) {
        self.value.zeroize()
    }
}

/// For these tests, we explicitly build a tokio runtime and spawn tests as blocking
/// tasks within the runtime. This allows us to block in the execution of a test without
/// blocking the tokio async driver
#[cfg(test)]
mod test {
    use std::{cell::RefCell, rc::Rc};

    use clear_on_drop::clear::Clear;
    use curve25519_dalek::scalar::Scalar;
    use rand_core::OsRng;
    use tokio::runtime::{Builder as RuntimeBuilder, Runtime};

    use crate::{beaver::DummySharedScalarSource, network::dummy_network::DummyMpcNetwork};

    use super::{MpcScalar, Visibility};

    /// Helper to create a tokio runtime that allows the implementation to block on async network
    /// results
    fn create_blockable_runtime() -> Runtime {
        RuntimeBuilder::new_multi_thread()
            .enable_all()
            .worker_threads(1)
            .max_blocking_threads(1)
            .build()
            .unwrap()
    }

    #[test]
    fn test_zero() {
        let network = Rc::new(RefCell::new(DummyMpcNetwork::new()));
        let beaver_source = Rc::new(RefCell::new(DummySharedScalarSource::new()));

        let expected =
            MpcScalar::from_public_scalar(Scalar::zero(), network.clone(), beaver_source.clone());
        let zero = MpcScalar::zero(network, beaver_source);

        assert_eq!(zero, expected);
    }

    #[test]
    fn test_open() {
        let rt = create_blockable_runtime();
        let handle = rt.spawn_blocking(|| {
            let network = Rc::new(RefCell::new(DummyMpcNetwork::new()));
            network
                .borrow_mut()
                .add_mock_scalars(vec![Scalar::from(1u8)]);

            let beaver_source = Rc::new(RefCell::new(DummySharedScalarSource::new()));
            let expected = MpcScalar::from_public_scalar(
                Scalar::from(2u8),
                network.clone(),
                beaver_source.clone(),
            );

            // Dummy network opens to the value we send it, so the mock parties each hold Scalar(1) for a
            // shared value of Scalar(2)
            let my_share = MpcScalar::from_u64_with_visibility(
                1u64,
                Visibility::Shared,
                network,
                beaver_source,
            );
            assert_eq!(my_share.open().unwrap(), expected);
        });

        rt.block_on(handle).unwrap();
    }

    #[test]
    fn test_add() {
        let rt = create_blockable_runtime();
        let handle = rt.spawn_blocking(|| {
            let network = Rc::new(RefCell::new(DummyMpcNetwork::new()));
            network
                .borrow_mut()
                .add_mock_scalars(vec![Scalar::from(2u8)]);

            let beaver_source = Rc::new(RefCell::new(DummySharedScalarSource::new()));

            // Assume that parties hold a secret share of [4] as individual shares of 2 each
            let shared_value1 = MpcScalar::from_u64_with_visibility(
                2u64,
                Visibility::Shared,
                network.clone(),
                beaver_source.clone(),
            );

            // Test adding a scalar value first
            let res = &shared_value1 + Scalar::from(3u64); // [4] + 3
            assert_eq!(res.visibility, Visibility::Shared);
            assert_eq!(
                res.open().unwrap(),
                MpcScalar::from_public_u64(7u64, network.clone(), beaver_source.clone())
            );

            // Test adding another shared value
            // Assume now that parties have additive shares of [5]
            // The peer holds 1, the local party holds 4
            let shared_value2 = MpcScalar::from_u64_with_visibility(
                4u64,
                Visibility::Shared,
                network.clone(),
                beaver_source.clone(),
            );

            network
                .borrow_mut()
                .add_mock_scalars(vec![Scalar::from(3u8)]); // The peer's share of [4] + [5]

            let res = shared_value1 + shared_value2;
            assert_eq!(res.visibility, Visibility::Shared);
            assert_eq!(
                res.open().unwrap(),
                MpcScalar::from_public_u64(9, network, beaver_source)
            )
        });

        rt.block_on(handle).unwrap();
    }

    #[test]
    fn test_add_associative() {
        let network = Rc::new(RefCell::new(DummyMpcNetwork::new()));
        let beaver_source = Rc::new(RefCell::new(DummySharedScalarSource::new()));

        // Add two random values, ensure associativity
        let mut rng = OsRng {};
        let v1 = MpcScalar::random(&mut rng, network, beaver_source);
        let v2 = Scalar::random(&mut rng);

        let res1 = &v1 + v2;
        let res2 = v2 + &v1;

        assert_eq!(res1, res2);
    }

    #[test]
    fn test_sub() {
        let rt = create_blockable_runtime();
        let handle = rt.spawn_blocking(|| {
            let network = Rc::new(RefCell::new(DummyMpcNetwork::new()));
            let beaver_source = Rc::new(RefCell::new(DummySharedScalarSource::new()));

            // Subtract a raw scalar from a shared value
            // Assume parties hold secret shares 2 and 1 of [3]
            let shared_value1 = MpcScalar::from_u64_with_visibility(
                2u64,
                Visibility::Shared,
                network.clone(),
                beaver_source.clone(),
            );
            network
                .borrow_mut()
                .add_mock_scalars(vec![Scalar::from(1u8)]);

            let res = &shared_value1 - Scalar::from(2u8);
            assert_eq!(res.visibility, Visibility::Shared);
            assert_eq!(
                res.open().unwrap(),
                MpcScalar::from_public_u64(1u64, network.clone(), beaver_source.clone())
            );

            // Subtract two shared values
            let shared_value2 = MpcScalar::from_u64_with_visibility(
                5,
                Visibility::Shared,
                network.clone(),
                beaver_source.clone(),
            );
            network
                .borrow_mut()
                .add_mock_scalars(vec![Scalar::from(2u8)]);

            let res = shared_value2 - shared_value1;
            assert_eq!(res.visibility, Visibility::Shared);
            assert_eq!(
                res.open().unwrap(),
                MpcScalar::from_public_u64(5, network, beaver_source)
            )
        });

        rt.block_on(handle).unwrap();
    }

    #[test]
    fn test_mul() {
        let rt = create_blockable_runtime();
        let handle = rt.spawn_blocking(|| {
            let network = Rc::new(RefCell::new(DummyMpcNetwork::new()));
            let beaver_source = Rc::new(RefCell::new(DummySharedScalarSource::new()));

            // Multiply a scalar with a shared value
            // Assume both parties have a sharing of [11], local party holds 6
            let shared_value1 = MpcScalar::from_u64_with_visibility(
                6u64,
                Visibility::Shared,
                network.clone(),
                beaver_source.clone(),
            );

            // Populate the network mock after the multiplication; this implicitly asserts that
            // no network call was used for multiplying by a scalar (assumed public)
            let res = &shared_value1 * Scalar::from(2u8);
            assert_eq!(res.visibility, Visibility::Shared);

            network
                .borrow_mut()
                .add_mock_scalars(vec![Scalar::from(10u8)]);

            assert_eq!(
                res.open().unwrap(),
                MpcScalar::from_public_u64(22, network.clone(), beaver_source.clone())
            );

            // Multiply a shared value with a public value
            let public_value =
                MpcScalar::from_public_u64(3u64, network.clone(), beaver_source.clone());

            // As above, populate the network mock after the multiplication
            let res = public_value * &shared_value1;
            assert_eq!(res.visibility, Visibility::Shared);

            network
                .borrow_mut()
                .add_mock_scalars(vec![Scalar::from(15u8)]);
            assert_eq!(
                res.open().unwrap(),
                MpcScalar::from_public_u64(33u64, network.clone(), beaver_source.clone())
            );

            // Multiply two shared values, a beaver triplet (a, b, c) will be needed
            // Populate the network mock with two openings:
            //      1. [shared1 - a]
            //      2. [shared2 - b]
            // Assume that the parties hold [shared2] = [12] where the peer holds 7 and the local holds 5
            let shared_value2 = MpcScalar::from_u64_with_visibility(
                5u64,
                Visibility::Shared,
                network.clone(),
                beaver_source.clone(),
            );
            network
                .borrow_mut()
                .add_mock_scalars(vec![Scalar::from(5u8), Scalar::from(7u8)]);

            // Populate the network with the peer's res share after the computation
            let res = shared_value1 * shared_value2;
            assert_eq!(res.visibility, Visibility::Shared);

            network
                .borrow_mut()
                .add_mock_scalars(vec![Scalar::from(0u64)]);

            assert_eq!(
                res.open().unwrap(),
                MpcScalar::from_public_u64(12 * 11, network, beaver_source)
            );
        });

        rt.block_on(handle).unwrap();
    }

    #[tokio::test]
    async fn test_clear() {
        let network = Rc::new(RefCell::new(DummyMpcNetwork::new()));
        let beaver_source = Rc::new(RefCell::new(DummySharedScalarSource::new()));
        let mut value = MpcScalar::from_public_u64(2, network, beaver_source);

        (&mut value).clear();
        assert_eq!(value.value(), Scalar::zero());
    }
}