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
// Copyright 2020-2021  MathxH Chen.
//
// Code is licensed under GPLv3.0 License.

#![allow(non_snake_case)]

use crate::dleq::DLEQ;
use crate::mpvss::MPVSS;
use crate::polynomial::Polynomial;
use crate::sharebox::{DistributionSharesBox, ShareBox};
use crate::util::Util;
use num_bigint::{BigInt, BigUint, RandBigInt, ToBigInt};
use num_integer::Integer;
use num_primes::Generator;
use num_traits::identities::{One, Zero};
use sha2::{Digest, Sha256};
use std::clone::Clone;
use std::collections::BTreeMap;
use std::option::Option;

/// A participant represents one party in the secret sharing scheme. The participant can share a secret among a group of other participants and it is then called the "dealer".
/// The receiving participants that receive a part of the secret can use it to reconstruct the secret Therefore the partticipants need to collaborate and exchange their parts.
/// A participant represents as a Node in the Distributed Public NetWork
#[derive(Debug, Clone, Default)]
pub struct Participant {
    mpvss: MPVSS,
    pub privatekey: BigInt,
    pub publickey: BigInt,
}

impl Participant {
    /// Create A default participant
    ///
    /// ## Example
    ///
    /// ```rust
    /// use mpvss_rs::Participant;
    /// let mut dealer = Participant::new();
    /// ```
    pub fn new() -> Self {
        Participant {
            mpvss: MPVSS::new(),
            privatekey: BigInt::zero(),
            publickey: BigInt::zero(),
        }
    }
    /// Initializes a new participant with the default MPVSS.
    ///
    /// ## Example
    ///
    /// ```rust
    /// use mpvss_rs::Participant;
    /// let mut dealer = Participant::new();
    /// dealer.initialize();
    /// ```
    pub fn initialize(&mut self) {
        self.privatekey = self.mpvss.generate_private_key();
        self.publickey = self.mpvss.generate_public_key(&self.privatekey);
    }

    fn distribute(
        &mut self,
        secret: &BigInt,
        publickeys: &[BigInt],
        threshold: u32,
        polynomial: &Polynomial,
        w: &BigInt,
    ) -> DistributionSharesBox {
        assert!(threshold <= publickeys.len() as u32);
        // Data the distribution shares box is going to be consisting of
        let mut commitments: Vec<BigInt> = Vec::new();
        let mut positions: BTreeMap<BigInt, i64> = BTreeMap::new();
        let mut X: BTreeMap<BigInt, BigInt> = BTreeMap::new();
        let mut shares: BTreeMap<BigInt, BigInt> = BTreeMap::new();
        let mut challenge_hasher = Sha256::new();

        // Temp variable
        let mut sampling_points: BTreeMap<BigInt, BigInt> = BTreeMap::new();
        let mut a: BTreeMap<BigInt, (BigInt, BigInt)> = BTreeMap::new();
        let mut dleq_w: BTreeMap<BigInt, BigInt> = BTreeMap::new();
        let mut position: i64 = 1;

        // Calculate Ploynomial Coefficients Commitments C_j = g^(a_j) under group of prime q, and  0 <= j < threshold
        for j in 0..threshold {
            commitments.push(
                self.mpvss.g.modpow(
                    &polynomial.coefficients[j as usize],
                    &self.mpvss.q,
                ),
            )
        }

        // Calculate Every Encrypted shares with every participant's public key generated from their own private key
        // Y_i = (y_i)^p(i)  X_i = g^p(i) =  C_0^(i^0) * C_1^(i^1) * C_2^(i^2) * ... * C_j^(i^j)  and 1 <= i <= n  0 <= j <= threshhold - 1
        // n is participant current total number
        // p(i) is secret share without encrypt on the ploynomial of the degree t - 1
        // y_i is participant public key
        // Y_i is encrypted secret share
        for pubkey in publickeys {
            positions.insert(pubkey.clone(), position);
            // calc P(position) % (q - 1), from P(1) to P(n), actually is from share 1 to share n
            let secret_share = polynomial.get_value(&BigInt::from(position))
                % (&self.mpvss.q - BigInt::one());
            sampling_points.insert(pubkey.clone(), secret_share.clone());

            // Calc X_i
            let mut x: BigInt = BigInt::one();
            let mut exponent: BigInt = BigInt::one();
            for j in 0..=threshold - 1 {
                x = (x * commitments[j as usize]
                    .modpow(&exponent, &self.mpvss.q))
                    % &self.mpvss.q;
                exponent = (exponent * BigInt::from(position))
                    % (&self.mpvss.q - BigInt::one());
            }

            X.insert(pubkey.clone(), x.clone());

            // Calc Y_i
            let encrypted_secret_share =
                pubkey.modpow(&secret_share, &self.mpvss.q);
            shares.insert(pubkey.clone(), encrypted_secret_share.clone());

            // DLEQ(g1,h2,g2,h2) => DLEQ(g,X_i,y_i,Y_i) => DLEQ(g,commintment_with_secret_share,pubkey,enrypted_secret_share_from_pubkey)
            // Prove That  g^alpha = commintment_with_secret_share and pubkey^alpha = encrypted_secret_share_from_pubkey has same alpha value
            let mut dleq = DLEQ::new();
            dleq.init2(
                self.mpvss.g.clone(),
                x.clone(),
                pubkey.clone(),
                encrypted_secret_share.clone(),
                self.mpvss.q.clone(),
                secret_share.clone(),
                w.clone(),
            );

            dleq_w.insert(pubkey.clone(), dleq.w.clone());
            // Calc a_1i, a_2i
            a.insert(pubkey.clone(), (dleq.get_a1(), dleq.get_a2()));

            // Update challenge hash
            // the challenge c for the protocol is computed as a cryptographic hash of X_i,Y_i,a_1i,a_2i, 1 <= i <= n
            challenge_hasher
                .update(x.to_biguint().unwrap().to_str_radix(10).as_bytes());
            challenge_hasher.update(
                encrypted_secret_share
                    .to_biguint()
                    .unwrap()
                    .to_str_radix(10)
                    .as_bytes(),
            );
            challenge_hasher.update(
                dleq.get_a1()
                    .to_biguint()
                    .unwrap()
                    .to_str_radix(10)
                    .as_bytes(),
            );
            challenge_hasher.update(
                dleq.get_a2()
                    .to_biguint()
                    .unwrap()
                    .to_str_radix(10)
                    .as_bytes(),
            );
            position += 1;
        } // end for participant's publickeys

        // the common challenge c
        let challenge_hash = challenge_hasher.finalize();
        let challenge_big_uint = BigUint::from_bytes_be(&challenge_hash[..])
            .mod_floor(&(self.mpvss.q.to_biguint().unwrap() - BigUint::one()));

        // Calc response r_i
        let mut responses: BTreeMap<BigInt, BigInt> = BTreeMap::new();
        for pubkey in publickeys {
            // DLEQ(g1,h2,g2,h2) => DLEQ(g,X_i,y_i,Y_i) => DLEQ(g,commintment_with_secret_share,pubkey,encrypted_secret_share_from_pubkey)
            let x_i = X.get(pubkey).unwrap();
            let encrypted_secret_share = shares.get(pubkey).unwrap();
            let secret_share = sampling_points.get(pubkey).unwrap();
            let w = dleq_w.get(pubkey).unwrap();
            let mut dleq = DLEQ::new();
            dleq.init2(
                self.mpvss.g.clone(),
                x_i.clone(),
                pubkey.clone(),
                encrypted_secret_share.clone(),
                self.mpvss.q.clone(),
                secret_share.clone(),
                w.clone(),
            );
            dleq.c = Some(challenge_big_uint.to_bigint().unwrap());
            let response = dleq.get_r().unwrap();
            responses.insert(pubkey.clone(), response);
        } // end for pubkeys Calc r_i

        // Calc U = secret xor SHA256(G^s) = secret xor SHA256(G^p(0)).
        // [Section 4]
        // σ ∈ Σ, where 2 ≤ |Σ| ≤ q.
        // the general procedure is to let the dealer first run the distribution protocol for a random value s ∈ Zq, and then publish U = σ ⊕ H(G^s),
        // where H is an appropriate cryptographic hash function. The reconstruction protocol will yield G^s, from which we obtain σ = U ⊕ H(G^s).
        let shared_value = self.mpvss.G.modpow(
            &polynomial.get_value(&BigInt::zero()).mod_floor(
                &(self.mpvss.q.to_bigint().unwrap() - BigInt::one()),
            ),
            &self.mpvss.q,
        );
        let sha256_hash = sha2::Sha256::digest(
            shared_value
                .to_biguint()
                .unwrap()
                .to_str_radix(10)
                .as_bytes(),
        );
        let hash_big_uint = BigUint::from_bytes_be(&sha256_hash[..])
            .mod_floor(&self.mpvss.q.to_biguint().unwrap());
        let U = secret.to_biguint().unwrap() ^ hash_big_uint;

        // The proof consists of the common challenge c and the n responses r_i.
        let mut shares_box = DistributionSharesBox::new();
        shares_box.init(
            &commitments,
            positions,
            shares,
            publickeys,
            &challenge_big_uint.to_bigint().unwrap(),
            responses,
            &U.to_bigint().unwrap(),
        );
        shares_box
    }

    /// Takes a secret as input and returns the distribution shares Box which is going to be submitted to all the participants the secret is going to be shared with.
    /// Those participants are specified by their public keys. They use the distribution shares box to verify that the shares are correct (without learning anything about the shares that are not supposed to be decrypted by them)
    /// and extract their encrypted shares. In fact, the distribution shares box can be published to everyone allowing even external parties to verify the integrity of the shares.
    ///
    /// - Parameters:
    ///   - secret: The value that is going to be shared among the other participants.
    ///   - publicKeys: Array of public keys of each participant the secret is to be shared with.
    ///   - threshold: The number of shares that is needed in order to reconstruct the secret. It must not be greater than the total number of participants.
    /// - Requires: `threshold` <= number of participants
    /// - Returns: The distribution shares Box that is published to everyone (especially but not only the participants) can check the shares' integrity. Furthermore the participants extract their shares from it.
    ///
    /// ## Example
    ///
    /// ```rust
    /// use mpvss_rs::Participant;
    /// use num_bigint::{BigUint, ToBigInt};
    ///
    /// let secret_message = String::from("Hello MPVSS Example.");
    /// let secret = BigUint::from_bytes_be(&secret_message.as_bytes());
    /// let mut dealer = Participant::new();
    /// dealer.initialize();
    /// let mut p1 = Participant::new();
    /// let mut p2 = Participant::new();
    /// let mut p3 = Participant::new();
    /// p1.initialize();
    /// p2.initialize();
    /// p3.initialize();
    ///
    /// let distribute_shares_box = dealer.distribute_secret(
    ///    &secret.to_bigint().unwrap(),
    ///    &vec![
    ///        p1.publickey.clone(),
    ///        p2.publickey.clone(),
    ///        p3.publickey.clone(),
    ///    ],
    ///    3,
    /// );
    /// ```
    pub fn distribute_secret(
        &mut self,
        secret: &BigInt,
        publickeys: &[BigInt],
        threshold: u32,
    ) -> DistributionSharesBox {
        let mut polynomial = Polynomial::new();
        polynomial
            .init((threshold - 1) as i32, &self.mpvss.q.to_bigint().unwrap());

        let mut rng = rand::thread_rng();
        let w: BigUint =
            rng.gen_biguint_below(&self.mpvss.q.to_biguint().unwrap());
        self.distribute(
            secret,
            publickeys,
            threshold,
            &polynomial,
            &w.to_bigint().unwrap(),
        )
    }

    fn extract_share(
        &self,
        shares_box: &DistributionSharesBox,
        private_key: &BigInt,
        w: &BigInt,
    ) -> Option<ShareBox> {
        let public_key = self.mpvss.generate_public_key(private_key);
        let encrypted_secret_share =
            shares_box.shares.get(&public_key).unwrap();

        // Decryption of the shares.
        // Using its private key x_i, each participant finds the decrypted share S_i = G^p(i) from Y_i by computing S_i = Y_i^(1/x_i).
        // Y_i is encrypted share: Y_i = y_i^p(i)
        // find modular multiplicative inverses of private key
        let privkey_inverse =
            Util::mod_inverse(private_key, &(&self.mpvss.q - BigInt::one()))
                .unwrap();
        let decrypted_share =
            encrypted_secret_share.modpow(&privkey_inverse, &self.mpvss.q);

        // To this end it suffices to prove knowledge of an α such that y_i= G^α and Y_i= S_i^α, which is accomplished by the non-interactive version of the protocol DLEQ(G,y_i,S_i,Y_i).
        // DLEQ(G,y_i,S_i,Y_i) => DLEQ(G, publickey, decrypted_share, encryted_share)
        // All of this is to prove and tell participants that the decrypted share is must use your own public key encrypted,
        // and only you can decrypt the share with your own private key and verify the share's proof
        let mut dleq = DLEQ::new();
        dleq.init2(
            self.mpvss.G.clone(),
            public_key.clone(),
            decrypted_share.clone(),
            encrypted_secret_share.clone(),
            self.mpvss.q.clone(),
            private_key.clone(),
            w.clone(),
        );

        let mut challenge_hasher = Sha256::new();
        challenge_hasher.update(
            public_key.to_biguint().unwrap().to_str_radix(10).as_bytes(),
        );
        challenge_hasher.update(
            encrypted_secret_share
                .to_biguint()
                .unwrap()
                .to_str_radix(10)
                .as_bytes(),
        );
        challenge_hasher.update(
            dleq.get_a1()
                .to_biguint()
                .unwrap()
                .to_str_radix(10)
                .as_bytes(),
        );
        challenge_hasher.update(
            dleq.get_a2()
                .to_biguint()
                .unwrap()
                .to_str_radix(10)
                .as_bytes(),
        );

        // the challenge c
        let challenge_hash = challenge_hasher.finalize();
        let challenge_big_uint = BigUint::from_bytes_be(&challenge_hash[..])
            .mod_floor(&(self.mpvss.q.to_biguint().unwrap() - BigUint::one()));
        dleq.c = Some(challenge_big_uint.to_bigint().unwrap());

        let mut share_box = ShareBox::new();
        share_box.init(
            public_key,
            decrypted_share,
            challenge_big_uint.to_bigint().unwrap(),
            dleq.get_r().unwrap(),
        );
        Some(share_box)
    }

    /// Extracts the share from a given distribution shares box that is addressed to the calling participant.
    /// The extracted share is boxed with a proof which allows the other participants to verify the share's correctness.
    ///
    /// - Parameters:
    ///   - shares_box: The distribution shares box that consists the share to be extracted.
    ///   - private_key: The participant's private key used to decrypt the share.
    /// - Returns: The share box that is to be submitted to all the other participants in order to reconstruct the secret.
    ///     It consists of the share itself and the proof that allows the receiving participant to verify its correctness.
    ///     Return `None` if the distribution shares box does not contain a share for the participant.
    ///
    /// ## Example
    ///
    /// ```rust
    /// use mpvss_rs::Participant;
    /// use num_bigint::{BigUint, ToBigInt};
    ///
    /// let secret_message = String::from("Hello MPVSS Example.");
    /// let secret = BigUint::from_bytes_be(&secret_message.as_bytes());
    /// let mut dealer = Participant::new();
    /// dealer.initialize();
    /// let mut p1 = Participant::new();
    /// let mut p2 = Participant::new();
    /// let mut p3 = Participant::new();
    /// p1.initialize();
    /// p2.initialize();
    /// p3.initialize();
    ///
    /// let distribute_shares_box = dealer.distribute_secret(
    ///    &secret.to_bigint().unwrap(),
    ///    &vec![
    ///        p1.publickey.clone(),
    ///        p2.publickey.clone(),
    ///        p3.publickey.clone(),
    ///    ],
    ///    3,
    /// );
    ///
    ///  let s1 = p1
    ///        .extract_secret_share(&distribute_shares_box, &p1.privatekey)
    ///        .unwrap();
    ///  let s2 = p2
    ///        .extract_secret_share(&distribute_shares_box, &p2.privatekey)
    ///        .unwrap();
    ///  let s3 = p3
    ///        .extract_secret_share(&distribute_shares_box, &p3.privatekey)
    ///        .unwrap();
    /// ```
    pub fn extract_secret_share(
        &self,
        shares_box: &DistributionSharesBox,
        private_key: &BigInt,
    ) -> Option<ShareBox> {
        let w = Generator::new_uint(self.mpvss.length as usize)
            .mod_floor(&self.mpvss.q.to_biguint().unwrap());
        self.extract_share(shares_box, private_key, &w.to_bigint().unwrap())
    }

    /// Verifies that the shares the distribution  shares box consists are consistent so that they can be used to reconstruct the secret later.
    ///
    /// - Parameter distribute_sharesbox: The distribution shares box whose consistency is to be verified.
    /// - Returns: Returns `true` if the shares are correct and `false` otherwise.
    ///
    /// ## Example
    ///
    /// ```rust
    /// use mpvss_rs::Participant;
    /// use num_bigint::{BigUint, ToBigInt};
    /// let secret_message = String::from("Hello MPVSS Example.");
    /// let secret = BigUint::from_bytes_be(&secret_message.as_bytes());
    /// let mut dealer = Participant::new();
    /// dealer.initialize();
    /// let mut p1 = Participant::new();
    /// let mut p2 = Participant::new();
    /// let mut p3 = Participant::new();
    /// p1.initialize();
    /// p2.initialize();
    /// p3.initialize();
    ///
    /// let distribute_shares_box = dealer.distribute_secret(
    ///     &secret.to_bigint().unwrap(),
    ///     &vec![
    ///         p1.publickey.clone(),
    ///         p2.publickey.clone(),
    ///         p3.publickey.clone(),
    ///     ],
    ///     3,
    /// );
    ///
    /// assert_eq!(
    ///     p1.verify_distribution_shares(&distribute_shares_box),
    ///     true
    /// );
    ///
    /// assert_eq!(
    ///     p2.verify_distribution_shares(&distribute_shares_box),
    ///     true
    /// );

    /// assert_eq!(
    ///     p3.verify_distribution_shares(&distribute_shares_box),
    ///     true
    /// );
    /// ```
    pub fn verify_distribution_shares(
        &self,
        distribute_sharesbox: &DistributionSharesBox,
    ) -> bool {
        self.mpvss.verify_distribution_shares(distribute_sharesbox)
    }

    /// Verifies if the share in the distribution share box was decrypted correctly by the respective participant.
    ///
    /// - Parameters:
    ///   - shareBox: The share box containing the share to be verified.
    ///   - distributionShareBox: The distribution share box that contains the share.
    ///   - publicKey: The public key of the sender of the share bundle.
    /// - Returns: Returns `true` if the share in the distribution share box matches the decryption of the encrypted share and `false` otherwise.
    ///
    /// ## Example
    ///
    /// ```rust
    /// use mpvss_rs::Participant;
    /// use num_bigint::{BigUint, ToBigInt};
    ///
    /// let secret_message = String::from("Hello MPVSS Example.");
    /// let secret = BigUint::from_bytes_be(&secret_message.as_bytes());
    /// let mut dealer = Participant::new();
    /// dealer.initialize();
    /// let mut p1 = Participant::new();
    /// let mut p2 = Participant::new();
    /// let mut p3 = Participant::new();
    /// p1.initialize();
    /// p2.initialize();
    /// p3.initialize();
    ///
    /// let distribute_shares_box = dealer.distribute_secret(
    ///    &secret.to_bigint().unwrap(),
    ///    &vec![
    ///        p1.publickey.clone(),
    ///        p2.publickey.clone(),
    ///        p3.publickey.clone(),
    ///    ],
    ///    3,
    /// );
    ///
    ///  let s1 = p1
    ///        .extract_secret_share(&distribute_shares_box, &p1.privatekey)
    ///        .unwrap();
    ///  let s2 = p2
    ///        .extract_secret_share(&distribute_shares_box, &p2.privatekey)
    ///        .unwrap();
    ///  let s3 = p3
    ///        .extract_secret_share(&distribute_shares_box, &p3.privatekey)
    ///        .unwrap();
    ///
    ///  assert_eq!(
    ///    p1.verify_share(&s2, &distribute_shares_box, &p2.publickey),
    ///      true
    ///   );
    ///
    ///  assert_eq!(
    ///    p2.verify_share(&s3, &distribute_shares_box, &p3.publickey),
    ///      true
    ///   );
    ///
    ///  assert_eq!(
    ///    p3.verify_share(&s1, &distribute_shares_box, &s1.publickey),
    ///      true
    ///   );
    /// ```
    pub fn verify_share(
        &self,
        sharebox: &ShareBox,
        distribution_sharebox: &DistributionSharesBox,
        publickey: &BigInt,
    ) -> bool {
        self.mpvss
            .verify_share(sharebox, distribution_sharebox, publickey)
    }

    /// Reconstruct secret from share boxs
    ///
    /// ## Example
    ///
    /// ```rust
    /// use mpvss_rs::Participant;
    /// use num_bigint::{BigUint, ToBigInt};
    /// let secret_message = String::from("Hello MPVSS Example.");
    /// let secret = BigUint::from_bytes_be(&secret_message.as_bytes());
    /// let mut dealer = Participant::new();
    /// dealer.initialize();
    /// let mut p1 = Participant::new();
    /// let mut p2 = Participant::new();
    /// let mut p3 = Participant::new();
    /// p1.initialize();
    /// p2.initialize();
    /// p3.initialize();
    ///
    /// let distribute_shares_box = dealer.distribute_secret(
    ///     &secret.to_bigint().unwrap(),
    ///     &vec![
    ///         p1.publickey.clone(),
    ///         p2.publickey.clone(),
    ///         p3.publickey.clone(),
    ///     ],
    ///     3,
    /// );
    ///
    /// assert_eq!(
    ///     p1.verify_distribution_shares(&distribute_shares_box),
    ///     true
    /// );
    ///
    /// assert_eq!(
    ///     p2.verify_distribution_shares(&distribute_shares_box),
    ///     true
    /// );
    ///
    /// assert_eq!(
    ///     p3.verify_distribution_shares(&distribute_shares_box),
    ///     true
    /// );
    ///
    ///
    /// let s1 = p1
    ///     .extract_secret_share(&distribute_shares_box, &p1.privatekey)
    ///     .unwrap();
    ///
    /// let s2 = p2
    ///     .extract_secret_share(&distribute_shares_box, &p2.privatekey)
    ///     .unwrap();
    /// let s3 = p3
    ///     .extract_secret_share(&distribute_shares_box, &p3.privatekey)
    ///     .unwrap();
    ///
    /// assert_eq!(
    ///     p1.verify_share(&s2, &distribute_shares_box, &p2.publickey),
    ///     true
    /// );
    ///
    /// assert_eq!(
    ///     p2.verify_share(&s3, &distribute_shares_box, &p3.publickey),
    ///     true
    /// );
    ///
    /// assert_eq!(
    ///     p3.verify_share(&s1, &distribute_shares_box, &s1.publickey),
    ///     true
    /// );
    ///
    /// let share_boxs = [s1, s2, s3];
    /// let r1 = p1
    ///     .reconstruct(&share_boxs, &distribute_shares_box)
    ///     .unwrap();
    /// let r2 = p2
    ///     .reconstruct(&share_boxs, &distribute_shares_box)
    ///     .unwrap();
    /// let r3 = p3
    ///     .reconstruct(&share_boxs, &distribute_shares_box)
    ///     .unwrap();
    ///
    /// let r1_str =
    ///     String::from_utf8(r1.to_biguint().unwrap().to_bytes_be()).unwrap();
    /// assert_eq!(secret_message.clone(), r1_str);
    /// let r2_str =
    ///     String::from_utf8(r2.to_biguint().unwrap().to_bytes_be()).unwrap();
    /// assert_eq!(secret_message.clone(), r2_str);
    /// let r3_str =
    ///     String::from_utf8(r3.to_biguint().unwrap().to_bytes_be()).unwrap();
    /// assert_eq!(secret_message.clone(), r3_str);
    /// ```
    pub fn reconstruct(
        &self,
        share_boxs: &[ShareBox],
        distribute_share_box: &DistributionSharesBox,
    ) -> Option<BigInt> {
        self.mpvss.reconstruct(share_boxs, distribute_share_box)
    }
}

#[cfg(test)]
mod tests {

    use super::BTreeMap;
    use super::BigInt;
    use super::Participant;
    use super::Polynomial;
    use super::MPVSS;
    use super::{DistributionSharesBox, ShareBox};
    use num_traits::{One, Zero};

    struct Setup {
        pub mpvss: MPVSS,
        pub privatekey: BigInt,
        pub secret: BigInt,
    }

    impl Setup {
        fn new() -> Self {
            let q = BigInt::from(179426549);
            let g = BigInt::from(1301081);
            let G = BigInt::from(15486487);

            let length: i64 = 64_i64;
            let mut mpvss = MPVSS::new();
            mpvss.length = length as u32;
            mpvss.g = g;
            mpvss.G = G;
            mpvss.q = q;

            return Setup {
                mpvss: mpvss,
                privatekey: BigInt::from(105929),
                secret: BigInt::from(1234567890),
            };
        }
    }

    // Use Fixed distribution shares box for tests
    fn get_distribute_shares_box() -> DistributionSharesBox {
        let setup = Setup::new();
        let mut dealer = Participant::new();
        dealer.mpvss = setup.mpvss.clone();
        dealer.privatekey = setup.privatekey.clone();
        dealer.publickey = setup.mpvss.generate_public_key(&setup.privatekey);

        let mut polynomial = Polynomial::new();
        polynomial.init_coefficients(&vec![
            BigInt::from(164102006),
            BigInt::from(43489589),
            BigInt::from(98100795),
        ]);
        let threshold: i32 = 3;
        // from participant 1 to 3
        let privatekeys =
            [BigInt::from(7901), BigInt::from(4801), BigInt::from(1453)];
        let mut publickeys = vec![];
        let w = BigInt::from(6345);

        for key in privatekeys.iter() {
            publickeys.push(setup.mpvss.generate_public_key(key));
        }

        return dealer.distribute(
            &setup.secret,
            &publickeys,
            threshold as u32,
            &polynomial,
            &w,
        );
    }

    // Use Fixed Share box for tests
    fn get_share_box() -> ShareBox {
        let distribution_shares_box = get_distribute_shares_box();
        // Use Participant 1's private key
        let private_key = BigInt::from(7901);
        let w = BigInt::from(1337);
        let mut participant = Participant::new();
        let setup = Setup::new();
        participant.mpvss = setup.mpvss.clone();
        participant.privatekey = private_key.clone();
        participant.publickey = setup.mpvss.generate_public_key(&private_key);

        participant
            .extract_share(&distribution_shares_box, &private_key, &w)
            .unwrap()
    }

    #[test]
    fn test_distribution() {
        let distribution = get_distribute_shares_box();

        let commitments = vec![
            BigInt::from(92318234),
            BigInt::from(76602245),
            BigInt::from(63484157),
        ];
        let mut shares: BTreeMap<BigInt, BigInt> = BTreeMap::new();
        shares
            .insert(distribution.publickeys[0].clone(), BigInt::from(42478042));
        shares
            .insert(distribution.publickeys[1].clone(), BigInt::from(80117658));
        shares
            .insert(distribution.publickeys[2].clone(), BigInt::from(86941725));

        let challenge = BigInt::from(41963410);
        let mut responses: BTreeMap<BigInt, BigInt> = BTreeMap::new();
        responses.insert(
            distribution.publickeys[0].clone(),
            BigInt::from(151565889),
        );
        responses.insert(
            distribution.publickeys[1].clone(),
            BigInt::from(146145105),
        );
        responses
            .insert(distribution.publickeys[2].clone(), BigInt::from(71350321));

        assert_eq!(distribution.publickeys[0], distribution.publickeys[0]);
        assert_eq!(distribution.publickeys[1], distribution.publickeys[1]);
        assert_eq!(distribution.publickeys[2], distribution.publickeys[2]);

        assert_eq!(distribution.challenge, challenge);

        for i in 0..=2 {
            assert_eq!(distribution.commitments[i], commitments[i]);
            assert_eq!(
                distribution.shares[&distribution.publickeys[i]],
                shares[&distribution.publickeys[i]]
            );
            assert_eq!(
                distribution.responses[&distribution.publickeys[i]],
                responses[&distribution.publickeys[i]]
            );
        }
    }

    #[test]
    fn test_distribution_verify() {
        let setup = Setup::new();
        let distribution = get_distribute_shares_box();
        assert_eq!(setup.mpvss.verify_distribution_shares(&distribution), true);
    }

    #[test]
    fn test_extract_share() {
        let share_box = get_share_box();
        assert_eq!(share_box.share, BigInt::from(164021044));
        assert_eq!(share_box.challenge, BigInt::from(134883166));
        assert_eq!(share_box.response, BigInt::from(81801891));
    }

    #[test]
    fn test_share_box_verify() {
        // participant 1 private key
        let private_key = BigInt::from(7901);
        let distribution_shares_box = get_distribute_shares_box();
        let share_box = get_share_box();

        let setup = Setup::new();
        assert_eq!(
            setup.mpvss.verify_share(
                &share_box,
                &distribution_shares_box,
                &setup.mpvss.generate_public_key(&private_key)
            ),
            true
        );
    }

    #[test]
    fn test_reconstruction_with_all_participants() {
        let distribution_shares_box = get_distribute_shares_box();
        let share_box1 = get_share_box();
        let mut share_box2 = ShareBox::new();
        share_box2.init(
            BigInt::from(132222922),
            BigInt::from(157312059),
            BigInt::zero(),
            BigInt::zero(),
        );
        let mut share_box3 = ShareBox::new();
        share_box3.init(
            BigInt::from(65136827),
            BigInt::from(63399333),
            BigInt::zero(),
            BigInt::zero(),
        );

        let setup = Setup::new();
        let share_boxs = [share_box1, share_box2, share_box3];
        let reconstructed_secret = setup
            .mpvss
            .reconstruct(&share_boxs, &distribution_shares_box)
            .unwrap();
        assert_eq!(reconstructed_secret, setup.secret);
    }

    // (3,4) threshhold reconstruct, participant 3 is not available, 1,2,4 is available
    #[test]
    fn test_reconstruction_with_sub_group() {
        let share_box1 = get_share_box();
        let mut share_box2 = ShareBox::new();
        share_box2.init(
            BigInt::from(132222922),
            BigInt::from(157312059),
            BigInt::zero(),
            BigInt::zero(),
        );

        let public_key4 = BigInt::from(42);
        let mut share_box4 = ShareBox::new();
        share_box4.init(
            public_key4.clone(),
            BigInt::from(59066181),
            BigInt::zero(),
            BigInt::zero(),
        );

        let mut positions = BTreeMap::new();
        positions.insert(share_box1.clone().publickey, 1_i64);
        positions.insert(share_box2.clone().publickey, 2_i64);
        positions.insert(share_box4.clone().publickey, 4_i64);

        let mut distribution_shares_box = DistributionSharesBox::new();
        distribution_shares_box.init(
            &vec![BigInt::zero(), BigInt::one(), BigInt::from(2)],
            positions,
            BTreeMap::new(),
            &vec![],
            &BigInt::zero(),
            BTreeMap::new(),
            &BigInt::from(1284073502),
        );

        let setup = Setup::new();
        let share_boxs = [share_box1, share_box2, share_box4];
        let reconstructed_secret = setup
            .mpvss
            .reconstruct(&share_boxs, &distribution_shares_box)
            .unwrap();
        assert_eq!(reconstructed_secret, setup.secret);
    }
}