Skip to main content

primitives/sharing/authenticated/
mod.rs

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