-
Notifications
You must be signed in to change notification settings - Fork 2
/
ellipticcurve.rs
77 lines (67 loc) · 2.54 KB
/
ellipticcurve.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//! This is documentation for the `ellipticcurve` module.
//!
//! The ellipticcurve module is meant to be used for bar.
use num_bigint::{BigInt, BigUint};
use std::fmt::Display;
use crate::field::Field;
use crate::ops::{AddRef, DivRef, MulRef, NegRef, ScMulRef, Serialize, SubRef};
/// EcScalar models the behaviour of a scalar to multiply points.
pub trait EcScalar: Display + AddRef + SubRef + MulRef + DivRef + NegRef + Serialize {}
/// EcPoint models the behaviour of a point on an elliptic curve.
pub trait EcPoint<T>: Display + AddRef + SubRef + NegRef + ScMulRef<T> + Encode + Eq
where
T: EcScalar,
{
fn is_zero(&self) -> bool;
}
/// Encode provides functionality for encoding elliptic curve points as
/// octet-strings
pub trait Encode {
fn encode(&self, compress: bool) -> Vec<u8>;
}
/// Decode provides functionality for decoding octet-strings into
/// elliptic curve points
pub trait Decode {
type Deser;
fn decode(&self, _: &[u8]) -> Result<Self::Deser, std::io::Error>;
}
/// Curve trait allows to implement elliptic curve operations.
pub trait EllipticCurve: Decode {
type F: Field;
type Scalar: EcScalar;
type Point: EcPoint<Self::Scalar>;
fn identity(&self) -> Self::Point;
fn new_point(&self, x: <Self::F as Field>::Elt, y: <Self::F as Field>::Elt) -> Self::Point;
fn new_scalar(&self, _: BigInt) -> Self::Scalar;
fn get_generator(&self) -> Self::Point;
fn is_on_curve(&self, _: &Self::Point) -> bool;
fn get_order(&self) -> BigUint;
fn get_cofactor(&self) -> BigInt;
fn get_field(&self) -> Self::F;
}
/// Rational map between two elliptic curves.
pub trait RationalMap {
type E0: EllipticCurve;
type E1: EllipticCurve;
fn domain(&self) -> Self::E0;
fn codomain(&self) -> Self::E1;
fn push(&self, p: <Self::E0 as EllipticCurve>::Point) -> <Self::E1 as EllipticCurve>::Point;
fn pull(&self, p: <Self::E1 as EllipticCurve>::Point) -> <Self::E0 as EllipticCurve>::Point;
}
/// Isogeny is a rational map between two elliptic curves.
pub trait Isogeny {
type E0: EllipticCurve;
type E1: EllipticCurve;
fn domain(&self) -> Self::E0;
fn codomain(&self) -> Self::E1;
fn push(&self, p: <Self::E0 as EllipticCurve>::Point) -> <Self::E1 as EllipticCurve>::Point;
}
/// MapToCurve is a deterministic function from an element of the field F
/// to a point on an elliptic curve E defined over F.
pub trait MapToCurve {
type E: EllipticCurve;
fn map(
&self,
_: &<<Self::E as EllipticCurve>::F as Field>::Elt,
) -> <Self::E as EllipticCurve>::Point;
}