Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove PartyIdx from high-level API #40

Merged
merged 2 commits into from
Oct 25, 2023
Merged
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
2 changes: 1 addition & 1 deletion synedrion/src/cggmp21/protocols/threshold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,8 @@ mod tests {
use rand_core::OsRng;

use super::ThresholdKeyShare;
use crate::cggmp21::{PartyIdx, TestParams};
use crate::curve::Scalar;
use crate::{PartyIdx, TestParams};

#[test]
fn threshold_key_share_centralized() {
Expand Down
2 changes: 1 addition & 1 deletion synedrion/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub use k256::ecdsa;
pub use signature;

pub use cggmp21::{
KeyShare, KeyShareChange, PartyIdx, ProductionParams, ProtocolResult, SchemeParams, TestParams,
KeyShare, KeyShareChange, ProductionParams, ProtocolResult, SchemeParams, TestParams,
ThresholdKeyShare,
};
pub use curve::RecoverableSignature;
2 changes: 1 addition & 1 deletion synedrion/src/sessions/broadcast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use serde::{Deserialize, Serialize};

use super::signed_message::{SignedMessage, VerifiedMessage};
use super::type_erased::{deserialize_message, serialize_message};
use crate::cggmp21::PartyIdx;
use crate::tools::collections::HoleVecAccum;
use crate::PartyIdx;

#[derive(Clone)]
pub(crate) struct BroadcastConsensus<Sig> {
Expand Down
53 changes: 21 additions & 32 deletions synedrion/src/sessions/constructors.rs
Original file line number Diff line number Diff line change
@@ -1,91 +1,81 @@
use alloc::format;
use core::fmt::Debug;

use rand_core::CryptoRngCore;
use serde::{Deserialize, Serialize};
use signature::hazmat::{PrehashVerifier, RandomizedPrehashSigner};
use signature::{
hazmat::{PrehashVerifier, RandomizedPrehashSigner},
Keypair,
};

use super::error::{Error, LocalError};
use super::error::LocalError;
use super::states::Session;
use crate::cggmp21::{
auxiliary, interactive_signing, keygen_and_aux, InteractiveSigningResult, KeyRefreshResult,
KeyShare, KeygenAndAuxResult, PartyIdx, SchemeParams,
KeyShare, KeygenAndAuxResult, SchemeParams,
};
use crate::curve::Scalar;

/// Prehashed message to sign.
pub type PrehashedMessage = [u8; 32];

/// Creates the initial state for the joined KeyGen and KeyRefresh+Auxiliary protocols.
#[allow(clippy::type_complexity)]
pub fn make_keygen_and_aux_session<P, Sig, Signer, Verifier>(
rng: &mut impl CryptoRngCore,
shared_randomness: &[u8],
signer: Signer,
verifiers: &[Verifier],
party_idx: PartyIdx,
) -> Result<Session<KeygenAndAuxResult<P>, Sig, Signer, Verifier>, Error<KeygenAndAuxResult<P>>>
) -> Result<Session<KeygenAndAuxResult<P>, Sig, Signer, Verifier>, LocalError>
where
Sig: Clone + Serialize + for<'de> Deserialize<'de> + PartialEq + Eq,
P: SchemeParams + 'static,
Signer: RandomizedPrehashSigner<Sig>,
Verifier: PrehashVerifier<Sig> + Clone,
Signer: RandomizedPrehashSigner<Sig> + Keypair<VerifyingKey = Verifier>,
Verifier: PrehashVerifier<Sig> + Debug + Clone + Ord,
{
Session::new::<keygen_and_aux::Round1<P>>(
rng,
shared_randomness,
signer,
party_idx,
verifiers,
(),
)
Session::new::<keygen_and_aux::Round1<P>>(rng, shared_randomness, signer, verifiers, ())
}

/// Creates the initial state for the KeyRefresh+Auxiliary protocol.
#[allow(clippy::type_complexity)]
pub fn make_key_refresh_session<P, Sig, Signer, Verifier>(
rng: &mut impl CryptoRngCore,
shared_randomness: &[u8],
signer: Signer,
verifiers: &[Verifier],
party_idx: PartyIdx,
) -> Result<Session<KeyRefreshResult<P>, Sig, Signer, Verifier>, Error<KeyRefreshResult<P>>>
) -> Result<Session<KeyRefreshResult<P>, Sig, Signer, Verifier>, LocalError>
where
Sig: Clone + Serialize + for<'de> Deserialize<'de> + PartialEq + Eq,
P: SchemeParams + 'static,
Signer: RandomizedPrehashSigner<Sig>,
Verifier: PrehashVerifier<Sig> + Clone,
Signer: RandomizedPrehashSigner<Sig> + Keypair<VerifyingKey = Verifier>,
Verifier: PrehashVerifier<Sig> + Debug + Clone + Ord,
{
Session::new::<auxiliary::Round1<P>>(rng, shared_randomness, signer, party_idx, verifiers, ())
Session::new::<auxiliary::Round1<P>>(rng, shared_randomness, signer, verifiers, ())
}

/// Creates the initial state for the joined Presigning and Signing protocols.
#[allow(clippy::type_complexity)]
pub fn make_interactive_signing_session<P, Sig, Signer, Verifier>(
rng: &mut impl CryptoRngCore,
shared_randomness: &[u8],
signer: Signer,
verifiers: &[Verifier],
key_share: &KeyShare<P>,
prehashed_message: &PrehashedMessage,
) -> Result<
Session<InteractiveSigningResult<P>, Sig, Signer, Verifier>,
Error<InteractiveSigningResult<P>>,
>
) -> Result<Session<InteractiveSigningResult<P>, Sig, Signer, Verifier>, LocalError>
where
Sig: Clone + Serialize + for<'de> Deserialize<'de> + PartialEq + Eq,
P: SchemeParams + 'static,
Signer: RandomizedPrehashSigner<Sig>,
Verifier: PrehashVerifier<Sig> + Clone,
Signer: RandomizedPrehashSigner<Sig> + Keypair<VerifyingKey = Verifier>,
Verifier: PrehashVerifier<Sig> + Debug + Clone + Ord,
{
// TODO: check that key share party index corresponds to the signer's position among the verifiers
if verifiers.len() != key_share.num_parties() {
return Err(Error::Local(LocalError::Init(format!(
return Err(LocalError(format!(
concat![
"Number of verifiers (got: {}) must be equal ",
"to the number of parties in the key share (got: {})"
],
verifiers.len(),
key_share.num_parties()
))));
)));
}

let scalar_message = Scalar::from_reduced_bytes(prehashed_message);
Expand All @@ -99,7 +89,6 @@ where
rng,
shared_randomness,
signer,
key_share.party_index(),
verifiers,
context,
)
Expand Down
48 changes: 16 additions & 32 deletions synedrion/src/sessions/error.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,19 @@
use alloc::string::String;

use super::broadcast::ConsensusError;
use super::type_erased::{AccumAddError, AccumFinalizeError};
use crate::cggmp21::{PartyIdx, ProtocolResult};
use crate::cggmp21::ProtocolResult;

/// Possible errors returned by session methods.
#[derive(Clone, Debug)]
pub enum Error<Res: ProtocolResult> {
pub enum Error<Res: ProtocolResult, Verifier> {
/// Indicates an error on this party's side.
/// Can be caused by an incorrect usage, a bug in the implementation, or some environment error.
Local(LocalError),
/// An unprovable fault of another party.
Remote {
/// The index of the failed party.
party: PartyIdx,
/// The error that occurred.
error: RemoteError,
},
/// A provable fault of another party.
// TODO: attach the party's messages up to this round for this to be verifiable by a third party
Provable {
/// The index of the failed party.
party: PartyIdx,
party: Verifier,
/// The error that occurred.
error: ProvableError<Res>,
},
Expand All @@ -33,33 +25,25 @@ pub enum Error<Res: ProtocolResult> {
/// The proof of correctness.
proof: Res::CorrectnessProof,
},
/// An error caused by remote party, unprovable at this level.
///
/// This error may be eventually provable if there are some external guarantees
/// provided by the communication channel.
Remote(RemoteError<Verifier>),
}

#[derive(Clone, Debug)]
pub enum LocalError {
/// An error while initializing the first round of a protocol.
///
/// Note that it can be returned in the middle of the session in case of
/// sequentially merged protocols (e.g. Presigning and Signing).
Init(String),
/// A mutable object was in an invalid state for calling a method.
///
/// This indicates a logic error either in the calling code or in the method code.
InvalidState(String),
/// A message could not be serialized.
///
/// Refer to the documentation of the chosen serialization library for more info.
CannotSerialize(String),
/// A message could not be signed.
///
/// Refer to the documentation of the chosen ECDSA library for more info.
CannotSign(String),
AccumFinalize(AccumFinalizeError),
AccumAdd(AccumAddError),
pub struct LocalError(pub(crate) String);

/// An unprovable fault of another party.
#[derive(Clone, Debug)]
pub struct RemoteError<Verifier> {
pub party: Verifier,
pub error: RemoteErrorEnum,
}

#[derive(Clone, Debug)]
pub enum RemoteError {
pub enum RemoteErrorEnum {
UnexpectedSessionId,
OutOfOrderMessage,
DuplicateMessage,
Expand Down
2 changes: 1 addition & 1 deletion synedrion/src/sessions/signed_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ impl<Sig> VerifiedMessage<Sig> {
rng,
message_hash(session_id, round, message_type, message_bytes).as_ref(),
)
.map_err(|err| LocalError::CannotSign(err.to_string()))?;
.map_err(|err| LocalError(err.to_string()))?;
Ok(Self(SignedMessage {
session_id: *session_id,
round,
Expand Down
Loading