Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion crates/node/src/assets/cleanup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ use crate::db::{DBCol, EPOCH_ID_KEY, SecretDB};
use crate::primitives;
use crate::providers::ecdsa::presign::PresignOutputWithParticipants;
use crate::providers::ecdsa::triple::PairedTriple;
use mpc_primitives::{EpochId, ReconstructionThreshold, domain::DomainId};
use mpc_primitives::{EpochId, domain::DomainId};
use serde::{self, Deserialize, Serialize};
use std::sync::Arc;
use threshold_signatures::ReconstructionThreshold;

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct EpochData {
Expand Down
3 changes: 2 additions & 1 deletion crates/node/src/assets/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ use ed25519_dalek::{SigningKey, VerifyingKey};
use k256::ProjectivePoint;
use mpc_contract::primitives::test_utils::gen_participants;
use mpc_contract::primitives::thresholds::{Threshold, ThresholdParameters};
use mpc_primitives::{EpochId, ReconstructionThreshold, domain::DomainId};
use mpc_primitives::{EpochId, domain::DomainId};
use near_time::FakeClock;
use rand::RngCore;
use rand::rngs::OsRng;
use serde::Serialize;
use serde::de::DeserializeOwned;
use std::sync::{Arc, Mutex};
use threshold_signatures::ReconstructionThreshold;
use threshold_signatures::ecdsa::Polynomial;
use threshold_signatures::ecdsa::ot_based_ecdsa::PresignOutput;
use threshold_signatures::ecdsa::ot_based_ecdsa::triples::{
Expand Down
8 changes: 4 additions & 4 deletions crates/node/src/coordinator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ use crate::web::DebugRequest;
use futures::FutureExt;
use futures::future::BoxFuture;
use mpc_node_config::ConfigFile;
use mpc_primitives::EpochId;
use mpc_primitives::domain::{Curve, DomainId, Protocol};
use mpc_primitives::{EpochId, ReconstructionThreshold};
use near_time::Clock;
use std::collections::HashMap;
use std::future::Future;
use std::sync::{Arc, Mutex};
use threshold_signatures::ReconstructionThreshold as TSReconstructionThreshold;
use threshold_signatures::ReconstructionThreshold;
use threshold_signatures::{confidential_key_derivation, ecdsa, frost::eddsa};
use tokio::select;
use tokio::sync::mpsc::unbounded_channel;
Expand Down Expand Up @@ -332,7 +332,7 @@ where
let (network_client, channel_receiver, _handle) =
run_network_client(Arc::new(sender), Box::new(receiver));
let threshold: usize = mpc_config.participants.threshold.try_into()?;
let threshold = TSReconstructionThreshold::from(threshold);
let threshold = ReconstructionThreshold::from(threshold);
if mpc_config.is_leader_for_key_event() {
keygen_leader(
network_client,
Expand Down Expand Up @@ -780,7 +780,7 @@ where
let args = Arc::new(ResharingArgs {
previous_keyset,
existing_keyshares,
new_threshold: TSReconstructionThreshold::from(new_threshold),
new_threshold: ReconstructionThreshold::from(new_threshold),
old_participants: current_running_state.participants,
});

Expand Down
8 changes: 4 additions & 4 deletions crates/node/src/key_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -727,11 +727,11 @@ mod tests {
use mpc_primitives::domain::DomainId;
use mpc_primitives::{AttemptId, EpochId, KeyEventId};
use near_mpc_contract_interface::types::{
DomainConfig, DomainPurpose, Protocol, ReconstructionThreshold,
DomainConfig, DomainPurpose, Protocol,
};
use std::collections::BTreeSet;
use std::sync::atomic::{AtomicUsize, Ordering};
use threshold_signatures::ReconstructionThreshold as TSReconstructionThreshold;
use threshold_signatures::ReconstructionThreshold;

#[rstest::rstest]
#[tokio::test(start_paused = true)]
Expand Down Expand Up @@ -888,7 +888,7 @@ mod tests {
domain: DomainConfig {
id: key_event_id.domain_id,
protocol: Protocol::CaitSith,
reconstruction_threshold: ReconstructionThreshold::new(2),
reconstruction_threshold: dtos::ReconstructionThreshold::new(2),
purpose: DomainPurpose::Sign,
},
started,
Expand All @@ -901,7 +901,7 @@ mod tests {
Arc::new(ResharingArgs {
previous_keyset: Keyset::new(EpochId::new(5), vec![]),
existing_keyshares: None,
new_threshold: TSReconstructionThreshold::from(3),
new_threshold: ReconstructionThreshold::from(3),
old_participants: ParticipantsConfig {
threshold: 3,
participants: vec![],
Expand Down
2 changes: 1 addition & 1 deletion crates/node/src/providers/ckd/sign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl CKDProvider {
let participants = self
.client
.select_random_active_participants_including_me(
threshold.value(),
threshold.try_as_usize()?,
&running_participants,
)
.context("Could not choose active participants for a ckd")?;
Expand Down
13 changes: 5 additions & 8 deletions crates/node/src/providers/ecdsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,10 @@ use mpc_node_config::ConfigFile;

use crate::types::SignatureId;
use borsh::{BorshDeserialize, BorshSerialize};
use mpc_primitives::ReconstructionThreshold;
use mpc_primitives::domain::DomainId;
use near_time::Clock;
use std::sync::Arc;
use threshold_signatures::ReconstructionThreshold as TSReconstructionThreshold;
use threshold_signatures::ReconstructionThreshold;
use threshold_signatures::ecdsa::KeygenOutput;
use threshold_signatures::ecdsa::Signature;
use threshold_signatures::frost_secp256k1::VerifyingKey;
Expand Down Expand Up @@ -193,14 +192,14 @@ impl SignatureProvider for EcdsaSignatureProvider {
}

async fn run_key_generation_client(
threshold: TSReconstructionThreshold,
threshold: ReconstructionThreshold,
channel: NetworkTaskChannel,
) -> anyhow::Result<Self::KeygenOutput> {
EcdsaSignatureProvider::run_key_generation_client_internal(threshold, channel).await
}

async fn run_key_resharing_client(
new_threshold: TSReconstructionThreshold,
new_threshold: ReconstructionThreshold,
my_share: Option<SigningShare>,
public_key: VerifyingKey,
old_participants: &ParticipantsConfig,
Expand Down Expand Up @@ -271,8 +270,6 @@ impl SignatureProvider for EcdsaSignatureProvider {
// and source `t` from `domain.reconstruction_threshold` rather than the
// network-wide threshold.
let threshold = ReconstructionThreshold::new(self.mpc_config.participants.threshold);
let threshold_usize: usize = threshold.inner().try_into()?;
let threshold_bound = TSReconstructionThreshold::from(threshold_usize);
let triple_store = self.triple_store_for_t(threshold)?;

let generate_triples = tracking::spawn(
Expand All @@ -282,7 +279,7 @@ impl SignatureProvider for EcdsaSignatureProvider {
self.mpc_config.clone(),
self.config.triple.clone().into(),
triple_store.clone(),
threshold_bound,
threshold,
),
);

Expand All @@ -294,7 +291,7 @@ impl SignatureProvider for EcdsaSignatureProvider {
&format!("generate presignatures for domain {}", domain_id.0),
Self::run_background_presignature_generation(
self.client.clone(),
threshold_bound,
threshold,
self.config.presignature.clone().into(),
triple_store.clone(),
*domain_id,
Expand Down
14 changes: 6 additions & 8 deletions crates/node/src/providers/ecdsa/presign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@ use crate::providers::ecdsa::{EcdsaSignatureProvider, EcdsaTaskId, KeygenOutput,
use crate::tracking::AutoAbortTaskCollection;
use crate::{metrics, tracking};
use mpc_node_config::PresignatureConfig;
use mpc_primitives::ReconstructionThreshold;
use mpc_primitives::domain::DomainId;
use near_time::Clock;
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, AtomicUsize};
use std::time::Duration;
use threshold_signatures::ReconstructionThreshold as TSReconstructionThreshold;
use threshold_signatures::ReconstructionThreshold;
use threshold_signatures::ecdsa::ot_based_ecdsa::triples::TripleGenerationOutput;
use threshold_signatures::ecdsa::ot_based_ecdsa::{
PresignArguments, PresignOutput, presign::presign,
Expand Down Expand Up @@ -66,7 +65,7 @@ impl EcdsaSignatureProvider {
/// so that needs to be separately handled.
pub(super) async fn run_background_presignature_generation(
client: Arc<MeshNetworkClient>,
threshold: TSReconstructionThreshold,
threshold: ReconstructionThreshold,
config: Arc<PresignatureConfig>,
triple_store: Arc<TripleStorage>,
domain_id: DomainId,
Expand Down Expand Up @@ -182,11 +181,10 @@ impl EcdsaSignatureProvider {
// Triple store to consume from is keyed by the presign's `t`, which
// equals the number of presign participants (same as triple
// participants — the leader pairs them).
let threshold_usize: usize = channel.participants().len();
let threshold = ReconstructionThreshold::new(threshold_usize.try_into()?);
let threshold = ReconstructionThreshold::from(channel.participants().len());
let triple_store = self.triple_store_for_t(threshold)?;
FollowerPresignComputation {
threshold: TSReconstructionThreshold::from(threshold_usize),
threshold,
keygen_out: domain_data.keyshare,
triple_store,
paired_triple_id,
Expand Down Expand Up @@ -220,7 +218,7 @@ impl HasParticipants for PresignOutputWithParticipants {
/// Performs an MPC presignature operation. This is shared for the initiator
/// and for passive participants.
pub struct PresignComputation {
threshold: TSReconstructionThreshold,
threshold: ReconstructionThreshold,
triple0: TripleGenerationOutput,
triple1: TripleGenerationOutput,
keygen_out: KeygenOutput,
Expand Down Expand Up @@ -260,7 +258,7 @@ impl MpcLeaderCentricComputation<PresignOutput> for PresignComputation {
/// The difference is: we need to read the triples from the triple store (which may fail),
/// and we need to write the presignature to the presignature store before completing.
pub struct FollowerPresignComputation {
pub threshold: TSReconstructionThreshold,
pub threshold: ReconstructionThreshold,
pub paired_triple_id: UniqueId,
pub keygen_out: KeygenOutput,
pub triple_store: Arc<TripleStorage>,
Expand Down
26 changes: 14 additions & 12 deletions crates/node/src/providers/ecdsa/triple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@ use crate::providers::HasParticipants;
use crate::providers::ecdsa::{EcdsaSignatureProvider, EcdsaTaskId};
use crate::tracking::AutoAbortTaskCollection;
use mpc_node_config::TripleConfig;
use mpc_primitives::ReconstructionThreshold;
use near_time::Clock;
use rand::rngs::OsRng;
use std::ops::Deref;
use std::sync::Arc;
use std::time::Duration;
use threshold_signatures::ReconstructionThreshold as TSReconstructionThreshold;
use threshold_signatures::ReconstructionThreshold;
use threshold_signatures::ecdsa::ot_based_ecdsa::triples::TripleGenerationOutput;
use threshold_signatures::participants::Participant;

Expand Down Expand Up @@ -74,11 +73,16 @@ impl EcdsaSignatureProvider {
mpc_config: Arc<MpcConfig>,
config: Arc<TripleConfig>,
triple_store: Arc<TripleStorage>,
threshold: TSReconstructionThreshold,
threshold: ReconstructionThreshold,
) -> ! {
let in_flight_generations = InFlightGenerationTracker::new();
let parallelism_limiter = Arc::new(tokio::sync::Semaphore::new(config.concurrency));
let mut tasks = AutoAbortTaskCollection::new();
// Converted once: loop-invariant, and this `-> !` loop has no error
// channel. Unreachable on supported targets (count fits in `usize`).
let threshold_usize = threshold
.try_as_usize()
.expect("reconstruction threshold fits in usize");
let running_participants: Vec<_> = mpc_config
.participants
.participants
Expand Down Expand Up @@ -107,7 +111,7 @@ impl EcdsaSignatureProvider {
< config.concurrency * 2 * SUPPORTED_TRIPLE_GENERATION_BATCH_SIZE
{
let participants = match client.select_random_active_participants_including_me(
threshold.value(),
threshold_usize,
&running_participants,
) {
Ok(participants) => participants,
Expand Down Expand Up @@ -208,11 +212,10 @@ impl EcdsaSignatureProvider {
// Cait-sith triple generation runs with exactly `t` participants, so we
// can derive the store's `t` from the channel's participant list
// without a wire-format change to `EcdsaTaskId::ManyTriples`.
let threshold_usize: usize = channel.participants().len();
let threshold = ReconstructionThreshold::new(threshold_usize.try_into()?);
let threshold = ReconstructionThreshold::from(channel.participants().len());
let triple_store = self.triple_store_for_t(threshold)?;
FollowerManyTripleGenerationComputation::<SUPPORTED_TRIPLE_GENERATION_BATCH_SIZE> {
threshold: TSReconstructionThreshold::from(threshold_usize),
threshold,
out_triple_id_start: start,
out_triple_store: triple_store,
}
Expand Down Expand Up @@ -240,7 +243,7 @@ impl HasParticipants for PairedTriple {
/// Generates many cait-sith triples at once. This can significantly save the
/// *number* of network messages.
pub struct ManyTripleGenerationComputation<const N: usize> {
pub threshold: TSReconstructionThreshold,
pub threshold: ReconstructionThreshold,
}

#[async_trait::async_trait]
Expand Down Expand Up @@ -288,7 +291,7 @@ impl<const N: usize> MpcLeaderCentricComputation<Vec<PairedTriple>>
/// The follower version of the triple generation. The difference is that the follower will only
/// complete the computation after successfully persisting the triples to storage.
pub struct FollowerManyTripleGenerationComputation<const N: usize> {
pub threshold: TSReconstructionThreshold,
pub threshold: ReconstructionThreshold,
pub out_triple_store: Arc<TripleStorage>,
pub out_triple_id_start: UniqueId,
}
Expand Down Expand Up @@ -348,7 +351,6 @@ mod tests {
use futures::{FutureExt, StreamExt, stream};
use std::collections::HashMap;
use std::sync::Arc;
use threshold_signatures::ReconstructionThreshold as TSReconstructionThreshold;
use threshold_signatures::test_utils::generate_participants;
use tokio::sync::mpsc;

Expand Down Expand Up @@ -405,7 +407,7 @@ mod tests {
panic!("Unexpected task id");
};
let triples = ManyTripleGenerationComputation::<TRIPLES_PER_BATCH> {
threshold: TSReconstructionThreshold::from(THRESHOLD),
threshold: ReconstructionThreshold::from(THRESHOLD),
}
.perform_leader_centric_computation(
channel,
Expand Down Expand Up @@ -453,7 +455,7 @@ mod tests {
let result = tracking::spawn(
&format!("task {:?}", task_id),
ManyTripleGenerationComputation::<TRIPLES_PER_BATCH> {
threshold: TSReconstructionThreshold::from(THRESHOLD),
threshold: ReconstructionThreshold::from(THRESHOLD),
}
.perform_leader_centric_computation(
channel,
Expand Down
2 changes: 1 addition & 1 deletion crates/node/src/providers/eddsa/sign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl EddsaSignatureProvider {
let participants = self
.client
.select_random_active_participants_including_me(
threshold.value(),
threshold.try_as_usize()?,
&running_participants,
)
.context("Can't choose active participants for a eddsa signature")?;
Expand Down
9 changes: 4 additions & 5 deletions crates/node/src/providers/robust_ecdsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ impl SignatureProvider for RobustEcdsaSignatureProvider {
) -> anyhow::Result<Self::KeygenOutput> {
let number_of_participants = channel.participants().len();
let robust_ecdsa_threshold =
translate_threshold(threshold.value(), number_of_participants)?;
translate_threshold(threshold.try_as_usize()?, number_of_participants)?;
EcdsaSignatureProvider::run_key_generation_client_internal(
ReconstructionThreshold::try_from(robust_ecdsa_threshold)?,
channel,
Expand All @@ -166,7 +166,7 @@ impl SignatureProvider for RobustEcdsaSignatureProvider {
) -> anyhow::Result<Self::KeygenOutput> {
let number_of_participants = channel.participants().len();
let new_robust_ecdsa_threshold =
translate_threshold(new_threshold.value(), number_of_participants)?;
translate_threshold(new_threshold.try_as_usize()?, number_of_participants)?;

// This is a bad hack, but cannot think of a better way to solve it, as the struct
// comes directly from generic implementations, so probably this is the best place
Expand All @@ -176,9 +176,8 @@ impl SignatureProvider for RobustEcdsaSignatureProvider {
old_participants.threshold.try_into()?,
old_participants.participants.len(),
)?;
old_participants_patched.threshold = ReconstructionThreshold::try_from(old_translated)?
.value()
.try_into()?;
old_participants_patched.threshold =
ReconstructionThreshold::try_from(old_translated)?.inner();

EcdsaSignatureProvider::run_key_resharing_client_internal(
ReconstructionThreshold::try_from(new_robust_ecdsa_threshold)?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ type PreparedSimulatedSig = PreparedOutputs<SignatureOption>;

/// Benches the signing protocol
fn bench_sign(c: &mut Criterion) {
let num = RECONSTRUCTION_LOWER_BOUND.value();
let num = RECONSTRUCTION_LOWER_BOUND
.try_as_usize()
.expect("reconstruction threshold fits in usize");
let max_malicious = *MAX_MALICIOUS;

let setup = setup_sign_snapshot(*RECONSTRUCTION_LOWER_BOUND);
Expand Down
Loading
Loading