Debian Codegraph @

third_party/rust/mls-rs/examples/large_group.rs

raw
1 // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2 // Copyright by contributors to this project.
3 // SPDX-License-Identifier: (Apache-2.0 OR MIT)
4
5 use mls_rs::{
6 client_builder::MlsConfig,
7 error::MlsError,
8 identity::{
9 basic::{BasicCredential, BasicIdentityProvider},
10 SigningIdentity,
11 },
12 mls_rules::{CommitOptions, DefaultMlsRules},
13 CipherSuite, CipherSuiteProvider, Client, CryptoProvider, Group,
14 };
15
16 const CIPHERSUITE: CipherSuite = CipherSuite::CURVE25519_AES128;
17 const GROUP_SIZES: [usize; 8] = [2, 3, 5, 9, 17, 33, 65, 129];
18
19 enum Case {
20 Best,
21 Worst,
22 }
23
24 fn bench_commit_size<P: CryptoProvider + Clone>(
25 case_group: Case,
26 crypto_provider: &P,
27 ) -> Result<(Vec<usize>, Vec<usize>), MlsError> {
28 let mut small_bench = vec![];
29 let mut large_bench = vec![];
30
31 for num_groups in GROUP_SIZES.iter().copied() {
32 let (small_commit, large_commit) = match case_group {
33 Case::Best => {
34 let mut groups = make_groups_best_case(num_groups, crypto_provider)?;
35 let small_commit = groups[num_groups - 1].commit(vec![])?.commit_message;
36 let large_commit = groups[0].commit(vec![])?.commit_message;
37 (small_commit, large_commit)
38 }
39 Case::Worst => {
40 let mut groups = make_groups_worst_case(num_groups, crypto_provider)?;
41 let small_commit = groups[num_groups - 1].commit(vec![])?.commit_message;
42 let large_commit = groups[0].commit(vec![])?.commit_message;
43 (small_commit, large_commit)
44 }
45 };
46
47 small_bench.push(small_commit.to_bytes()?.len());
48 large_bench.push(large_commit.to_bytes()?.len());
49 }
50
51 Ok((small_bench, large_bench))
52 }
53
54 // Bob[0] crates a group. Repeat for `i=0` to `num_groups - 1` times : Bob[i] adds Bob[i+1]
55 fn make_groups_best_case<P: CryptoProvider + Clone>(
56 num_groups: usize,
57 crypto_provider: &P,
58 ) -> Result<Vec<Group<impl MlsConfig>>, MlsError> {
59 let bob_client = make_client(crypto_provider.clone(), &make_name(0))?;
60
61 let bob_group = bob_client.create_group(Default::default(), Default::default())?;
62
63 let mut groups = vec![bob_group];
64
65 for i in 0..(num_groups - 1) {
66 let bob_client = make_client(crypto_provider.clone(), &make_name(i + 1))?;
67
68 // The new client generates a key package.
69 let bob_kpkg =
70 bob_client.generate_key_package_message(Default::default(), Default::default())?;
71
72 // Last group sends a commit adding the new client to the group.
73 let commit = groups
74 .last_mut()
75 .unwrap()
76 .commit_builder()
77 .add_member(bob_kpkg)?
78 .build()?;
79
80 // All other groups process the commit.
81 for group in groups.iter_mut().rev().skip(1) {
82 group.process_incoming_message(commit.commit_message.clone())?;
83 }
84
85 // The last group applies the generated commit.
86 groups.last_mut().unwrap().apply_pending_commit()?;
87
88 // The new member joins.
89 let (bob_group, _info) = bob_client.join_group(None, &commit.welcome_messages[0])?;
90
91 groups.push(bob_group);
92 }
93
94 Ok(groups)
95 }
96
97 // Alice creates a group by adding `num_groups - 1` clients in one commit.
98 fn make_groups_worst_case<P: CryptoProvider + Clone>(
99 num_groups: usize,
100 crypto_provider: &P,
101 ) -> Result<Vec<Group<impl MlsConfig>>, MlsError> {
102 let alice_client = make_client(crypto_provider.clone(), &make_name(0))?;
103
104 let mut alice_group = alice_client.create_group(Default::default(), Default::default())?;
105
106 let bob_clients = (0..(num_groups - 1))
107 .map(|i| make_client(crypto_provider.clone(), &make_name(i + 1)))
108 .collect::<Result<Vec<_>, _>>()?;
109
110 // Alice adds all Bob's clients in a single commit.
111 let mut commit_builder = alice_group.commit_builder();
112
113 for bob_client in &bob_clients {
114 let bob_kpkg =
115 bob_client.generate_key_package_message(Default::default(), Default::default())?;
116 commit_builder = commit_builder.add_member(bob_kpkg)?;
117 }
118
119 let welcome_message = &commit_builder.build()?.welcome_messages[0];
120
121 alice_group.apply_pending_commit()?;
122
123 // Bob's clients join the group.
124 let mut groups = vec![alice_group];
125
126 for bob_client in &bob_clients {
127 let (bob_group, _info) = bob_client.join_group(None, welcome_message)?;
128 groups.push(bob_group);
129 }
130
131 Ok(groups)
132 }
133
134 fn make_client<P: CryptoProvider + Clone>(
135 crypto_provider: P,
136 name: &str,
137 ) -> Result<Client<impl MlsConfig>, MlsError> {
138 let cipher_suite = crypto_provider.cipher_suite_provider(CIPHERSUITE).unwrap();
139
140 // Generate a signature key pair.
141 let (secret, public) = cipher_suite.signature_key_generate().unwrap();
142
143 // Create a basic credential for the session.
144 // NOTE: BasicCredential is for demonstration purposes and not recommended for production.
145 // X.509 credentials are recommended.
146 let basic_identity = BasicCredential::new(name.as_bytes().to_vec());
147 let signing_identity = SigningIdentity::new(basic_identity.into_credential(), public);
148
149 Ok(Client::builder()
150 .identity_provider(BasicIdentityProvider)
151 .crypto_provider(crypto_provider)
152 .mls_rules(
153 DefaultMlsRules::new()
154 .with_commit_options(CommitOptions::new().with_path_required(true)),
155 )
156 .signing_identity(signing_identity, secret, CIPHERSUITE)
157 .build())
158 }
159
160 fn make_name(i: usize) -> String {
161 format!("bob {i:08}")
162 }
163
164 fn main() -> Result<(), MlsError> {
165 let crypto_provider = mls_rs_crypto_openssl::OpensslCryptoProvider::default();
166
167 println!("Demonstrate that performance depends on a) group evolution and b) a members position in the tree.\n");
168
169 let (small_bench_bc, large_bench_bc) = bench_commit_size(Case::Best, &crypto_provider)?;
170 let (small_bench_wc, large_bench_wc) = bench_commit_size(Case::Worst, &crypto_provider)?;
171
172 println!("\nBest case a), worst case b) : commit size is θ(log(n)) bytes.");
173 println!("group sizes n :\n{GROUP_SIZES:?}\ncommit sizes :\n{large_bench_bc:?}");
174
175 println!("\nWorst case a), worst case b) : commit size is θ(n) bytes.");
176 println!("group sizes n :\n{GROUP_SIZES:?}\ncommit sizes :\n{large_bench_wc:?}");
177
178 println!(
179 "\nBest case b) : if n-1 is a power of 2, commit size is θ(1) bytes, independent of a)."
180 );
181 println!("group sizes n :\n{GROUP_SIZES:?}\ncommit sizes, best case a) :\n{small_bench_bc:?}");
182 println!("commit sizes, worst case a) :\n{small_bench_wc:?}");
183
184 Ok(())
185 }