lambdaworks_math/elliptic_curve/
traits.rs

1use crate::{
2    cyclic_group::IsGroup,
3    errors::PairingError,
4    field::{element::FieldElement, traits::IsField},
5};
6use core::fmt::Debug;
7
8#[derive(Debug, PartialEq, Eq)]
9pub enum EllipticCurveError {
10    InvalidPoint,
11}
12
13pub trait IsEllipticCurve {
14    /// BaseField is the field used for each of the coordinates of a point p
15    /// belonging to the curve.
16    type BaseField: IsField + Clone + Debug;
17
18    /// The representation of the point. For example it can be projective
19    /// coordinates, affine coordinates, XYZZ, depending on the curve and its
20    /// possible optimizations.
21    type PointRepresentation: IsGroup + FromAffine<Self::BaseField>;
22
23    /// Returns the generator of the main subgroup.
24    fn generator() -> Self::PointRepresentation;
25
26    /// Returns an affine point.
27    fn create_point_from_affine(
28        x: FieldElement<Self::BaseField>,
29        y: FieldElement<Self::BaseField>,
30    ) -> Result<Self::PointRepresentation, EllipticCurveError> {
31        Self::PointRepresentation::from_affine(x, y)
32    }
33}
34
35pub trait FromAffine<F: IsField>: Sized {
36    fn from_affine(x: FieldElement<F>, y: FieldElement<F>) -> Result<Self, EllipticCurveError>;
37}
38
39pub trait IsPairing {
40    type G1Point: IsGroup;
41    type G2Point: IsGroup;
42    type OutputField: IsField;
43
44    /// Compute the product of the pairings for a list of point pairs.
45    /// More efficient than just calling the pairing for each pair of points individually
46    fn compute_batch(
47        pairs: &[(&Self::G1Point, &Self::G2Point)],
48    ) -> Result<FieldElement<Self::OutputField>, PairingError>;
49
50    /// Compute the ate pairing between point `p` in G1 and `q` in G2.
51    fn compute(
52        p: &Self::G1Point,
53        q: &Self::G2Point,
54    ) -> Result<FieldElement<Self::OutputField>, PairingError> {
55        Self::compute_batch(&[(p, q)])
56    }
57}