Skip to main content

core_utils/preprocessing/
mod.rs

1use std::fmt::Debug;
2
3use primitives::{
4    algebra::elliptic_curve::Curve,
5    sharing::{Reconstructible, VerifiableWith},
6};
7use serde::{de::DeserializeOwned, Serialize};
8use wincode::{SchemaRead, SchemaWrite};
9
10use crate::{
11    circuit::Circuit,
12    preprocessing::{errors::PreprocessingBundlerError, iterator::CerberusPreprocessingIterator},
13};
14
15#[cfg(any(test, feature = "dev"))]
16pub mod dealer;
17pub mod errors;
18pub mod iterator;
19
20/// A marker trait for preprocessing types that can be served in the online phase, maybe requiring
21/// some associated data to be generated/requested/verified. Includes verification and random
22/// generation capabilities.
23pub trait Preprocessing:
24    Debug
25    + Sized
26    + Send
27    + Sync
28    + Clone
29    + PartialEq
30    + Serialize
31    + DeserializeOwned
32    + SchemaWrite<Src = Self>
33    + for<'a> SchemaRead<'a>
34    + Reconstructible
35    + VerifiableWith<VerificationData = <Self as Preprocessing>::AssociatedData>
36{
37    /// The associated data that is used to generate/request/verify this preprocessing type. E.g.,
38    /// an exponent for `PowPair`. Can be empty if no additional data is needed.
39    type AssociatedData: Send
40        + Sync
41        + Clone
42        + PartialEq
43        + Serialize
44        + DeserializeOwned
45        + SchemaWrite<Src = Self::AssociatedData>
46        + for<'a> SchemaRead<'a, Dst = Self::AssociatedData>;
47}
48
49impl<
50        Data: Send
51            + Sync
52            + Clone
53            + PartialEq
54            + Serialize
55            + DeserializeOwned
56            + SchemaWrite<Src = Data>
57            + for<'a> SchemaRead<'a, Dst = Data>,
58        T: Debug
59            + Sized
60            + Send
61            + Sync
62            + Clone
63            + PartialEq
64            + Serialize
65            + DeserializeOwned
66            + VerifiableWith<VerificationData = Data>
67            + for<'a> wincode::SchemaRead<'a, Dst = T>
68            + wincode::SchemaWrite<Src = T>,
69    > Preprocessing for T
70{
71    type AssociatedData = Data;
72}
73
74/// Bundles all preprocessing sources for an MPC protocol session.
75///
76/// Implementations are responsible for fetching all preprocessing required for a circuit
77/// concurrently and packaging the results into a [`CerberusPreprocessingIterator`]. Because
78/// each implementation owns its sources as disjoint struct fields, it can issue all network
79/// requests in a single `try_join!` and return after a single `await`, rather than making
80/// one sequential round-trip per source type.
81#[allow(async_fn_in_trait)]
82pub trait PreprocessingBundler<C: Curve>: Send {
83    /// Fetches all preprocessing required by `circuit` and returns a ready-to-use iterator.
84    ///
85    /// Implementations should issue all source requests concurrently (e.g. via
86    /// `tokio::try_join!`) so that this resolves after a single network barrier rather
87    /// than N sequential ones.
88    async fn fetch_for(
89        &mut self,
90        circuit: &Circuit<C>,
91    ) -> Result<CerberusPreprocessingIterator<C>, PreprocessingBundlerError>;
92}