pub mod pairwise;
use itertools::enumerate;
pub use pairwise::*;
use crate::{errors::PrimitiveError, sharing::Reconstructible, types::PeerIndex};
pub trait Verifiable: Reconstructible {
fn verify(&self, openings: Vec<Self::Opening>) -> Result<(), PrimitiveError>;
fn verify_from(
&self,
_opening: Self::Opening,
_peer_index: PeerIndex,
) -> Result<(), PrimitiveError> {
unimplemented!(
"Type {} does not implement verify_from",
std::any::type_name::<Self>()
)
}
fn verify_all(shares: Vec<Self>) -> Result<(), PrimitiveError> {
if shares.len() < 2 {
return Err(PrimitiveError::MinimumLength(2, shares.len()));
}
let mut all_openings = shares
.iter()
.map(|share| share.open_to_all_others())
.collect::<Vec<_>>();
enumerate(shares.iter()).try_for_each(|(i, share)| {
let my_openings = enumerate(all_openings.iter_mut())
.filter(|(j, _)| i != *j)
.map(|(_, opening)| opening.next())
.collect::<Option<Vec<_>>>()
.ok_or(PrimitiveError::InvalidPeerIndex(i, shares.len() - 1))?;
share.verify(my_openings)
})
}
}