Skip to content

Commit 60f1e47

Browse files
gilcu3nocktoshi
authored andcommitted
refactor(fix): relocate bench_utils ckd functions (near#3634)
1 parent 5875301 commit 60f1e47

3 files changed

Lines changed: 77 additions & 63 deletions

File tree

crates/threshold-signatures/benches/bench_utils.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
clippy::indexing_slicing
66
)]
77

8+
#[path = "bench_utils/ckd.rs"]
9+
mod ckd;
810
#[path = "bench_utils/dkg.rs"]
911
mod dkg;
1012
#[path = "bench_utils/frost_eddsa.rs"]
@@ -14,6 +16,7 @@ mod ot_based_ecdsa;
1416
#[path = "bench_utils/robust_ecdsa.rs"]
1517
mod robust_ecdsa;
1618

19+
pub use ckd::*;
1720
pub use dkg::*;
1821
pub use frost_eddsa::*;
1922
pub use ot_based_ecdsa::*;
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
use rand::Rng;
2+
use rand_core::{CryptoRngCore, SeedableRng};
3+
4+
use threshold_signatures::{
5+
ReconstructionThreshold,
6+
confidential_key_derivation::{
7+
self as ckd,
8+
ciphersuite::{Field as _, Group as _},
9+
},
10+
participants::Participant,
11+
protocol::Protocol,
12+
test_utils::{MockCryptoRng, generate_participants_with_random_ids, run_keygen},
13+
};
14+
15+
use super::MAX_MALICIOUS;
16+
17+
pub fn prepare_ckd<R: CryptoRngCore + SeedableRng + Send + 'static>(
18+
threshold: ReconstructionThreshold,
19+
rng: &mut R,
20+
) -> PreparedCkdPackage {
21+
let num_participants = threshold.value();
22+
// collect all participants
23+
let participants = generate_participants_with_random_ids(num_participants, rng);
24+
let key_packages = run_keygen(&participants, *MAX_MALICIOUS + 1, rng);
25+
26+
// choose a coordinator at random
27+
let coordinator_index = rng.gen_range(0..num_participants);
28+
let coordinator = participants[coordinator_index];
29+
30+
let mut protocols = Vec::with_capacity(participants.len());
31+
32+
let mut app_id: [u8; 32] = [0u8; 32];
33+
rng.fill_bytes(&mut app_id);
34+
let app_id = ckd::AppId::try_new(app_id).expect("cannot fail");
35+
36+
let scalar_rng = MockCryptoRng::seed_from_u64(rng.next_u64());
37+
let app_sk = ckd::Scalar::random(scalar_rng);
38+
let app_pk = ckd::ElementG1::generator() * app_sk;
39+
40+
for (p, keygen_out) in &key_packages {
41+
let rng_p = MockCryptoRng::seed_from_u64(rng.next_u64());
42+
let protocol = ckd::protocol::ckd(
43+
&participants,
44+
coordinator,
45+
*p,
46+
keygen_out.clone(),
47+
app_id.clone(),
48+
app_pk,
49+
rng_p,
50+
)
51+
.map(|ckd| Box::new(ckd) as Box<dyn Protocol<Output = ckd::CKDOutputOption>>)
52+
.expect("Ckd should succeed");
53+
protocols.push((*p, protocol));
54+
}
55+
56+
PreparedCkdPackage {
57+
protocols,
58+
index: coordinator_index,
59+
key_packages,
60+
app_id,
61+
app_pk,
62+
}
63+
}
64+
65+
pub struct PreparedCkdPackage {
66+
pub protocols: Vec<(
67+
Participant,
68+
Box<dyn Protocol<Output = ckd::CKDOutputOption>>,
69+
)>,
70+
pub index: usize,
71+
pub key_packages: Vec<(Participant, ckd::KeygenOutput)>,
72+
pub app_id: ckd::AppId,
73+
pub app_pk: ckd::ElementG1,
74+
}

crates/threshold-signatures/benches/bench_utils/frost_eddsa.rs

Lines changed: 0 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@ use rand_core::{CryptoRngCore, SeedableRng};
33

44
use threshold_signatures::{
55
ReconstructionThreshold,
6-
confidential_key_derivation::{
7-
self as ckd,
8-
ciphersuite::{Field as _, Group as _},
9-
},
106
frost::eddsa,
117
participants::Participant,
128
protocol::Protocol,
@@ -170,62 +166,3 @@ pub struct FrostEd25519SigV2 {
170166
pub key_packages: Vec<(Participant, eddsa::KeygenOutput)>,
171167
pub message: Vec<u8>,
172168
}
173-
174-
pub fn prepare_ckd<R: CryptoRngCore + SeedableRng + Send + 'static>(
175-
threshold: ReconstructionThreshold,
176-
rng: &mut R,
177-
) -> PreparedCkdPackage {
178-
let num_participants = threshold.value();
179-
// collect all participants
180-
let participants = generate_participants_with_random_ids(num_participants, rng);
181-
let key_packages = run_keygen(&participants, *MAX_MALICIOUS + 1, rng);
182-
183-
// choose a coordinator at random
184-
let coordinator_index = rng.gen_range(0..num_participants);
185-
let coordinator = participants[coordinator_index];
186-
187-
let mut protocols = Vec::with_capacity(participants.len());
188-
189-
let mut app_id: [u8; 32] = [0u8; 32];
190-
rng.fill_bytes(&mut app_id);
191-
let app_id = ckd::AppId::try_new(app_id).expect("cannot fail");
192-
193-
let scalar_rng = MockCryptoRng::seed_from_u64(rng.next_u64());
194-
let app_sk = ckd::Scalar::random(scalar_rng);
195-
let app_pk = ckd::ElementG1::generator() * app_sk;
196-
197-
for (p, keygen_out) in &key_packages {
198-
let rng_p = MockCryptoRng::seed_from_u64(rng.next_u64());
199-
let protocol = ckd::protocol::ckd(
200-
&participants,
201-
coordinator,
202-
*p,
203-
keygen_out.clone(),
204-
app_id.clone(),
205-
app_pk,
206-
rng_p,
207-
)
208-
.map(|ckd| Box::new(ckd) as Box<dyn Protocol<Output = ckd::CKDOutputOption>>)
209-
.expect("Ckd should succeed");
210-
protocols.push((*p, protocol));
211-
}
212-
213-
PreparedCkdPackage {
214-
protocols,
215-
index: coordinator_index,
216-
key_packages,
217-
app_id,
218-
app_pk,
219-
}
220-
}
221-
222-
pub struct PreparedCkdPackage {
223-
pub protocols: Vec<(
224-
Participant,
225-
Box<dyn Protocol<Output = ckd::CKDOutputOption>>,
226-
)>,
227-
pub index: usize,
228-
pub key_packages: Vec<(Participant, ckd::KeygenOutput)>,
229-
pub app_id: ckd::AppId,
230-
pub app_pk: ckd::ElementG1,
231-
}

0 commit comments

Comments
 (0)