Skip to main content

primitives/sharing/authenticated/
mod.rs

1pub mod pairwise;
2
3use itertools::enumerate;
4pub use pairwise::*;
5
6use crate::{errors::PrimitiveError, sharing::Reconstructible, types::PeerIndex};
7
8/// Verify secret share/s from one or more openings.
9pub trait Verifiable: Reconstructible {
10    /// Verify the shares from all peers given your share and all other peers' openings.
11    fn verify(&self, openings: Vec<Self::Opening>) -> Result<(), PrimitiveError>;
12
13    /// Verify an opening from a specific peer given your share and the opening.
14    /// Only applicable to publicly verifiable or pairwise authenticated shares.
15    fn verify_from(
16        &self,
17        _opening: Self::Opening,
18        _peer_index: PeerIndex,
19    ) -> Result<(), PrimitiveError> {
20        unimplemented!(
21            "Type {} does not implement verify_from",
22            std::any::type_name::<Self>()
23        )
24    }
25
26    /// Verify all shares by opening each share towards all other peers and performing
27    /// pairwise verification of the openings.
28    fn verify_all(shares: Vec<Self>) -> Result<(), PrimitiveError> {
29        if shares.len() < 2 {
30            return Err(PrimitiveError::MinimumLength(2, shares.len()));
31        }
32        // Open each share to all other peers.
33        let mut all_openings = shares
34            .iter()
35            .map(|share| share.open_to_all_others())
36            .collect::<Vec<_>>();
37        // Use each share to verify the openings of all other peers.
38        enumerate(shares.iter()).try_for_each(|(i, share)| {
39            let my_openings = enumerate(all_openings.iter_mut())
40                .filter(|(j, _)| i != *j)
41                .map(|(_, opening)| opening.next())
42                .collect::<Option<Vec<_>>>()
43                .ok_or(PrimitiveError::InvalidPeerIndex(i, shares.len() - 1))?;
44            share.verify(my_openings)
45        })
46    }
47}