generic_ec_core/
coords.rs

1//! Exposes information about point coordinates
2//!
3//! `Curve` by default does not require that points expose their coordinates.
4//! Some curves implementations intentionally don't expose points coordinates,
5//! and, generally, most of the EC algorithms don't need them. Each curve
6//! implementation may optionally expose affine coordinates by implementing
7//! trait from this module.
8#[cfg(feature = "serde")]
9use serde::{Deserialize, Serialize};
10
11use crate::Curve;
12
13/// A point that has affine x coordinate
14pub trait HasAffineX: Curve {
15    /// Returns point affine x coordinate
16    ///
17    /// Returns `None` if it's point at infinity
18    fn x(point: &Self::Point) -> Option<Self::CoordinateArray>;
19}
20
21/// A point that has affine x coordinate and parity of y coordinate
22pub trait HasAffineXAndParity: Curve + HasAffineX {
23    /// Returns point x coordinate and parity of y coordinate
24    ///
25    /// Returns `None` if it's point at infinity
26    fn x_and_parity(point: &Self::Point) -> Option<(Self::CoordinateArray, Parity)>;
27    /// Construct a point from x coordinate and parity of y coordinate
28    ///
29    /// Returns `None` if input does not correspond to a valid point (but you
30    /// still need to check that the point is [on curve](super::OnCurve) and
31    /// has [no small component](super::SmallFactor))
32    fn from_x_and_parity(x: &Self::CoordinateArray, y_parity: Parity) -> Option<Self::Point>;
33}
34
35/// A point that has affine y coordinate
36pub trait HasAffineY: Curve {
37    /// Returns point affine y coordinate
38    ///
39    /// Returns `None` if it's point at infinity
40    fn y(point: &Self::Point) -> Option<Self::CoordinateArray>;
41}
42
43/// A point that has affine x and y coordinates
44pub trait HasAffineXY: Curve + HasAffineX + HasAffineY {
45    /// Returns point affine x and y coordinates
46    ///
47    /// Returns `None` if it's point at infinity
48    fn x_and_y(point: &Self::Point) -> Option<(Self::CoordinateArray, Self::CoordinateArray)>;
49    /// Construct a point from x and y coordinates
50    ///
51    /// Returns `None` if input does not correspond to a valid point (but you
52    /// still need to check that the point is [on curve](super::OnCurve) and
53    /// has [no small component](super::SmallFactor))
54    fn from_x_and_y(x: &Self::CoordinateArray, y: &Self::CoordinateArray) -> Option<Self::Point>;
55}
56
57/// A point that always has affine y coordinate
58pub trait AlwaysHasAffineY: Curve {
59    /// Returns point affine y coordinate
60    fn y(point: &Self::Point) -> Self::CoordinateArray;
61}
62
63/// A point that always has affine y coordinate and sign of x coordinate
64pub trait AlwaysHasAffineYAndSign: Curve + AlwaysHasAffineY {
65    /// Returns y coordinate and sign of x coordinate
66    fn y_and_sign(point: &Self::Point) -> (Sign, Self::CoordinateArray);
67    /// Constructs a point from y coordinate and sign of x coordinate
68    ///
69    /// Returns `None` if input does not correspond to a valid point (but you
70    /// still need to check that the point is [on curve](super::OnCurve) and
71    /// has [no small component](super::SmallFactor))
72    fn from_y_and_sign(x_sign: Sign, y: &Self::CoordinateArray) -> Option<Self::Point>;
73}
74
75/// Sign of coordinate
76#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
77#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
78#[repr(u8)]
79pub enum Sign {
80    /// Coordinate has negative sign
81    Negative = 0,
82    /// Coordinate has non-negative sign
83    NonNegative = 1,
84}
85
86impl Sign {
87    /// Checks whether coordinate is negative
88    pub fn is_negative(&self) -> bool {
89        *self == Self::Negative
90    }
91}
92
93/// Parity of coordinate
94#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
95#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
96#[repr(u8)]
97pub enum Parity {
98    /// Coordinate is odd
99    Odd = 0,
100    /// Coordinate is even
101    Even = 1,
102}
103
104impl Parity {
105    /// Checks whether coordinate is odd
106    pub fn is_odd(&self) -> bool {
107        *self == Self::Odd
108    }
109
110    /// Checks whether coordinate is even
111    pub fn is_even(&self) -> bool {
112        *self == Self::Even
113    }
114}