Skip to content

Commit

Permalink
Temporary commit 2
Browse files Browse the repository at this point in the history
  • Loading branch information
fjarri committed Aug 28, 2024
1 parent 155c3e3 commit 75d5d45
Show file tree
Hide file tree
Showing 7 changed files with 268 additions and 130 deletions.
25 changes: 17 additions & 8 deletions synedrion/src/cggmp21/protocols/key_init_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,17 @@ pub(crate) enum KeyInitErrorType {
impl<P: SchemeParams, I: Ord + Clone + Serialize + for<'de> Deserialize<'de>>
EvidenceRequiresMessages<I> for KeyInitError<P, I>
{
fn requires_messages(&self) -> &[(u8, bool)] {
fn requires_echos(&self) -> &[u8] {
match self.error {
KeyInitErrorType::R2HashMismatch => &[(1, false), (2, false)],
KeyInitErrorType::R3InvalidSchProof => &[(2, true), (3, false)],
KeyInitErrorType::R2HashMismatch => &[],
KeyInitErrorType::R3InvalidSchProof => &[2],
}
}

fn requires_bcs(&self) -> &[u8] {
match self.error {
KeyInitErrorType::R2HashMismatch => &[1, 2],
KeyInitErrorType::R3InvalidSchProof => &[3],
}
}

Expand All @@ -42,17 +49,19 @@ impl<P: SchemeParams, I: Ord + Clone + Serialize + for<'de> Deserialize<'de>>
shared_randomness: &[u8],
other_ids: &BTreeSet<I>,
my_id: &I,
messages: &BTreeMap<(u8, bool), Message>,
bcs: &BTreeMap<u8, Message>,
_dms: &BTreeMap<u8, Message>,
echos: &BTreeMap<u8, Message>,
) -> bool {
match self.error {
KeyInitErrorType::R2HashMismatch => {
let r1 = messages[&(1, false)].to_typed().unwrap();
let r2 = messages[&(2, false)].to_typed().unwrap();
let r1 = bcs[&1].to_typed().unwrap();
let r2 = bcs[&2].to_typed().unwrap();
self.verify_r2_hash_mismatch(shared_randomness, other_ids, my_id, &r1, &r2)
}
KeyInitErrorType::R3InvalidSchProof => {
let r2 = messages[&(2, true)].to_typed_echo().unwrap();
let r3 = messages[&(3, false)].to_typed().unwrap();
let r2 = echos[&2].to_typed_echo().unwrap();
let r3 = bcs[&3].to_typed().unwrap();
self.verify_r3_invalid_sch_proof(shared_randomness, other_ids, my_id, &r2, &r3)
}
}
Expand Down
24 changes: 21 additions & 3 deletions synedrion/src/rounds/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use displaydoc::Display;
use rand_core::CryptoRngCore;
use serde::{Deserialize, Serialize};

/// A type suitable to serve as a party identifier.
pub trait PartyId: Debug + Ord + Clone + Serialize + for<'de> Deserialize<'de> {}

impl<T: Debug + Ord + Clone + Serialize + for<'de> Deserialize<'de>> PartyId for T {}
Expand Down Expand Up @@ -206,18 +207,35 @@ pub(crate) use no_direct_messages;

use crate::sessions::Message;

/// A trait specifying which messages the evidence needs to prove a party's fault,
/// and how to do it.
// TODO: rename this
pub(crate) trait EvidenceRequiresMessages<I> {
fn requires_messages(&self) -> &[(u8, bool)] {
// TODO (#74): this trait should not be visible to the user,
// but I can't figure out at the moment how to do that.
pub trait EvidenceRequiresMessages<I> {
/// Returns the list of rounds and the indicator of whether a regular message
/// or the echo of all messages is needed.
fn requires_bcs(&self) -> &[u8] {
unimplemented!()
}

fn requires_dms(&self) -> &[u8] {
unimplemented!()
}

fn requires_echos(&self) -> &[u8] {
unimplemented!()
}

/// Given the required messages, returns ``true`` if the party was proven to be malicious.
fn verify_malicious(
&self,
_shared_randomness: &[u8],
_other_ids: &BTreeSet<I>,
_my_id: &I,
_messages: &BTreeMap<(u8, bool), Message>,
_bcs: &BTreeMap<u8, Message>,
_dms: &BTreeMap<u8, Message>,
_echos: &BTreeMap<u8, Message>,
) -> bool {
unimplemented!()
}
Expand Down
2 changes: 2 additions & 0 deletions synedrion/src/sessions/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ use crate::rounds::ProtocolResult;
pub enum Error<Res: ProtocolResult<Verifier>, Sig, Verifier> {
/// Indicates an error on this party's side.
Local(LocalError),
/// An evidence of of another party behaving maliciously,
/// with the evidence attached.
Evidence(Evidence<Res, Sig, Verifier>),
/// A provable fault of another party.
// TODO (#43): attach the party's messages up to this round
Expand Down
107 changes: 83 additions & 24 deletions synedrion/src/sessions/evidence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,63 +4,122 @@ use core::marker::PhantomData;
use serde::Deserialize;
use signature::hazmat::PrehashVerifier;

use super::message_bundle::{MessageBundle, VerifiedMessageBundle};
use super::session::Messages;
use super::signed_message::SignedMessage;
use crate::rounds::{EvidenceRequiresMessages, ProtocolResult};

#[derive(Debug, Clone)]
pub struct Evidence<Res: ProtocolResult<Verifier>, Sig, Verifier> {
party: Verifier,

Check failure on line 14 in synedrion/src/sessions/evidence.rs

View workflow job for this annotation

GitHub Actions / format (ubuntu-latest)

fields `party` and `message_bundle` are never read
result: Res::ProvableError,
message_bundle: MessageBundle,
// Map round number -> message signed by the offending party
messages: BTreeMap<(u8, bool), SignedMessage>,
bcs: BTreeMap<u8, SignedMessage>,
dms: BTreeMap<u8, SignedMessage>,
echos: BTreeMap<u8, SignedMessage>,
phantom: PhantomData<Sig>,
}

impl<Res: ProtocolResult<Verifier>, Sig: Clone + for<'de> Deserialize<'de>, Verifier>
Evidence<Res, Sig, Verifier>
where
Verifier: Clone + PrehashVerifier<Sig>,
impl<
Res: ProtocolResult<Verifier>,
Sig: Clone + for<'de> Deserialize<'de>,
Verifier: Clone + Ord,
> Evidence<Res, Sig, Verifier>
{
fn new(
pub(crate) fn new(
party: &Verifier,
result: Res::ProvableError,
all_messages: &BTreeMap<(u8, bool), SignedMessage>,
message_bundle: VerifiedMessageBundle,
messages: &Messages<Verifier>,
) -> Self {
let messages = result
.requires_messages()
let bcs = result
.requires_bcs()
.iter()
.map(|(round_num, echo)| {
(
(*round_num, *echo),
all_messages[&(*round_num, *echo)].clone(),
)
})
.map(|round_num| (*round_num, messages.bcs[round_num][party].clone()))
.collect();
let dms = result
.requires_dms()
.iter()
.map(|round_num| (*round_num, messages.dms[round_num][party].clone()))
.collect();
let echos = result
.requires_echos()
.iter()
.map(|round_num| (*round_num, messages.echos[round_num][party].clone()))
.collect();

Self {
party: party.clone(),
result,
messages,
message_bundle: message_bundle.into_unverified(),
bcs,
dms,
echos,
phantom: PhantomData,
}
}
}

fn verify_malicious(
impl<Res: ProtocolResult<Verifier>, Sig: Clone + for<'de> Deserialize<'de>, Verifier>
Evidence<Res, Sig, Verifier>
where
Verifier: Clone + PrehashVerifier<Sig>,
{
pub fn verify_malicious(
&self,
verifier: &Verifier,
shared_randomness: &[u8],
other_ids: &BTreeSet<Verifier>,
my_id: &Verifier,
) -> bool {
let vmessages = self
.messages
let bcs = self
.bcs
.iter()
.map(|((round, echo), message)| {
let vmessage = message.clone().verify(verifier).unwrap();
((*round, *echo), vmessage.serialized_message().clone())
.map(|(round, message)| {
(
*round,
message
.clone()
.verify(verifier)
.unwrap()
.serialized_message()
.clone(),
)
})
.collect::<BTreeMap<_, _>>();
.collect();
let dms = self
.dms
.iter()
.map(|(round, message)| {
(
*round,
message
.clone()
.verify(verifier)
.unwrap()
.serialized_message()
.clone(),
)
})
.collect();
let echos = self
.echos
.iter()
.map(|(round, message)| {
(
*round,
message
.clone()
.verify(verifier)
.unwrap()
.serialized_message()
.clone(),
)
})
.collect();

self.result
.verify_malicious(shared_randomness, other_ids, my_id, &vmessages)
.verify_malicious(shared_randomness, other_ids, my_id, &bcs, &dms, &echos)
}
}
29 changes: 24 additions & 5 deletions synedrion/src/sessions/message_bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ impl MessageBundle {
pub(crate) struct VerifiedMessageBundle(MessageBundleEnum<VerifiedMessage>);

impl VerifiedMessageBundle {
pub fn into_unverified(self) -> MessageBundle {
unimplemented!()
}

pub fn broadcast_message(&self) -> Option<&Message> {
match &self.0 {
MessageBundleEnum::Broadcast(msg) => Some(msg.serialized_message()),
Expand All @@ -144,6 +148,21 @@ impl VerifiedMessageBundle {
}
}

pub fn direct_message(&self) -> Option<&Message> {
match &self.0 {
MessageBundleEnum::Direct(msg) => Some(msg.serialized_message()),
MessageBundleEnum::Both { direct, .. } => Some(direct.serialized_message()),
_ => None,
}
}

pub fn echo_message(&self) -> Option<&Message> {
match &self.0 {
MessageBundleEnum::Echo(msg) => Some(msg.serialized_message()),
_ => None,
}
}

pub fn broadcast_full(&self) -> Option<&VerifiedMessage> {
match &self.0 {
MessageBundleEnum::Broadcast(msg) => Some(msg),
Expand All @@ -152,17 +171,17 @@ impl VerifiedMessageBundle {
}
}

pub fn direct_message(&self) -> Option<&Message> {
pub fn direct_full(&self) -> Option<&VerifiedMessage> {
match &self.0 {
MessageBundleEnum::Direct(msg) => Some(msg.serialized_message()),
MessageBundleEnum::Both { direct, .. } => Some(direct.serialized_message()),
MessageBundleEnum::Direct(msg) => Some(msg),
MessageBundleEnum::Both { direct, .. } => Some(direct),
_ => None,
}
}

pub fn echo_message(&self) -> Option<&Message> {
pub fn echo_full(&self) -> Option<&VerifiedMessage> {
match &self.0 {
MessageBundleEnum::Echo(msg) => Some(msg.serialized_message()),
MessageBundleEnum::Echo(msg) => Some(msg),
_ => None,
}
}
Expand Down
Loading

0 comments on commit 75d5d45

Please sign in to comment.