zkstd/macros/
curve.rs

1pub mod edwards;
2pub mod weierstrass;
3
4/// curve reference basic operation macro
5#[macro_export]
6macro_rules! curve_arithmetic_extension {
7    ($curve:ident, $scalar:ident, $extended:ident) => {
8        impl Eq for $curve {}
9
10        impl Default for $curve {
11            fn default() -> Self {
12                Self::ADDITIVE_IDENTITY
13            }
14        }
15
16        impl<'a, 'b> Add<&'b $curve> for &'a $curve {
17            type Output = $extended;
18
19            fn add(self, rhs: &'b $curve) -> $extended {
20                *self + *rhs
21            }
22        }
23
24        impl<'b> Add<&'b $curve> for $curve {
25            type Output = $extended;
26
27            fn add(self, rhs: &'b $curve) -> $extended {
28                &self + rhs
29            }
30        }
31
32        impl<'a> Add<$curve> for &'a $curve {
33            type Output = $extended;
34
35            fn add(self, rhs: $curve) -> $extended {
36                self + &rhs
37            }
38        }
39
40        impl<'a, 'b> Sub<&'b $curve> for &'a $curve {
41            type Output = $extended;
42
43            fn sub(self, rhs: &'b $curve) -> $extended {
44                *self - *rhs
45            }
46        }
47
48        impl<'b> Sub<&'b $curve> for $curve {
49            type Output = $extended;
50
51            fn sub(self, rhs: &'b $curve) -> $extended {
52                &self - rhs
53            }
54        }
55
56        impl<'a> Sub<$curve> for &'a $curve {
57            type Output = $extended;
58
59            fn sub(self, rhs: $curve) -> $extended {
60                self - &rhs
61            }
62        }
63
64        impl<'a> Mul<&'a $scalar> for $curve {
65            type Output = $extended;
66
67            fn mul(self, rhs: &'a $scalar) -> Self::Output {
68                self * *rhs
69            }
70        }
71
72        impl<'a> Mul<$scalar> for &'a $curve {
73            type Output = $extended;
74
75            fn mul(self, rhs: $scalar) -> Self::Output {
76                *self * rhs
77            }
78        }
79
80        impl<'a, 'b> Mul<&'b $scalar> for &'a $curve {
81            type Output = $extended;
82
83            fn mul(self, rhs: &'b $scalar) -> Self::Output {
84                *self * *rhs
85            }
86        }
87
88        impl<'a> Mul<&'a $curve> for $scalar {
89            type Output = $extended;
90
91            fn mul(self, rhs: &'a $curve) -> Self::Output {
92                self * *rhs
93            }
94        }
95
96        impl<'a> Mul<$curve> for &'a $scalar {
97            type Output = $extended;
98
99            fn mul(self, rhs: $curve) -> Self::Output {
100                *self * rhs
101            }
102        }
103
104        impl<'a, 'b> Mul<&'b $curve> for &'a $scalar {
105            type Output = $extended;
106
107            fn mul(self, rhs: &'b $curve) -> Self::Output {
108                *self * *rhs
109            }
110        }
111    };
112}
113
114/// affine and projective coordinate mixed basic operation
115#[macro_export]
116macro_rules! mixed_curve_operations {
117    ($affine:ident, $extended:ident) => {
118        impl Add<$extended> for $affine {
119            type Output = $extended;
120
121            fn add(self, rhs: $extended) -> $extended {
122                add_mixed_point(self, rhs)
123            }
124        }
125
126        impl<'a, 'b> Add<&'b $extended> for &'a $affine {
127            type Output = $extended;
128
129            fn add(self, rhs: &'b $extended) -> $extended {
130                *self + *rhs
131            }
132        }
133
134        impl<'b> Add<&'b $extended> for $affine {
135            type Output = $extended;
136
137            fn add(self, rhs: &'b $extended) -> $extended {
138                &self + rhs
139            }
140        }
141
142        impl<'a> Add<$extended> for &'a $affine {
143            type Output = $extended;
144
145            fn add(self, rhs: $extended) -> $extended {
146                self + &rhs
147            }
148        }
149
150        impl Sub<$extended> for $affine {
151            type Output = $extended;
152
153            fn sub(self, rhs: $extended) -> $extended {
154                add_mixed_point(self, -rhs)
155            }
156        }
157
158        impl<'a, 'b> Sub<&'b $extended> for &'a $affine {
159            type Output = $extended;
160
161            fn sub(self, rhs: &'b $extended) -> $extended {
162                *self + *rhs
163            }
164        }
165
166        impl<'b> Sub<&'b $extended> for $affine {
167            type Output = $extended;
168
169            fn sub(self, rhs: &'b $extended) -> $extended {
170                &self + rhs
171            }
172        }
173
174        impl<'a> Sub<$extended> for &'a $affine {
175            type Output = $extended;
176
177            fn sub(self, rhs: $extended) -> $extended {
178                self + &rhs
179            }
180        }
181
182        impl Add<$affine> for $extended {
183            type Output = $extended;
184
185            fn add(self, rhs: $affine) -> $extended {
186                add_mixed_point(rhs, self)
187            }
188        }
189
190        impl<'a, 'b> Add<&'b $affine> for &'a $extended {
191            type Output = $extended;
192
193            fn add(self, rhs: &'b $affine) -> $extended {
194                *self + *rhs
195            }
196        }
197
198        impl<'b> Add<&'b $affine> for $extended {
199            type Output = $extended;
200
201            fn add(self, rhs: &'b $affine) -> $extended {
202                &self + rhs
203            }
204        }
205
206        impl<'a> Add<$affine> for &'a $extended {
207            type Output = $extended;
208
209            fn add(self, rhs: $affine) -> $extended {
210                self + &rhs
211            }
212        }
213
214        impl Sub<$affine> for $extended {
215            type Output = $extended;
216
217            fn sub(self, rhs: $affine) -> $extended {
218                add_mixed_point(-rhs, self)
219            }
220        }
221
222        impl<'a, 'b> Sub<&'b $affine> for &'a $extended {
223            type Output = $extended;
224
225            fn sub(self, rhs: &'b $affine) -> $extended {
226                *self - *rhs
227            }
228        }
229
230        impl<'b> Sub<&'b $affine> for $extended {
231            type Output = $extended;
232
233            fn sub(self, rhs: &'b $affine) -> $extended {
234                &self - rhs
235            }
236        }
237
238        impl<'a> Sub<$affine> for &'a $extended {
239            type Output = $extended;
240
241            fn sub(self, rhs: $affine) -> $extended {
242                self - &rhs
243            }
244        }
245
246        impl AddAssign<$affine> for $extended {
247            fn add_assign(&mut self, rhs: $affine) {
248                *self = add_mixed_point(rhs, *self)
249            }
250        }
251
252        impl<'a> AddAssign<&'a $affine> for $extended {
253            fn add_assign(&mut self, rhs: &'a $affine) {
254                *self = add_mixed_point(*rhs, *self)
255            }
256        }
257
258        impl SubAssign<$affine> for $extended {
259            fn sub_assign(&mut self, rhs: $affine) {
260                *self = add_mixed_point(-rhs, *self)
261            }
262        }
263
264        impl<'a> SubAssign<&'a $affine> for $extended {
265            fn sub_assign(&mut self, rhs: &'a $affine) {
266                *self = add_mixed_point(-*rhs, *self)
267            }
268        }
269    };
270}
271
272pub use {curve_arithmetic_extension, mixed_curve_operations};