Skip to content

Commit 43dec2b

Browse files
committed
Move data transformation from precomputed_keskey to fixture_builder
1 parent 5750b62 commit 43dec2b

File tree

2 files changed

+22
-15
lines changed

2 files changed

+22
-15
lines changed

mithril-common/src/test_utils/fixture_builder.rs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,22 @@ impl MithrilFixtureBuilder {
148148
key_buffer: &'a mut [u8],
149149
kes_key_seed: &'a mut [u8],
150150
) -> (Sum6KesBytes, PublicKey) {
151-
if let Some(cached_value) = precomputed_keskey::cached_kes_key(kes_key_seed) {
152-
return cached_value;
151+
if let Some((kes_bytes, kes_verification_key)) =
152+
MithrilFixtureBuilder::cached_kes_key(kes_key_seed)
153+
{
154+
(kes_bytes, kes_verification_key)
155+
} 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)
153158
}
154-
// TODO We can log a warning to indicate that the cache is not used
155-
MithrilFixtureBuilder::generate_kes_key(key_buffer, kes_key_seed)
159+
}
160+
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+
})
156167
}
157168

158169
fn generate_kes_key<'a>(
@@ -302,7 +313,9 @@ mod tests {
302313
}
303314

304315
/// Verify that there is cached kes key for a number of party id.
305-
/// If the cache is not up to date, it will generate the code to use as cache.
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.
306319
#[test]
307320
fn verify_kes_key_cache_content() {
308321
// Generate code that should be in the match instruction of cached_kes_key.
@@ -337,7 +350,7 @@ mod tests {
337350
let cached_kes_key: Vec<_> = cold_keys
338351
.iter()
339352
.filter_map(|cold_key| {
340-
precomputed_keskey::cached_kes_key(cold_key).map(
353+
MithrilFixtureBuilder::cached_kes_key(cold_key).map(
341354
|(kes_bytes, kes_verification_key)| {
342355
(cold_key.as_slice(), kes_bytes.0, kes_verification_key)
343356
},
@@ -356,7 +369,7 @@ mod tests {
356369

357370
let kes_key_seed = fixture.generate_cold_key_seed(precomputed_number);
358371
assert!(
359-
precomputed_keskey::cached_kes_key(kes_key_seed.as_slice()).is_none(),
372+
MithrilFixtureBuilder::cached_kes_key(kes_key_seed.as_slice()).is_none(),
360373
"We checked precomputed keskey up to {} but it seems to be more.",
361374
precomputed_number
362375
);

mithril-common/src/test_utils/precomputed_keskey.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
use kes_summed_ed25519::PublicKey;
2-
3-
use crate::crypto_helper::Sum6KesBytes;
4-
5-
pub fn cached_kes_key(kes_key_seed: &[u8]) -> Option<(Sum6KesBytes, PublicKey)> {
6-
let (kes_bytes_cached, kes_verification_key_cached) = match kes_key_seed {
1+
pub fn cached_kes_key(kes_key_seed: &[u8]) -> Option<([u8; 612], [u8; 32])> {
2+
let (kes_bytes, kes_verification_key) = match kes_key_seed {
73
[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] => {
84
(
95
[
@@ -466,7 +462,5 @@ pub fn cached_kes_key(kes_key_seed: &[u8]) -> Option<(Sum6KesBytes, PublicKey)>
466462
}
467463
_ => return None,
468464
};
469-
let kes_verification_key = PublicKey::from_bytes(&kes_verification_key_cached).unwrap();
470-
let kes_bytes = Sum6KesBytes(kes_bytes_cached);
471465
Some((kes_bytes, kes_verification_key))
472466
}

0 commit comments

Comments
 (0)