primitives/algebra/elliptic_curve/
curve.rs1use std::{hash::Hash, ops::ShrAssign};
2
3use derive_more::derive::Display;
4use elliptic_curve::{
5 group::{self, GroupEncoding},
6 ops::{Invert, MulByGenerator},
7 scalar::{FromUintUnchecked, IsHigh},
8 Curve as EllipticCurve,
9 FieldBytes,
10 ScalarPrimitive,
11};
12use ff::PrimeField;
13use subtle::{ConditionallySelectable, ConstantTimeEq, CtOption};
14
15use crate::algebra::{
16 field::{FieldElement, FieldExtension, SubfieldElement},
17 uniform_bytes::FromUniformBytes,
18};
19
20pub trait Curve: EllipticCurve {
24 const SCALAR_BIG_ENDIAN: bool;
26 const POINT_BIG_ENDIAN: bool;
28
29 const BASE_FIELD_BIG_ENDIAN: bool;
30 type Point: ConditionallySelectable
42 + ConstantTimeEq
43 + Default
44 + MulByGenerator
51 + group::Group<Scalar = Self::Scalar>
52 + GroupEncoding
53 + Hash
54 + FromExtendedEdwards<BaseFieldElement = BaseFieldElement<Self>>
55 + ToExtendedEdwards<BaseFieldElement = BaseFieldElement<Self>>;
56
57 type Scalar: AsRef<Self::Scalar>
70 + From<ScalarPrimitive<Self>>
72 + FromUintUnchecked<Uint = Self::Uint>
73 + Into<FieldBytes<Self>>
74 + Into<ScalarPrimitive<Self>>
75 + Into<Self::Uint>
76 + Invert<Output = CtOption<Self::Scalar>>
77 + FromUniformBytes
78 + IsHigh
79 + PartialOrd
80 + ShrAssign<usize>
81 + PrimeField
82 + FieldExtension<Subfield = Self::Scalar>
83 + Hash;
84
85 type BaseField: PrimeField + FieldExtension + ShrAssign<usize> + FromUniformBytes + Hash;
86
87 fn hash_to_curve(bytes: &[u8]) -> Self::Point;
88}
89
90pub trait FromExtendedEdwards {
91 type BaseFieldElement;
92
93 fn from_extended_edwards(coordinates: [Self::BaseFieldElement; 4]) -> Option<Self>
94 where
95 Self: Sized;
96}
97
98#[derive(Display)]
99pub struct PointAtInfinityError;
100
101pub trait ToExtendedEdwards {
102 type BaseFieldElement;
103
104 fn to_extended_edwards(self) -> Result<[Self::BaseFieldElement; 4], PointAtInfinityError>;
105}
106
107pub type ScalarField<C> = <C as Curve>::Scalar;
109pub type BaseField<C> = <C as Curve>::BaseField;
111
112pub type Scalar<C> = SubfieldElement<ScalarField<C>>;
114pub type BaseFieldElement<C> = SubfieldElement<BaseField<C>>;
116
117pub type ScalarAsExtension<C> = FieldElement<<C as Curve>::Scalar>;
119pub type BaseFieldAsExtension<C> = FieldElement<BaseField<C>>;