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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
mod group;
mod test;

pub use group::*;
pub use test::*;

#[macro_export]
macro_rules! curve_operation {
    ($scalar:ident, $range:ident, $a:ident, $b:ident, $affine:ident, $projective:ident, $x:ident, $y:ident) => {
        use zero_crypto::behave::*;
        use zero_crypto::common::*;

        affine_group_operation!($affine, $range, $scalar, $x, $y);
        projective_group_operation!($projective, $range, $scalar, $x, $y);

        impl ParityCmp for $affine {}
        impl ParityCmp for $projective {}
        impl Basic for $affine {}
        impl Basic for $projective {}

        impl Curve for $affine {
            type Range = $range;

            const PARAM_A: $range = $a;
            const PARAM_B: $range = $b;

            fn is_identity(self) -> bool {
                self.is_infinity
            }

            fn double(self) -> Self {
                Self::from(double_point(self.to_projective()))
            }

            fn is_on_curve(self) -> bool {
                if self.is_infinity {
                    true
                } else {
                    self.y.square() == self.x.square() * self.x + Self::PARAM_B
                }
            }
        }

        impl Curve for $projective {
            type Range = $range;

            const PARAM_A: $range = $a;
            const PARAM_B: $range = $b;

            fn is_identity(self) -> bool {
                self.z == Self::Range::zero()
            }

            fn double(self) -> Self {
                double_point(self)
            }

            fn is_on_curve(self) -> bool {
                if self.is_identity() {
                    true
                } else {
                    self.y.square() * self.z
                        == self.x.square() * self.x + Self::PARAM_B * self.z.square() * self.z
                }
            }
        }

        impl From<$affine> for $projective {
            fn from(a: $affine) -> $projective {
                a.to_projective()
            }
        }

        impl Affine for $affine {
            type Projective = $projective;

            fn to_projective(self) -> Self::Projective {
                if self.is_identity() {
                    Self::Projective::ADDITIVE_IDENTITY
                } else {
                    Self::Projective {
                        x: self.x,
                        y: self.y,
                        z: Self::Range::one(),
                    }
                }
            }
        }

        impl From<$projective> for $affine {
            fn from(p: $projective) -> $affine {
                p.to_affine()
            }
        }

        impl Projective for $projective {
            type Affine = $affine;

            fn to_affine(self) -> Self::Affine {
                match self.z.invert() {
                    Some(z_inv) => Self::Affine {
                        x: self.x * z_inv,
                        y: self.y * z_inv,
                        is_infinity: self.z == Self::Range::zero(),
                    },
                    None => Self::Affine::ADDITIVE_IDENTITY,
                }
            }

            fn get_x(&self) -> Self::Range {
                self.x
            }

            fn get_y(&self) -> Self::Range {
                self.y
            }

            fn get_z(&self) -> Self::Range {
                self.z
            }

            fn set_x(&mut self, value: Self::Range) {
                self.x = value;
            }

            fn set_y(&mut self, value: Self::Range) {
                self.y = value;
            }

            fn set_z(&mut self, value: Self::Range) {
                self.z = value;
            }
        }
    };
}

pub use curve_operation;