Skip to content

Commit

Permalink
Use the randomized signer trait to sign messages
Browse files Browse the repository at this point in the history
  • Loading branch information
fjarri committed Jul 7, 2023
1 parent f57dcf5 commit 75cee39
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 9 deletions.
2 changes: 1 addition & 1 deletion synedrion/src/protocols/generic.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use alloc::string::String;
use alloc::vec::Vec;
use core::marker::PhantomData;
use core::fmt;
use core::marker::PhantomData;

use rand_core::CryptoRngCore;
use serde::{Deserialize, Serialize};
Expand Down
4 changes: 2 additions & 2 deletions synedrion/src/sessions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ mod type_erased;

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

use crate::curve::{RecoverableSignature, Scalar};
use crate::protocols::{common::KeyShare, interactive_signing};
Expand All @@ -30,7 +30,7 @@ pub fn make_interactive_signing_session<P, Sig, Signer, Verifier>(
where
Sig: Clone + Serialize + for<'de> Deserialize<'de> + PartialEq + Eq,
P: SchemeParams + 'static,
Signer: PrehashSigner<Sig>,
Signer: RandomizedPrehashSigner<Sig>,
Verifier: PrehashVerifier<Sig> + Clone,
{
let scalar_message = Scalar::from_reduced_bytes(prehashed_message);
Expand Down
9 changes: 6 additions & 3 deletions synedrion/src/sessions/signed_message.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use alloc::boxed::Box;
use alloc::string::ToString;

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

use super::error::{MyFault, TheirFault};
use crate::tools::hashing::{Chain, Hash, HashOutput, Hashable};
Expand Down Expand Up @@ -71,7 +72,8 @@ pub(crate) struct VerifiedMessage<Sig>(SignedMessage<Sig>);

impl<Sig> VerifiedMessage<Sig> {
pub(crate) fn new(
signer: &impl PrehashSigner<Sig>,
rng: &mut impl CryptoRngCore,
signer: &impl RandomizedPrehashSigner<Sig>,
session_id: &SessionId,
round: u8,
broadcast_consensus: bool,
Expand All @@ -84,7 +86,8 @@ impl<Sig> VerifiedMessage<Sig> {
// so that these signatures could be verified by a third party.

let signature = signer
.sign_prehash(
.sign_prehash_with_rng(
rng,
message_hash(session_id, round, broadcast_consensus, message_bytes).as_ref(),
)
.map_err(|err| MyFault::SigningError(err.to_string()))?;
Expand Down
11 changes: 8 additions & 3 deletions synedrion/src/sessions/states.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use alloc::vec::Vec;

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

use super::broadcast::BroadcastConsensus;
use super::error::{Error, MyFault, TheirFault};
Expand Down Expand Up @@ -60,7 +60,7 @@ pub struct ReceivingState<Res, Sig, Signer, Verifier> {

impl<Res, Sig, Signer, Verifier> SendingState<Res, Sig, Signer, Verifier>
where
Signer: PrehashSigner<Sig>,
Signer: RandomizedPrehashSigner<Sig>,
Verifier: Clone + PrehashVerifier<Sig>,
Sig: Clone + Serialize + for<'de> Deserialize<'de> + PartialEq + Eq,
{
Expand Down Expand Up @@ -96,6 +96,7 @@ where
}

fn sign_messages(
rng: &mut impl CryptoRngCore,
signer: &Signer,
session_id: &SessionId,
to_send: &ToSendSerialized,
Expand All @@ -105,6 +106,7 @@ where
Ok(match &to_send {
ToSendSerialized::Broadcast(message_bytes) => {
let message = VerifiedMessage::new(
rng,
signer,
session_id,
round_num,
Expand All @@ -118,6 +120,7 @@ where
let mut signed_messages = Vec::with_capacity(messages.len());
for (index, message_bytes) in messages.iter() {
let signed_message = VerifiedMessage::new(
rng,
signer,
session_id,
round_num,
Expand All @@ -144,6 +147,7 @@ where
let (receiving_round, to_send) =
round.to_receiving_state(rng, context.verifiers.len(), context.party_idx);
let signed_to_send = Self::sign_messages(
rng,
&context.signer,
&context.session_id,
&to_send,
Expand All @@ -161,6 +165,7 @@ where
let round_num = next_round.round_num() - 1;
let to_send = bc.to_send();
let signed_to_send = Self::sign_messages(
rng,
&context.signer,
&context.session_id,
&to_send,
Expand Down Expand Up @@ -237,7 +242,7 @@ pub enum FinalizeOutcome<Res, Sig, Signer, Verifier> {

impl<Res, Sig, Signer, Verifier> ReceivingState<Res, Sig, Signer, Verifier>
where
Signer: PrehashSigner<Sig>,
Signer: RandomizedPrehashSigner<Sig>,
Verifier: Clone + PrehashVerifier<Sig>,
Sig: Clone + Serialize + for<'de> Deserialize<'de> + PartialEq + Eq,
{
Expand Down

0 comments on commit 75cee39

Please sign in to comment.