Skip to main content

mpvss_all/
mpvss_all.rs

1// Copyright 2020-2026 MathxH Chen.
2//
3// Code is licensed under MIT Apache Dual License
4
5use mpvss_rs::Participant;
6use mpvss_rs::groups::ModpGroup;
7use mpvss_rs::{string_from_secret, string_to_secret};
8use num_bigint::{RandBigInt, ToBigInt};
9
10fn main() {
11    let group = ModpGroup::new();
12    let secret_message = String::from("Hello MPVSS Example.");
13    let mut dealer = Participant::with_arc(group.clone());
14    dealer.initialize();
15    let mut p1 = Participant::with_arc(ModpGroup::new());
16    let mut p2 = Participant::with_arc(ModpGroup::new());
17    let mut p3 = Participant::with_arc(ModpGroup::new());
18    p1.initialize();
19    p2.initialize();
20    p3.initialize();
21
22    let publickeys = vec![
23        p1.publickey.clone(),
24        p2.publickey.clone(),
25        p3.publickey.clone(),
26    ];
27
28    let distribute_shares_box = dealer.distribute_secret(
29        &string_to_secret(&secret_message),
30        &publickeys,
31        3,
32    );
33
34    assert_eq!(p1.verify_distribution_shares(&distribute_shares_box), true);
35    assert_eq!(p2.verify_distribution_shares(&distribute_shares_box), true);
36    assert_eq!(p3.verify_distribution_shares(&distribute_shares_box), true);
37
38    // p1 extracts the share. [p2 and p3 do this as well.]
39    let mut rng = rand::thread_rng();
40    let w: num_bigint::BigInt = rng
41        .gen_biguint_below(&group.modulus().to_biguint().unwrap())
42        .to_bigint()
43        .unwrap();
44
45    let s1 = p1
46        .extract_secret_share(&distribute_shares_box, &p1.privatekey, &w)
47        .unwrap();
48
49    // p1, p2 and p3 exchange their descrypted shares.
50    let s2 = p2
51        .extract_secret_share(&distribute_shares_box, &p2.privatekey, &w)
52        .unwrap();
53    let s3 = p3
54        .extract_secret_share(&distribute_shares_box, &p3.privatekey, &w)
55        .unwrap();
56
57    // p1 verifies the share received from p2. [Actually everybody verifies every received share.]
58    assert_eq!(
59        p1.verify_share(&s2, &distribute_shares_box, &p2.publickey),
60        true
61    );
62
63    assert_eq!(
64        p2.verify_share(&s3, &distribute_shares_box, &p3.publickey),
65        true
66    );
67
68    assert_eq!(
69        p3.verify_share(&s1, &distribute_shares_box, &s1.publickey),
70        true
71    );
72
73    let share_boxs = [s1, s2, s3];
74    let r1 = dealer
75        .reconstruct(&share_boxs, &distribute_shares_box)
76        .unwrap();
77    let r2 = dealer
78        .reconstruct(&share_boxs, &distribute_shares_box)
79        .unwrap();
80    let r3 = dealer
81        .reconstruct(&share_boxs, &distribute_shares_box)
82        .unwrap();
83
84    let r1_str = string_from_secret(&r1);
85    assert_eq!(secret_message.clone(), r1_str);
86    let r2_str = string_from_secret(&r2);
87    assert_eq!(secret_message.clone(), r2_str);
88    let r3_str = string_from_secret(&r3);
89    assert_eq!(secret_message.clone(), r3_str);
90
91    println!("secret message: {}", secret_message);
92    println!("r1 str: {}", r1_str);
93    println!("r2 str: {}", r2_str);
94    println!("r3 str: {}", r3_str);
95}