acir_field/
generic_ark.rs1use num_bigint::BigUint;
2
3pub trait AcirField:
5 Sized
6 + std::fmt::Display
7 + std::fmt::Debug
8 + Default
9 + Clone
10 + Copy
11 + std::ops::Neg<Output = Self>
12 + std::ops::Add<Self, Output = Self>
13 + std::ops::Sub<Self, Output = Self>
14 + std::ops::Mul<Self, Output = Self>
15 + std::ops::Div<Self, Output = Self>
16 + std::ops::AddAssign<Self>
17 + std::ops::SubAssign<Self>
18 + PartialOrd
19 + From<usize>
20 + From<u128>
21 + From<u32>
23 + From<bool>
26 + std::hash::Hash
27 + Eq
28 + 'static
29{
30 fn one() -> Self;
31 fn zero() -> Self;
32
33 fn is_zero(&self) -> bool;
34 fn is_one(&self) -> bool;
35 fn pow(&self, exponent: &Self) -> Self;
36
37 fn max_num_bits() -> u32;
43
44 fn max_num_bytes() -> u32;
49
50 fn modulus() -> BigUint;
51
52 fn num_bits(&self) -> u32;
54
55 fn to_u128(self) -> u128;
58
59 fn try_into_u128(self) -> Option<u128>;
61
62 fn to_i128(self) -> i128;
65
66 fn try_into_i128(self) -> Option<i128>;
67
68 fn try_to_u64(&self) -> Option<u64>;
69
70 fn try_to_u32(&self) -> Option<u32>;
71
72 fn inverse(&self) -> Self;
75
76 fn to_hex(self) -> String;
80
81 fn to_short_hex(self) -> String;
85
86 fn from_hex(hex_str: &str) -> Option<Self>;
87
88 fn to_be_bytes(self) -> Vec<u8>;
89
90 fn from_be_bytes_reduce(bytes: &[u8]) -> Self;
92
93 fn from_le_bytes_reduce(bytes: &[u8]) -> Self;
95
96 fn to_le_bytes(self) -> Vec<u8>;
98
99 fn fetch_nearest_bytes(&self, num_bits: usize) -> Vec<u8>;
102}
103
104#[macro_export]
115macro_rules! field_wrapper {
116 ($wrapper:ident, $field:ident) => {
117 #[derive(
118 Clone,
119 Debug,
120 PartialEq,
121 Eq,
122 Hash,
123 PartialOrd,
124 Copy,
125 Default,
126 serde::Serialize,
127 serde::Deserialize,
128 )]
129 struct $wrapper(pub $field);
130
131 impl $crate::AcirField for $wrapper {
132 fn one() -> Self {
133 Self($field::one())
134 }
135
136 fn zero() -> Self {
137 Self($field::zero())
138 }
139
140 fn is_zero(&self) -> bool {
141 self.0.is_zero()
142 }
143
144 fn is_one(&self) -> bool {
145 self.0.is_one()
146 }
147
148 fn pow(&self, exponent: &Self) -> Self {
149 Self(self.0.pow(&exponent.0))
150 }
151
152 fn max_num_bits() -> u32 {
153 $field::max_num_bits()
154 }
155
156 fn max_num_bytes() -> u32 {
157 $field::max_num_bytes()
158 }
159
160 fn modulus() -> ::num_bigint::BigUint {
161 $field::modulus()
162 }
163
164 fn num_bits(&self) -> u32 {
165 self.0.num_bits()
166 }
167
168 fn to_u128(self) -> u128 {
169 self.0.to_u128()
170 }
171
172 fn try_into_u128(self) -> Option<u128> {
173 self.0.try_into_u128()
174 }
175
176 fn try_into_i128(self) -> Option<i128> {
177 self.0.try_into_i128()
178 }
179
180 fn to_i128(self) -> i128 {
181 self.0.to_i128()
182 }
183
184 fn try_to_u64(&self) -> Option<u64> {
185 self.0.try_to_u64()
186 }
187
188 fn try_to_u32(&self) -> Option<u32> {
189 self.0.try_to_u32()
190 }
191
192 fn inverse(&self) -> Self {
193 Self(self.0.inverse())
194 }
195
196 fn to_hex(self) -> String {
197 self.0.to_hex()
198 }
199
200 fn to_short_hex(self) -> String {
201 self.0.to_short_hex()
202 }
203
204 fn from_hex(hex_str: &str) -> Option<Self> {
205 $field::from_hex(hex_str).map(Self)
206 }
207
208 fn to_be_bytes(self) -> Vec<u8> {
209 self.0.to_be_bytes()
210 }
211
212 fn from_be_bytes_reduce(bytes: &[u8]) -> Self {
213 Self($field::from_be_bytes_reduce(bytes))
214 }
215
216 fn from_le_bytes_reduce(bytes: &[u8]) -> Self {
217 Self($field::from_le_bytes_reduce(bytes))
218 }
219
220 fn to_le_bytes(self) -> Vec<u8> {
221 self.0.to_le_bytes()
222 }
223
224 fn fetch_nearest_bytes(&self, num_bits: usize) -> Vec<u8> {
225 self.0.fetch_nearest_bytes(num_bits)
226 }
227 }
228
229 impl From<bool> for $wrapper {
230 fn from(value: bool) -> Self {
231 Self($field::from(value))
232 }
233 }
234
235 impl From<u128> for $wrapper {
236 fn from(value: u128) -> Self {
237 Self($field::from(value))
238 }
239 }
240
241 impl From<u32> for $wrapper {
242 fn from(value: u32) -> Self {
243 Self($field::from(value))
244 }
245 }
246
247 impl From<usize> for $wrapper {
248 fn from(value: usize) -> Self {
249 Self($field::from(value))
250 }
251 }
252
253 impl std::ops::SubAssign<$wrapper> for $wrapper {
254 fn sub_assign(&mut self, rhs: $wrapper) {
255 self.0.sub_assign(rhs.0);
256 }
257 }
258
259 impl std::ops::AddAssign<$wrapper> for $wrapper {
260 fn add_assign(&mut self, rhs: $wrapper) {
261 self.0.add_assign(rhs.0);
262 }
263 }
264
265 impl std::ops::Add<$wrapper> for $wrapper {
266 type Output = Self;
267
268 fn add(self, rhs: $wrapper) -> Self::Output {
269 Self(self.0.add(rhs.0))
270 }
271 }
272
273 impl std::ops::Sub<$wrapper> for $wrapper {
274 type Output = Self;
275
276 fn sub(self, rhs: $wrapper) -> Self::Output {
277 Self(self.0.sub(rhs.0))
278 }
279 }
280
281 impl std::ops::Mul<$wrapper> for $wrapper {
282 type Output = Self;
283
284 fn mul(self, rhs: $wrapper) -> Self::Output {
285 Self(self.0.mul(rhs.0))
286 }
287 }
288
289 impl std::ops::Div<$wrapper> for $wrapper {
290 type Output = Self;
291
292 fn div(self, rhs: $wrapper) -> Self::Output {
293 Self(self.0.div(rhs.0))
294 }
295 }
296
297 impl std::ops::Neg for $wrapper {
298 type Output = Self;
299
300 fn neg(self) -> Self::Output {
301 Self(self.0.neg())
302 }
303 }
304
305 impl std::fmt::Display for $wrapper {
306 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
307 self.0.fmt(f)
308 }
309 }
310 };
311}