1use crate::UniformRand;
2use ark_serialize::{
3 CanonicalDeserialize, CanonicalDeserializeWithFlags, CanonicalSerialize,
4 CanonicalSerializeWithFlags, EmptyFlags, Flags,
5};
6use ark_std::{
7 fmt::{Debug, Display},
8 hash::Hash,
9 iter::*,
10 ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign},
11 vec::*,
12};
13
14pub use ark_ff_macros;
15pub use num_traits::{One, Zero};
16use zeroize::Zeroize;
17
18pub mod utils;
19
20#[macro_use]
21pub mod arithmetic;
22
23#[macro_use]
24pub mod models;
25pub use self::models::*;
26
27pub mod field_hashers;
28
29mod prime;
30pub use prime::*;
31
32mod fft_friendly;
33pub use fft_friendly::*;
34
35mod cyclotomic;
36pub use cyclotomic::*;
37
38mod sqrt;
39pub use sqrt::*;
40
41#[cfg(feature = "parallel")]
42use ark_std::cmp::max;
43#[cfg(feature = "parallel")]
44use rayon::prelude::*;
45
46pub trait AdditiveGroup:
47 Eq
48 + 'static
49 + Sized
50 + CanonicalSerialize
51 + CanonicalDeserialize
52 + Copy
53 + Clone
54 + Default
55 + Send
56 + Sync
57 + Hash
58 + Debug
59 + Display
60 + UniformRand
61 + Zeroize
62 + Zero
63 + Neg<Output = Self>
64 + Add<Self, Output = Self>
65 + Sub<Self, Output = Self>
66 + Mul<<Self as AdditiveGroup>::Scalar, Output = Self>
67 + AddAssign<Self>
68 + SubAssign<Self>
69 + MulAssign<<Self as AdditiveGroup>::Scalar>
70 + for<'a> Add<&'a Self, Output = Self>
71 + for<'a> Sub<&'a Self, Output = Self>
72 + for<'a> Mul<&'a <Self as AdditiveGroup>::Scalar, Output = Self>
73 + for<'a> AddAssign<&'a Self>
74 + for<'a> SubAssign<&'a Self>
75 + for<'a> MulAssign<&'a <Self as AdditiveGroup>::Scalar>
76 + for<'a> Add<&'a mut Self, Output = Self>
77 + for<'a> Sub<&'a mut Self, Output = Self>
78 + for<'a> Mul<&'a mut <Self as AdditiveGroup>::Scalar, Output = Self>
79 + for<'a> AddAssign<&'a mut Self>
80 + for<'a> SubAssign<&'a mut Self>
81 + for<'a> MulAssign<&'a mut <Self as AdditiveGroup>::Scalar>
82 + ark_std::iter::Sum<Self>
83 + for<'a> ark_std::iter::Sum<&'a Self>
84{
85 type Scalar: Field;
86
87 const ZERO: Self;
89
90 #[must_use]
92 fn double(&self) -> Self {
93 let mut copy = *self;
94 copy.double_in_place();
95 copy
96 }
97 fn double_in_place(&mut self) -> &mut Self {
99 self.add_assign(*self);
100 self
101 }
102
103 fn neg_in_place(&mut self) -> &mut Self {
105 *self = -(*self);
106 self
107 }
108}
109
110pub trait Field:
160 'static
161 + Copy
162 + Clone
163 + Debug
164 + Display
165 + Default
166 + Send
167 + Sync
168 + Eq
169 + Zero
170 + One
171 + Ord
172 + Neg<Output = Self>
173 + UniformRand
174 + Zeroize
175 + Sized
176 + Hash
177 + CanonicalSerialize
178 + CanonicalSerializeWithFlags
179 + CanonicalDeserialize
180 + CanonicalDeserializeWithFlags
181 + AdditiveGroup<Scalar = Self>
182 + Div<Self, Output = Self>
183 + DivAssign<Self>
184 + for<'a> Div<&'a Self, Output = Self>
185 + for<'a> DivAssign<&'a Self>
186 + for<'a> Div<&'a mut Self, Output = Self>
187 + for<'a> DivAssign<&'a mut Self>
188 + for<'a> core::iter::Product<&'a Self>
189 + From<u128>
190 + From<u64>
191 + From<u32>
192 + From<u16>
193 + From<u8>
194 + From<i128>
195 + From<i64>
196 + From<i32>
197 + From<i16>
198 + From<i8>
199 + From<bool>
200 + Product<Self>
201{
202 type BasePrimeField: PrimeField;
203
204 const SQRT_PRECOMP: Option<SqrtPrecomputation<Self>>;
206
207 const ONE: Self;
209
210 fn characteristic() -> &'static [u64] {
213 Self::BasePrimeField::characteristic()
214 }
215
216 fn extension_degree() -> u64;
219
220 fn to_base_prime_field_elements(&self) -> impl Iterator<Item = Self::BasePrimeField>;
221
222 fn from_base_prime_field_elems(
225 elems: impl IntoIterator<Item = Self::BasePrimeField>,
226 ) -> Option<Self>;
227
228 fn from_base_prime_field(elem: Self::BasePrimeField) -> Self;
237
238 fn from_random_bytes(bytes: &[u8]) -> Option<Self> {
244 Self::from_random_bytes_with_flags::<EmptyFlags>(bytes).map(|f| f.0)
245 }
246
247 fn from_random_bytes_with_flags<F: Flags>(bytes: &[u8]) -> Option<(Self, F)>;
254
255 fn legendre(&self) -> LegendreSymbol;
260
261 #[must_use]
263 fn sqrt(&self) -> Option<Self> {
264 match Self::SQRT_PRECOMP {
265 Some(tv) => tv.sqrt(self),
266 None => unimplemented!(),
267 }
268 }
269
270 fn sqrt_in_place(&mut self) -> Option<&mut Self> {
272 (*self).sqrt().map(|sqrt| {
273 *self = sqrt;
274 self
275 })
276 }
277
278 #[must_use]
280 fn square(&self) -> Self;
281
282 fn square_in_place(&mut self) -> &mut Self;
284
285 #[must_use]
287 fn inverse(&self) -> Option<Self>;
288
289 fn inverse_in_place(&mut self) -> Option<&mut Self>;
292
293 #[inline]
295 fn sum_of_products<const T: usize>(a: &[Self; T], b: &[Self; T]) -> Self {
296 let mut sum = Self::zero();
297 for i in 0..a.len() {
298 sum += a[i] * b[i];
299 }
300 sum
301 }
302
303 fn frobenius_map_in_place(&mut self, power: usize);
306
307 #[must_use]
310 fn frobenius_map(&self, power: usize) -> Self {
311 let mut this = *self;
312 this.frobenius_map_in_place(power);
313 this
314 }
315
316 #[must_use]
319 fn pow<S: AsRef<[u64]>>(&self, exp: S) -> Self {
320 let mut res = Self::one();
321
322 for i in crate::BitIteratorBE::without_leading_zeros(exp) {
323 res.square_in_place();
324
325 if i {
326 res *= self;
327 }
328 }
329 res
330 }
331
332 #[inline]
340 fn pow_with_table<S: AsRef<[u64]>>(powers_of_2: &[Self], exp: S) -> Option<Self> {
341 let mut res = Self::one();
342 for (pow, bit) in crate::BitIteratorLE::without_trailing_zeros(exp).enumerate() {
343 if bit {
344 res *= powers_of_2.get(pow)?;
345 }
346 }
347 Some(res)
348 }
349
350 fn mul_by_base_prime_field(&self, elem: &Self::BasePrimeField) -> Self;
351}
352
353pub fn batch_inversion<F: Field>(v: &mut [F]) {
355 batch_inversion_and_mul(v, &F::one());
356}
357
358#[cfg(not(feature = "parallel"))]
359pub fn batch_inversion_and_mul<F: Field>(v: &mut [F], coeff: &F) {
361 serial_batch_inversion_and_mul(v, coeff);
362}
363
364#[cfg(feature = "parallel")]
365pub fn batch_inversion_and_mul<F: Field>(v: &mut [F], coeff: &F) {
367 let min_elements_per_thread = 1;
369 let num_cpus_available = rayon::current_num_threads();
370 let num_elems = v.len();
371 let num_elem_per_thread = max(num_elems / num_cpus_available, min_elements_per_thread);
372
373 v.par_chunks_mut(num_elem_per_thread).for_each(|mut chunk| {
375 serial_batch_inversion_and_mul(&mut chunk, coeff);
376 });
377}
378
379fn serial_batch_inversion_and_mul<F: Field>(v: &mut [F], coeff: &F) {
382 let mut prod = Vec::with_capacity(v.len());
390 let mut tmp = F::one();
391 for f in v.iter().filter(|f| !f.is_zero()) {
392 tmp.mul_assign(f);
393 prod.push(tmp);
394 }
395
396 tmp = tmp.inverse().unwrap(); tmp *= coeff;
401
402 for (f, s) in v.iter_mut()
404 .rev()
406 .filter(|f| !f.is_zero())
408 .zip(prod.into_iter().rev().skip(1).chain(Some(F::one())))
410 {
411 let new_tmp = tmp * *f;
413 *f = tmp * &s;
414 tmp = new_tmp;
415 }
416}
417
418#[cfg(all(test, feature = "std"))]
419mod std_tests {
420 use crate::BitIteratorLE;
421
422 #[test]
423 fn bit_iterator_le() {
424 let bits = BitIteratorLE::new(&[0, 1 << 10]).collect::<Vec<_>>();
425 dbg!(&bits);
426 assert!(bits[74]);
427 for (i, bit) in bits.into_iter().enumerate() {
428 if i != 74 {
429 assert!(!bit)
430 } else {
431 assert!(bit)
432 }
433 }
434 }
435}
436
437#[cfg(test)]
438mod no_std_tests {
439 use super::*;
440 use ark_std::{str::FromStr, test_rng};
441 use num_bigint::*;
442
443 use ark_test_curves::{
447 ark_ff::{batch_inversion, batch_inversion_and_mul, PrimeField},
448 bls12_381::Fr,
449 };
450
451 #[test]
452 fn test_batch_inversion() {
453 let mut random_coeffs = Vec::<Fr>::new();
454 let vec_size = 1000;
455
456 for _ in 0..=vec_size {
457 random_coeffs.push(Fr::rand(&mut test_rng()));
458 }
459
460 let mut random_coeffs_inv = random_coeffs.clone();
461 batch_inversion::<Fr>(&mut random_coeffs_inv);
462 for i in 0..=vec_size {
463 assert_eq!(random_coeffs_inv[i] * random_coeffs[i], Fr::one());
464 }
465 let rand_multiplier = Fr::rand(&mut test_rng());
466 let mut random_coeffs_inv_shifted = random_coeffs.clone();
467 batch_inversion_and_mul(&mut random_coeffs_inv_shifted, &rand_multiplier);
468 for i in 0..=vec_size {
469 assert_eq!(
470 random_coeffs_inv_shifted[i] * random_coeffs[i],
471 rand_multiplier
472 );
473 }
474 }
475
476 #[test]
477 pub fn test_from_ints() {
478 let felt2 = Fr::one() + Fr::one();
479 let felt16 = felt2 * felt2 * felt2 * felt2;
480
481 assert_eq!(Fr::from(1u8), Fr::one());
482 assert_eq!(Fr::from(1u16), Fr::one());
483 assert_eq!(Fr::from(1u32), Fr::one());
484 assert_eq!(Fr::from(1u64), Fr::one());
485 assert_eq!(Fr::from(1u128), Fr::one());
486 assert_eq!(Fr::from(-1i8), -Fr::one());
487 assert_eq!(Fr::from(-1i64), -Fr::one());
488
489 assert_eq!(Fr::from(0), Fr::zero());
490
491 assert_eq!(Fr::from(-16i32), -felt16);
492 assert_eq!(Fr::from(16u32), felt16);
493 assert_eq!(Fr::from(16i64), felt16);
494
495 assert_eq!(Fr::from(-2i128), -felt2);
496 assert_eq!(Fr::from(2u16), felt2);
497 }
498
499 #[test]
500 fn test_from_into_biguint() {
501 let mut rng = ark_std::test_rng();
502
503 let modulus_bits = Fr::MODULUS_BIT_SIZE;
504 let modulus: num_bigint::BigUint = Fr::MODULUS.into();
505
506 let mut rand_bytes = Vec::new();
507 for _ in 0..(2 * modulus_bits / 8) {
508 rand_bytes.push(u8::rand(&mut rng));
509 }
510
511 let rand = BigUint::from_bytes_le(&rand_bytes);
512
513 let a: BigUint = Fr::from(rand.clone()).into();
514 let b = rand % modulus;
515
516 assert_eq!(a, b);
517 }
518
519 #[test]
520 fn test_from_be_bytes_mod_order() {
521 use ark_std::{rand::Rng, string::ToString};
527 use ark_test_curves::ark_ff::BigInteger;
528 use num_bigint::BigUint;
529
530 let ref_modulus = BigUint::from_bytes_be(&Fr::MODULUS.to_bytes_be());
531
532 let mut test_vectors = vec![
533 vec![0u8],
535 vec![1u8],
537 vec![255u8],
539 vec![1u8, 0u8],
541 vec![1u8, 0u8, 255u8],
543 vec![
545 115u8, 237u8, 167u8, 83u8, 41u8, 157u8, 125u8, 72u8, 51u8, 57u8, 216u8, 8u8, 9u8,
546 161u8, 216u8, 5u8, 83u8, 189u8, 164u8, 2u8, 255u8, 254u8, 91u8, 254u8, 255u8,
547 255u8, 255u8, 255u8, 0u8, 0u8, 0u8,
548 ],
549 vec![
551 115u8, 237u8, 167u8, 83u8, 41u8, 157u8, 125u8, 72u8, 51u8, 57u8, 216u8, 8u8, 9u8,
552 161u8, 216u8, 5u8, 83u8, 189u8, 164u8, 2u8, 255u8, 254u8, 91u8, 254u8, 255u8,
553 255u8, 255u8, 255u8, 0u8, 0u8, 1u8,
554 ],
555 vec![
557 115u8, 237u8, 167u8, 83u8, 41u8, 157u8, 125u8, 72u8, 51u8, 57u8, 216u8, 8u8, 9u8,
558 161u8, 216u8, 5u8, 83u8, 189u8, 164u8, 2u8, 255u8, 254u8, 91u8, 254u8, 255u8,
559 255u8, 255u8, 255u8, 0u8, 0u8, 0u8, 0u8,
560 ],
561 vec![
563 115u8, 237u8, 167u8, 83u8, 41u8, 157u8, 125u8, 72u8, 51u8, 57u8, 216u8, 8u8, 9u8,
564 161u8, 216u8, 5u8, 83u8, 189u8, 164u8, 2u8, 255u8, 254u8, 91u8, 254u8, 255u8,
565 255u8, 255u8, 255u8, 0u8, 0u8, 0u8, 1u8,
566 ],
567 vec![
569 115u8, 237u8, 167u8, 83u8, 41u8, 157u8, 125u8, 72u8, 51u8, 57u8, 216u8, 8u8, 9u8,
570 161u8, 216u8, 5u8, 83u8, 189u8, 164u8, 2u8, 255u8, 254u8, 91u8, 254u8, 255u8,
571 255u8, 255u8, 255u8, 0u8, 0u8, 0u8, 2u8,
572 ],
573 vec![
575 231u8, 219u8, 78u8, 166u8, 83u8, 58u8, 250u8, 144u8, 102u8, 115u8, 176u8, 16u8,
576 19u8, 67u8, 176u8, 10u8, 167u8, 123u8, 72u8, 5u8, 255u8, 252u8, 183u8, 253u8,
577 255u8, 255u8, 255u8, 254u8, 0u8, 0u8, 0u8, 2u8,
578 ],
579 vec![
581 115u8, 237u8, 167u8, 83u8, 41u8, 157u8, 125u8, 72u8, 51u8, 57u8, 216u8, 8u8, 9u8,
582 161u8, 216u8, 5u8, 83u8, 189u8, 164u8, 2u8, 255u8, 254u8, 91u8, 254u8, 255u8,
583 255u8, 255u8, 255u8, 0u8, 0u8, 0u8, 1u8, 0u8,
584 ],
585 vec![
587 1u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8,
588 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8,
589 17u8,
590 ],
591 vec![
593 1u8, 115u8, 237u8, 167u8, 83u8, 41u8, 157u8, 125u8, 72u8, 51u8, 57u8, 216u8, 8u8,
594 9u8, 161u8, 216u8, 5u8, 83u8, 189u8, 164u8, 2u8, 255u8, 254u8, 91u8, 254u8, 255u8,
595 255u8, 255u8, 255u8, 0u8, 0u8, 0u8, 20u8,
596 ],
597 vec![
599 1u8, 0u8, 115u8, 237u8, 167u8, 83u8, 41u8, 157u8, 125u8, 72u8, 51u8, 57u8, 216u8,
600 8u8, 9u8, 161u8, 216u8, 5u8, 83u8, 189u8, 164u8, 2u8, 255u8, 254u8, 91u8, 254u8,
601 255u8, 255u8, 255u8, 255u8, 0u8, 0u8, 0u8, 82u8,
602 ],
603 ];
604 for i in 1..512 {
606 let mut rng = test_rng();
607 let data: Vec<u8> = (0..i).map(|_| rng.gen()).collect();
608 test_vectors.push(data);
609 }
610 for i in test_vectors {
611 let mut expected_biguint = BigUint::from_bytes_be(&i);
612 expected_biguint =
614 expected_biguint.modpow(&BigUint::from_bytes_be(&[1u8]), &ref_modulus);
615 let expected_string = expected_biguint.to_string();
616 let expected = Fr::from_str(&expected_string).unwrap();
617 let actual = Fr::from_be_bytes_mod_order(&i);
618 assert_eq!(expected, actual, "failed on test {:?}", i);
619 }
620 }
621}