qudoku/
sharing.rs

1use crate::{sha256, Evaluation, LagrangePolynomial, Polynomial, StandardFormPolynomial};
2use secp::{MaybePoint, MaybeScalar};
3
4/// Represents a secret share held by a shareholder.
5pub type SecretShare = Evaluation<MaybeScalar, MaybeScalar>;
6
7/// Represents a point share intended for distribution.
8/// Derived by multiplying a secret share with a fixed point `Q`.
9pub type PointShare = Evaluation<MaybeScalar, MaybePoint>;
10
11/// Represents the secret-sharing polynomial available to the dealer in its
12/// original standard form, composed of a set of scalar coefficients.
13pub type SecretSharingPolynomial = StandardFormPolynomial<MaybeScalar>;
14
15/// Represents the point-sharing polynomial available to the dealer in standard
16/// form, composed of a set of point coefficients.
17pub type PointSharingPolynomial = StandardFormPolynomial<MaybePoint>;
18
19/// Represents a secret-sharing polynomial interpolated from a set of shares.
20pub type InterpolatedSecretPolynomial = LagrangePolynomial<MaybeScalar, MaybeScalar>;
21
22/// Represents a point-sharing polynomial interpolated from a set of shares.
23pub type InterpolatedPointPolynomial = LagrangePolynomial<MaybeScalar, MaybePoint>;
24
25macro_rules! impl_issue_share {
26    ( $t:ty, $share:ty ) => {
27        impl $t {
28            /// Issue a share at the given input `x`.
29            pub fn issue_share(&self, x: MaybeScalar) -> $share {
30                Evaluation {
31                    input: x,
32                    output: self.evaluate(x),
33                }
34            }
35        }
36    };
37}
38
39impl_issue_share! { SecretSharingPolynomial, SecretShare }
40impl_issue_share! { PointSharingPolynomial, PointShare }
41impl_issue_share! { InterpolatedSecretPolynomial, SecretShare }
42impl_issue_share! { InterpolatedPointPolynomial, PointShare }
43
44macro_rules! impl_derive_secret {
45    ( $t:ty ) => {
46        impl $t {
47            /// Derive a secret `c` by hashing the output point produced by
48            /// evaluating the polynomial on `x`.
49            pub fn derive_secret(&self, x: MaybeScalar) -> [u8; 32] {
50                sha256(&self.evaluate(x).serialize())
51            }
52        }
53    };
54}
55
56impl_derive_secret! { PointSharingPolynomial }
57impl_derive_secret! { InterpolatedPointPolynomial }