arcium-primitives 0.4.1

Arcium primitives
Documentation
pub mod batched;
pub mod curve_key;
pub mod curve_share;
pub mod field_key;
pub mod field_share;
pub mod scalar_key;
pub mod scalar_share;

pub use batched::*;
pub use curve_key::*;
pub use curve_share::*;
pub use field_key::*;
pub use field_share::*;
use itertools::enumerate;
pub use scalar_key::*;
pub use scalar_share::*;
use serde::{de::DeserializeOwned, Serialize};

use crate::{
    algebra::field::mersenne::Mersenne107,
    errors::PrimitiveError,
    sharing::Reconstructible,
    types::PeerIndex,
};

pub type Mersenne107Share = FieldShare<Mersenne107>;

/// Number of parties
type NParties = usize;

/// Indicates whether the local peer is the first peer based on an agreed-upon ordering.
pub type IsFirstPeer = bool;

/// Verify secret share/s from one or more openings.
pub trait Verifiable: Reconstructible {
    /// Verify openings from all peers.
    fn verify(&self, openings: &[Self::Opening]) -> Result<(), PrimitiveError>;

    /// Verify an opening from a specific peer.
    fn verify_from(
        &self,
        _opening: &Self::Opening,
        _peer_index: PeerIndex,
    ) -> Result<(), PrimitiveError>;

    /// Verify all shares by opening each share towards all other peers and performing
    /// pairwise verification of the openings.
    fn verify_all(shares: &[Self]) -> Result<(), PrimitiveError>;
}

/// Verify secret share/s from one or more openings, with additional associated data.
pub trait VerifiableWith: Reconstructible {
    /// The associated data that is used to verify this secret shared type.
    type VerificationData: Send + Sync + Clone + Eq + Serialize + DeserializeOwned;

    /// Verify the correlated randomness from all peers given your share and all other peers'
    /// openings.
    fn verify_with(
        &self,
        openings: &[Self::Opening],
        verification_data: Self::VerificationData,
    ) -> Result<(), PrimitiveError>;

    /// Verify an opening from a specific peer given your share and the opening.
    fn verify_from_peer_with(
        &self,
        _opening: &Self::Opening,
        _peer_index: PeerIndex,
        _verification_data: Self::VerificationData,
    ) -> 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_with(
        shares: &[Self],
        verification_data: Self::VerificationData,
    ) -> 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_with(my_openings.as_slice(), verification_data.clone())
        })
    }
}

impl<T: VerifiableWith<VerificationData = ()>> Verifiable for T {
    fn verify(&self, openings: &[Self::Opening]) -> Result<(), PrimitiveError> {
        self.verify_with(openings, ())
    }

    fn verify_from(
        &self,
        opening: &Self::Opening,
        peer_index: PeerIndex,
    ) -> Result<(), PrimitiveError> {
        self.verify_from_peer_with(opening, peer_index, ())
    }

    fn verify_all(shares: &[Self]) -> Result<(), PrimitiveError> {
        Self::verify_all_with(shares, ())
    }
}