curv/arithmetic/
macros.rs

1#[doc(hidden)]
2#[macro_export]
3macro_rules! __bigint_impl_from {
4    ($($type:ty),*$(,)?) => {
5        $(
6        impl From<$type> for BigInt {
7            fn from(x: $type) -> Self {
8                BN::from(x).wrap()
9            }
10        }
11        )*
12    };
13}
14
15#[doc(hidden)]
16#[macro_export]
17macro_rules! __bigint_impl_ops {
18    () => {};
19    ($op: ident $func:ident, $($rest:tt)*) => {
20        impl ops::$op for &BigInt {
21            type Output = BigInt;
22            fn $func(self, rhs: Self) -> Self::Output {
23                self.inner_ref().$func(rhs.inner_ref()).wrap()
24            }
25        }
26        impl ops::$op for BigInt {
27            type Output = BigInt;
28            fn $func(self, rhs: Self) -> Self::Output {
29                self.into_inner().$func(rhs.into_inner()).wrap()
30            }
31        }
32        impl ops::$op<BigInt> for &BigInt {
33            type Output = BigInt;
34            fn $func(self, rhs: BigInt) -> Self::Output {
35                self.inner_ref().$func(rhs.into_inner()).wrap()
36            }
37        }
38        impl ops::$op<&BigInt> for BigInt {
39            type Output = BigInt;
40            fn $func(self, rhs: &BigInt) -> Self::Output {
41                self.into_inner().$func(rhs.inner_ref()).wrap()
42            }
43        }
44        $crate::__bigint_impl_ops!{ $($rest)* }
45    };
46    ($op: ident $func:ident $primitive:ty, $($rest:tt)*) => {
47        impl ops::$op<$primitive> for BigInt {
48            type Output = BigInt;
49            fn $func(self, rhs: $primitive) -> Self::Output {
50                self.into_inner().$func(rhs).wrap()
51            }
52        }
53        impl ops::$op<$primitive> for &BigInt {
54            type Output = BigInt;
55            fn $func(self, rhs: $primitive) -> Self::Output {
56                (&self.inner_ref()).$func(rhs).wrap()
57            }
58        }
59        $crate::__bigint_impl_ops!{ $($rest)* }
60    };
61    ($op: ident $func:ident $primitive:ty [swap], $($rest:tt)*) => {
62        impl ops::$op<$primitive> for BigInt {
63            type Output = BigInt;
64            fn $func(self, rhs: $primitive) -> Self::Output {
65                self.into_inner().$func(rhs).wrap()
66            }
67        }
68        impl ops::$op<$primitive> for &BigInt {
69            type Output = BigInt;
70            fn $func(self, rhs: $primitive) -> Self::Output {
71                (&self.inner_ref()).$func(rhs).wrap()
72            }
73        }
74        impl ops::$op<BigInt> for $primitive {
75            type Output = BigInt;
76            fn $func(self, rhs: BigInt) -> Self::Output {
77                self.$func(rhs.into_inner()).wrap()
78            }
79        }
80        impl ops::$op<&BigInt> for $primitive {
81            type Output = BigInt;
82            fn $func(self, rhs: &BigInt) -> Self::Output {
83                self.$func(rhs.inner_ref()).wrap()
84            }
85        }
86        $crate::__bigint_impl_ops!{ $($rest)* }
87    };
88}
89
90#[doc(hidden)]
91#[macro_export]
92macro_rules! __bigint_impl_assigns {
93    () => {};
94    ($trait:ident $fn:ident, $($rest:tt)*) => {
95        impl ops::$trait for BigInt {
96            fn $fn(&mut self, rhs: BigInt) {
97                self.inner_mut().$fn(rhs.into_inner())
98            }
99        }
100        impl ops::$trait<&BigInt> for BigInt {
101            fn $fn(&mut self, rhs: &BigInt) {
102                self.inner_mut().$fn(rhs.inner_ref())
103            }
104        }
105        $crate::__bigint_impl_assigns!{ $($rest)* }
106    };
107    ($trait:ident $fn:ident $primitive:ident, $($rest:tt)*) => {
108        impl ops::$trait<$primitive> for BigInt {
109            fn $fn(&mut self, rhs: $primitive) {
110                self.inner_mut().$fn(rhs)
111            }
112        }
113        $crate::__bigint_impl_assigns!{ $($rest)* }
114    };
115}