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::InvalidParameters(
85                "At least two shares are required for verification.".to_string(),
86            ));
87        }
88        // Open each share to all other peers.
89        let mut all_openings = shares
90            .iter()
91            .map(|share| share.open_to_all_others())
92            .collect::<Vec<_>>();
93        // Use each share to verify the openings of all other peers.
94        enumerate(shares.iter()).try_for_each(|(i, share)| {
95            let my_openings = enumerate(all_openings.iter_mut())
96                .filter(|(j, _)| i != *j)
97                .map(|(_, opening)| opening.next())
98                .collect::<Option<Vec<_>>>()
99                .ok_or(PrimitiveError::WrongPeerIndex(i, shares.len() - 1))?;
100            share.verify_with(my_openings.as_slice(), verification_data.clone())
101        })
102    }
103}
104
105impl<T: VerifiableWith<VerificationData = ()>> Verifiable for T {
106    fn verify(&self, openings: &[Self::Opening]) -> Result<(), PrimitiveError> {
107        self.verify_with(openings, ())
108    }
109
110    fn verify_from(
111        &self,
112        opening: &Self::Opening,
113        peer_index: PeerIndex,
114    ) -> Result<(), PrimitiveError> {
115        self.verify_from_peer_with(opening, peer_index, ())
116    }
117
118    fn verify_all(shares: &[Self]) -> Result<(), PrimitiveError> {
119        Self::verify_all_with(shares, ())
120    }
121}