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
use crate::as_raw::{AsRaw, TryFromRaw};
use crate::coords::*;
use crate::core::coords as coords_core;
use crate::core::*;

use super::definition::Point;

impl<E: Curve> HasAffineX<E> for Point<E>
where
    E: coords_core::HasAffineX,
{
    fn x(&self) -> Option<Coordinate<E>> {
        E::x(self.as_raw()).map(Coordinate::new)
    }
}

impl<E: Curve> HasAffineXAndParity<E> for Point<E>
where
    E: coords_core::HasAffineXAndParity,
{
    fn x_and_parity(&self) -> Option<(Coordinate<E>, Parity)> {
        E::x_and_parity(self.as_raw()).map(|(x, p)| (Coordinate::new(x), p))
    }

    fn from_x_and_parity(x: &Coordinate<E>, y_parity: Parity) -> Option<Self> {
        E::from_x_and_parity(x.as_array(), y_parity).and_then(Self::try_from_raw)
    }
}

impl<E: Curve> HasAffineY<E> for Point<E>
where
    E: coords_core::HasAffineY,
{
    fn y(&self) -> Option<Coordinate<E>> {
        E::y(self.as_raw()).map(Coordinate::new)
    }
}

impl<E: Curve> HasAffineXY<E> for Point<E>
where
    E: coords_core::HasAffineXY,
{
    fn coords(&self) -> Option<Coordinates<E>> {
        let (x, y) = E::x_and_y(self.as_raw())?;
        Some(Coordinates {
            x: Coordinate::new(x),
            y: Coordinate::new(y),
        })
    }

    fn from_coords(coords: &Coordinates<E>) -> Option<Self> {
        E::from_x_and_y(coords.x.as_array(), coords.y.as_array()).and_then(Self::try_from_raw)
    }
}

impl<E: Curve> AlwaysHasAffineY<E> for Point<E>
where
    E: coords_core::AlwaysHasAffineY,
{
    fn y(&self) -> Coordinate<E> {
        Coordinate::new(E::y(self.as_raw()))
    }
}

impl<E: Curve> AlwaysHasAffineYAndSign<E> for Point<E>
where
    E: coords_core::AlwaysHasAffineYAndSign,
{
    fn y_and_sign(&self) -> (Sign, Coordinate<E>) {
        let (sign, coord) = E::y_and_sign(self.as_raw());
        (sign, Coordinate::new(coord))
    }

    fn from_y_and_sign(x_sign: Sign, y: &Coordinate<E>) -> Option<Self> {
        E::from_y_and_sign(x_sign, y.as_array()).and_then(Point::try_from_raw)
    }
}