Skip to content

Commit

Permalink
refactor: solo keygen shares key generation code with genesis
Browse files Browse the repository at this point in the history
  • Loading branch information
msgmaxim committed May 24, 2022
1 parent 29b256d commit f57e369
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 38 deletions.
9 changes: 3 additions & 6 deletions engine/src/multisig/client/keygen/keygen_frost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ pub fn derive_aggregate_pubkey<P: ECPoint>(
if !allow_high_pubkey && !pubkey.is_compatible() {
Err(anyhow::Error::msg("pubkey is not compatible"))
} else if check_high_degree_commitments(commitments) {
// Sanity check (the chance of this failing is practically zero due to the
// Sanity check (the chance of this failing is infinitesimal due to the
// hash commitment stage at the beginning of the ceremony)
Err(anyhow::Error::msg("high degree coefficient is zero"))
} else {
Expand Down Expand Up @@ -511,20 +511,17 @@ pub mod genesis {

pub fn generate_key_data<P: ECPoint>(
signers: &[AccountId],
rng: &mut Rng,
) -> anyhow::Result<(KeyId, HashMap<AccountId, KeygenResultInfo<P>>)> {
let params = ThresholdParameters::from_share_count(signers.len() as AuthorityCount);
let n = params.share_count;
let t = params.threshold;

use crate::multisig::client::PartyIdxMapping;
use rand_legacy::FromEntropy;

let mut rng = Rng::from_entropy();

let (commitments, outgoing_secret_shares): (BTreeMap<_, _>, BTreeMap<_, _>) = (1..=n)
.map(|idx| {
let (_secret, commitments, shares) =
generate_secret_and_shares::<P>(&mut rng, n, t);
let (_secret, commitments, shares) = generate_secret_and_shares::<P>(rng, n, t);
((idx, DKGCommitment { commitments }), (idx, shares))
})
.unzip();
Expand Down
37 changes: 6 additions & 31 deletions engine/src/multisig/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub mod ceremony_manager;
#[cfg(test)]
mod genesis;

use std::{collections::BTreeSet, sync::Arc};
use std::collections::BTreeSet;

use crate::{
common::format_iterator,
Expand Down Expand Up @@ -394,37 +394,12 @@ pub fn single_party_keygen<Point: ECPoint>(
my_account_id: AccountId,
mut rng: Rng,
) -> KeygenResultInfo<Point> {
use crate::multisig::crypto::{ECScalar, KeyShare};

let params = ThresholdParameters::from_share_count(1);

// By default this will have a 50/50 chance of generating
// a contract incompatible signature to match the behavior
// of multi-party ceremonies. Toggle this off to always
// generate a contract compatible signature.
const ALLOWING_HIGH_PUBKEY: bool = true;

let (secret_key, public_key) = loop {
let secret_key = Point::Scalar::random(&mut rng);

let public_key = Point::from_scalar(&secret_key);

if public_key.is_compatible() || ALLOWING_HIGH_PUBKEY {
break (secret_key, public_key);
loop {
if let Ok((_key_id, key_data)) =
keygen::generate_key_data::<Point>(&[my_account_id.clone()], &mut rng)
{
return key_data[&my_account_id].clone();
}
};

KeygenResultInfo {
key: Arc::new(KeygenResult {
key_share: KeyShare {
y: public_key,
x_i: secret_key,
},
// This is not going to be used in solo ceremonies
party_public_keys: vec![public_key],
}),
validator_map: Arc::new(PartyIdxMapping::from_unsorted_signers(&[my_account_id])),
params,
}
}

Expand Down
6 changes: 5 additions & 1 deletion engine/src/multisig/client/tests/keygen_unit_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1446,6 +1446,10 @@ async fn genesis_keys_can_sign() {
.map(|i| AccountId::new([*i; 32]))
.collect();

use rand_legacy::FromEntropy;

let mut rng = Rng::from_entropy();

// Limit iteration count so we don't loop forever
// in case there is a bug
const MAX_KEYGEN_ATTEMPTS: usize = 20;
Expand All @@ -1454,7 +1458,7 @@ async fn genesis_keys_can_sign() {

let (key_id, key_data) = loop {
attempt_counter += 1;
match keygen::generate_key_data::<Point>(&account_ids) {
match keygen::generate_key_data::<Point>(&account_ids, &mut rng) {
Ok(result) => break result,
Err(_) => {
if attempt_counter >= MAX_KEYGEN_ATTEMPTS {
Expand Down

0 comments on commit f57e369

Please sign in to comment.