arnaucube
arnaucube/ark-r1cs-stdPublic Self-hosted repository
ark-r1cs-std/r1cs-std/src/macros.rs
r1cs-std/src/macros.rsRaw
  1#[allow(unused_braces)]
  2/// Implements arithmetic traits (eg: `Add`, `Sub`, `Mul`) for the given type using the impl in `$impl`.
  3///
  4/// Used primarily for implementing these traits for `FieldVar`s and `GroupVar`s.
  5#[macro_export]
  6macro_rules! impl_ops {
  7    (
  8        $type: ty,
  9        $native: ty,
 10        $trait: ident,
 11        $fn: ident,
 12        $assign_trait: ident,
 13        $assign_fn: ident,
 14        $impl: expr,
 15        $constant_impl: expr,
 16        $($args:tt)*
 17    ) => {
 18        impl_bounded_ops!($type, $native, $trait, $fn, $assign_trait, $assign_fn, $impl, $constant_impl, ($($args)+), );
 19    };
 20}
 21
 22/// Implements arithmetic traits (eg: `Add`, `Sub`, `Mul`) for the given type using the impl in `$impl`.
 23///
 24/// Used primarily for implementing these traits for `FieldVar`s and `GroupVar`s.
 25///
 26/// When compared to `impl_ops`, this macro allows specifying additional trait bounds.
 27#[macro_export]
 28macro_rules! impl_bounded_ops {
 29    (
 30        $type: ty,
 31        $native: ty,
 32        $trait: ident,
 33        $fn: ident,
 34        $assign_trait: ident,
 35        $assign_fn: ident,
 36        $impl: expr,
 37        $constant_impl: expr,
 38        ($($params:tt)+),
 39        $($bounds:tt)*
 40    ) => {
 41        impl<'a, $($params)+> core::ops::$trait<&'a $type> for &'a $type
 42        where
 43            $($bounds)*
 44        {
 45            type Output = $type;
 46
 47            #[tracing::instrument(target = "r1cs", skip(self))]
 48            #[allow(unused_braces)]
 49            fn $fn(self, other: Self) -> Self::Output {
 50                $impl(self, other)
 51            }
 52        }
 53
 54        impl<'a, $($params)+> core::ops::$trait<$type> for &'a $type
 55        where
 56            $($bounds)*
 57        {
 58            type Output = $type;
 59
 60            #[tracing::instrument(target = "r1cs", skip(self))]
 61            #[allow(unused_braces)]
 62            fn $fn(self, other: $type) -> Self::Output {
 63                core::ops::$trait::$fn(self, &other)
 64            }
 65        }
 66
 67        impl<'a, $($params)+> core::ops::$trait<&'a $type> for $type
 68        where
 69            $($bounds)*
 70        {
 71            type Output = $type;
 72
 73            #[tracing::instrument(target = "r1cs", skip(self))]
 74            #[allow(unused_braces)]
 75            fn $fn(self, other: &'a $type) -> Self::Output {
 76                core::ops::$trait::$fn(&self, other)
 77            }
 78        }
 79
 80        impl<$($params)+> core::ops::$trait<$type> for $type
 81        where
 82
 83            $($bounds)*
 84        {
 85            type Output = $type;
 86
 87            #[tracing::instrument(target = "r1cs", skip(self))]
 88            #[allow(unused_braces)]
 89            fn $fn(self, other: $type) -> Self::Output {
 90                core::ops::$trait::$fn(&self, &other)
 91            }
 92        }
 93
 94        impl<$($params)+> core::ops::$assign_trait<$type> for $type
 95        where
 96
 97            $($bounds)*
 98        {
 99            #[tracing::instrument(target = "r1cs", skip(self))]
100            #[allow(unused_braces)]
101            fn $assign_fn(&mut self, other: $type) {
102                let result = core::ops::$trait::$fn(&*self, &other);
103                *self = result
104            }
105        }
106
107        impl<'a, $($params)+> core::ops::$assign_trait<&'a $type> for $type
108        where
109
110            $($bounds)*
111        {
112            #[tracing::instrument(target = "r1cs", skip(self))]
113            #[allow(unused_braces)]
114            fn $assign_fn(&mut self, other: &'a $type) {
115                let result = core::ops::$trait::$fn(&*self, other);
116                *self = result
117            }
118        }
119
120        impl<'a, $($params)+> core::ops::$trait<$native> for &'a $type
121        where
122
123            $($bounds)*
124        {
125            type Output = $type;
126
127            #[tracing::instrument(target = "r1cs", skip(self))]
128            #[allow(unused_braces)]
129            fn $fn(self, other: $native) -> Self::Output {
130                $constant_impl(self, other)
131            }
132        }
133
134        impl<$($params)+> core::ops::$trait<$native> for $type
135        where
136
137            $($bounds)*
138        {
139            type Output = $type;
140
141            #[tracing::instrument(target = "r1cs", skip(self))]
142            #[allow(unused_braces)]
143            fn $fn(self, other: $native) -> Self::Output {
144                core::ops::$trait::$fn(&self, other)
145            }
146        }
147
148        impl<$($params)+> core::ops::$assign_trait<$native> for $type
149        where
150
151            $($bounds)*
152        {
153
154            #[tracing::instrument(target = "r1cs", skip(self))]
155            #[allow(unused_braces)]
156            fn $assign_fn(&mut self, other: $native) {
157                let result = core::ops::$trait::$fn(&*self, other);
158                *self = result
159            }
160        }
161    }
162}