openzeppelin_crypto/curve/
helpers.rs

1//! Helper macros for implementing common traits for curve types.
2
3/// Implements additive operations by deferring to an implementation on &Self.
4#[macro_export]
5macro_rules! impl_additive_ops_from_ref {
6    ($type:ident, $params:ident) => {
7        #[allow(unused_qualifications)]
8        impl<P: $params> core::ops::Add<Self> for $type<P> {
9            type Output = Self;
10
11            #[inline]
12            fn add(self, other: Self) -> Self {
13                let mut result = self;
14                result += &other;
15                result
16            }
17        }
18
19        #[allow(unused_qualifications)]
20        impl<'a, P: $params> core::ops::Add<&'a mut Self> for $type<P> {
21            type Output = Self;
22
23            #[inline]
24            fn add(self, other: &'a mut Self) -> Self {
25                let mut result = self;
26                result += &*other;
27                result
28            }
29        }
30
31        impl<'b, P: $params> core::ops::Add<$type<P>> for &'b $type<P> {
32            type Output = $type<P>;
33
34            #[inline]
35            fn add(self, mut other: $type<P>) -> $type<P> {
36                other += self;
37                other
38            }
39        }
40
41        #[allow(unused_qualifications)]
42        impl<'a, 'b, P: $params> core::ops::Add<&'a $type<P>> for &'b $type<P> {
43            type Output = $type<P>;
44
45            #[inline]
46            fn add(self, other: &'a $type<P>) -> $type<P> {
47                let mut result = *self;
48                result += &*other;
49                result
50            }
51        }
52
53        #[allow(unused_qualifications)]
54        impl<'a, 'b, P: $params> core::ops::Add<&'a mut $type<P>>
55            for &'b $type<P>
56        {
57            type Output = $type<P>;
58
59            #[inline]
60            fn add(self, other: &'a mut $type<P>) -> $type<P> {
61                let mut result = *self;
62                result += &*other;
63                result
64            }
65        }
66
67        impl<'b, P: $params> core::ops::Sub<$type<P>> for &'b $type<P> {
68            type Output = $type<P>;
69
70            #[inline]
71            fn sub(self, other: $type<P>) -> $type<P> {
72                let mut result = *self;
73                result -= &other;
74                result
75            }
76        }
77
78        #[allow(unused_qualifications)]
79        impl<'a, 'b, P: $params> core::ops::Sub<&'a $type<P>> for &'b $type<P> {
80            type Output = $type<P>;
81
82            #[inline]
83            fn sub(self, other: &'a $type<P>) -> $type<P> {
84                let mut result = *self;
85                result -= &*other;
86                result
87            }
88        }
89
90        #[allow(unused_qualifications)]
91        impl<'a, 'b, P: $params> core::ops::Sub<&'a mut $type<P>>
92            for &'b $type<P>
93        {
94            type Output = $type<P>;
95
96            #[inline]
97            fn sub(self, other: &'a mut $type<P>) -> $type<P> {
98                let mut result = *self;
99                result -= &*other;
100                result
101            }
102        }
103
104        #[allow(unused_qualifications)]
105        impl<P: $params> core::ops::Sub<Self> for $type<P> {
106            type Output = Self;
107
108            #[inline]
109            fn sub(self, other: Self) -> Self {
110                let mut result = self;
111                result -= &other;
112                result
113            }
114        }
115
116        #[allow(unused_qualifications)]
117        impl<'a, P: $params> core::ops::Sub<&'a mut Self> for $type<P> {
118            type Output = Self;
119
120            #[inline]
121            fn sub(self, other: &'a mut Self) -> Self {
122                let mut result = self;
123                result -= &*other;
124                result
125            }
126        }
127
128        #[allow(unused_qualifications)]
129        impl<P: $params> core::iter::Sum<Self> for $type<P> {
130            fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
131                iter.fold(Self::zero(), core::ops::Add::add)
132            }
133        }
134
135        #[allow(unused_qualifications)]
136        impl<'a, P: $params> core::iter::Sum<&'a Self> for $type<P> {
137            fn sum<I: Iterator<Item = &'a Self>>(iter: I) -> Self {
138                iter.fold(Self::zero(), core::ops::Add::add)
139            }
140        }
141
142        #[allow(unused_qualifications)]
143        impl<P: $params> core::ops::AddAssign<Self> for $type<P> {
144            fn add_assign(&mut self, other: Self) {
145                *self += &other
146            }
147        }
148
149        #[allow(unused_qualifications)]
150        impl<P: $params> core::ops::SubAssign<Self> for $type<P> {
151            fn sub_assign(&mut self, other: Self) {
152                *self -= &other
153            }
154        }
155
156        #[allow(unused_qualifications)]
157        impl<'a, P: $params> core::ops::AddAssign<&'a mut Self> for $type<P> {
158            fn add_assign(&mut self, other: &'a mut Self) {
159                *self += &*other
160            }
161        }
162
163        #[allow(unused_qualifications)]
164        impl<'a, P: $params> core::ops::SubAssign<&'a mut Self> for $type<P> {
165            fn sub_assign(&mut self, other: &'a mut Self) {
166                *self -= &*other
167            }
168        }
169    };
170}