mpc_stark/
lib.rs

1#![deny(unsafe_code)]
2#![deny(missing_docs)]
3#![deny(clippy::missing_docs_in_private_items)]
4#![allow(incomplete_features)]
5#![feature(inherent_associated_types)]
6
7//! Defines an MPC implementation over the Stark curve that allows for out-of-order execution of
8//! the underlying MPC circuit
9
10use std::{
11    cell::RefCell,
12    rc::Rc,
13    sync::{Arc, RwLock},
14};
15
16use algebra::{scalar::Scalar, stark_curve::StarkPoint};
17use beaver::SharedValueSource;
18
19use network::MpcNetwork;
20use rand::thread_rng;
21
22pub mod algebra;
23pub mod beaver;
24#[cfg(feature = "benchmarks")]
25pub mod buffer;
26#[cfg(not(feature = "benchmarks"))]
27pub(crate) mod buffer;
28pub mod commitment;
29pub mod error;
30mod fabric;
31#[cfg(feature = "benchmarks")]
32pub use fabric::*;
33#[cfg(not(feature = "benchmarks"))]
34pub use fabric::{FabricInner, MpcFabric, ResultHandle, ResultId, ResultValue};
35pub mod network;
36
37// -------------
38// | Constants |
39// -------------
40
41/// The first party
42pub const PARTY0: u64 = 0;
43/// The second party
44pub const PARTY1: u64 = 1;
45
46/// Generate a random curve point by multiplying a random scalar with the
47/// Stark curve group generator
48pub fn random_point() -> StarkPoint {
49    let mut rng = thread_rng();
50    StarkPoint::generator() * Scalar::random(&mut rng)
51}
52
53// --------------------
54// | Crate-wide Types |
55// --------------------
56
57/// A type alias for a shared locked value
58type Shared<T> = Arc<RwLock<T>>;
59
60/// SharedNetwork wraps a network implementation in a borrow-safe container
61/// while providing interior mutability
62#[allow(type_alias_bounds)]
63pub type SharedNetwork<N: MpcNetwork + Send> = Rc<RefCell<N>>;
64/// A type alias for a shared, mutable reference to an underlying beaver source
65#[allow(type_alias_bounds)]
66pub type BeaverSource<S: SharedValueSource> = Rc<RefCell<S>>;
67
68#[cfg(any(test, feature = "test_helpers"))]
69pub mod test_helpers {
70    //! Defines test helpers for use in unit and integration tests, as well as benchmarks
71    use futures::Future;
72
73    use crate::{
74        beaver::PartyIDBeaverSource,
75        network::{MockNetwork, NoRecvNetwork, UnboundedDuplexStream},
76        MpcFabric, PARTY0, PARTY1,
77    };
78
79    /// Create a mock fabric
80    pub fn mock_fabric() -> MpcFabric {
81        let network = NoRecvNetwork::default();
82        let beaver_source = PartyIDBeaverSource::default();
83
84        MpcFabric::new(network, beaver_source)
85    }
86
87    /// Run a mock MPC connected by a duplex stream as the mock network
88    ///
89    /// This will spawn two tasks to execute either side of the MPC
90    ///
91    /// Returns the outputs of both parties
92    pub async fn execute_mock_mpc<T, S, F>(mut f: F) -> (T, T)
93    where
94        T: Send + 'static,
95        S: Future<Output = T> + Send + 'static,
96        F: FnMut(MpcFabric) -> S,
97    {
98        // Build a duplex stream to broker communication between the two parties
99        let (party0_stream, party1_stream) = UnboundedDuplexStream::new_duplex_pair();
100        let party0_fabric = MpcFabric::new(
101            MockNetwork::new(PARTY0, party0_stream),
102            PartyIDBeaverSource::new(PARTY0),
103        );
104        let party1_fabric = MpcFabric::new(
105            MockNetwork::new(PARTY1, party1_stream),
106            PartyIDBeaverSource::new(PARTY1),
107        );
108
109        // Spawn two tasks to execute the MPC
110        let fabric0 = party0_fabric.clone();
111        let fabric1 = party1_fabric.clone();
112        let party0_task = tokio::spawn(f(fabric0));
113        let party1_task = tokio::spawn(f(fabric1));
114
115        let party0_output = party0_task.await.unwrap();
116        let party1_output = party1_task.await.unwrap();
117
118        // Shutdown the fabrics
119        party0_fabric.shutdown();
120        party1_fabric.shutdown();
121
122        (party0_output, party1_output)
123    }
124}