elliptic_curve/
arithmetic.rs

1//! Elliptic curve arithmetic traits.
2
3use crate::{
4    ops::{Invert, LinearCombination, MulByGenerator, Reduce, ShrAssign},
5    point::AffineCoordinates,
6    scalar::{FromUintUnchecked, IsHigh},
7    Curve, FieldBytes, PrimeCurve, ScalarPrimitive,
8};
9use core::fmt::Debug;
10use subtle::{ConditionallySelectable, ConstantTimeEq, CtOption};
11use zeroize::DefaultIsZeroes;
12
13/// Elliptic curve with an arithmetic implementation.
14pub trait CurveArithmetic: Curve {
15    /// Elliptic curve point in affine coordinates.
16    type AffinePoint: 'static
17        + AffineCoordinates<FieldRepr = FieldBytes<Self>>
18        + Copy
19        + ConditionallySelectable
20        + ConstantTimeEq
21        + Debug
22        + Default
23        + DefaultIsZeroes
24        + Eq
25        + PartialEq
26        + Sized
27        + Send
28        + Sync;
29
30    /// Elliptic curve point in projective coordinates.
31    ///
32    /// Note: the following bounds are provided by [`group::Group`]:
33    /// - `'static`
34    /// - [`Copy`]
35    /// - [`Clone`]
36    /// - [`Debug`]
37    /// - [`Eq`]
38    /// - [`Sized`]
39    /// - [`Send`]
40    /// - [`Sync`]
41    type ProjectivePoint: ConditionallySelectable
42        + ConstantTimeEq
43        + Default
44        + DefaultIsZeroes
45        + From<Self::AffinePoint>
46        + Into<Self::AffinePoint>
47        + LinearCombination
48        + MulByGenerator
49        + group::Curve<AffineRepr = Self::AffinePoint>
50        + group::Group<Scalar = Self::Scalar>;
51
52    /// Scalar field modulo this curve's order.
53    ///
54    /// Note: the following bounds are provided by [`ff::Field`]:
55    /// - `'static`
56    /// - [`Copy`]
57    /// - [`Clone`]
58    /// - [`ConditionallySelectable`]
59    /// - [`ConstantTimeEq`]
60    /// - [`Debug`]
61    /// - [`Default`]
62    /// - [`Send`]
63    /// - [`Sync`]
64    type Scalar: AsRef<Self::Scalar>
65        + DefaultIsZeroes
66        + From<ScalarPrimitive<Self>>
67        + FromUintUnchecked<Uint = Self::Uint>
68        + Into<FieldBytes<Self>>
69        + Into<ScalarPrimitive<Self>>
70        + Into<Self::Uint>
71        + Invert<Output = CtOption<Self::Scalar>>
72        + IsHigh
73        + PartialOrd
74        + Reduce<Self::Uint, Bytes = FieldBytes<Self>>
75        + ShrAssign<usize>
76        + ff::Field
77        + ff::PrimeField<Repr = FieldBytes<Self>>;
78}
79
80/// Prime order elliptic curve with projective arithmetic implementation.
81pub trait PrimeCurveArithmetic:
82    PrimeCurve + CurveArithmetic<ProjectivePoint = Self::CurveGroup>
83{
84    /// Prime order elliptic curve group.
85    type CurveGroup: group::prime::PrimeCurve<Affine = <Self as CurveArithmetic>::AffinePoint>;
86}