vsss_rs/element/
group_element.rs

1use crate::*;
2use core::{
3    fmt::{self, Display, Formatter},
4    ops::{Add, AddAssign, Deref, DerefMut, Mul, MulAssign, Neg, Sub, SubAssign},
5};
6#[cfg(feature = "bigint")]
7use crypto_bigint::{modular::constant_mod::ResidueParams, ArrayEncoding, Uint};
8#[cfg(feature = "bigint")]
9use elliptic_curve::ops::Reduce;
10use rand_core::{CryptoRng, RngCore};
11#[cfg(feature = "zeroize")]
12use zeroize::DefaultIsZeroes;
13
14/// A share verifier group element.
15pub type ShareVerifierGroup<G> = ValueGroup<G>;
16
17/// A share element represented as a group field element.
18#[derive(Debug, Copy, Clone, Default, Eq, PartialEq)]
19#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
20#[repr(transparent)]
21pub struct ValueGroup<G: Group + GroupEncoding + Default>(
22    #[cfg_attr(feature = "serde", serde(with = "elliptic_curve_tools::group"))] pub G,
23);
24
25impl<G: Group + GroupEncoding + Default> Display for ValueGroup<G> {
26    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
27        for &b in self.0.to_bytes().as_ref() {
28            write!(f, "{:02x}", b)?;
29        }
30        Ok(())
31    }
32}
33
34impl<G: Group + GroupEncoding + Default> Deref for ValueGroup<G> {
35    type Target = G;
36
37    fn deref(&self) -> &Self::Target {
38        &self.0
39    }
40}
41
42impl<G: Group + GroupEncoding + Default> DerefMut for ValueGroup<G> {
43    fn deref_mut(&mut self) -> &mut Self::Target {
44        &mut self.0
45    }
46}
47
48impl<G: Group + GroupEncoding + Default> AsRef<G> for ValueGroup<G> {
49    fn as_ref(&self) -> &G {
50        &self.0
51    }
52}
53
54impl<G: Group + GroupEncoding + Default> AsMut<G> for ValueGroup<G> {
55    fn as_mut(&mut self) -> &mut G {
56        &mut self.0
57    }
58}
59
60impl<G: Group + GroupEncoding + Default> From<G> for ValueGroup<G> {
61    fn from(value: G) -> Self {
62        Self(value)
63    }
64}
65
66impl<G: Group + GroupEncoding + Default> Add for ValueGroup<G> {
67    type Output = Self;
68
69    fn add(self, rhs: Self) -> Self::Output {
70        Self(self.0 + rhs.0)
71    }
72}
73
74impl<G: Group + GroupEncoding + Default> AddAssign for ValueGroup<G> {
75    fn add_assign(&mut self, rhs: Self) {
76        self.0 += rhs.0;
77    }
78}
79
80impl<G: Group + GroupEncoding + Default> Sub for ValueGroup<G> {
81    type Output = Self;
82
83    fn sub(self, rhs: Self) -> Self::Output {
84        Self(self.0 - rhs.0)
85    }
86}
87
88impl<G: Group + GroupEncoding + Default> SubAssign for ValueGroup<G> {
89    fn sub_assign(&mut self, rhs: Self) {
90        self.0 -= rhs.0;
91    }
92}
93
94impl<G: Group + GroupEncoding + Default> Neg for ValueGroup<G> {
95    type Output = Self;
96
97    fn neg(self) -> Self::Output {
98        Self(-self.0)
99    }
100}
101
102impl<G: Group + GroupEncoding + Default> ShareElement for ValueGroup<G> {
103    type Serialization = G::Repr;
104
105    type Inner = G;
106
107    fn random(rng: impl RngCore + CryptoRng) -> Self {
108        Self(G::random(rng))
109    }
110    fn zero() -> Self {
111        Self(<G as Group>::identity())
112    }
113
114    fn one() -> Self {
115        Self(<G as Group>::generator())
116    }
117
118    fn is_zero(&self) -> Choice {
119        G::is_identity(self)
120    }
121
122    fn serialize(&self) -> Self::Serialization {
123        self.to_bytes()
124    }
125
126    fn deserialize(serialized: &Self::Serialization) -> VsssResult<Self> {
127        Option::from(G::from_bytes(serialized))
128            .map(Self)
129            .ok_or(Error::InvalidShareElement)
130    }
131
132    fn from_slice(vec: &[u8]) -> VsssResult<Self> {
133        let mut repr = G::Repr::default();
134        if vec.len() != repr.as_ref().len() {
135            return Err(Error::InvalidShareElement);
136        }
137        repr.as_mut().copy_from_slice(vec);
138        Option::from(G::from_bytes(&repr))
139            .map(Self)
140            .ok_or(Error::InvalidShareElement)
141    }
142
143    #[cfg(any(feature = "alloc", feature = "std"))]
144    fn to_vec(&self) -> Vec<u8> {
145        self.to_bytes().as_ref().to_vec()
146    }
147}
148
149impl<G: Group + GroupEncoding + Default> Mul<IdentifierPrimeField<G::Scalar>> for ValueGroup<G> {
150    type Output = Self;
151
152    fn mul(self, rhs: IdentifierPrimeField<G::Scalar>) -> Self::Output {
153        Self(self.0 * rhs.0)
154    }
155}
156
157impl<G: Group + GroupEncoding + Default> Mul<&IdentifierPrimeField<G::Scalar>> for ValueGroup<G> {
158    type Output = Self;
159
160    fn mul(self, rhs: &IdentifierPrimeField<G::Scalar>) -> Self::Output {
161        Self(self.0 * rhs.0)
162    }
163}
164
165impl<G: Group + GroupEncoding + Default> Mul<IdentifierPrimeField<G::Scalar>> for &ValueGroup<G> {
166    type Output = ValueGroup<G>;
167
168    fn mul(self, rhs: IdentifierPrimeField<G::Scalar>) -> Self::Output {
169        ValueGroup(self.0 * rhs.0)
170    }
171}
172
173impl<G: Group + GroupEncoding + Default> Mul<&IdentifierPrimeField<G::Scalar>> for &ValueGroup<G> {
174    type Output = ValueGroup<G>;
175
176    fn mul(self, rhs: &IdentifierPrimeField<G::Scalar>) -> Self::Output {
177        ValueGroup(self.0 * rhs.0)
178    }
179}
180
181impl<G: Group + GroupEncoding + Default> MulAssign<IdentifierPrimeField<G::Scalar>>
182    for ValueGroup<G>
183{
184    fn mul_assign(&mut self, rhs: IdentifierPrimeField<G::Scalar>) {
185        self.0 *= rhs.0;
186    }
187}
188
189impl<G: Group + GroupEncoding + Default> MulAssign<&IdentifierPrimeField<G::Scalar>>
190    for ValueGroup<G>
191{
192    fn mul_assign(&mut self, rhs: &IdentifierPrimeField<G::Scalar>) {
193        self.0 *= rhs.0;
194    }
195}
196
197impl<G: Group + GroupEncoding + Default> From<&IdentifierPrimeField<G::Scalar>> for ValueGroup<G> {
198    fn from(id: &IdentifierPrimeField<G::Scalar>) -> Self {
199        Self(G::generator() * id.0)
200    }
201}
202
203#[cfg(feature = "primitive")]
204impl<G: Group + GroupEncoding + Default, P: Primitive<BYTES>, const BYTES: usize>
205    Mul<IdentifierPrimitive<P, BYTES>> for ValueGroup<G>
206{
207    type Output = Self;
208
209    fn mul(self, rhs: IdentifierPrimitive<P, BYTES>) -> Self::Output {
210        let id = IdentifierPrimeField::<G::Scalar>::from(&rhs);
211        Self(self.0 * id.0)
212    }
213}
214
215#[cfg(feature = "primitive")]
216impl<G: Group + GroupEncoding + Default, P: Primitive<BYTES>, const BYTES: usize>
217    Mul<&IdentifierPrimitive<P, BYTES>> for ValueGroup<G>
218{
219    type Output = Self;
220
221    fn mul(self, rhs: &IdentifierPrimitive<P, BYTES>) -> Self::Output {
222        let id = IdentifierPrimeField::<G::Scalar>::from(rhs);
223        Self(self.0 * id.0)
224    }
225}
226
227#[cfg(feature = "primitive")]
228impl<G: Group + GroupEncoding + Default, P: Primitive<BYTES>, const BYTES: usize>
229    Mul<IdentifierPrimitive<P, BYTES>> for &ValueGroup<G>
230{
231    type Output = ValueGroup<G>;
232
233    fn mul(self, rhs: IdentifierPrimitive<P, BYTES>) -> Self::Output {
234        let id = IdentifierPrimeField::<G::Scalar>::from(&rhs);
235        ValueGroup(self.0 * id.0)
236    }
237}
238
239#[cfg(feature = "primitive")]
240impl<G: Group + GroupEncoding + Default, P: Primitive<BYTES>, const BYTES: usize>
241    Mul<&IdentifierPrimitive<P, BYTES>> for &ValueGroup<G>
242{
243    type Output = ValueGroup<G>;
244
245    fn mul(self, rhs: &IdentifierPrimitive<P, BYTES>) -> Self::Output {
246        let id = IdentifierPrimeField::<G::Scalar>::from(rhs);
247        ValueGroup(self.0 * id.0)
248    }
249}
250
251#[cfg(feature = "primitive")]
252impl<G: Group + GroupEncoding + Default, P: Primitive<BYTES>, const BYTES: usize>
253    MulAssign<IdentifierPrimitive<P, BYTES>> for ValueGroup<G>
254{
255    fn mul_assign(&mut self, rhs: IdentifierPrimitive<P, BYTES>) {
256        let id = IdentifierPrimeField::<G::Scalar>::from(&rhs);
257        self.0 *= id.0;
258    }
259}
260
261#[cfg(feature = "primitive")]
262impl<G: Group + GroupEncoding + Default, P: Primitive<BYTES>, const BYTES: usize>
263    MulAssign<&IdentifierPrimitive<P, BYTES>> for ValueGroup<G>
264{
265    fn mul_assign(&mut self, rhs: &IdentifierPrimitive<P, BYTES>) {
266        let id = IdentifierPrimeField::<G::Scalar>::from(rhs);
267        self.0 *= id.0;
268    }
269}
270
271#[cfg(feature = "bigint")]
272impl<G: Group + GroupEncoding + Default, const LIMBS: usize> Mul<IdentifierUint<LIMBS>>
273    for ValueGroup<G>
274where
275    Uint<LIMBS>: ArrayEncoding,
276    G::Scalar: Reduce<Uint<LIMBS>>,
277{
278    type Output = Self;
279
280    fn mul(self, rhs: IdentifierUint<LIMBS>) -> Self::Output {
281        let id = IdentifierPrimeField::<G::Scalar>::from(&rhs);
282        Self(self.0 * id.0)
283    }
284}
285
286#[cfg(feature = "bigint")]
287impl<G: Group + GroupEncoding + Default, const LIMBS: usize> Mul<&IdentifierUint<LIMBS>>
288    for ValueGroup<G>
289where
290    Uint<LIMBS>: ArrayEncoding,
291    G::Scalar: Reduce<Uint<LIMBS>>,
292{
293    type Output = Self;
294
295    fn mul(self, rhs: &IdentifierUint<LIMBS>) -> Self::Output {
296        let id = IdentifierPrimeField::<G::Scalar>::from(rhs);
297        Self(self.0 * id.0)
298    }
299}
300
301#[cfg(feature = "bigint")]
302impl<G: Group + GroupEncoding + Default, const LIMBS: usize> Mul<IdentifierUint<LIMBS>>
303    for &ValueGroup<G>
304where
305    Uint<LIMBS>: ArrayEncoding,
306    G::Scalar: Reduce<Uint<LIMBS>>,
307{
308    type Output = ValueGroup<G>;
309
310    fn mul(self, rhs: IdentifierUint<LIMBS>) -> Self::Output {
311        let id = IdentifierPrimeField::<G::Scalar>::from(&rhs);
312        ValueGroup(self.0 * id.0)
313    }
314}
315
316#[cfg(feature = "bigint")]
317impl<G: Group + GroupEncoding + Default, const LIMBS: usize> Mul<&IdentifierUint<LIMBS>>
318    for &ValueGroup<G>
319where
320    Uint<LIMBS>: ArrayEncoding,
321    G::Scalar: Reduce<Uint<LIMBS>>,
322{
323    type Output = ValueGroup<G>;
324
325    fn mul(self, rhs: &IdentifierUint<LIMBS>) -> Self::Output {
326        let id = IdentifierPrimeField::<G::Scalar>::from(rhs);
327        ValueGroup(self.0 * id.0)
328    }
329}
330
331#[cfg(feature = "bigint")]
332impl<G: Group + GroupEncoding + Default, const LIMBS: usize> MulAssign<IdentifierUint<LIMBS>>
333    for ValueGroup<G>
334where
335    Uint<LIMBS>: ArrayEncoding,
336    G::Scalar: Reduce<Uint<LIMBS>>,
337{
338    fn mul_assign(&mut self, rhs: IdentifierUint<LIMBS>) {
339        let id = IdentifierPrimeField::<G::Scalar>::from(&rhs);
340        self.0 *= id.0;
341    }
342}
343
344#[cfg(feature = "bigint")]
345impl<G: Group + GroupEncoding + Default, const LIMBS: usize> MulAssign<&IdentifierUint<LIMBS>>
346    for ValueGroup<G>
347where
348    Uint<LIMBS>: ArrayEncoding,
349    G::Scalar: Reduce<Uint<LIMBS>>,
350{
351    fn mul_assign(&mut self, rhs: &IdentifierUint<LIMBS>) {
352        let id = IdentifierPrimeField::<G::Scalar>::from(rhs);
353        self.0 *= id.0;
354    }
355}
356
357#[cfg(feature = "bigint")]
358impl<G: Group + GroupEncoding + Default, MOD: ResidueParams<LIMBS>, const LIMBS: usize>
359    Mul<IdentifierResidue<MOD, LIMBS>> for ValueGroup<G>
360where
361    Uint<LIMBS>: ArrayEncoding,
362    G::Scalar: Reduce<Uint<LIMBS>>,
363{
364    type Output = Self;
365
366    fn mul(self, rhs: IdentifierResidue<MOD, LIMBS>) -> Self::Output {
367        let id = IdentifierPrimeField::<G::Scalar>::from(&rhs);
368        Self(self.0 * id.0)
369    }
370}
371
372#[cfg(feature = "bigint")]
373impl<G: Group + GroupEncoding + Default, MOD: ResidueParams<LIMBS>, const LIMBS: usize>
374    Mul<&IdentifierResidue<MOD, LIMBS>> for ValueGroup<G>
375where
376    Uint<LIMBS>: ArrayEncoding,
377    G::Scalar: Reduce<Uint<LIMBS>>,
378{
379    type Output = Self;
380
381    fn mul(self, rhs: &IdentifierResidue<MOD, LIMBS>) -> Self::Output {
382        let id = IdentifierPrimeField::<G::Scalar>::from(rhs);
383        Self(self.0 * id.0)
384    }
385}
386
387#[cfg(feature = "bigint")]
388impl<G: Group + GroupEncoding + Default, MOD: ResidueParams<LIMBS>, const LIMBS: usize>
389    Mul<IdentifierResidue<MOD, LIMBS>> for &ValueGroup<G>
390where
391    Uint<LIMBS>: ArrayEncoding,
392    G::Scalar: Reduce<Uint<LIMBS>>,
393{
394    type Output = ValueGroup<G>;
395
396    fn mul(self, rhs: IdentifierResidue<MOD, LIMBS>) -> Self::Output {
397        let id = IdentifierPrimeField::<G::Scalar>::from(&rhs);
398        ValueGroup(self.0 * id.0)
399    }
400}
401
402#[cfg(feature = "bigint")]
403impl<G: Group + GroupEncoding + Default, MOD: ResidueParams<LIMBS>, const LIMBS: usize>
404    Mul<&IdentifierResidue<MOD, LIMBS>> for &ValueGroup<G>
405where
406    Uint<LIMBS>: ArrayEncoding,
407    G::Scalar: Reduce<Uint<LIMBS>>,
408{
409    type Output = ValueGroup<G>;
410
411    fn mul(self, rhs: &IdentifierResidue<MOD, LIMBS>) -> Self::Output {
412        let id = IdentifierPrimeField::<G::Scalar>::from(rhs);
413        ValueGroup(self.0 * id.0)
414    }
415}
416
417#[cfg(feature = "bigint")]
418impl<G: Group + GroupEncoding + Default, MOD: ResidueParams<LIMBS>, const LIMBS: usize>
419    MulAssign<IdentifierResidue<MOD, LIMBS>> for ValueGroup<G>
420where
421    Uint<LIMBS>: ArrayEncoding,
422    G::Scalar: Reduce<Uint<LIMBS>>,
423{
424    fn mul_assign(&mut self, rhs: IdentifierResidue<MOD, LIMBS>) {
425        let id = IdentifierPrimeField::<G::Scalar>::from(&rhs);
426        self.0 *= id.0;
427    }
428}
429
430#[cfg(feature = "bigint")]
431impl<G: Group + GroupEncoding + Default, MOD: ResidueParams<LIMBS>, const LIMBS: usize>
432    MulAssign<&IdentifierResidue<MOD, LIMBS>> for ValueGroup<G>
433where
434    Uint<LIMBS>: ArrayEncoding,
435    G::Scalar: Reduce<Uint<LIMBS>>,
436{
437    fn mul_assign(&mut self, rhs: &IdentifierResidue<MOD, LIMBS>) {
438        let id = IdentifierPrimeField::<G::Scalar>::from(rhs);
439        self.0 *= id.0;
440    }
441}
442
443#[cfg(feature = "zeroize")]
444impl<G: Group + GroupEncoding + Default + DefaultIsZeroes> DefaultIsZeroes for ValueGroup<G> {}
445
446impl<G: Group + GroupEncoding + Default> ValueGroup<G> {
447    /// Create the additive identity element.
448    pub fn identity() -> Self {
449        Self(G::identity())
450    }
451
452    /// Create the multiplicative identity element.
453    pub fn generator() -> Self {
454        Self(G::generator())
455    }
456}