Skip to main content

ed448_goldilocks/
macros.rs

1// -*- mode: rust; -*-
2//
3// This file is part of curve25519-dalek.
4// Copyright (c) 2016-2021 isis agora lovecruft
5// Copyright (c) 2016-2019 Henry de Valence
6// See LICENSE for licensing information.
7//
8// Authors:
9// - isis agora lovecruft <[email protected]>
10// - Henry de Valence <[email protected]>
11
12//! Internal macros.
13
14/// Define borrow and non-borrow variants of `Add`.
15macro_rules! define_add_variants {
16    ($(GENERIC = $generic:ident: $bound:ident,)? LHS = $lhs:ty, RHS = $rhs:ty, Output = $out:ty) => {
17        impl<'b $(, $generic: $bound)?> Add<&'b $rhs> for $lhs {
18            type Output = $out;
19
20            fn add(self, rhs: &'b $rhs) -> $out {
21                &self + rhs
22            }
23        }
24
25        impl<'a $(, $generic: $bound)?> Add<$rhs> for &'a $lhs {
26            type Output = $out;
27
28            fn add(self, rhs: $rhs) -> $out {
29                self + &rhs
30            }
31        }
32
33        impl $(<$generic: $bound>)? Add<$rhs> for $lhs {
34            type Output = $out;
35
36            fn add(self, rhs: $rhs) -> $out {
37                &self + &rhs
38            }
39        }
40    };
41}
42
43/// Define non-borrow variants of `AddAssign`.
44macro_rules! define_add_assign_variants {
45    (LHS = $lhs:ty, RHS = $rhs:ty) => {
46        impl AddAssign<$rhs> for $lhs {
47            fn add_assign(&mut self, rhs: $rhs) {
48                *self += &rhs;
49            }
50        }
51    };
52}
53
54/// Define borrow and non-borrow variants of `Sub`.
55macro_rules! define_sub_variants {
56    ($(GENERIC = $generic:ident: $bound:ident,)? LHS = $lhs:ty, RHS = $rhs:ty, Output = $out:ty) => {
57        impl<'b $(, $generic: $bound)?> Sub<&'b $rhs> for $lhs {
58            type Output = $out;
59
60            fn sub(self, rhs: &'b $rhs) -> $out {
61                &self - rhs
62            }
63        }
64
65        impl<'a $(, $generic: $bound)?> Sub<$rhs> for &'a $lhs {
66            type Output = $out;
67
68            fn sub(self, rhs: $rhs) -> $out {
69                self - &rhs
70            }
71        }
72
73        impl $(<$generic: $bound>)? Sub<$rhs> for $lhs {
74            type Output = $out;
75
76            fn sub(self, rhs: $rhs) -> $out {
77                &self - &rhs
78            }
79        }
80    };
81}
82
83/// Define non-borrow variants of `SubAssign`.
84macro_rules! define_sub_assign_variants {
85    (LHS = $lhs:ty, RHS = $rhs:ty) => {
86        impl SubAssign<$rhs> for $lhs {
87            fn sub_assign(&mut self, rhs: $rhs) {
88                *self -= &rhs;
89            }
90        }
91    };
92}
93
94/// Define borrow and non-borrow variants of `Mul`.
95macro_rules! define_mul_variants {
96    ($(GENERIC = $generic:ident: $bound:ident,)? LHS = $lhs:ty, RHS = $rhs:ty, Output = $out:ty) => {
97        impl<'b $(, $generic: $bound)?> Mul<&'b $rhs> for $lhs {
98            type Output = $out;
99
100            fn mul(self, rhs: &'b $rhs) -> $out {
101                &self * rhs
102            }
103        }
104
105        impl<'a $(, $generic: $bound)?> Mul<$rhs> for &'a $lhs {
106            type Output = $out;
107
108            fn mul(self, rhs: $rhs) -> $out {
109                self * &rhs
110            }
111        }
112
113        impl $(<$generic: $bound>)? Mul<$rhs> for $lhs {
114            type Output = $out;
115
116            fn mul(self, rhs: $rhs) -> $out {
117                &self * &rhs
118            }
119        }
120    };
121}
122
123/// Define non-borrow variants of `MulAssign`.
124macro_rules! define_mul_assign_variants {
125    (LHS = $lhs:ty, RHS = $rhs:ty) => {
126        impl MulAssign<$rhs> for $lhs {
127            fn mul_assign(&mut self, rhs: $rhs) {
128                *self *= &rhs;
129            }
130        }
131    };
132}