Skip to main content

primitives/sharing/
mod.rs

1// Secret sharing schemes.
2pub mod authenticated;
3pub mod reconstructible;
4pub mod threat_model;
5pub mod unauthenticated;
6
7use std::{
8    fmt::Debug,
9    ops::{Add, Neg, Sub},
10};
11
12pub use authenticated::*;
13pub use reconstructible::Reconstructible;
14use serde::{de::DeserializeOwned, Serialize};
15pub use threat_model::*;
16pub use unauthenticated::*;
17use wincode::{SchemaRead, SchemaWrite};
18
19/// Secret share bounds, allowing arithmetic operations and reconstruction. Sharing/inputting a
20/// secret might requre communication and is thus deferred to the online phase.
21pub trait SecretShare:
22    Reconstructible
23    + PlaintextOps<<Self as Reconstructible>::Value>
24    + Clone
25    + Debug
26    + Send
27    + Sync
28    + 'static
29    + PartialEq
30    + Serialize
31    + DeserializeOwned
32    + SchemaWrite<Src = Self>
33    + for<'de> SchemaRead<'de, Dst = Self>
34    + for<'a> Add<&'a Self, Output = Self>
35    + for<'s> Sub<&'s Self, Output = Self>
36    + Neg<Output = Self>
37{
38}
39
40impl<T> SecretShare for T where
41    T: Reconstructible
42        + PlaintextOps<<Self as Reconstructible>::Value>
43        + Clone
44        + Debug
45        + Send
46        + Sync
47        + 'static
48        + PartialEq
49        + Serialize
50        + DeserializeOwned
51        + SchemaWrite<Src = Self>
52        + for<'de> SchemaRead<'de, Dst = Self>
53        + for<'a> Add<&'a Self, Output = Self>
54        + for<'a> Sub<&'a Self, Output = Self>
55        + Neg<Output = Self>
56{
57}
58
59/// Operates a secret share with a public plaintext (addition/subtraction). These are often
60/// asymmetric (e.g., only the first peer folds the constant into its share). Scaling a share by a
61/// public constant is expressed directly through `Mul` where needed, so it is not part of this
62/// trait.
63pub trait PlaintextOps<Plaintext = <Self as Reconstructible>::Value>: Reconstructible {
64    /// Add a plaintext to the share, consuming the share and returning a new one.
65    fn add_plaintext(self, ptx: &Plaintext, is_first_peer: bool) -> Self;
66
67    /// Subtract a plaintext from the share, consuming the share and returning a new one.
68    fn sub_plaintext(self, ptx: &Plaintext, is_first_peer: bool) -> Self;
69}