snarkvm_console_types_group/
arithmetic.rs

1// Copyright 2024 Aleo Network Foundation
2// This file is part of the snarkVM library.
3
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at:
7
8// http://www.apache.org/licenses/LICENSE-2.0
9
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16use super::*;
17
18impl<E: Environment> Neg for Group<E> {
19    type Output = Group<E>;
20
21    /// Returns the `negation` of `self`.
22    #[inline]
23    fn neg(self) -> Self::Output {
24        Group::from_projective(-self.group)
25    }
26}
27
28impl<E: Environment> Add<Group<E>> for Group<E> {
29    type Output = Group<E>;
30
31    /// Returns the `sum` of `self` and `other`.
32    #[inline]
33    fn add(self, other: Group<E>) -> Self::Output {
34        Group::from_projective(self.group + other.group)
35    }
36}
37
38impl<E: Environment> Add<&Group<E>> for Group<E> {
39    type Output = Group<E>;
40
41    /// Returns the `sum` of `self` and `other`.
42    #[inline]
43    fn add(self, other: &Group<E>) -> Self::Output {
44        Group::from_projective(self.group + other.group)
45    }
46}
47
48impl<E: Environment> AddAssign<Group<E>> for Group<E> {
49    /// Adds `other` to `self`.
50    #[inline]
51    fn add_assign(&mut self, other: Group<E>) {
52        self.group += other.group;
53    }
54}
55
56impl<E: Environment> AddAssign<&Group<E>> for Group<E> {
57    /// Adds `other` to `self`.
58    #[inline]
59    fn add_assign(&mut self, other: &Group<E>) {
60        self.group += other.group;
61    }
62}
63
64impl<E: Environment> Sub<Group<E>> for Group<E> {
65    type Output = Group<E>;
66
67    /// Returns the `difference` of `self` and `other`.
68    #[inline]
69    fn sub(self, other: Group<E>) -> Self::Output {
70        Group::from_projective(self.group - other.group)
71    }
72}
73
74impl<E: Environment> Sub<&Group<E>> for Group<E> {
75    type Output = Group<E>;
76
77    /// Returns the `difference` of `self` and `other`.
78    #[inline]
79    fn sub(self, other: &Group<E>) -> Self::Output {
80        Group::from_projective(self.group - other.group)
81    }
82}
83
84impl<E: Environment> SubAssign<Group<E>> for Group<E> {
85    /// Subtracts `other` from `self`.
86    #[inline]
87    fn sub_assign(&mut self, other: Group<E>) {
88        self.group -= other.group;
89    }
90}
91
92impl<E: Environment> SubAssign<&Group<E>> for Group<E> {
93    /// Subtracts `other` from `self`.
94    #[inline]
95    fn sub_assign(&mut self, other: &Group<E>) {
96        self.group -= other.group;
97    }
98}
99
100impl<E: Environment> Mul<Scalar<E>> for Group<E> {
101    type Output = Group<E>;
102
103    /// Returns the `product` of `self` and `other`.
104    #[inline]
105    fn mul(self, other: Scalar<E>) -> Self::Output {
106        Group::from_projective(self.group * *other)
107    }
108}
109
110impl<E: Environment> Mul<&Scalar<E>> for Group<E> {
111    type Output = Group<E>;
112
113    /// Returns the `product` of `self` and `other`.
114    #[inline]
115    fn mul(self, other: &Scalar<E>) -> Self::Output {
116        Group::from_projective(self.group * **other)
117    }
118}
119
120impl<E: Environment> Mul<Group<E>> for Scalar<E> {
121    type Output = Group<E>;
122
123    /// Returns the `product` of `self` and `other`.
124    #[inline]
125    fn mul(self, other: Group<E>) -> Self::Output {
126        Group::from_projective(other.group * *self)
127    }
128}
129
130impl<E: Environment> Mul<&Group<E>> for Scalar<E> {
131    type Output = Group<E>;
132
133    /// Returns the `product` of `self` and `other`.
134    #[inline]
135    fn mul(self, other: &Group<E>) -> Self::Output {
136        Group::from_projective(other.group * *self)
137    }
138}
139
140impl<E: Environment> MulAssign<Scalar<E>> for Group<E> {
141    /// Multiplies `self` by `other`.
142    #[inline]
143    fn mul_assign(&mut self, other: Scalar<E>) {
144        self.group *= *other;
145    }
146}
147
148impl<E: Environment> MulAssign<&Scalar<E>> for Group<E> {
149    /// Multiplies `self` by `other`.
150    #[inline]
151    fn mul_assign(&mut self, other: &Scalar<E>) {
152        self.group *= **other;
153    }
154}
155
156impl<E: Environment> Double for Group<E> {
157    type Output = Group<E>;
158
159    /// Returns the `double` of `self`.
160    #[inline]
161    fn double(&self) -> Self::Output {
162        Group::from_projective(self.group.double())
163    }
164}
165
166impl<E: Environment> Sum<Group<E>> for Group<E> {
167    /// Returns the `sum` of `self` and `other`.
168    #[inline]
169    fn sum<I: Iterator<Item = Group<E>>>(iter: I) -> Self {
170        iter.fold(Group::zero(), |a, b| a + b)
171    }
172}
173
174impl<'a, E: Environment> Sum<&'a Group<E>> for Group<E> {
175    /// Returns the `sum` of `self` and `other`.
176    #[inline]
177    fn sum<I: Iterator<Item = &'a Group<E>>>(iter: I) -> Self {
178        iter.fold(Group::zero(), |a, b| a + b)
179    }
180}