1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
use std::{cell::RefCell, cmp::Ordering, rc::Rc};

use beaver::SharedValueSource;
use curve25519_dalek::scalar::Scalar;

use network::MpcNetwork;

pub mod authenticated_ristretto;
pub mod authenticated_scalar;
pub mod beaver;
pub mod commitment;
pub mod error;
pub mod fabric;
mod macros;
pub mod mpc_ristretto;
pub mod mpc_scalar;
pub mod network;

/// SharedNetwork wraps a network implementation in a borrow-safe container
/// while providing interior mutability
#[allow(type_alias_bounds)]
pub type SharedNetwork<N: MpcNetwork + Send> = Rc<RefCell<N>>;
#[allow(type_alias_bounds)]
pub type BeaverSource<S: SharedValueSource<Scalar>> = Rc<RefCell<S>>;

/// A wrapper trait that allows for implementing generic comparisons
pub trait Visible {
    fn visibility(&self) -> Visibility;
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
/// Visibility determines what information peers have for values allocated
/// in the network.
pub enum Visibility {
    /// The below are in increasing order of visibility
    /// A value that only one party holds, can be *shared* into Shared
    /// or *opened* into Public
    Private,
    /// Shared in which neither party knows the underlying value
    /// Can be *opened* into Public
    Shared,
    /// Public, both parties know the value
    Public,
}

/// Convenience methods for comparing visibilities on various types
impl Visibility {
    /// Returns the minimum visibility between two scalars
    pub(crate) fn min_visibility_two(a: &impl Visible, b: &impl Visible) -> Visibility {
        if a.visibility().lt(&b.visibility()) {
            a.visibility()
        } else {
            b.visibility()
        }
    }
}

/// An implementation of Ord for Visibilities
/// Note that when two items are SharedWithOwner, but have different owners
/// they are said to be equal; we let the caller handle differences
impl Ord for Visibility {
    fn cmp(&self, other: &Self) -> Ordering {
        match self {
            Visibility::Private => match other {
                Visibility::Private => Ordering::Equal,
                _ => Ordering::Less,
            },
            Visibility::Shared => match other {
                Visibility::Private => Ordering::Greater,
                Visibility::Shared => Ordering::Equal,
                _ => Ordering::Less,
            },
            Visibility::Public => match other {
                Visibility::Public => Ordering::Equal,
                _ => Ordering::Greater,
            },
        }
    }
}

impl PartialOrd for Visibility {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}