pub struct ModpGroup { /* private fields */ }Expand description
2048-bit MODP Group from RFC 3526 (Group ID 14)
§Group Parameters
q: Safe prime (2048-bit)g: Sophie Germain prime = (q-1)/2 (subgroup order)G: Generator = 2
The prime is: 2^2048 - 2^1984 - 1 + 2^64 * { [2^1918 pi] + 124476 }
Implementations§
Source§impl ModpGroup
impl ModpGroup
Sourcepub fn new() -> Arc<Self>
pub fn new() -> Arc<Self>
Create a new MODP group using RFC 3526 2048-bit prime
Examples found in repository?
examples/mpvss_all.rs (line 11)
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}More examples
examples/mpvss_sub.rs (line 11)
10fn main() {
11 let group = ModpGroup::new();
12 let secret_message = String::from("Hello Sub 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 let mut p4 = Participant::with_arc(ModpGroup::new());
19 p1.initialize();
20 p2.initialize();
21 p3.initialize();
22 p4.initialize();
23
24 let publickeys = vec![
25 p1.publickey.clone(),
26 p2.publickey.clone(),
27 p3.publickey.clone(),
28 p4.publickey.clone(),
29 ];
30
31 let distribute_shares_box = dealer.distribute_secret(
32 &string_to_secret(&secret_message),
33 &publickeys,
34 3,
35 );
36
37 assert_eq!(p1.verify_distribution_shares(&distribute_shares_box), true);
38 assert_eq!(p2.verify_distribution_shares(&distribute_shares_box), true);
39 assert_eq!(p3.verify_distribution_shares(&distribute_shares_box), true);
40 assert_eq!(p4.verify_distribution_shares(&distribute_shares_box), true);
41
42 // p1 extracts the share. [p2, p3 and p4 do this as well.]
43 let mut rng = rand::thread_rng();
44 let w: num_bigint::BigInt = rng
45 .gen_biguint_below(&group.modulus().to_biguint().unwrap())
46 .to_bigint()
47 .unwrap();
48
49 let s1 = p1
50 .extract_secret_share(&distribute_shares_box, &p1.privatekey, &w)
51 .unwrap();
52
53 // p1, p2, p3, p4 exchange their descrypted shares.
54 let s2 = p2
55 .extract_secret_share(&distribute_shares_box, &p2.privatekey, &w)
56 .unwrap();
57 let s3 = p3
58 .extract_secret_share(&distribute_shares_box, &p3.privatekey, &w)
59 .unwrap();
60 let s4 = p4
61 .extract_secret_share(&distribute_shares_box, &p4.privatekey, &w)
62 .unwrap();
63
64 // p1 verifies the share received from p2. [Actually everybody verifies every received share.]
65 assert_eq!(
66 p1.verify_share(&s2, &distribute_shares_box, &p2.publickey),
67 true
68 );
69
70 assert_eq!(
71 p2.verify_share(&s3, &distribute_shares_box, &p3.publickey),
72 true
73 );
74
75 assert_eq!(
76 p3.verify_share(&s1, &distribute_shares_box, &s1.publickey),
77 true
78 );
79
80 assert_eq!(
81 p4.verify_share(&s2, &distribute_shares_box, &s2.publickey),
82 true
83 );
84
85 // Threshold is 3, so p1, p2, p4 can reconstruct (or any 3 participants)
86 let share_boxs = [s1.clone(), s2.clone(), s4.clone()];
87 let r1 = dealer
88 .reconstruct(&share_boxs, &distribute_shares_box)
89 .unwrap();
90 let r2 = dealer
91 .reconstruct(&share_boxs, &distribute_shares_box)
92 .unwrap();
93 let r3 = dealer
94 .reconstruct(&share_boxs, &distribute_shares_box)
95 .unwrap();
96 let r4 = dealer
97 .reconstruct(&share_boxs, &distribute_shares_box)
98 .unwrap();
99
100 let r1_str = string_from_secret(&r1);
101 assert_eq!(secret_message.clone(), r1_str);
102 let r2_str = string_from_secret(&r2);
103 assert_eq!(secret_message.clone(), r2_str);
104 let r3_str = string_from_secret(&r3);
105 assert_eq!(secret_message.clone(), r3_str);
106 let r4_str = string_from_secret(&r4);
107 assert_eq!(secret_message.clone(), r4_str);
108
109 println!("secret message: {}", secret_message);
110 println!("r1 str: {}", r1_str);
111 println!("r2 str: {}", r2_str);
112 println!("r3 str: {}", r3_str);
113 println!("r4 str: {}", r4_str);
114}Sourcepub fn init(length: u32) -> Arc<Self>
pub fn init(length: u32) -> Arc<Self>
Initialize a MODP group by generating a safe prime of length bits
Sourcepub fn modulus(&self) -> &BigInt
pub fn modulus(&self) -> &BigInt
Get the safe prime modulus q
Examples found in repository?
examples/mpvss_all.rs (line 41)
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}More examples
examples/mpvss_sub.rs (line 45)
10fn main() {
11 let group = ModpGroup::new();
12 let secret_message = String::from("Hello Sub 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 let mut p4 = Participant::with_arc(ModpGroup::new());
19 p1.initialize();
20 p2.initialize();
21 p3.initialize();
22 p4.initialize();
23
24 let publickeys = vec![
25 p1.publickey.clone(),
26 p2.publickey.clone(),
27 p3.publickey.clone(),
28 p4.publickey.clone(),
29 ];
30
31 let distribute_shares_box = dealer.distribute_secret(
32 &string_to_secret(&secret_message),
33 &publickeys,
34 3,
35 );
36
37 assert_eq!(p1.verify_distribution_shares(&distribute_shares_box), true);
38 assert_eq!(p2.verify_distribution_shares(&distribute_shares_box), true);
39 assert_eq!(p3.verify_distribution_shares(&distribute_shares_box), true);
40 assert_eq!(p4.verify_distribution_shares(&distribute_shares_box), true);
41
42 // p1 extracts the share. [p2, p3 and p4 do this as well.]
43 let mut rng = rand::thread_rng();
44 let w: num_bigint::BigInt = rng
45 .gen_biguint_below(&group.modulus().to_biguint().unwrap())
46 .to_bigint()
47 .unwrap();
48
49 let s1 = p1
50 .extract_secret_share(&distribute_shares_box, &p1.privatekey, &w)
51 .unwrap();
52
53 // p1, p2, p3, p4 exchange their descrypted shares.
54 let s2 = p2
55 .extract_secret_share(&distribute_shares_box, &p2.privatekey, &w)
56 .unwrap();
57 let s3 = p3
58 .extract_secret_share(&distribute_shares_box, &p3.privatekey, &w)
59 .unwrap();
60 let s4 = p4
61 .extract_secret_share(&distribute_shares_box, &p4.privatekey, &w)
62 .unwrap();
63
64 // p1 verifies the share received from p2. [Actually everybody verifies every received share.]
65 assert_eq!(
66 p1.verify_share(&s2, &distribute_shares_box, &p2.publickey),
67 true
68 );
69
70 assert_eq!(
71 p2.verify_share(&s3, &distribute_shares_box, &p3.publickey),
72 true
73 );
74
75 assert_eq!(
76 p3.verify_share(&s1, &distribute_shares_box, &s1.publickey),
77 true
78 );
79
80 assert_eq!(
81 p4.verify_share(&s2, &distribute_shares_box, &s2.publickey),
82 true
83 );
84
85 // Threshold is 3, so p1, p2, p4 can reconstruct (or any 3 participants)
86 let share_boxs = [s1.clone(), s2.clone(), s4.clone()];
87 let r1 = dealer
88 .reconstruct(&share_boxs, &distribute_shares_box)
89 .unwrap();
90 let r2 = dealer
91 .reconstruct(&share_boxs, &distribute_shares_box)
92 .unwrap();
93 let r3 = dealer
94 .reconstruct(&share_boxs, &distribute_shares_box)
95 .unwrap();
96 let r4 = dealer
97 .reconstruct(&share_boxs, &distribute_shares_box)
98 .unwrap();
99
100 let r1_str = string_from_secret(&r1);
101 assert_eq!(secret_message.clone(), r1_str);
102 let r2_str = string_from_secret(&r2);
103 assert_eq!(secret_message.clone(), r2_str);
104 let r3_str = string_from_secret(&r3);
105 assert_eq!(secret_message.clone(), r3_str);
106 let r4_str = string_from_secret(&r4);
107 assert_eq!(secret_message.clone(), r4_str);
108
109 println!("secret message: {}", secret_message);
110 println!("r1 str: {}", r1_str);
111 println!("r2 str: {}", r2_str);
112 println!("r3 str: {}", r3_str);
113 println!("r4 str: {}", r4_str);
114}Sourcepub fn subgroup_order_value(&self) -> &BigInt
pub fn subgroup_order_value(&self) -> &BigInt
Get the subgroup order g (Sophie Germain prime)
Trait Implementations§
Source§impl Group for ModpGroup
impl Group for ModpGroup
Source§fn subgroup_order(&self) -> &Self::Scalar
fn subgroup_order(&self) -> &Self::Scalar
Subgroup order (g=(q-1)/2 for MODP, n for EC with cofactor 1)
Source§fn generator(&self) -> Self::Element
fn generator(&self) -> Self::Element
Main generator G (used for commitments and public key generation) Read more
Source§fn subgroup_generator(&self) -> Self::Element
fn subgroup_generator(&self) -> Self::Element
Subgroup generator g (used for computing commitments C_j = g^a_j) Read more
Source§fn exp(&self, base: &Self::Element, scalar: &Self::Scalar) -> Self::Element
fn exp(&self, base: &Self::Element, scalar: &Self::Scalar) -> Self::Element
Group exponentiation/scalar multiplication: base^exp (MODP) or exp*base (EC) Read more
Source§fn mul(&self, a: &Self::Element, b: &Self::Element) -> Self::Element
fn mul(&self, a: &Self::Element, b: &Self::Element) -> Self::Element
Group multiplication: A * B (MODP) or A + B (EC) Read more
Source§fn scalar_inverse(&self, x: &Self::Scalar) -> Option<Self::Scalar>
fn scalar_inverse(&self, x: &Self::Scalar) -> Option<Self::Scalar>
Scalar modular inverse (for decryption and Lagrange interpolation) Read more
Source§fn element_inverse(&self, x: &Self::Element) -> Option<Self::Element>
fn element_inverse(&self, x: &Self::Element) -> Option<Self::Element>
Element inverse (for handling negative Lagrange coefficients) Read more
Source§fn hash_to_scalar(&self, data: &[u8]) -> Self::Scalar
fn hash_to_scalar(&self, data: &[u8]) -> Self::Scalar
Hash bytes to scalar (for DLEQ challenges) Read more
Source§fn element_to_bytes(&self, elem: &Self::Element) -> Vec<u8> ⓘ
fn element_to_bytes(&self, elem: &Self::Element) -> Vec<u8> ⓘ
Serialize element to bytes (for hashing and storage)
Source§fn bytes_to_element(&self, bytes: &[u8]) -> Option<Self::Element>
fn bytes_to_element(&self, bytes: &[u8]) -> Option<Self::Element>
Deserialize bytes to element Read more
Source§fn generate_private_key(&self) -> Self::Scalar
fn generate_private_key(&self) -> Self::Scalar
Generate a random private key (scalar coprime to group order) Read more
Source§fn generate_public_key(&self, private_key: &Self::Scalar) -> Self::Element
fn generate_public_key(&self, private_key: &Self::Scalar) -> Self::Element
Derive public key from private key: P = G^k (MODP) or P = k*G (EC)
Source§fn scalar_mul(&self, a: &Self::Scalar, b: &Self::Scalar) -> Self::Scalar
fn scalar_mul(&self, a: &Self::Scalar, b: &Self::Scalar) -> Self::Scalar
Scalar multiplication: (a * b) mod order Read more
Auto Trait Implementations§
impl Freeze for ModpGroup
impl RefUnwindSafe for ModpGroup
impl Send for ModpGroup
impl Sync for ModpGroup
impl Unpin for ModpGroup
impl UnsafeUnpin for ModpGroup
impl UnwindSafe for ModpGroup
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more