ark_ff/fields/
arithmetic.rs

1// Implements AddAssign on Self by deferring to an implementation on &Self
2#[macro_export]
3macro_rules! impl_additive_ops_from_ref {
4    ($type: ident, $params: ident) => {
5        #[allow(unused_qualifications)]
6        impl<P: $params> core::ops::Add<Self> for $type<P> {
7            type Output = Self;
8
9            #[inline]
10            fn add(self, other: Self) -> Self {
11                let mut result = self;
12                result.add_assign(&other);
13                result
14            }
15        }
16
17        #[allow(unused_qualifications)]
18        impl<'a, P: $params> core::ops::Add<&'a mut Self> for $type<P> {
19            type Output = Self;
20
21            #[inline]
22            fn add(self, other: &'a mut Self) -> Self {
23                let mut result = self;
24                result.add_assign(&*other);
25                result
26            }
27        }
28
29        impl<'b, P: $params> core::ops::Add<$type<P>> for &'b $type<P> {
30            type Output = $type<P>;
31
32            #[inline]
33            fn add(self, mut other: $type<P>) -> $type<P> {
34                other.add_assign(self);
35                other
36            }
37        }
38
39        #[allow(unused_qualifications)]
40        impl<'a, 'b, P: $params> core::ops::Add<&'a $type<P>> for &'b $type<P> {
41            type Output = $type<P>;
42
43            #[inline]
44            fn add(self, other: &'a $type<P>) -> $type<P> {
45                let mut result = *self;
46                result.add_assign(&*other);
47                result
48            }
49        }
50
51        #[allow(unused_qualifications)]
52        impl<'a, 'b, P: $params> core::ops::Add<&'a mut $type<P>> for &'b $type<P> {
53            type Output = $type<P>;
54
55            #[inline]
56            fn add(self, other: &'a mut $type<P>) -> $type<P> {
57                let mut result = *self;
58                result.add_assign(&*other);
59                result
60            }
61        }
62
63        impl<'b, P: $params> core::ops::Sub<$type<P>> for &'b $type<P> {
64            type Output = $type<P>;
65
66            #[inline]
67            fn sub(self, other: $type<P>) -> $type<P> {
68                let mut result = *self;
69                result.sub_assign(&other);
70                result
71            }
72        }
73
74        #[allow(unused_qualifications)]
75        impl<'a, 'b, P: $params> core::ops::Sub<&'a $type<P>> for &'b $type<P> {
76            type Output = $type<P>;
77
78            #[inline]
79            fn sub(self, other: &'a $type<P>) -> $type<P> {
80                let mut result = *self;
81                result.sub_assign(&*other);
82                result
83            }
84        }
85
86        #[allow(unused_qualifications)]
87        impl<'a, 'b, P: $params> core::ops::Sub<&'a mut $type<P>> for &'b $type<P> {
88            type Output = $type<P>;
89
90            #[inline]
91            fn sub(self, other: &'a mut $type<P>) -> $type<P> {
92                let mut result = *self;
93                result.sub_assign(&*other);
94                result
95            }
96        }
97
98        #[allow(unused_qualifications)]
99        impl<P: $params> core::ops::Sub<Self> for $type<P> {
100            type Output = Self;
101
102            #[inline]
103            fn sub(self, other: Self) -> Self {
104                let mut result = self;
105                result.sub_assign(&other);
106                result
107            }
108        }
109
110        #[allow(unused_qualifications)]
111        impl<'a, P: $params> core::ops::Sub<&'a mut Self> for $type<P> {
112            type Output = Self;
113
114            #[inline]
115            fn sub(self, other: &'a mut Self) -> Self {
116                let mut result = self;
117                result.sub_assign(&*other);
118                result
119            }
120        }
121
122        #[allow(unused_qualifications)]
123        impl<P: $params> core::iter::Sum<Self> for $type<P> {
124            fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
125                iter.fold(Self::zero(), core::ops::Add::add)
126            }
127        }
128
129        #[allow(unused_qualifications)]
130        impl<'a, P: $params> core::iter::Sum<&'a Self> for $type<P> {
131            fn sum<I: Iterator<Item = &'a Self>>(iter: I) -> Self {
132                iter.fold(Self::zero(), core::ops::Add::add)
133            }
134        }
135
136        #[allow(unused_qualifications)]
137        impl<P: $params> core::ops::AddAssign<Self> for $type<P> {
138            fn add_assign(&mut self, other: Self) {
139                self.add_assign(&other)
140            }
141        }
142
143        #[allow(unused_qualifications)]
144        impl<P: $params> core::ops::SubAssign<Self> for $type<P> {
145            fn sub_assign(&mut self, other: Self) {
146                self.sub_assign(&other)
147            }
148        }
149
150        #[allow(unused_qualifications)]
151        impl<'a, P: $params> core::ops::AddAssign<&'a mut Self> for $type<P> {
152            fn add_assign(&mut self, other: &'a mut Self) {
153                self.add_assign(&*other)
154            }
155        }
156
157        #[allow(unused_qualifications)]
158        impl<'a, P: $params> core::ops::SubAssign<&'a mut Self> for $type<P> {
159            fn sub_assign(&mut self, other: &'a mut Self) {
160                self.sub_assign(&*other)
161            }
162        }
163    };
164}
165
166// Implements `MulAssign` and `DivAssign` by deferring to an implementation on &Self
167#[macro_export]
168macro_rules! impl_multiplicative_ops_from_ref {
169    ($type: ident, $params: ident) => {
170        #[allow(unused_qualifications)]
171        impl<P: $params> core::ops::Mul<Self> for $type<P> {
172            type Output = Self;
173
174            #[inline]
175            fn mul(self, other: Self) -> Self {
176                let mut result = self;
177                result.mul_assign(&other);
178                result
179            }
180        }
181
182        #[allow(unused_qualifications)]
183        impl<P: $params> core::ops::Div<Self> for $type<P> {
184            type Output = Self;
185
186            #[inline]
187            fn div(self, other: Self) -> Self {
188                let mut result = self;
189                result.div_assign(&other);
190                result
191            }
192        }
193
194        #[allow(unused_qualifications)]
195        impl<'a, P: $params> core::ops::Mul<&'a mut Self> for $type<P> {
196            type Output = Self;
197
198            #[inline]
199            fn mul(self, other: &'a mut Self) -> Self {
200                let mut result = self;
201                result.mul_assign(&*other);
202                result
203            }
204        }
205
206        #[allow(unused_qualifications)]
207        impl<'a, P: $params> core::ops::Div<&'a mut Self> for $type<P> {
208            type Output = Self;
209
210            #[inline]
211            fn div(self, other: &'a mut Self) -> Self {
212                let mut result = self;
213                result.div_assign(&*other);
214                result
215            }
216        }
217
218        impl<'b, P: $params> core::ops::Mul<$type<P>> for &'b $type<P> {
219            type Output = $type<P>;
220
221            #[inline]
222            fn mul(self, mut other: $type<P>) -> $type<P> {
223                other.mul_assign(self);
224                other
225            }
226        }
227
228        #[allow(unused_qualifications)]
229        impl<'a, 'b, P: $params> core::ops::Mul<&'a $type<P>> for &'b $type<P> {
230            type Output = $type<P>;
231
232            #[inline]
233            fn mul(self, other: &'a $type<P>) -> $type<P> {
234                let mut result = *self;
235                result.mul_assign(&*other);
236                result
237            }
238        }
239
240        #[allow(unused_qualifications)]
241        impl<'a, 'b, P: $params> core::ops::Mul<&'a mut $type<P>> for &'b $type<P> {
242            type Output = $type<P>;
243
244            #[inline]
245            fn mul(self, other: &'a mut $type<P>) -> $type<P> {
246                let mut result = *self;
247                result.mul_assign(&*other);
248                result
249            }
250        }
251
252        impl<'b, P: $params> core::ops::Div<$type<P>> for &'b $type<P> {
253            type Output = $type<P>;
254
255            #[inline]
256            fn div(self, other: $type<P>) -> $type<P> {
257                let mut result = *self;
258                result.div_assign(&other);
259                result
260            }
261        }
262
263        #[allow(unused_qualifications)]
264        impl<'a, 'b, P: $params> core::ops::Div<&'a $type<P>> for &'b $type<P> {
265            type Output = $type<P>;
266
267            #[inline]
268            fn div(self, other: &'a $type<P>) -> $type<P> {
269                let mut result = *self;
270                result.div_assign(&*other);
271                result
272            }
273        }
274
275        #[allow(unused_qualifications)]
276        impl<'a, 'b, P: $params> core::ops::Div<&'a mut $type<P>> for &'b $type<P> {
277            type Output = $type<P>;
278
279            #[inline]
280            fn div(self, other: &'a mut $type<P>) -> $type<P> {
281                let mut result = *self;
282                result.div_assign(&*other);
283                result
284            }
285        }
286
287        #[allow(unused_qualifications)]
288        impl<P: $params> core::iter::Product<Self> for $type<P> {
289            fn product<I: Iterator<Item = Self>>(iter: I) -> Self {
290                iter.fold(Self::one(), core::ops::Mul::mul)
291            }
292        }
293
294        #[allow(unused_qualifications)]
295        impl<'a, P: $params> core::iter::Product<&'a Self> for $type<P> {
296            fn product<I: Iterator<Item = &'a Self>>(iter: I) -> Self {
297                iter.fold(Self::one(), Mul::mul)
298            }
299        }
300
301        #[allow(unused_qualifications)]
302        impl<P: $params> core::ops::MulAssign<Self> for $type<P> {
303            fn mul_assign(&mut self, other: Self) {
304                self.mul_assign(&other)
305            }
306        }
307
308        #[allow(unused_qualifications)]
309        impl<'a, P: $params> core::ops::DivAssign<&'a mut Self> for $type<P> {
310            fn div_assign(&mut self, other: &'a mut Self) {
311                self.div_assign(&*other)
312            }
313        }
314
315        #[allow(unused_qualifications)]
316        impl<'a, P: $params> core::ops::MulAssign<&'a mut Self> for $type<P> {
317            fn mul_assign(&mut self, other: &'a mut Self) {
318                self.mul_assign(&*other)
319            }
320        }
321
322        #[allow(unused_qualifications)]
323        impl<P: $params> core::ops::DivAssign<Self> for $type<P> {
324            fn div_assign(&mut self, other: Self) {
325                self.div_assign(&other)
326            }
327        }
328    };
329}