Skip to content

Commit d34b20b

Browse files
committed
Improve naming and comments
1 parent 43dec2b commit d34b20b

File tree

3 files changed

+38
-36
lines changed

3 files changed

+38
-36
lines changed

mithril-common/src/test_utils/fixture_builder.rs

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use kes_summed_ed25519::{kes::Sum6Kes, traits::KesSk, PublicKey};
1+
use kes_summed_ed25519::{kes::Sum6Kes, traits::KesSk, PublicKey as KesPublicKey};
22
use rand_chacha::ChaCha20Rng;
33
use rand_core::{RngCore, SeedableRng};
44

@@ -11,7 +11,7 @@ use crate::{
1111
test_utils::{fake_data, mithril_fixture::MithrilFixture},
1212
};
1313

14-
use super::precomputed_keskey;
14+
use super::precomputed_kes_key;
1515

1616
/// A builder of mithril types.
1717
pub struct MithrilFixtureBuilder {
@@ -144,58 +144,61 @@ impl MithrilFixtureBuilder {
144144
}
145145
}
146146

147-
fn provide_kes_key<'a>(
148-
key_buffer: &'a mut [u8],
149-
kes_key_seed: &'a mut [u8],
150-
) -> (Sum6KesBytes, PublicKey) {
147+
fn provide_kes_key(kes_key_seed: &mut [u8]) -> (Sum6KesBytes, KesPublicKey) {
151148
if let Some((kes_bytes, kes_verification_key)) =
152149
MithrilFixtureBuilder::cached_kes_key(kes_key_seed)
153150
{
154151
(kes_bytes, kes_verification_key)
155152
} else {
156-
// TODO We can log a warning to indicate that the cache is not used
157-
MithrilFixtureBuilder::generate_kes_key(key_buffer, kes_key_seed)
153+
println!(
154+
"KES key not found in test cache, generating a new one for the seed {:?}.",
155+
kes_key_seed
156+
);
157+
MithrilFixtureBuilder::generate_kes_key(kes_key_seed)
158158
}
159159
}
160160

161-
fn cached_kes_key(kes_key_seed: &[u8]) -> Option<(Sum6KesBytes, PublicKey)> {
162-
precomputed_keskey::cached_kes_key(kes_key_seed).map(|(kes_bytes, kes_verification_key)| {
163-
let kes_verification_key = PublicKey::from_bytes(&kes_verification_key).unwrap();
164-
let kes_bytes = Sum6KesBytes(kes_bytes);
165-
(kes_bytes, kes_verification_key)
166-
})
161+
fn cached_kes_key(kes_key_seed: &[u8]) -> Option<(Sum6KesBytes, KesPublicKey)> {
162+
precomputed_kes_key::cached_kes_key(kes_key_seed).map(
163+
|(kes_bytes, kes_verification_key)| {
164+
let kes_verification_key = KesPublicKey::from_bytes(&kes_verification_key).unwrap();
165+
let kes_bytes = Sum6KesBytes(kes_bytes);
166+
167+
(kes_bytes, kes_verification_key)
168+
},
169+
)
167170
}
168171

169-
fn generate_kes_key<'a>(
170-
key_buffer: &'a mut [u8],
171-
kes_key_seed: &'a mut [u8],
172-
) -> (Sum6KesBytes, PublicKey) {
173-
let (kes_secret_key, kes_verification_key) = Sum6Kes::keygen(key_buffer, kes_key_seed);
172+
fn generate_kes_key(kes_key_seed: &mut [u8]) -> (Sum6KesBytes, KesPublicKey) {
173+
let mut key_buffer = [0u8; Sum6Kes::SIZE + 4];
174+
175+
let (kes_secret_key, kes_verification_key) = Sum6Kes::keygen(&mut key_buffer, kes_key_seed);
174176
let mut kes_bytes = Sum6KesBytes([0u8; Sum6Kes::SIZE + 4]);
175177
kes_bytes.0.copy_from_slice(&kes_secret_key.clone_sk());
178+
176179
(kes_bytes, kes_verification_key)
177180
}
178181

179182
fn generate_cold_key_seed(&self, party_index: usize) -> Vec<u8> {
180-
let mut cold_key_seed: Vec<u8> = (party_index)
183+
let mut cold_key_seed: Vec<_> = (party_index)
181184
.to_le_bytes()
182185
.iter()
183186
.zip(self.party_id_seed)
184187
.map(|(v1, v2)| v1 + v2)
185188
.collect();
186189
cold_key_seed.resize(32, 0);
190+
187191
cold_key_seed
188192
}
189193

190194
fn build_party_with_operational_certificate(&self, party_index: usize) -> PartyId {
191-
let cold_key_seed: Vec<u8> = self.generate_cold_key_seed(party_index).to_vec();
195+
let cold_key_seed = self.generate_cold_key_seed(party_index).to_vec();
192196
let mut kes_key_seed = cold_key_seed.clone();
193197

194198
let keypair =
195199
ColdKeyGenerator::create_deterministic_keypair(cold_key_seed.try_into().unwrap());
196-
let mut kes_key_buffer = [0u8; Sum6Kes::SIZE + 4];
197200
let (kes_bytes, kes_verification_key) =
198-
MithrilFixtureBuilder::provide_kes_key(&mut kes_key_buffer, &mut kes_key_seed);
201+
MithrilFixtureBuilder::provide_kes_key(&mut kes_key_seed);
199202
let operational_certificate = OpCert::new(kes_verification_key, 0, 0, keypair);
200203
let party_id = operational_certificate
201204
.compute_protocol_party_id()
@@ -219,7 +222,6 @@ impl MithrilFixtureBuilder {
219222
#[cfg(test)]
220223
mod tests {
221224
use super::*;
222-
use kes_summed_ed25519::{kes::Sum6Kes, traits::KesSk, PublicKey};
223225
use std::collections::BTreeSet;
224226

225227
#[test]
@@ -312,15 +314,14 @@ mod tests {
312314
}
313315
}
314316

315-
/// Verify that there is cached kes key for a number of party id.
316-
/// If the cache is not up to date, the test will generate the code that could be copy/paste into the [precomputed_keskey] module.
317-
/// The number of party id that should be in cache is defined in the test.
318-
/// If we want lore or less, just change `precomputed_number` value and launch the test to generate the code.
317+
/// Verify that there is a cached kes key for a number of party id.
318+
/// If the cache is not up to date, the test will generate the code that can be copied/pasted into the [precomputed_kes_key] module.
319+
/// The number of party id that should be in cache is defined with `precomputed_number`
319320
#[test]
320321
fn verify_kes_key_cache_content() {
321-
// Generate code that should be in the match instruction of cached_kes_key.
322-
// It could be copy paste to update the cache.
323-
fn generate_code(party_ids: &Vec<(&[u8], [u8; 612], PublicKey)>) -> String {
322+
// Generate code that should be in the `cached_kes_key` function of the `precomputed_kes_key.rs` file.
323+
// It can be copied and pasted to update the cache.
324+
fn generate_code(party_ids: &Vec<(&[u8], [u8; 612], KesPublicKey)>) -> String {
324325
party_ids
325326
.iter()
326327
.map(|(key, i, p)| format!("{:?} => ({:?}, {:?}),", key, i, p.as_bytes()))
@@ -338,10 +339,9 @@ mod tests {
338339
let computed_keys_key: Vec<_> = cold_keys
339340
.iter()
340341
.map(|cold_key| {
341-
let mut kes_key_buffer = [0u8; Sum6Kes::SIZE + 4];
342342
let mut kes_key_seed: Vec<u8> = cold_key.clone();
343343
let (kes_bytes, kes_verification_key) =
344-
MithrilFixtureBuilder::generate_kes_key(&mut kes_key_buffer, &mut kes_key_seed);
344+
MithrilFixtureBuilder::generate_kes_key(&mut kes_key_seed);
345345

346346
(cold_key.as_slice(), kes_bytes.0, kes_verification_key)
347347
})
@@ -363,14 +363,14 @@ mod tests {
363363

364364
assert_eq!(
365365
computed_keys_key, cached_kes_key,
366-
"Precomputed keskeys should be:\n{}\nbut seems to be:\n{}",
366+
"Precomputed KES keys should be:\n{}\nbut seems to be:\n{}",
367367
expected_code, actual_code
368368
);
369369

370370
let kes_key_seed = fixture.generate_cold_key_seed(precomputed_number);
371371
assert!(
372372
MithrilFixtureBuilder::cached_kes_key(kes_key_seed.as_slice()).is_none(),
373-
"We checked precomputed keskey up to {} but it seems to be more.",
373+
"We checked precomputed KES keys up to {} but it seems to be more.",
374374
precomputed_number
375375
);
376376
}

mithril-common/src/test_utils/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ mod cardano_transactions_builder;
1717
mod certificate_chain_builder;
1818
mod fixture_builder;
1919
mod mithril_fixture;
20-
mod precomputed_keskey;
20+
mod precomputed_kes_key;
2121
mod temp_dir;
2222

2323
#[cfg(feature = "test_http_server")]

mithril-common/src/test_utils/precomputed_keskey.rs renamed to mithril-common/src/test_utils/precomputed_kes_key.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/// If there is a need to update this data, we can run the `verify_kes_key_cache_content` test in `fixture_builder.rs`
2+
/// which generates the code of this function.
13
pub fn cached_kes_key(kes_key_seed: &[u8]) -> Option<([u8; 612], [u8; 32])> {
24
let (kes_bytes, kes_verification_key) = match kes_key_seed {
35
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] => {

0 commit comments

Comments
 (0)