Skip to content

Commit

Permalink
test(crypto): CRP-2311 use clib functions to corrupt dealings
Browse files Browse the repository at this point in the history
  • Loading branch information
altkdf committed Jan 17, 2024
1 parent 26f1e16 commit 27e0d68
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 31 deletions.
35 changes: 14 additions & 21 deletions rs/crypto/test_utils/canister_threshold_sigs/src/lib.rs
Expand Up @@ -3,7 +3,7 @@
use crate::node::{Node, Nodes};
use ic_crypto_internal_threshold_sig_ecdsa::test_utils::{corrupt_dealing, ComplaintCorrupter};
use ic_crypto_internal_threshold_sig_ecdsa::{
EccScalar, IDkgComplaintInternal, IDkgDealingInternal, MEGaCiphertext, NodeIndex, Seed,
IDkgComplaintInternal, IDkgDealingInternal, NodeIndex, Seed,
};
use ic_crypto_temp_crypto::{TempCryptoComponent, TempCryptoComponentGeneric};
use ic_interfaces::crypto::IDkgProtocol;
Expand Down Expand Up @@ -2124,6 +2124,7 @@ pub fn corrupt_dealings_and_generate_complaints<'a, R: RngCore + CryptoRng>(
*index_to_corrupt,
&mut transcript.verified_dealings,
complainer_index,
rng,
)
});

Expand Down Expand Up @@ -2179,38 +2180,30 @@ fn generate_and_verify_opening(
opening
}

fn corrupt_signed_dealing_for_one_receiver(
fn corrupt_signed_dealing_for_one_receiver<R: Rng + CryptoRng>(
dealing_index_to_corrupt: NodeIndex,
dealings: &mut BTreeMap<NodeIndex, BatchSignedIDkgDealing>,
receiver_index: NodeIndex,
rng: &mut R,
) {
let signed_dealing = dealings
.get_mut(&dealing_index_to_corrupt)
.unwrap_or_else(|| panic!("Missing dealing at index {:?}", dealing_index_to_corrupt));
let invalidated_internal_dealing_raw = {
let mut internal_dealing =
let internal_dealing =
IDkgDealingInternal::deserialize(&signed_dealing.idkg_dealing().internal_dealing_raw)
.expect("failed to deserialize internal dealing");
match internal_dealing.ciphertext {
MEGaCiphertext::Single(ref mut ctext) => {
let corrupted_ctext = corrupt_ecc_scalar(&ctext.ctexts[receiver_index as usize]);
ctext.ctexts[receiver_index as usize] = corrupted_ctext;
}
MEGaCiphertext::Pairs(ref mut ctext) => {
let (ctext_1, ctext_2) = ctext.ctexts[receiver_index as usize].clone();
let corrupted_ctext_1 = corrupt_ecc_scalar(&ctext_1);
ctext.ctexts[receiver_index as usize] = (corrupted_ctext_1, ctext_2);
}
};
internal_dealing

let corrupted_internal_dealing =
ic_crypto_internal_threshold_sig_ecdsa::test_utils::corrupt_dealing(
&internal_dealing,
&[receiver_index],
Seed::from_rng(rng),
)
.expect("failed to corrupt dealing");
corrupted_internal_dealing
.serialize()
.expect("failed to serialize internal dealing")
};
signed_dealing.content.content.internal_dealing_raw = invalidated_internal_dealing_raw;
}

fn corrupt_ecc_scalar(value: &EccScalar) -> EccScalar {
value
.add(&EccScalar::one(value.curve_type()))
.expect("Corruption for testing failed")
}
15 changes: 5 additions & 10 deletions rs/crypto/tests/canister_threshold_sigs.rs
Expand Up @@ -30,7 +30,7 @@ use ic_types::crypto::canister_threshold_sig::idkg::{
InitialIDkgDealings, SignedIDkgDealing,
};
use ic_types::crypto::canister_threshold_sig::{ExtendedDerivationPath, ThresholdEcdsaSigInputs};
use ic_types::crypto::{AlgorithmId, BasicSigOf, CryptoError};
use ic_types::crypto::{AlgorithmId, CryptoError};
use ic_types::{NodeId, Randomness};
use maplit::hashset;
use rand::distributions::uniform::SampleRange;
Expand Down Expand Up @@ -3496,15 +3496,10 @@ mod verify_initial_dealings {
.load_previous_transcripts_and_create_signed_dealings(&reshare_params);
let mut signed_dealings_vec = signed_dealings.into_values().collect::<Vec<_>>();
if corrupt_first_dealing {
if let Some(first_signed_dealing) = signed_dealings_vec.first_mut() {
let corrupted_sig = {
let mut sig_clone =
first_signed_dealing.signature.signature.get_ref().clone();
sig_clone.0.push(0xff);
BasicSigOf::new(sig_clone)
};
first_signed_dealing.signature.signature = corrupted_sig;
}
signed_dealings_vec
.first_mut()
.map(|sd| *sd = sd.clone().into_builder().corrupt_signature().build())
.expect("no dealings");
}

InitialIDkgDealings::new(reshare_params.clone(), signed_dealings_vec)
Expand Down

0 comments on commit 27e0d68

Please sign in to comment.