use rand_core::RngCore;
use serde::{Deserialize, Serialize};
use std::fmt::{Debug, Display};
use std::marker::PhantomData;
pub trait Element:
Clone + Display + Debug + Eq + Serialize + for<'a> Deserialize<'a> + PartialEq + Send + Sync
{
type RHS;
fn new() -> Self;
fn one() -> Self;
fn add(&mut self, s2: &Self);
fn mul(&mut self, mul: &Self::RHS);
fn rand<R: RngCore>(rng: &mut R) -> Self;
fn zero() -> Self {
Self::new()
}
}
pub trait Scalar: Element {
fn set_int(&mut self, i: u64);
fn inverse(&self) -> Option<Self>;
fn negate(&mut self);
fn sub(&mut self, other: &Self);
}
pub trait Point: Element {
type Error: Debug;
fn map(&mut self, data: &[u8]) -> Result<(), <Self as Point>::Error>;
}
pub trait Curve: Clone + Debug + Send + Sync {
type Scalar: Scalar<RHS = Self::Scalar>;
type Point: Point<RHS = Self::Scalar>;
fn scalar() -> Self::Scalar {
Self::Scalar::new()
}
fn point() -> Self::Point {
Self::Point::one()
}
}
pub trait PairingCurve: Clone + Debug {
type Scalar: Scalar<RHS = Self::Scalar>;
type G1: Point<RHS = Self::Scalar>;
type G2: Point<RHS = Self::Scalar>;
type GT: Element;
fn pair(a: &Self::G1, b: &Self::G2) -> Self::GT;
}
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct CurveFrom<S: Scalar, P: Point> {
s: PhantomData<S>,
p: PhantomData<P>,
}
impl<S, P> Curve for CurveFrom<S, P>
where
S: Scalar<RHS = S>,
P: Point<RHS = S>,
{
type Scalar = S;
type Point = P;
}
pub(super) type G1Curve<C> = CurveFrom<<C as PairingCurve>::Scalar, <C as PairingCurve>::G1>;
pub(super) type G2Curve<C> = CurveFrom<<C as PairingCurve>::Scalar, <C as PairingCurve>::G2>;