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