Skip to main content

vsss_rs/
element.rs

1//! Share identifiers for secret sharing schemes.
2
3#[cfg(all(feature = "bigint", any(feature = "alloc", feature = "std")))]
4mod biguint;
5#[cfg(all(feature = "bigint", any(feature = "alloc", feature = "std")))]
6pub mod boxed_uint;
7#[cfg(feature = "curve")]
8mod group_element;
9#[cfg(feature = "bigint")]
10mod monty_residue;
11#[cfg(feature = "curve")]
12mod prime_field;
13#[cfg(feature = "primitive")]
14mod primitive;
15#[cfg(feature = "bigint")]
16mod residue;
17#[cfg(feature = "bigint")]
18pub mod uint;
19
20#[cfg(all(feature = "bigint", any(feature = "alloc", feature = "std")))]
21pub use biguint::*;
22#[cfg(all(feature = "bigint", any(feature = "alloc", feature = "std")))]
23pub use boxed_uint::*;
24#[cfg(feature = "curve")]
25pub use group_element::*;
26#[cfg(feature = "bigint")]
27pub use monty_residue::*;
28#[cfg(feature = "curve")]
29pub use prime_field::*;
30#[cfg(feature = "primitive")]
31pub use primitive::*;
32#[cfg(feature = "bigint")]
33pub use residue::*;
34
35use crate::*;
36
37use core::{
38    fmt::Debug,
39    ops::{Add, AddAssign, Deref, DerefMut, Mul, MulAssign, Neg, Sub, SubAssign},
40};
41use rand_core::CryptoRng;
42use subtle::Choice;
43
44/// A value used to represent a share element for secret shares.
45/// A share element can either be the share identifier or the share value.
46pub trait ShareElement:
47    Sized
48    + Clone
49    + Default
50    + Debug
51    + Eq
52    + PartialEq
53    + Deref<Target = Self::Inner>
54    + DerefMut<Target = Self::Inner>
55    + AsRef<Self::Inner>
56    + AsMut<Self::Inner>
57    + From<Self::Inner>
58{
59    /// The serialized form of the share element.
60    type Serialization: AsRef<[u8]> + AsMut<[u8]> + 'static;
61    /// The inner type of the share element.
62    type Inner: ShareElementInner;
63
64    /// Generate a random share element.
65    fn random(rng: impl CryptoRng) -> Self;
66    /// Defines an additive identity element for the share identifier.
67    fn zero() -> Self;
68    /// Defines a multiplicative identity element for the share identifier.
69    fn one() -> Self;
70    /// Check if the share identifier is zero.
71    fn is_zero(&self) -> Choice;
72    /// Serialize the share identifier.
73    fn serialize(&self) -> Self::Serialization;
74    /// Deserialize the share identifier.
75    fn deserialize(serialized: &Self::Serialization) -> VsssResult<Self>;
76    /// Attempt to convert the byte sequence to a share element.
77    fn from_slice(slice: &[u8]) -> VsssResult<Self>;
78    #[cfg(any(feature = "alloc", feature = "std"))]
79    /// Serialize the share identifier to a byte vector.
80    fn to_vec(&self) -> Vec<u8>;
81}
82
83/// A share identifier for secret sharing schemes.
84pub trait ShareIdentifier: ShareElement<Inner: ShareIdentifierInner> {
85    /// Add to the identifier by the increment value.
86    fn inc(&mut self, increment: &Self);
87
88    /// Invert the share identifier.
89    fn invert(&self) -> VsssResult<Self>;
90
91    /// Draw a polynomial coefficient uniformly at random over the
92    /// entire field, *including zero*.
93    ///
94    /// This exists because `ShareElement::random` is overloaded for
95    /// small-field identifiers (GF(256), GF(16)) to return only
96    /// non-zero values — non-zero is required for x-coordinates
97    /// (f(0) = secret) but biases polynomial coefficients away from
98    /// zero, which is exploitable in re-sharing scenarios.
99    ///
100    /// The default implementation delegates to `ShareElement::random`,
101    /// which for prime fields is already uniform over the whole field.
102    /// Small fields where `random` is constrained to non-zero must
103    /// override this to return a uniform sample over the full field.
104    fn random_coefficient(rng: impl CryptoRng) -> Self {
105        <Self as ShareElement>::random(rng)
106    }
107}
108
109/// Objects that represent the ability to verify Shamir shares.
110pub trait ShareVerifier<S: Share>:
111    ShareElement
112    + Copy
113    + Add<Output = Self>
114    + Sub<Output = Self>
115    + AddAssign
116    + SubAssign
117    + Neg<Output = Self>
118    + Mul<S::Identifier, Output = Self>
119    + Mul<S::Value, Output = Self>
120    + MulAssign<S::Identifier>
121    + MulAssign<S::Value>
122    + for<'a> Mul<&'a S::Identifier, Output = Self>
123    + for<'a> Mul<&'a S::Value, Output = Self>
124    + for<'a> MulAssign<&'a S::Identifier>
125    + for<'a> MulAssign<&'a S::Value>
126{
127}
128
129impl<
130    S: Share,
131    SV: ShareElement
132        + Copy
133        + Add<Output = Self>
134        + Sub<Output = Self>
135        + AddAssign
136        + SubAssign
137        + Neg<Output = Self>
138        + Mul<S::Identifier, Output = Self>
139        + Mul<S::Value, Output = Self>
140        + MulAssign<S::Identifier>
141        + MulAssign<S::Value>
142        + for<'a> Mul<&'a S::Identifier, Output = Self>
143        + for<'a> Mul<&'a S::Value, Output = Self>
144        + for<'a> MulAssign<&'a S::Identifier>
145        + for<'a> MulAssign<&'a S::Value>,
146> ShareVerifier<S> for SV
147{
148}
149
150/// A share element inner type for secret sharing schemes.
151pub trait ShareElementInner:
152    Sized
153    + Debug
154    + Eq
155    + PartialEq
156    + Clone
157    + Default
158    + 'static
159    + Add<Output = Self>
160    + Sub<Output = Self>
161    + AddAssign
162    + SubAssign
163    + for<'a> Add<&'a Self, Output = Self>
164    + for<'a> Sub<&'a Self, Output = Self>
165    + for<'a> AddAssign<&'a Self>
166    + for<'a> SubAssign<&'a Self>
167{
168}
169
170impl<
171    I: Sized
172        + Debug
173        + Eq
174        + PartialEq
175        + Clone
176        + Default
177        + 'static
178        + Add<Output = Self>
179        + Sub<Output = Self>
180        + AddAssign
181        + SubAssign
182        + for<'a> Add<&'a Self, Output = Self>
183        + for<'a> Sub<&'a Self, Output = Self>
184        + for<'a> AddAssign<&'a Self>
185        + for<'a> SubAssign<&'a Self>,
186> ShareElementInner for I
187{
188}
189
190/// A share identifier inner type for secret sharing schemes.
191pub trait ShareIdentifierInner:
192    ShareElementInner
193    + Mul<Output = Self>
194    + MulAssign
195    + for<'a> Mul<&'a Self, Output = Self>
196    + for<'a> MulAssign<&'a Self>
197{
198}
199
200impl<
201    E: ShareElementInner
202        + Mul<Output = Self>
203        + MulAssign
204        + for<'a> Mul<&'a Self, Output = Self>
205        + for<'a> MulAssign<&'a Self>,
206> ShareIdentifierInner for E
207{
208}