Skip to main content

mpvss_all/
mpvss_all.rs

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