bls12_381/
util.rs
1#[inline(always)]
3pub const fn adc(a: u64, b: u64, carry: u64) -> (u64, u64) {
4 let ret = (a as u128) + (b as u128) + (carry as u128);
5 (ret as u64, (ret >> 64) as u64)
6}
7
8#[inline(always)]
10pub const fn sbb(a: u64, b: u64, borrow: u64) -> (u64, u64) {
11 let ret = (a as u128).wrapping_sub((b as u128) + ((borrow >> 63) as u128));
12 (ret as u64, (ret >> 64) as u64)
13}
14
15#[inline(always)]
17pub const fn mac(a: u64, b: u64, c: u64, carry: u64) -> (u64, u64) {
18 let ret = (a as u128) + ((b as u128) * (c as u128)) + (carry as u128);
19 (ret as u64, (ret >> 64) as u64)
20}
21
22macro_rules! impl_add_binop_specify_output {
23 ($lhs:ident, $rhs:ident, $output:ident) => {
24 impl<'b> Add<&'b $rhs> for $lhs {
25 type Output = $output;
26
27 #[inline]
28 fn add(self, rhs: &'b $rhs) -> $output {
29 &self + rhs
30 }
31 }
32
33 impl<'a> Add<$rhs> for &'a $lhs {
34 type Output = $output;
35
36 #[inline]
37 fn add(self, rhs: $rhs) -> $output {
38 self + &rhs
39 }
40 }
41
42 impl Add<$rhs> for $lhs {
43 type Output = $output;
44
45 #[inline]
46 fn add(self, rhs: $rhs) -> $output {
47 &self + &rhs
48 }
49 }
50 };
51}
52
53macro_rules! impl_sub_binop_specify_output {
54 ($lhs:ident, $rhs:ident, $output:ident) => {
55 impl<'b> Sub<&'b $rhs> for $lhs {
56 type Output = $output;
57
58 #[inline]
59 fn sub(self, rhs: &'b $rhs) -> $output {
60 &self - rhs
61 }
62 }
63
64 impl<'a> Sub<$rhs> for &'a $lhs {
65 type Output = $output;
66
67 #[inline]
68 fn sub(self, rhs: $rhs) -> $output {
69 self - &rhs
70 }
71 }
72
73 impl Sub<$rhs> for $lhs {
74 type Output = $output;
75
76 #[inline]
77 fn sub(self, rhs: $rhs) -> $output {
78 &self - &rhs
79 }
80 }
81 };
82}
83
84macro_rules! impl_binops_additive_specify_output {
85 ($lhs:ident, $rhs:ident, $output:ident) => {
86 impl_add_binop_specify_output!($lhs, $rhs, $output);
87 impl_sub_binop_specify_output!($lhs, $rhs, $output);
88 };
89}
90
91macro_rules! impl_binops_multiplicative_mixed {
92 ($lhs:ident, $rhs:ident, $output:ident) => {
93 impl<'b> Mul<&'b $rhs> for $lhs {
94 type Output = $output;
95
96 #[inline]
97 fn mul(self, rhs: &'b $rhs) -> $output {
98 &self * rhs
99 }
100 }
101
102 impl<'a> Mul<$rhs> for &'a $lhs {
103 type Output = $output;
104
105 #[inline]
106 fn mul(self, rhs: $rhs) -> $output {
107 self * &rhs
108 }
109 }
110
111 impl Mul<$rhs> for $lhs {
112 type Output = $output;
113
114 #[inline]
115 fn mul(self, rhs: $rhs) -> $output {
116 &self * &rhs
117 }
118 }
119 };
120}
121
122macro_rules! impl_binops_additive {
123 ($lhs:ident, $rhs:ident) => {
124 impl_binops_additive_specify_output!($lhs, $rhs, $lhs);
125
126 impl SubAssign<$rhs> for $lhs {
127 #[inline]
128 fn sub_assign(&mut self, rhs: $rhs) {
129 *self = &*self - &rhs;
130 }
131 }
132
133 impl AddAssign<$rhs> for $lhs {
134 #[inline]
135 fn add_assign(&mut self, rhs: $rhs) {
136 *self = &*self + &rhs;
137 }
138 }
139
140 impl<'b> SubAssign<&'b $rhs> for $lhs {
141 #[inline]
142 fn sub_assign(&mut self, rhs: &'b $rhs) {
143 *self = &*self - rhs;
144 }
145 }
146
147 impl<'b> AddAssign<&'b $rhs> for $lhs {
148 #[inline]
149 fn add_assign(&mut self, rhs: &'b $rhs) {
150 *self = &*self + rhs;
151 }
152 }
153 };
154}
155
156macro_rules! impl_binops_multiplicative {
157 ($lhs:ident, $rhs:ident) => {
158 impl_binops_multiplicative_mixed!($lhs, $rhs, $lhs);
159
160 impl MulAssign<$rhs> for $lhs {
161 #[inline]
162 fn mul_assign(&mut self, rhs: $rhs) {
163 *self = &*self * &rhs;
164 }
165 }
166
167 impl<'b> MulAssign<&'b $rhs> for $lhs {
168 #[inline]
169 fn mul_assign(&mut self, rhs: &'b $rhs) {
170 *self = &*self * rhs;
171 }
172 }
173 };
174}