Skip to main content

primitives/sharing/authenticated/batched/
scalar_shares.rs

1use std::ops::Mul;
2
3use crate::{
4    algebra::elliptic_curve::{BaseField, Curve, Point, ScalarField},
5    sharing::{FieldShares, OpenFieldShares, PointShares},
6    types::{heap_array::curve_arrays::CurvePoints, Positive},
7};
8
9// === Scalar shares === //
10
11pub type OpenScalarShares<C, M> = OpenFieldShares<ScalarField<C>, M>;
12pub type OpenBaseFieldShares<C, M> = OpenFieldShares<BaseField<C>, M>;
13
14pub type ScalarShares<C, M> = FieldShares<ScalarField<C>, M>;
15pub type BaseFieldShares<C, M> = FieldShares<BaseField<C>, M>;
16
17// === CurvePoint(s) x ScalarShares === //
18
19#[macros::op_variants(owned, borrowed, flipped)]
20impl<'a, C: Curve, M: Positive> Mul<&'a CurvePoints<C, M>> for ScalarShares<C, M> {
21    type Output = PointShares<C, M>;
22
23    #[inline]
24    fn mul(self, other: &'a CurvePoints<C, M>) -> Self::Output {
25        PointShares {
26            value: self.value * other,
27            macs: self.macs.iter().map(|mac| mac * other).collect(),
28            keys: self.keys.iter().map(|key| key * other).collect(),
29        }
30    }
31}
32
33// --- One-to-many multiplication --- //
34
35#[macros::op_variants(owned, borrowed, flipped)]
36impl<'a, C: Curve, M: Positive> Mul<&'a Point<C>> for ScalarShares<C, M> {
37    type Output = PointShares<C, M>;
38
39    #[inline]
40    fn mul(self, other: &'a Point<C>) -> Self::Output {
41        PointShares {
42            value: self.value * other,
43            macs: self.macs.iter().map(|mac| mac * other).collect(),
44            keys: self.keys.iter().map(|key| key * other).collect(),
45        }
46    }
47}
48
49// --- ScalarShares x CurvePoint(s) --- //
50
51#[macros::op_variants(owned, borrowed, flipped)]
52impl<'a, C: Curve, M: Positive> Mul<&'a ScalarShares<C, M>> for CurvePoints<C, M> {
53    type Output = PointShares<C, M>;
54
55    #[inline]
56    fn mul(self, other: &'a ScalarShares<C, M>) -> Self::Output {
57        PointShares {
58            value: &other.value * &self,
59            macs: other.macs.iter().map(|mac| mac * &self).collect(),
60            keys: other.keys.iter().map(|key| key * &self).collect(),
61        }
62    }
63}
64
65// --- One-to-many multiplication --- //
66
67#[macros::op_variants(owned, borrowed, flipped)]
68impl<'a, C: Curve, M: Positive> Mul<&'a ScalarShares<C, M>> for Point<C> {
69    type Output = PointShares<C, M>;
70
71    #[inline]
72    fn mul(self, other: &'a ScalarShares<C, M>) -> Self::Output {
73        PointShares {
74            value: &other.value * self,
75            macs: other.macs.iter().map(|mac| mac * self).collect(),
76            keys: other.keys.iter().map(|key| key * self).collect(),
77        }
78    }
79}