threshold_bls/
group.rs

1//! Traits for operating on Groups and Elliptic Curves.
2
3use rand_core::RngCore;
4use serde::{Deserialize, Serialize};
5use std::fmt::{Debug, Display};
6use std::marker::PhantomData;
7
8/// Element represents an element of a group with the additive notation
9/// which is also equipped with a multiplication transformation.
10/// Two implementations are for Scalar which forms a ring so RHS is the same
11/// and Point which can be multiplied by a scalar of its prime field.
12pub trait Element:
13    Clone + Display + Debug + Eq + Serialize + for<'a> Deserialize<'a> + PartialEq + Send + Sync
14{
15    /// The right-hand-side argument for multiplication
16    type RHS;
17
18    /// Returns the zero element of the group
19    fn new() -> Self;
20
21    /// Returns the one element of the group
22    fn one() -> Self;
23
24    /// Adds the RHS  element to the LHS element in place
25    fn add(&mut self, s2: &Self);
26
27    /// Multiplies the LHS element by the RHS element in place
28    fn mul(&mut self, mul: &Self::RHS);
29
30    /// Samples a random element using the provided RNG
31    fn rand<R: RngCore>(rng: &mut R) -> Self;
32
33    /// Returns the zero element of the group
34    fn zero() -> Self {
35        Self::new()
36    }
37}
38
39/// Scalar can be multiplied by only a Scalar, no other elements.
40pub trait Scalar: Element {
41    fn set_int(&mut self, i: u64);
42    fn inverse(&self) -> Option<Self>;
43    fn negate(&mut self);
44    fn sub(&mut self, other: &Self);
45    // TODO
46}
47
48/// Basic point functionality that can be multiplied by a scalar
49pub trait Point: Element {
50    /// Error which may occur while mapping to the group
51    type Error: Debug;
52
53    /// Maps the provided data to a group element
54    fn map(&mut self, data: &[u8]) -> Result<(), <Self as Point>::Error>;
55}
56
57/// A group holds functionalities to create scalar and points related; it is
58/// similar to the Engine definition, just much more simpler.
59pub trait Curve: Clone + Debug + Send + Sync {
60    /// The curve's scalar
61    type Scalar: Scalar<RHS = Self::Scalar>;
62
63    /// The curve's point
64    type Point: Point<RHS = Self::Scalar>;
65
66    /// scalar returns the identity element of the field.
67    fn scalar() -> Self::Scalar {
68        Self::Scalar::new()
69    }
70
71    /// point returns the default additive generator of the group.
72    fn point() -> Self::Point {
73        Self::Point::one()
74    }
75}
76
77/// A curve equipped with a bilinear pairing operation.
78pub trait PairingCurve: Clone + Debug {
79    type Scalar: Scalar<RHS = Self::Scalar>;
80
81    type G1: Point<RHS = Self::Scalar>;
82
83    type G2: Point<RHS = Self::Scalar>;
84
85    type GT: Element;
86
87    /// Perfors a pairing operation between the 2 group elements
88    fn pair(a: &Self::G1, b: &Self::G2) -> Self::GT;
89}
90
91#[derive(Debug, Clone, PartialEq, Serialize)]
92/// Helper which binds together a scalar with a group type to form a curve
93pub struct CurveFrom<S: Scalar, P: Point> {
94    s: PhantomData<S>,
95    p: PhantomData<P>,
96}
97
98impl<S, P> Curve for CurveFrom<S, P>
99where
100    S: Scalar<RHS = S>,
101    P: Point<RHS = S>,
102{
103    type Scalar = S;
104    type Point = P;
105}
106
107pub(super) type G1Curve<C> = CurveFrom<<C as PairingCurve>::Scalar, <C as PairingCurve>::G1>;
108pub(super) type G2Curve<C> = CurveFrom<<C as PairingCurve>::Scalar, <C as PairingCurve>::G2>;