1use rand_core::RngCore;
4use serde::{Deserialize, Serialize};
5use std::fmt::{Debug, Display};
6use std::marker::PhantomData;
7
8pub trait Element:
13 Clone + Display + Debug + Eq + Serialize + for<'a> Deserialize<'a> + PartialEq + Send + Sync
14{
15 type RHS;
17
18 fn new() -> Self;
20
21 fn one() -> Self;
23
24 fn add(&mut self, s2: &Self);
26
27 fn mul(&mut self, mul: &Self::RHS);
29
30 fn rand<R: RngCore>(rng: &mut R) -> Self;
32
33 fn zero() -> Self {
35 Self::new()
36 }
37}
38
39pub 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 }
47
48pub trait Point: Element {
50 type Error: Debug;
52
53 fn map(&mut self, data: &[u8]) -> Result<(), <Self as Point>::Error>;
55}
56
57pub trait Curve: Clone + Debug + Send + Sync {
60 type Scalar: Scalar<RHS = Self::Scalar>;
62
63 type Point: Point<RHS = Self::Scalar>;
65
66 fn scalar() -> Self::Scalar {
68 Self::Scalar::new()
69 }
70
71 fn point() -> Self::Point {
73 Self::Point::one()
74 }
75}
76
77pub 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 fn pair(a: &Self::G1, b: &Self::G2) -> Self::GT;
89}
90
91#[derive(Debug, Clone, PartialEq, Serialize)]
92pub 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>;