Skip to main content

vsss_rs/
element.rs

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