Skip to content

Commit

Permalink
fix: allow dkg with n=t (#458)
Browse files Browse the repository at this point in the history
  • Loading branch information
1xstj authored Feb 5, 2024
1 parent 7edc163 commit 802ef40
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 7 deletions.
4 changes: 2 additions & 2 deletions pallets/dkg/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ impl<T: Config> Pallet<T> {
}

// Ensure a sufficient number of unique signers are present
ensure!(known_signers.len() > data.threshold.into(), Error::<T>::NotEnoughSigners);
ensure!(known_signers.len() >= data.threshold.into(), Error::<T>::NotEnoughSigners);

Ok(())
}
Expand Down Expand Up @@ -231,7 +231,7 @@ impl<T: Config> Pallet<T> {
}

// Ensure a sufficient number of unique signers are present
ensure!(known_signers.len() > data.threshold.into(), Error::<T>::NotEnoughSigners);
ensure!(known_signers.len() >= data.threshold.into(), Error::<T>::NotEnoughSigners);

Ok(())
}
Expand Down
54 changes: 52 additions & 2 deletions pallets/dkg/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,32 @@ fn dkg_key_verifcation_works_for_ecdsa() {
threshold: 1,
};

// should fail for signing different keys
assert_ok!(DKG::verify(JobResult::DKGPhaseOne(job_to_verify)),);
});
}

#[test]
fn dkg_key_verifcation_works_for_ecdsa_when_n_equals_t() {
new_test_ext().execute_with(|| {
let mut participant_one = mock_pub_key_ecdsa();
let mut participant_two = mock_pub_key_ecdsa();
let signature_one = mock_signature_ecdsa(participant_one, participant_one);
let signature_two = mock_signature_ecdsa(participant_two, participant_one);
let job_to_verify = DKGTSSKeySubmissionResult {
signature_type: DigitalSignatureType::Ecdsa,
key: participant_one.to_raw_vec().try_into().unwrap(),
participants: vec![
participant_one.as_mut().to_vec().try_into().unwrap(),
participant_two.as_mut().to_vec().try_into().unwrap(),
]
.try_into()
.unwrap(),
signatures: vec![signature_two.try_into().unwrap(), signature_one.try_into().unwrap()]
.try_into()
.unwrap(),
threshold: 2,
};

assert_ok!(DKG::verify(JobResult::DKGPhaseOne(job_to_verify)),);
});
}
Expand Down Expand Up @@ -255,7 +280,32 @@ fn dkg_key_verifcation_works_for_schnorr() {
threshold: 1,
};

// should fail for signing different keys
assert_ok!(DKG::verify(JobResult::DKGPhaseOne(job_to_verify)),);
});
}

#[test]
fn dkg_key_verifcation_works_for_schnorr_when_n_equals_t() {
new_test_ext().execute_with(|| {
let mut participant_one = mock_pub_key_sr25519();
let mut participant_two = mock_pub_key_sr25519();
let signature_one = mock_signature_sr25519(participant_one, participant_one);
let signature_two = mock_signature_sr25519(participant_two, participant_one);
let job_to_verify = DKGTSSKeySubmissionResult {
signature_type: DigitalSignatureType::SchnorrSr25519,
key: participant_one.to_raw_vec().try_into().unwrap(),
participants: vec![
participant_one.as_mut().to_vec().try_into().unwrap(),
participant_two.as_mut().to_vec().try_into().unwrap(),
]
.try_into()
.unwrap(),
signatures: vec![signature_two.try_into().unwrap(), signature_one.try_into().unwrap()]
.try_into()
.unwrap(),
threshold: 2,
};

assert_ok!(DKG::verify(JobResult::DKGPhaseOne(job_to_verify)),);
});
}
Expand Down
5 changes: 3 additions & 2 deletions pallets/jobs/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ fn jobs_submission_e2e_works_for_dkg() {
.collect::<Vec<_>>()
.try_into()
.unwrap(),
threshold: 5,
threshold: 6,
permitted_caller: None,
role_type: threshold_signature_role_type,
}),
Expand Down Expand Up @@ -154,6 +154,7 @@ fn jobs_submission_e2e_works_for_dkg() {
);
Balances::make_free_balance_be(&mock_pub_key(TEN), 100);

// should work when n = t
let submission = JobSubmission {
expiry: 10,
ttl: 200,
Expand All @@ -164,7 +165,7 @@ fn jobs_submission_e2e_works_for_dkg() {
.collect::<Vec<_>>()
.try_into()
.unwrap(),
threshold: 3,
threshold: 5,
permitted_caller: Some(mock_pub_key(TEN)),
role_type: threshold_signature_role_type,
}),
Expand Down
2 changes: 1 addition & 1 deletion primitives/src/jobs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ impl<AccountId, MaxParticipants: Get<u32> + Clone, MaxSubmissionLen: Get<u32>>
/// This function is intended for simple checks and may need improvement in the future.
pub fn sanity_check(&self) -> bool {
match self {
JobType::DKGTSSPhaseOne(info) => info.participants.len() > info.threshold.into(),
JobType::DKGTSSPhaseOne(info) => info.participants.len() >= info.threshold.into(),
JobType::ZkSaaSPhaseOne(info) => !info.participants.is_empty(),
_ => true,
}
Expand Down

0 comments on commit 802ef40

Please sign in to comment.