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

Bump Synedrion to latest master #946

Merged
merged 15 commits into from
Jul 18, 2024
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
17 changes: 15 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ x25519-dalek ={ version="2.0.1", features=["static_secrets"], optional=true }
entropy-protocol={ version="0.2.0", path="../protocol", optional=true, default-features=false }
reqwest ={ version="0.12.5", features=["json", "stream"], optional=true }
base64 ={ version="0.22.0", optional=true }
synedrion ={ git="https://github.com/entropyxyz/synedrion", rev="25373111cbb01e1a25d8a5c5bb8f4652c725b3f1", optional=true }
synedrion ={ git="https://github.com/entropyxyz/synedrion", rev="3be1339c21384a8e60a1534f1d3bfdd022662e63", optional=true }
hex ={ version="0.4.3", optional=true }
anyhow ="1.0.86"

Expand Down
2 changes: 1 addition & 1 deletion crates/kvdb/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ zeroize ={ version="1.8", features=["zeroize_derive"], default-features=
rpassword ={ version="7.3.1", default-features=false }
scrypt ={ version="0.11.0", default-features=false, features=["std"] }
chacha20poly1305={ version="0.9", features=["alloc"], default-features=false }
synedrion ={ git="https://github.com/entropyxyz/synedrion", rev="25373111cbb01e1a25d8a5c5bb8f4652c725b3f1" }
synedrion ={ git="https://github.com/entropyxyz/synedrion", rev="3be1339c21384a8e60a1534f1d3bfdd022662e63" }

# Async
tokio ={ version="1.38", features=["macros", "sync", "fs", "rt-multi-thread", "io-util"] }
Expand Down
2 changes: 1 addition & 1 deletion crates/protocol/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ edition ='2021'
[dependencies]
async-trait ="0.1.81"
entropy-shared ={ version="0.2.0", path="../shared", default-features=false }
synedrion ={ git="https://github.com/entropyxyz/synedrion", rev="25373111cbb01e1a25d8a5c5bb8f4652c725b3f1" }
synedrion ={ git="https://github.com/entropyxyz/synedrion", rev="3be1339c21384a8e60a1534f1d3bfdd022662e63" }
serde ={ version="1.0", features=["derive"], default-features=false }
subxt ={ version="0.35.3", default-features=false }
sp-core ={ version="31.0.0", default-features=false, features=["full_crypto", "serde"] }
Expand Down
40 changes: 21 additions & 19 deletions crates/protocol/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@

use synedrion::{
sessions, AuxGenResult, InteractiveSigningResult, KeyInitResult, KeyResharingResult,
MappedResult,
ProtocolResult,
};
use thiserror::Error;

use crate::{protocol_message::ProtocolMessage, KeyParams, PartyId};

#[derive(Debug, Error)]
pub enum GenericProtocolError<Res: MappedResult<PartyId>> {
pub enum GenericProtocolError<Res: ProtocolResult> {
#[error("Synedrion session error {0}")]
Joined(Box<sessions::Error<Res, PartyId>>),
#[error("Incoming message stream error: {0}")]
Expand All @@ -33,28 +33,28 @@ pub enum GenericProtocolError<Res: MappedResult<PartyId>> {
Mpsc(#[from] tokio::sync::mpsc::error::SendError<ProtocolMessage>),
}

impl<Res: MappedResult<PartyId>> From<sessions::LocalError> for GenericProtocolError<Res> {
impl<Res: ProtocolResult> From<sessions::LocalError> for GenericProtocolError<Res> {
fn from(err: sessions::LocalError) -> Self {
Self::Joined(Box::new(sessions::Error::Local(err)))
}
}

impl<Res: MappedResult<PartyId>> From<sessions::RemoteError<PartyId>>
for GenericProtocolError<Res>
{
impl<Res: ProtocolResult> From<sessions::RemoteError<PartyId>> for GenericProtocolError<Res> {
fn from(err: sessions::RemoteError<PartyId>) -> Self {
Self::Joined(Box::new(sessions::Error::Remote(err)))
}
}

impl<Res: MappedResult<PartyId>> From<sessions::Error<Res, PartyId>> for GenericProtocolError<Res> {
impl<Res: ProtocolResult> From<sessions::Error<Res, PartyId>> for GenericProtocolError<Res> {
fn from(err: sessions::Error<Res, PartyId>) -> Self {
Self::Joined(Box::new(err))
}
}

impl From<GenericProtocolError<InteractiveSigningResult<KeyParams>>> for ProtocolExecutionErr {
fn from(err: GenericProtocolError<InteractiveSigningResult<KeyParams>>) -> Self {
impl From<GenericProtocolError<InteractiveSigningResult<KeyParams, PartyId>>>
for ProtocolExecutionErr
{
fn from(err: GenericProtocolError<InteractiveSigningResult<KeyParams, PartyId>>) -> Self {
tracing::error!("{:?}", err);
match err {
GenericProtocolError::Joined(err) => ProtocolExecutionErr::SigningProtocolError(err),
Expand All @@ -65,8 +65,8 @@ impl From<GenericProtocolError<InteractiveSigningResult<KeyParams>>> for Protoco
}
}

impl From<GenericProtocolError<KeyInitResult<KeyParams>>> for ProtocolExecutionErr {
fn from(err: GenericProtocolError<KeyInitResult<KeyParams>>) -> Self {
impl From<GenericProtocolError<KeyInitResult<KeyParams, PartyId>>> for ProtocolExecutionErr {
fn from(err: GenericProtocolError<KeyInitResult<KeyParams, PartyId>>) -> Self {
tracing::error!("{:?}", err);
match err {
GenericProtocolError::Joined(err) => ProtocolExecutionErr::KeyInitProtocolError(err),
Expand All @@ -77,8 +77,8 @@ impl From<GenericProtocolError<KeyInitResult<KeyParams>>> for ProtocolExecutionE
}
}

impl From<GenericProtocolError<KeyResharingResult<KeyParams>>> for ProtocolExecutionErr {
fn from(err: GenericProtocolError<KeyResharingResult<KeyParams>>) -> Self {
impl From<GenericProtocolError<KeyResharingResult<KeyParams, PartyId>>> for ProtocolExecutionErr {
fn from(err: GenericProtocolError<KeyResharingResult<KeyParams, PartyId>>) -> Self {
tracing::error!("{:?}", err);
match err {
GenericProtocolError::Joined(err) => ProtocolExecutionErr::KeyReshareProtocolError(err),
Expand All @@ -89,8 +89,8 @@ impl From<GenericProtocolError<KeyResharingResult<KeyParams>>> for ProtocolExecu
}
}

impl From<GenericProtocolError<AuxGenResult<KeyParams>>> for ProtocolExecutionErr {
fn from(err: GenericProtocolError<AuxGenResult<KeyParams>>) -> Self {
impl From<GenericProtocolError<AuxGenResult<KeyParams, PartyId>>> for ProtocolExecutionErr {
fn from(err: GenericProtocolError<AuxGenResult<KeyParams, PartyId>>) -> Self {
tracing::error!("{:?}", err);
match err {
GenericProtocolError::Joined(err) => ProtocolExecutionErr::AuxGenProtocolError(err),
Expand All @@ -109,13 +109,15 @@ pub enum ProtocolExecutionErr {
#[error("Synedrion session creation error: {0}")]
SessionCreation(sessions::LocalError),
#[error("Synedrion signing session error")]
SigningProtocolError(Box<sessions::Error<InteractiveSigningResult<KeyParams>, PartyId>>),
SigningProtocolError(
Box<sessions::Error<InteractiveSigningResult<KeyParams, PartyId>, PartyId>>,
),
#[error("Synedrion key init session error")]
KeyInitProtocolError(Box<sessions::Error<KeyInitResult<KeyParams>, PartyId>>),
KeyInitProtocolError(Box<sessions::Error<KeyInitResult<KeyParams, PartyId>, PartyId>>),
#[error("Synedrion key reshare session error")]
KeyReshareProtocolError(Box<sessions::Error<KeyResharingResult<KeyParams>, PartyId>>),
KeyReshareProtocolError(Box<sessions::Error<KeyResharingResult<KeyParams, PartyId>, PartyId>>),
#[error("Synedrion aux generation session error")]
AuxGenProtocolError(Box<sessions::Error<AuxGenResult<KeyParams>, PartyId>>),
AuxGenProtocolError(Box<sessions::Error<AuxGenResult<KeyParams, PartyId>, PartyId>>),
#[error("Broadcast error: {0}")]
Broadcast(#[from] Box<tokio::sync::broadcast::error::SendError<ProtocolMessage>>),
#[error("Mpsc send error: {0}")]
Expand Down
76 changes: 49 additions & 27 deletions crates/protocol/src/execute_protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use synedrion::{
k256::EncodedPoint,
make_aux_gen_session, make_interactive_signing_session, make_key_init_session,
make_key_resharing_session,
sessions::{FinalizeOutcome, Session},
sessions::{FinalizeOutcome, Session, SessionId as SynedrionSessionId},
signature::{self, hazmat::RandomizedPrehashSigner},
AuxInfo, KeyResharingInputs, KeyShare, NewHolder, OldHolder, PrehashedMessage,
RecoverableSignature, ThresholdKeyShare,
Expand All @@ -39,6 +39,8 @@ use crate::{
DkgSubsession, KeyParams, KeyShareWithAuxInfo, PartyId, SessionId,
};

use std::collections::BTreeSet;

pub type ChannelIn = mpsc::Receiver<ProtocolMessage>;
pub type ChannelOut = Broadcaster;

Expand Down Expand Up @@ -67,11 +69,11 @@ impl RandomizedPrehashSigner<sr25519::Signature> for PairWrapper {
}
}

async fn execute_protocol_generic<Res: synedrion::MappedResult<PartyId>>(
async fn execute_protocol_generic<Res: synedrion::ProtocolResult>(
mut chans: Channels,
session: Session<Res, sr25519::Signature, PairWrapper, PartyId>,
session_id_hash: [u8; 32],
) -> Result<(Res::MappedSuccess, mpsc::Receiver<ProtocolMessage>), GenericProtocolError<Res>> {
) -> Result<(Res::Success, mpsc::Receiver<ProtocolMessage>), GenericProtocolError<Res>> {
let tx = &chans.0;
let rx = &mut chans.1;

Expand Down Expand Up @@ -112,7 +114,7 @@ async fn execute_protocol_generic<Res: synedrion::MappedResult<PartyId>>(
))
})?;

if let ProtocolMessagePayload::CombinedMessage(payload) = message.payload.clone() {
if let ProtocolMessagePayload::MessageBundle(payload) = message.payload.clone() {
if message.session_id_hash == session_id_hash {
break (message.from, *payload);
} else {
Expand Down Expand Up @@ -170,15 +172,16 @@ pub async fn execute_signing_protocol(
tracing::debug!("Executing signing protocol");
tracing::trace!("Using key share with verifying key {:?}", &key_share.verifying_key());

let party_ids: Vec<PartyId> = threshold_accounts.iter().cloned().map(PartyId::new).collect();
let party_ids: BTreeSet<PartyId> =
threshold_accounts.iter().cloned().map(PartyId::new).collect();

let pair = PairWrapper(threshold_pair.clone());

let session_id_hash = session_id.blake2(None)?;

let session = make_interactive_signing_session(
&mut OsRng,
&session_id_hash,
SynedrionSessionId::from_seed(session_id_hash.as_slice()),
pair,
&party_ids,
key_share,
Expand Down Expand Up @@ -206,24 +209,26 @@ pub async fn execute_dkg(
tracing::debug!("Executing DKG");
let broadcaster = chans.0.clone();

let mut party_ids: Vec<PartyId> =
let party_ids: BTreeSet<PartyId> =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 for a set rather than a mutable vector

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙌 thank mr bogdan 🙌

threshold_accounts.iter().cloned().map(PartyId::new).collect();
party_ids.sort();

let pair = PairWrapper(threshold_pair.clone());

let my_party_id = PartyId::new(AccountId32(threshold_pair.public().0));

let session_id_hash = session_id.blake2(Some(DkgSubsession::KeyInit))?;
let (mut key_init_parties, includes_me) =
let (key_init_parties, includes_me) =
get_key_init_parties(&my_party_id, threshold, &party_ids, &session_id_hash)?;
key_init_parties.sort();

let (verifying_key, old_holder, chans) = if includes_me {
// First run the key init session.
let session =
make_key_init_session(&mut OsRng, &session_id_hash, pair.clone(), &key_init_parties)
.map_err(ProtocolExecutionErr::SessionCreation)?;
let session = make_key_init_session(
&mut OsRng,
SynedrionSessionId::from_seed(session_id_hash.as_slice()),
pair.clone(),
&key_init_parties,
)
.map_err(ProtocolExecutionErr::SessionCreation)?;

let (init_keyshare, rx) = execute_protocol_generic(chans, session, session_id_hash).await?;

Expand All @@ -248,7 +253,7 @@ pub async fn execute_dkg(
}
(
verifying_key,
Some(OldHolder { key_share: init_keyshare.to_threshold_key_share() }),
Some(OldHolder { key_share: ThresholdKeyShare::from_key_share(&init_keyshare) }),
chans,
)
} else {
Expand Down Expand Up @@ -289,9 +294,14 @@ pub async fn execute_dkg(
};

let session_id_hash = session_id.blake2(Some(DkgSubsession::Reshare))?;
let session =
make_key_resharing_session(&mut OsRng, &session_id_hash, pair.clone(), &party_ids, &inputs)
.map_err(ProtocolExecutionErr::SessionCreation)?;
let session = make_key_resharing_session(
&mut OsRng,
SynedrionSessionId::from_seed(session_id_hash.as_slice()),
pair.clone(),
&party_ids,
inputs,
)
.map_err(ProtocolExecutionErr::SessionCreation)?;
let (new_key_share_option, rx) =
execute_protocol_generic(chans, session, session_id_hash).await?;
let new_key_share =
Expand All @@ -303,8 +313,13 @@ pub async fn execute_dkg(

// Now run the aux gen protocol to get AuxInfo
let session_id_hash = session_id.blake2(Some(DkgSubsession::AuxGen))?;
let session = make_aux_gen_session(&mut OsRng, &session_id_hash, pair, &party_ids)
.map_err(ProtocolExecutionErr::SessionCreation)?;
let session = make_aux_gen_session(
&mut OsRng,
SynedrionSessionId::from_seed(session_id_hash.as_slice()),
pair,
&party_ids,
)
.map_err(ProtocolExecutionErr::SessionCreation)?;
let aux_info = execute_protocol_generic(chans, session, session_id_hash).await?.0;
tracing::info!("Finished aux gen protocol");

Expand All @@ -327,7 +342,8 @@ pub async fn execute_proactive_refresh(
tracing::debug!("Executing proactive refresh");
tracing::debug!("Signing with {:?}", &threshold_pair.public());

let party_ids: Vec<PartyId> = threshold_accounts.iter().cloned().map(PartyId::new).collect();
let party_ids: BTreeSet<PartyId> =
threshold_accounts.iter().cloned().map(PartyId::new).collect();
let pair = PairWrapper(threshold_pair.clone());
let verifying_key = old_key.verifying_key();

Expand All @@ -344,9 +360,14 @@ pub async fn execute_proactive_refresh(
new_holders: party_ids.clone(),
new_threshold: threshold,
};
let session =
make_key_resharing_session(&mut OsRng, &session_id_hash, pair, &party_ids, &inputs)
.map_err(ProtocolExecutionErr::SessionCreation)?;
let session = make_key_resharing_session(
&mut OsRng,
SynedrionSessionId::from_seed(session_id_hash.as_slice()),
pair,
&party_ids,
inputs,
)
.map_err(ProtocolExecutionErr::SessionCreation)?;

let new_key_share = execute_protocol_generic(chans, session, session_id_hash).await?.0;

Expand All @@ -357,10 +378,11 @@ pub async fn execute_proactive_refresh(
fn get_key_init_parties(
my_party_id: &PartyId,
threshold: usize,
validators: &[PartyId],
validators: &BTreeSet<PartyId>,
session_id_hash: &[u8],
) -> Result<(Vec<PartyId>, bool), ProtocolExecutionErr> {
let mut parties = vec![];
) -> Result<(BTreeSet<PartyId>, bool), ProtocolExecutionErr> {
let validators = validators.iter().cloned().collect::<Vec<PartyId>>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i guess for this to work we need this to turn the set into a vector with consistent ordering. Since the tests pass this must be the case.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this works because BTreeSet requires that any item inserted into the set implements Ord, meaning that the contents of the set will be ordered at any point in time.

let mut parties = BTreeSet::new();
let mut includes_self = false;
let number = BigUint::from_bytes_be(session_id_hash);
let start_index_big = &number % validators.len();
Expand All @@ -372,7 +394,7 @@ fn get_key_init_parties(
if member == my_party_id {
includes_self = true;
}
parties.push(member.clone());
parties.insert(member.clone());
}

Ok((parties, includes_self))
Expand Down
Loading