Skip to content

Commit a058e89

Browse files
authored
chore: Implement From<MasterPublicKeyId> for AlgorithmId (#5962)
This is cleaner and avoids some duplication of `algorithm_for_key_id` functions.
1 parent e44bb4c commit a058e89

File tree

12 files changed

+122
-143
lines changed

12 files changed

+122
-143
lines changed

rs/consensus/idkg/src/complaints.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -977,7 +977,7 @@ impl<'a> Action<'a> {
977977
#[cfg(test)]
978978
mod tests {
979979
use super::*;
980-
use crate::{test_utils::*, utils::algorithm_for_key_id};
980+
use crate::test_utils::*;
981981
use assert_matches::assert_matches;
982982
use ic_consensus_utils::crypto::SignVerify;
983983
use ic_crypto_test_utils_canister_threshold_sigs::CanisterThresholdSigTestEnvironment;
@@ -1915,7 +1915,7 @@ mod tests {
19151915
with_test_replica_logger(|logger| {
19161916
let env = CanisterThresholdSigTestEnvironment::new(3, &mut rng);
19171917
let (_, _, transcript) =
1918-
create_corrupted_transcript(&env, &mut rng, algorithm_for_key_id(&key_id));
1918+
create_corrupted_transcript(&env, &mut rng, AlgorithmId::from(key_id.inner()));
19191919

19201920
let crypto = env
19211921
.nodes

rs/consensus/idkg/src/payload_builder.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -752,10 +752,7 @@ mod tests {
752752
use super::*;
753753
use crate::{
754754
test_utils::*,
755-
utils::{
756-
algorithm_for_key_id, block_chain_reader,
757-
generate_responses_to_signature_request_contexts,
758-
},
755+
utils::{block_chain_reader, generate_responses_to_signature_request_contexts},
759756
};
760757
use assert_matches::assert_matches;
761758
use ic_consensus_mocks::{dependencies, Dependencies};
@@ -792,7 +789,7 @@ mod tests {
792789
idkg::IDkgTranscript, ThresholdEcdsaCombinedSignature,
793790
ThresholdSchnorrCombinedSignature,
794791
},
795-
CryptoHash, CryptoHashOf, ExtendedDerivationPath,
792+
AlgorithmId, CryptoHash, CryptoHashOf, ExtendedDerivationPath,
796793
},
797794
messages::CallbackId,
798795
time::UNIX_EPOCH,
@@ -1423,7 +1420,7 @@ mod tests {
14231420
assert_eq!(request.key_id(), key_id.clone());
14241421
assert_eq!(
14251422
reshare_params.as_ref().algorithm_id,
1426-
algorithm_for_key_id(&key_id.clone())
1423+
AlgorithmId::from(key_id.inner())
14271424
);
14281425
for transcript_ref in reshare_params.as_ref().get_refs() {
14291426
assert_ne!(transcript_ref.height, new_summary_height);
@@ -1475,7 +1472,7 @@ mod tests {
14751472
assert_eq!(request.key_id(), key_id.clone());
14761473
assert_eq!(
14771474
reshare_params.as_ref().algorithm_id,
1478-
algorithm_for_key_id(&key_id.clone())
1475+
AlgorithmId::from(key_id.inner())
14791476
);
14801477
for transcript_ref in reshare_params.as_ref().get_refs() {
14811478
assert_eq!(transcript_ref.height, new_summary_height);
@@ -1486,7 +1483,7 @@ mod tests {
14861483
// have been resolved/copied into the summary block
14871484
assert_eq!(summary.idkg_transcripts.len(), expected_transcripts.len());
14881485
for (id, transcript) in &summary.idkg_transcripts {
1489-
assert_eq!(transcript.algorithm_id, algorithm_for_key_id(&key_id));
1486+
assert_eq!(transcript.algorithm_id, AlgorithmId::from(key_id.inner()));
14901487
assert!(expected_transcripts.contains(id));
14911488
}
14921489
})
@@ -1996,7 +1993,7 @@ mod tests {
19961993
caller: user_test_id(1).get(),
19971994
derivation_path: vec![],
19981995
};
1999-
let algorithm = algorithm_for_key_id(&key_id);
1996+
let algorithm = AlgorithmId::from(key_id.inner());
20001997
let test_inputs = match key_id.inner() {
20011998
MasterPublicKeyId::Ecdsa(_) => {
20021999
TestSigInputs::from(&generate_tecdsa_protocol_inputs(
@@ -2462,7 +2459,7 @@ mod tests {
24622459

24632460
// Generate initial dealings
24642461
let initial_dealings =
2465-
dummy_initial_idkg_dealing_for_tests(algorithm_for_key_id(&key_id), &mut rng);
2462+
dummy_initial_idkg_dealing_for_tests(AlgorithmId::from(key_id.inner()), &mut rng);
24662463
let init_tid = initial_dealings.params().transcript_id();
24672464

24682465
// Step 1: initial bootstrap payload should be created successfully

rs/consensus/idkg/src/payload_builder/key_transcript.rs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
use crate::{
2-
payload_builder::IDkgPayloadError, pre_signer::IDkgTranscriptBuilder,
3-
utils::algorithm_for_key_id,
4-
};
1+
use crate::{payload_builder::IDkgPayloadError, pre_signer::IDkgTranscriptBuilder};
52
use ic_logger::{info, ReplicaLogger};
63
use ic_types::{
74
consensus::idkg::{
85
self, HasIDkgMasterPublicKeyId, IDkgBlockReader, IDkgUIDGenerator, MasterKeyTranscript,
96
TranscriptAttributes,
107
},
11-
crypto::canister_threshold_sig::idkg::IDkgTranscript,
8+
crypto::{canister_threshold_sig::idkg::IDkgTranscript, AlgorithmId},
129
Height, NodeId, RegistryVersion,
1310
};
1411
use std::collections::BTreeSet;
@@ -126,7 +123,7 @@ pub(super) fn update_next_key_transcript(
126123
dealers_set,
127124
receivers_set,
128125
registry_version,
129-
algorithm_for_key_id(&key_transcript.key_id()),
126+
AlgorithmId::from(key_transcript.key_id().inner()),
130127
),
131128
);
132129
}
@@ -213,12 +210,9 @@ pub(super) fn update_next_key_transcript(
213210
#[cfg(test)]
214211
mod tests {
215212
use super::*;
216-
use crate::{
217-
test_utils::{
218-
create_reshare_unmasked_transcript_param, set_up_idkg_payload, IDkgPayloadTestHelper,
219-
TestIDkgBlockReader, TestIDkgTranscriptBuilder,
220-
},
221-
utils::algorithm_for_key_id,
213+
use crate::test_utils::{
214+
create_reshare_unmasked_transcript_param, set_up_idkg_payload, IDkgPayloadTestHelper,
215+
TestIDkgBlockReader, TestIDkgTranscriptBuilder,
222216
};
223217
use ic_crypto_test_utils_canister_threshold_sigs::{
224218
dummy_values::dummy_initial_idkg_dealing_for_tests, generate_key_transcript, node::Nodes,
@@ -474,7 +468,7 @@ mod tests {
474468
}
475469
assert_eq!(
476470
completed_transcript.algorithm_id,
477-
algorithm_for_key_id(&key_id)
471+
AlgorithmId::from(key_id.inner())
478472
);
479473
assert_eq!(payload.single_key_transcript().key_id(), key_id);
480474
}
@@ -612,7 +606,7 @@ mod tests {
612606
&unmasked_transcript,
613607
&target_subnet_nodes_ids,
614608
registry_version,
615-
algorithm_for_key_id(&key_id),
609+
AlgorithmId::from(key_id.inner()),
616610
);
617611
let (params, transcript) =
618612
idkg::unpack_reshare_of_unmasked_params(cur_height, &reshare_params).unwrap();
@@ -623,7 +617,7 @@ mod tests {
623617
payload.single_key_transcript_mut().next_in_creation =
624618
idkg::KeyTranscriptCreation::XnetReshareOfUnmaskedParams((
625619
Box::new(dummy_initial_idkg_dealing_for_tests(
626-
algorithm_for_key_id(&key_id),
620+
AlgorithmId::from(key_id.inner()),
627621
&mut rng,
628622
)),
629623
params,

rs/consensus/idkg/src/payload_builder/pre_signatures.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
use crate::{
2-
payload_builder::IDkgPayloadError, pre_signer::IDkgTranscriptBuilder,
3-
utils::algorithm_for_key_id,
4-
};
1+
use crate::{payload_builder::IDkgPayloadError, pre_signer::IDkgTranscriptBuilder};
52
use ic_logger::{debug, error, ReplicaLogger};
63
use ic_management_canister_types_private::MasterPublicKeyId;
74
use ic_registry_subnet_features::ChainKeyConfig;
@@ -15,7 +12,7 @@ use ic_types::{
1512
HasIDkgMasterPublicKeyId, IDkgMasterPublicKeyId, IDkgUIDGenerator, PreSigId,
1613
TranscriptAttributes, UnmaskedTranscriptWithAttributes,
1714
},
18-
crypto::canister_threshold_sig::idkg::IDkgTranscript,
15+
crypto::{canister_threshold_sig::idkg::IDkgTranscript, AlgorithmId},
1916
messages::CallbackId,
2017
Height, NodeId, RegistryVersion,
2118
};
@@ -426,7 +423,7 @@ fn new_random_config(
426423
dealers,
427424
receivers,
428425
summary_registry_version,
429-
algorithm_for_key_id(key_id),
426+
AlgorithmId::from(key_id.inner()),
430427
)
431428
}
432429

@@ -447,7 +444,7 @@ pub fn new_random_unmasked_config(
447444
dealers,
448445
receivers,
449446
summary_registry_version,
450-
algorithm_for_key_id(key_id),
447+
AlgorithmId::from(key_id.inner()),
451448
)
452449
}
453450

@@ -533,7 +530,7 @@ pub(super) mod test_utils {
533530

534531
#[cfg(test)]
535532
pub(super) mod tests {
536-
use super::{algorithm_for_key_id, test_utils::*, *};
533+
use super::{test_utils::*, *};
537534
use crate::test_utils::{
538535
create_available_pre_signature, create_available_pre_signature_with_key_transcript,
539536
into_idkg_contexts, set_up_idkg_payload, IDkgPayloadTestHelper, TestIDkgBlockReader,
@@ -628,7 +625,7 @@ pub(super) mod tests {
628625
MasterPublicKeyId::Schnorr(transcript.key_id.clone())
629626
);
630627
let config = transcript.blinder_unmasked_config.as_ref();
631-
assert_eq!(config.algorithm_id, algorithm_for_key_id(key_id));
628+
assert_eq!(config.algorithm_id, AlgorithmId::from(key_id.inner()));
632629
assert_eq!(config.registry_version, registry_version);
633630
assert_eq!(config.dealers, config.receivers);
634631
assert_eq!(config.dealers, BTreeSet::from(*nodes));
@@ -1043,7 +1040,7 @@ pub(super) mod tests {
10431040
.expect("Translating should succeed");
10441041
assert_eq!(
10451042
translated.blinder_unmasked().algorithm_id,
1046-
algorithm_for_key_id(&key_id)
1043+
AlgorithmId::from(key_id.inner())
10471044
);
10481045
}
10491046

@@ -1078,7 +1075,7 @@ pub(super) mod tests {
10781075
&env,
10791076
&dealers,
10801077
&receivers,
1081-
algorithm_for_key_id(&key_id),
1078+
AlgorithmId::from(key_id.inner()),
10821079
&mut rng,
10831080
);
10841081
let key_transcript2 =
@@ -1185,7 +1182,7 @@ pub(super) mod tests {
11851182
&env,
11861183
&dealers,
11871184
&receivers,
1188-
algorithm_for_key_id(&key_id),
1185+
AlgorithmId::from(key_id.inner()),
11891186
&mut rng,
11901187
);
11911188
let other_key_transcript =

rs/consensus/idkg/src/payload_builder/resharing.rs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,6 @@ mod tests {
189189
create_reshare_request, set_up_idkg_payload, TestIDkgBlockReader,
190190
TestIDkgTranscriptBuilder,
191191
},
192-
utils::algorithm_for_key_id,
193192
};
194193
use assert_matches::assert_matches;
195194
use ic_crypto_test_utils_canister_threshold_sigs::dummy_values::{
@@ -200,7 +199,10 @@ mod tests {
200199
use ic_management_canister_types_private::ComputeInitialIDkgDealingsResponse;
201200
use ic_test_utilities_consensus::idkg::*;
202201
use ic_test_utilities_types::ids::subnet_test_id;
203-
use ic_types::consensus::idkg::{IDkgMasterPublicKeyId, IDkgPayload};
202+
use ic_types::{
203+
consensus::idkg::{IDkgMasterPublicKeyId, IDkgPayload},
204+
crypto::AlgorithmId,
205+
};
204206

205207
fn set_up(
206208
key_ids: Vec<IDkgMasterPublicKeyId>,
@@ -248,7 +250,7 @@ mod tests {
248250
initial_dealings.insert(
249251
i,
250252
dummy_initial_idkg_dealing_for_tests(
251-
algorithm_for_key_id(key_id),
253+
AlgorithmId::from(key_id.inner()),
252254
&mut reproducible_rng(),
253255
),
254256
);
@@ -324,7 +326,10 @@ mod tests {
324326
.ongoing_xnet_reshares
325327
.get(&request)
326328
.expect("should exist");
327-
assert_eq!(params.as_ref().algorithm_id, algorithm_for_key_id(&key_id));
329+
assert_eq!(
330+
params.as_ref().algorithm_id,
331+
AlgorithmId::from(key_id.inner())
332+
);
328333

329334
assert!(payload.xnet_reshare_agreements.is_empty());
330335
}
@@ -348,14 +353,17 @@ mod tests {
348353
.ongoing_xnet_reshares
349354
.get(&request)
350355
.expect("should exist");
351-
assert_eq!(params.as_ref().algorithm_id, algorithm_for_key_id(&key_id));
356+
assert_eq!(
357+
params.as_ref().algorithm_id,
358+
AlgorithmId::from(key_id.inner())
359+
);
352360
let params_2 = payload
353361
.ongoing_xnet_reshares
354362
.get(&request_2)
355363
.expect("should exist");
356364
assert_eq!(
357365
params_2.as_ref().algorithm_id,
358-
algorithm_for_key_id(&key_id)
366+
AlgorithmId::from(key_id.inner())
359367
);
360368
assert!(payload.xnet_reshare_agreements.is_empty());
361369
}
@@ -426,7 +434,10 @@ mod tests {
426434
.unwrap()
427435
.as_ref()
428436
.clone();
429-
assert_eq!(reshare_params_1.algorithm_id, algorithm_for_key_id(&key_id));
437+
assert_eq!(
438+
reshare_params_1.algorithm_id,
439+
AlgorithmId::from(key_id.inner())
440+
);
430441
let dealings_1 = dummy_dealings(reshare_params_1.transcript_id, &reshare_params_1.dealers);
431442
transcript_builder.add_dealings(reshare_params_1.transcript_id, dealings_1.clone());
432443
update_completed_reshare_requests(
@@ -460,7 +471,10 @@ mod tests {
460471
.unwrap()
461472
.as_ref()
462473
.clone();
463-
assert_eq!(reshare_params_2.algorithm_id, algorithm_for_key_id(&key_id));
474+
assert_eq!(
475+
reshare_params_2.algorithm_id,
476+
AlgorithmId::from(key_id.inner())
477+
);
464478
let dealings_2 = dummy_dealings(reshare_params_2.transcript_id, &reshare_params_2.dealers);
465479
transcript_builder.add_dealings(reshare_params_2.transcript_id, dealings_2.clone());
466480
update_completed_reshare_requests(

rs/consensus/idkg/src/payload_verifier.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,6 @@ mod test {
645645
signatures::update_signature_agreements,
646646
},
647647
test_utils::*,
648-
utils::algorithm_for_key_id,
649648
};
650649
use assert_matches::assert_matches;
651650
use ic_crypto_test_utils_canister_threshold_sigs::{
@@ -730,7 +729,7 @@ mod test {
730729
env.nodes.ids(),
731730
env.nodes.ids(),
732731
registry_version,
733-
algorithm_for_key_id(&key_id),
732+
AlgorithmId::from(key_id.inner()),
734733
));
735734
curr_payload.single_key_transcript_mut().next_in_creation =
736735
idkg::KeyTranscriptCreation::Created(transcript_ref_0);
@@ -830,7 +829,10 @@ mod test {
830829

831830
// Create completed dealings for request 1.
832831
let reshare_params = payload.ongoing_xnet_reshares.get(&req_1).unwrap().as_ref();
833-
assert_eq!(reshare_params.algorithm_id, algorithm_for_key_id(&key_id));
832+
assert_eq!(
833+
reshare_params.algorithm_id,
834+
AlgorithmId::from(key_id.inner())
835+
);
834836
let dealings = dummy_dealings(reshare_params.transcript_id, &reshare_params.dealers);
835837
transcript_builder.add_dealings(reshare_params.transcript_id, dealings);
836838
update_completed_reshare_requests(
@@ -864,7 +866,10 @@ mod test {
864866

865867
// Create another request and dealings
866868
let reshare_params = payload.ongoing_xnet_reshares.get(&req_2).unwrap().as_ref();
867-
assert_eq!(reshare_params.algorithm_id, algorithm_for_key_id(&key_id));
869+
assert_eq!(
870+
reshare_params.algorithm_id,
871+
AlgorithmId::from(key_id.inner())
872+
);
868873
let dealings = dummy_dealings(reshare_params.transcript_id, &reshare_params.dealers);
869874
transcript_builder.add_dealings(reshare_params.transcript_id, dealings);
870875
let mut prev_payload = payload.clone();
@@ -1271,7 +1276,7 @@ mod test {
12711276
dealers,
12721277
receivers,
12731278
registry_version,
1274-
algorithm_for_key_id(&master_public_key_id),
1279+
AlgorithmId::from(master_public_key_id.inner()),
12751280
);
12761281
env.nodes.run_idkg_and_create_and_verify_transcript(
12771282
&param.as_ref().translate(&block_reader).unwrap(),

rs/consensus/idkg/src/pre_signer.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1360,7 +1360,7 @@ impl TranscriptState {
13601360
#[cfg(test)]
13611361
mod tests {
13621362
use super::*;
1363-
use crate::{test_utils::*, utils::algorithm_for_key_id};
1363+
use crate::test_utils::*;
13641364
use assert_matches::assert_matches;
13651365
use ic_crypto_test_utils_canister_threshold_sigs::{
13661366
setup_masked_random_params, CanisterThresholdSigTestEnvironment, IDkgParticipants,
@@ -1372,7 +1372,7 @@ mod tests {
13721372
use ic_test_utilities_types::ids::{NODE_1, NODE_2, NODE_3, NODE_4};
13731373
use ic_types::{
13741374
consensus::idkg::{IDkgMasterPublicKeyId, IDkgObject},
1375-
crypto::{BasicSig, BasicSigOf, CryptoHash},
1375+
crypto::{AlgorithmId, BasicSig, BasicSigOf, CryptoHash},
13761376
time::UNIX_EPOCH,
13771377
Height, RegistryVersion,
13781378
};
@@ -2770,7 +2770,7 @@ mod tests {
27702770
);
27712771
let params = setup_masked_random_params(
27722772
&env,
2773-
algorithm_for_key_id(&key_id),
2773+
AlgorithmId::from(key_id.inner()),
27742774
&dealers,
27752775
&receivers,
27762776
&mut rng,

0 commit comments

Comments
 (0)