primitives/sharing/authenticated/
scalar_share.rs

1use std::ops::Mul;
2
3use super::{FieldShare, OpenFieldShare, PointShare};
4use crate::algebra::{
5    elliptic_curve::{BaseField, Curve, Point, ScalarField},
6    field::binary::Gf2_128,
7};
8
9// === Scalar shares === //
10
11pub type OpenScalarShare<C> = OpenFieldShare<ScalarField<C>>;
12pub type OpenBaseFieldShare<C> = OpenFieldShare<BaseField<C>>;
13pub type OpenBitShare = OpenFieldShare<Gf2_128>;
14
15pub type ScalarShare<C> = FieldShare<ScalarField<C>>;
16pub type BaseFieldShare<C> = FieldShare<BaseField<C>>;
17pub type BitShare = FieldShare<Gf2_128>;
18
19// === Curve point multiplication === //
20
21impl<C: Curve> Mul<Point<C>> for ScalarShare<C> {
22    type Output = PointShare<C>;
23
24    #[inline]
25    fn mul(self, other: Point<C>) -> PointShare<C> {
26        PointShare {
27            value: self.value * other,
28            macs: self.macs.iter().map(|mac| mac * other).collect(),
29            keys: self.keys.iter().map(|key| key * other).collect(),
30        }
31    }
32}
33
34impl<'a, C: Curve> Mul<&'a Point<C>> for &'a ScalarShare<C> {
35    type Output = PointShare<C>;
36
37    #[inline]
38    fn mul(self, other: &'a Point<C>) -> PointShare<C> {
39        PointShare {
40            value: self.value * other,
41            keys: self.keys.iter().map(|key| key * other).collect(),
42            macs: self.macs.iter().map(|mac| mac * other).collect(),
43        }
44    }
45}
46
47impl<'a, C: Curve> Mul<&'a Point<C>> for ScalarShare<C> {
48    type Output = PointShare<C>;
49
50    #[inline]
51    fn mul(self, other: &'a Point<C>) -> PointShare<C> {
52        PointShare {
53            value: self.value * other,
54            keys: self.keys.iter().map(|key| key * other).collect(),
55            macs: self.macs.iter().map(|mac| mac * other).collect(),
56        }
57    }
58}
59
60impl<C: Curve> Mul<Point<C>> for &ScalarShare<C> {
61    type Output = PointShare<C>;
62
63    #[inline]
64    fn mul(self, other: Point<C>) -> PointShare<C> {
65        PointShare {
66            value: self.value * other,
67            keys: self.keys.iter().map(|key| key * other).collect(),
68            macs: self.macs.iter().map(|mac| mac * other).collect(),
69        }
70    }
71}
72
73impl<C: Curve> Mul<ScalarShare<C>> for Point<C> {
74    type Output = PointShare<C>;
75
76    #[inline]
77    fn mul(self, other: ScalarShare<C>) -> PointShare<C> {
78        PointShare {
79            value: other.value * self,
80            macs: other.macs.iter().map(|mac| mac * self).collect(),
81            keys: other.keys.iter().map(|key| key * self).collect(),
82        }
83    }
84}
85
86impl<'a, C: Curve> Mul<&'a ScalarShare<C>> for &'a Point<C> {
87    type Output = PointShare<C>;
88
89    #[inline]
90    fn mul(self, other: &'a ScalarShare<C>) -> PointShare<C> {
91        PointShare {
92            value: other.value * self,
93            macs: other.macs.iter().map(|mac| mac * self).collect(),
94            keys: other.keys.iter().map(|key| key * self).collect(),
95        }
96    }
97}
98
99impl<'a, C: Curve> Mul<&'a ScalarShare<C>> for Point<C> {
100    type Output = PointShare<C>;
101
102    #[inline]
103    fn mul(self, other: &'a ScalarShare<C>) -> PointShare<C> {
104        PointShare {
105            value: other.value * self,
106            macs: other.macs.iter().map(|mac| mac * self).collect(),
107            keys: other.keys.iter().map(|key| key * self).collect(),
108        }
109    }
110}
111
112impl<C: Curve> Mul<ScalarShare<C>> for &Point<C> {
113    type Output = PointShare<C>;
114
115    #[inline]
116    fn mul(self, other: ScalarShare<C>) -> PointShare<C> {
117        PointShare {
118            value: other.value * self,
119            macs: other.macs.iter().map(|mac| mac * self).collect(),
120            keys: other.keys.iter().map(|key| key * self).collect(),
121        }
122    }
123}