arcium-primitives 0.6.0

Arcium primitives
Documentation
pub mod pairwise;

use itertools::enumerate;
pub use pairwise::*;

use crate::{errors::PrimitiveError, sharing::Reconstructible, types::PeerIndex};

/// Verify secret share/s from one or more openings.
pub trait Verifiable: Reconstructible {
    /// Verify the shares from all peers given your share and all other peers' openings.
    fn verify(&self, openings: Vec<Self::Opening>) -> Result<(), PrimitiveError>;

    /// Verify an opening from a specific peer given your share and the opening.
    /// Only applicable to publicly verifiable or pairwise authenticated shares.
    fn verify_from(
        &self,
        _opening: Self::Opening,
        _peer_index: PeerIndex,
    ) -> Result<(), PrimitiveError> {
        unimplemented!(
            "Type {} does not implement verify_from",
            std::any::type_name::<Self>()
        )
    }

    /// Verify all shares by opening each share towards all other peers and performing
    /// pairwise verification of the openings.
    fn verify_all(shares: Vec<Self>) -> Result<(), PrimitiveError> {
        if shares.len() < 2 {
            return Err(PrimitiveError::MinimumLength(2, shares.len()));
        }
        // Open each share to all other peers.
        let mut all_openings = shares
            .iter()
            .map(|share| share.open_to_all_others())
            .collect::<Vec<_>>();
        // Use each share to verify the openings of all other peers.
        enumerate(shares.iter()).try_for_each(|(i, share)| {
            let my_openings = enumerate(all_openings.iter_mut())
                .filter(|(j, _)| i != *j)
                .map(|(_, opening)| opening.next())
                .collect::<Option<Vec<_>>>()
                .ok_or(PrimitiveError::InvalidPeerIndex(i, shares.len() - 1))?;
            share.verify(my_openings)
        })
    }
}