Skip to content

Commit 93d8c49

Browse files
authored
test(crypto): CRP-587 Add various missing NI-DKG verification tests (#8406)
1 parent 5e48a7d commit 93d8c49

File tree

3 files changed

+322
-58
lines changed

3 files changed

+322
-58
lines changed

rs/crypto/src/sign/threshold_sig.rs

Lines changed: 7 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -149,14 +149,12 @@ impl ThresholdSigVerifierInternal {
149149
signer,
150150
)?;
151151

152-
threshold_sig_csp_client
153-
.threshold_verify_individual_signature(
154-
AlgorithmId::from(public_key),
155-
message.as_signed_bytes().as_slice(),
156-
csp_signature,
157-
public_key,
158-
)
159-
.map_err(panic_on_illegal_individual_sig_verification_state)
152+
threshold_sig_csp_client.threshold_verify_individual_signature(
153+
AlgorithmId::from(public_key),
154+
message.as_signed_bytes().as_slice(),
155+
csp_signature,
156+
public_key,
157+
)
160158
}
161159
}
162160

@@ -254,24 +252,6 @@ fn coeffs_and_index(
254252
Ok((public_coeffs, node_index))
255253
}
256254

257-
fn panic_on_illegal_individual_sig_verification_state(error: CryptoError) -> CryptoError {
258-
match error {
259-
CryptoError::InvalidArgument { .. } | CryptoError::MalformedPublicKey { .. } => panic!(
260-
"Illegal state: the algorithm of the public key from the threshold signature data \
261-
store (which is based on the algorithm of the public coefficients in the store) is \
262-
not supported: {error}"
263-
),
264-
CryptoError::MalformedSignature { .. } => unreachable!(
265-
"This case cannot occur because `CryptoError::MalformedSignature` is returned only \
266-
if the given signature was not a \
267-
`CspSignature::ThresBls12_381(ThresBls12_381_Signature::Individual)`, but we know \
268-
for sure that it has this type because this is the type returned by \
269-
`CspSignature::try_from(ThresholdSigShareOf)`."
270-
),
271-
_ => error,
272-
}
273-
}
274-
275255
impl ThresholdSigVerifierInternal {
276256
pub fn combine_threshold_sig_shares<C: ThresholdSignatureCspClient, H: Signable>(
277257
lockable_threshold_sig_data_store: &LockableThresholdSigDataStore,
@@ -290,7 +270,7 @@ impl ThresholdSigVerifierInternal {
290270
public_coefficients,
291271
)
292272
.map_err(map_csp_combine_sigs_error)?;
293-
combined_threshold_sig_or_panic(csp_signature)
273+
CombinedThresholdSigOf::try_from(csp_signature)
294274
}
295275
}
296276

@@ -345,15 +325,6 @@ fn index_for_node_id(
345325
.ok_or_else(|| node_id_missing_error(node_id, dkg_id))
346326
}
347327

348-
fn combined_threshold_sig_or_panic<H: Signable>(
349-
csp_signature: CspSignature,
350-
) -> CryptoResult<CombinedThresholdSigOf<H>> {
351-
Ok(CombinedThresholdSigOf::try_from(csp_signature).expect(
352-
"The CSP must return a signature of type \
353-
`CspSignature::ThresBls12_381(ThresBls12_381_Signature::Combined)`.",
354-
))
355-
}
356-
357328
fn map_csp_combine_sigs_error(error: CryptoError) -> CryptoError {
358329
match error {
359330
CryptoError::MalformedSignature { .. } | CryptoError::InvalidArgument { .. } => error,

rs/crypto/src/sign/threshold_sig/tests.rs

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use super::*;
22
use crate::sign::tests::KEY_ID;
33
use crate::sign::tests::KEY_ID_STRING;
44
use crate::sign::threshold_sig::ThresholdSigDataStore;
5+
use assert_matches::assert_matches;
56
use ic_crypto_internal_csp::types::{CspPublicCoefficients, ThresBls12_381_Signature};
67
use ic_crypto_internal_csp_test_utils::types::{
78
csp_sig_thres_bls12381_combined_from_array_of, csp_sig_thres_bls12381_indiv_from_array_of,
@@ -35,7 +36,6 @@ pub const NI_DKG_ID_1: NiDkgId = NiDkgId {
3536

3637
mod sign_threshold {
3738
use super::*;
38-
use assert_matches::assert_matches;
3939
use ic_crypto_internal_csp::key_id::KeyId;
4040

4141
#[test]
@@ -570,65 +570,60 @@ mod verify_threshold_sig_share {
570570
}
571571

572572
#[test]
573-
#[should_panic(
574-
expected = "Illegal state: the algorithm of the public key from the threshold \
575-
signature data store (which is based on the algorithm of the public coefficients in \
576-
the store) is not supported"
577-
)]
578-
fn should_panic_if_csp_returns_invalid_argument_error() {
573+
fn should_forward_error_if_csp_returns_invalid_argument_error() {
579574
let (sig_share, message, csp_public_key) = (sig_share(), signable_mock(), csp_public_key());
580575
let threshold_sig_data_store =
581576
threshold_sig_data_store_with_coeffs_and_pubkey(&NI_DKG_ID_1, NODE_ID, csp_public_key);
582577
let csp = csp_with_verify_indiv_sig_returning_once(Err(invalid_argument()));
583578

584-
let _panic = ThresholdSigVerifierInternal::verify_threshold_sig_share(
579+
let result = ThresholdSigVerifierInternal::verify_threshold_sig_share(
585580
&threshold_sig_data_store,
586581
&csp,
587582
&sig_share,
588583
&message,
589584
&NI_DKG_ID_1,
590585
NODE_ID,
591586
);
587+
588+
assert_matches!(result, Err(CryptoError::InvalidArgument { .. }));
592589
}
593590

594591
#[test]
595-
#[should_panic(
596-
expected = "Illegal state: the algorithm of the public key from the threshold signature data \
597-
store (which is based on the algorithm of the public coefficients in the store) is \
598-
not supported"
599-
)]
600-
fn should_panic_if_csp_returns_malformed_public_key_error() {
592+
fn should_forward_error_if_csp_returns_malformed_public_key_error() {
601593
let (sig_share, message, csp_public_key) = (sig_share(), signable_mock(), csp_public_key());
602594
let threshold_sig_data_store =
603595
threshold_sig_data_store_with_coeffs_and_pubkey(&NI_DKG_ID_1, NODE_ID, csp_public_key);
604596
let csp = csp_with_verify_indiv_sig_returning_once(Err(malformed_public_key()));
605597

606-
let _panic = ThresholdSigVerifierInternal::verify_threshold_sig_share(
598+
let result = ThresholdSigVerifierInternal::verify_threshold_sig_share(
607599
&threshold_sig_data_store,
608600
&csp,
609601
&sig_share,
610602
&message,
611603
&NI_DKG_ID_1,
612604
NODE_ID,
613605
);
606+
607+
assert_eq!(result, Err(malformed_public_key()));
614608
}
615609

616610
#[test]
617-
#[should_panic(expected = "This case cannot occur")]
618-
fn should_panic_if_csp_returns_malformed_signature_error() {
611+
fn should_forward_error_if_csp_returns_malformed_signature_error() {
619612
let (sig_share, message, csp_public_key) = (sig_share(), signable_mock(), csp_public_key());
620613
let threshold_sig_data_store =
621614
threshold_sig_data_store_with_coeffs_and_pubkey(&NI_DKG_ID_1, NODE_ID, csp_public_key);
622615
let csp = csp_with_verify_indiv_sig_returning_once(Err(malformed_signature()));
623616

624-
let _panic = ThresholdSigVerifierInternal::verify_threshold_sig_share(
617+
let result = ThresholdSigVerifierInternal::verify_threshold_sig_share(
625618
&threshold_sig_data_store,
626619
&csp,
627620
&sig_share,
628621
&message,
629622
&NI_DKG_ID_1,
630623
NODE_ID,
631624
);
625+
626+
assert_eq!(result, Err(malformed_signature()));
632627
}
633628

634629
fn csp_with_indiv_pk_returning_once(
@@ -853,9 +848,7 @@ mod combine_threshold_sig_shares {
853848
}
854849

855850
#[test]
856-
#[should_panic(expected = "The CSP must return a signature of type \
857-
`CspSignature::ThresBls12_381(ThresBls12_381_Signature::Combined)`.")]
858-
fn should_panic_if_csp_returns_wrong_signature_type() {
851+
fn should_return_malformed_signature_error_if_csp_returns_wrong_signature_type() {
859852
let shares = shares(vec![(
860853
NODE_1,
861854
threshold_sig_share(vec![1; IndividualSignatureBytes::SIZE]),
@@ -867,12 +860,14 @@ mod combine_threshold_sig_shares {
867860
let threshold_sig_data_store =
868861
threshold_sig_data_store_with(&NI_DKG_ID_1, pub_coeffs(), indices);
869862

870-
let _panic = ThresholdSigVerifierInternal::combine_threshold_sig_shares(
863+
let result = ThresholdSigVerifierInternal::combine_threshold_sig_shares(
871864
&threshold_sig_data_store,
872865
&csp,
873866
shares,
874867
&NI_DKG_ID_1,
875868
);
869+
870+
assert_matches!(result, Err(CryptoError::MalformedSignature { .. }));
876871
}
877872

878873
#[test]

0 commit comments

Comments
 (0)