Skip to main content

primitives/sharing/authenticated/
mod.rs

1pub mod pairwise;
2
3use itertools::enumerate;
4pub use pairwise::*;
5use serde::{de::DeserializeOwned, Serialize};
6
7use crate::{errors::PrimitiveError, sharing::Reconstructible, types::PeerIndex};
8
9/// Verify secret share/s from one or more openings.
10pub trait Verifiable: Reconstructible {
11    /// Verify openings from all peers.
12    fn verify(&self, openings: Vec<Self::Opening>) -> Result<(), PrimitiveError>;
13
14    /// Verify an opening from a specific peer.
15    fn verify_from(
16        &self,
17        _opening: Self::Opening,
18        _peer_index: PeerIndex,
19    ) -> Result<(), PrimitiveError>;
20
21    /// Verify all shares by opening each share towards all other peers and performing
22    /// pairwise verification of the openings.
23    fn verify_all(shares: Vec<Self>) -> Result<(), PrimitiveError>;
24}
25
26/// Verify secret share/s from one or more openings, with additional associated data.
27pub trait VerifiableWith: Reconstructible {
28    /// The associated data that is used to verify this secret shared type.
29    type VerificationData: Send + Sync + Clone + Eq + Serialize + DeserializeOwned;
30
31    /// Verify the correlated randomness from all peers given your share and all other peers'
32    /// openings.
33    fn verify_with(
34        &self,
35        openings: Vec<Self::Opening>,
36        verification_data: Self::VerificationData,
37    ) -> Result<(), PrimitiveError>;
38
39    /// Verify an opening from a specific peer given your share and the opening.
40    fn verify_from_peer_with(
41        &self,
42        _opening: Self::Opening,
43        _peer_index: PeerIndex,
44        _verification_data: Self::VerificationData,
45    ) -> Result<(), PrimitiveError> {
46        unimplemented!(
47            "Type {} does not implement verify_from",
48            std::any::type_name::<Self>()
49        )
50    }
51
52    /// Verify all shares by opening each share towards all other peers and performing
53    /// pairwise verification of the openings.
54    fn verify_all_with(
55        shares: Vec<Self>,
56        verification_data: Self::VerificationData,
57    ) -> Result<(), PrimitiveError> {
58        if shares.len() < 2 {
59            return Err(PrimitiveError::MinimumLength(2, shares.len()));
60        }
61        // Open each share to all other peers.
62        let mut all_openings = shares
63            .iter()
64            .map(|share| share.open_to_all_others())
65            .collect::<Vec<_>>();
66        // Use each share to verify the openings of all other peers.
67        enumerate(shares.iter()).try_for_each(|(i, share)| {
68            let my_openings = enumerate(all_openings.iter_mut())
69                .filter(|(j, _)| i != *j)
70                .map(|(_, opening)| opening.next())
71                .collect::<Option<Vec<_>>>()
72                .ok_or(PrimitiveError::InvalidPeerIndex(i, shares.len() - 1))?;
73            share.verify_with(my_openings, verification_data.clone())
74        })
75    }
76}
77
78impl<T: VerifiableWith<VerificationData = ()>> Verifiable for T {
79    fn verify(&self, openings: Vec<Self::Opening>) -> Result<(), PrimitiveError> {
80        self.verify_with(openings, ())
81    }
82
83    fn verify_from(
84        &self,
85        opening: Self::Opening,
86        peer_index: PeerIndex,
87    ) -> Result<(), PrimitiveError> {
88        self.verify_from_peer_with(opening, peer_index, ())
89    }
90
91    fn verify_all(shares: Vec<Self>) -> Result<(), PrimitiveError> {
92        Self::verify_all_with(shares, ())
93    }
94}