halo2curves/derive/
mod.rs

1#[macro_use]
2pub mod curve;
3#[macro_use]
4pub mod field;
5#[macro_use]
6pub mod pairing;
7
8#[macro_export]
9macro_rules! impl_binops_calls {
10    ($field:ident) => {
11        impl ::core::ops::Neg for $field {
12            type Output = $field;
13
14            #[inline]
15            fn neg(self) -> $field {
16                -&self
17            }
18        }
19
20        impl<'a> ::core::ops::Neg for &'a $field {
21            type Output = $field;
22
23            #[inline]
24            fn neg(self) -> $field {
25                self.neg()
26            }
27        }
28
29        impl<'a, 'b> ::core::ops::Sub<&'b $field> for &'a $field {
30            type Output = $field;
31
32            #[inline]
33            fn sub(self, rhs: &'b $field) -> $field {
34                self.sub(rhs)
35            }
36        }
37
38        impl<'a, 'b> ::core::ops::Add<&'b $field> for &'a $field {
39            type Output = $field;
40
41            #[inline]
42            fn add(self, rhs: &'b $field) -> $field {
43                self.add(rhs)
44            }
45        }
46
47        impl<'a, 'b> ::core::ops::Mul<&'b $field> for &'a $field {
48            type Output = $field;
49
50            #[inline]
51            fn mul(self, rhs: &'b $field) -> $field {
52                self.mul(rhs)
53            }
54        }
55    };
56}
57
58#[macro_export]
59macro_rules! impl_add_binop_specify_output {
60    ($lhs:ident, $rhs:ident, $output:ident) => {
61        impl<'b> ::core::ops::Add<&'b $rhs> for $lhs {
62            type Output = $output;
63
64            #[inline]
65            fn add(self, rhs: &'b $rhs) -> $output {
66                &self + rhs
67            }
68        }
69
70        impl<'a> ::core::ops::Add<$rhs> for &'a $lhs {
71            type Output = $output;
72
73            #[inline]
74            fn add(self, rhs: $rhs) -> $output {
75                self + &rhs
76            }
77        }
78
79        impl ::core::ops::Add<$rhs> for $lhs {
80            type Output = $output;
81
82            #[inline]
83            fn add(self, rhs: $rhs) -> $output {
84                &self + &rhs
85            }
86        }
87    };
88}
89
90#[macro_export]
91macro_rules! impl_sub_binop_specify_output {
92    ($lhs:ident, $rhs:ident, $output:ident) => {
93        impl<'b> ::core::ops::Sub<&'b $rhs> for $lhs {
94            type Output = $output;
95
96            #[inline]
97            fn sub(self, rhs: &'b $rhs) -> $output {
98                &self - rhs
99            }
100        }
101
102        impl<'a> ::core::ops::Sub<$rhs> for &'a $lhs {
103            type Output = $output;
104
105            #[inline]
106            fn sub(self, rhs: $rhs) -> $output {
107                self - &rhs
108            }
109        }
110
111        impl ::core::ops::Sub<$rhs> for $lhs {
112            type Output = $output;
113
114            #[inline]
115            fn sub(self, rhs: $rhs) -> $output {
116                &self - &rhs
117            }
118        }
119    };
120}
121
122#[macro_export]
123macro_rules! impl_binops_additive_specify_output {
124    ($lhs:ident, $rhs:ident, $output:ident) => {
125        $crate::impl_add_binop_specify_output!($lhs, $rhs, $output);
126        $crate::impl_sub_binop_specify_output!($lhs, $rhs, $output);
127    };
128}
129
130#[macro_export]
131macro_rules! impl_binops_multiplicative_mixed {
132    ($lhs:ident, $rhs:ident, $output:ident) => {
133        impl<'b> ::core::ops::Mul<&'b $rhs> for $lhs {
134            type Output = $output;
135
136            #[inline]
137            fn mul(self, rhs: &'b $rhs) -> $output {
138                &self * rhs
139            }
140        }
141
142        impl<'a> ::core::ops::Mul<$rhs> for &'a $lhs {
143            type Output = $output;
144
145            #[inline]
146            fn mul(self, rhs: $rhs) -> $output {
147                self * &rhs
148            }
149        }
150
151        impl ::core::ops::Mul<$rhs> for $lhs {
152            type Output = $output;
153
154            #[inline]
155            fn mul(self, rhs: $rhs) -> $output {
156                &self * &rhs
157            }
158        }
159    };
160}
161
162#[macro_export]
163macro_rules! impl_binops_additive {
164    ($lhs:ident, $rhs:ident) => {
165        $crate::impl_binops_additive_specify_output!($lhs, $rhs, $lhs);
166
167        impl ::core::ops::SubAssign<$rhs> for $lhs {
168            #[inline]
169            fn sub_assign(&mut self, rhs: $rhs) {
170                *self = &*self - &rhs;
171            }
172        }
173
174        impl ::core::ops::AddAssign<$rhs> for $lhs {
175            #[inline]
176            fn add_assign(&mut self, rhs: $rhs) {
177                *self = &*self + &rhs;
178            }
179        }
180
181        impl<'b> ::core::ops::SubAssign<&'b $rhs> for $lhs {
182            #[inline]
183            fn sub_assign(&mut self, rhs: &'b $rhs) {
184                *self = &*self - rhs;
185            }
186        }
187
188        impl<'b> ::core::ops::AddAssign<&'b $rhs> for $lhs {
189            #[inline]
190            fn add_assign(&mut self, rhs: &'b $rhs) {
191                *self = &*self + rhs;
192            }
193        }
194    };
195}
196
197#[macro_export]
198macro_rules! impl_binops_multiplicative {
199    ($lhs:ident, $rhs:ident) => {
200        $crate::impl_binops_multiplicative_mixed!($lhs, $rhs, $lhs);
201
202        impl ::core::ops::MulAssign<$rhs> for $lhs {
203            #[inline]
204            fn mul_assign(&mut self, rhs: $rhs) {
205                *self = &*self * &rhs;
206            }
207        }
208
209        impl<'b> ::core::ops::MulAssign<&'b $rhs> for $lhs {
210            #[inline]
211            fn mul_assign(&mut self, rhs: &'b $rhs) {
212                *self = &*self * rhs;
213            }
214        }
215    };
216}
217
218#[macro_export]
219macro_rules! impl_sum_prod {
220    ($f:ident) => {
221        impl<T: ::core::borrow::Borrow<$f>> ::core::iter::Sum<T> for $f {
222            fn sum<I: Iterator<Item = T>>(iter: I) -> Self {
223                iter.fold(Self::zero(), |acc, item| acc + item.borrow())
224            }
225        }
226
227        impl<T: ::core::borrow::Borrow<$f>> ::core::iter::Product<T> for $f {
228            fn product<I: Iterator<Item = T>>(iter: I) -> Self {
229                iter.fold(Self::one(), |acc, item| acc * item.borrow())
230            }
231        }
232    };
233}