Clearmatics Libff  0.1
C++ library for Finite Fields and Elliptic Curves
bn_utils.tcc
Go to the documentation of this file.
1 /** @file
2  *****************************************************************************
3  * @author This file is part of libff, developed by SCIPR Lab
4  * and contributors (see AUTHORS).
5  * @copyright MIT license (see LICENSE file)
6  *****************************************************************************/
7 
8 #ifndef BN_UTILS_TCC_
9 #define BN_UTILS_TCC_
10 
11 namespace libff
12 {
13 
14 template<typename FieldT> void bn_batch_invert(std::vector<FieldT> &vec)
15 {
16  std::vector<FieldT> prod;
17  prod.reserve(vec.size());
18 
19  FieldT acc = 1;
20 
21  for (auto el : vec) {
22  assert(!el.isZero());
23  prod.emplace_back(acc);
24  FieldT::mul(acc, acc, el);
25  }
26 
27  FieldT acc_inverse = acc;
28  acc_inverse.inverse();
29 
30  for (long i = vec.size() - 1; i >= 0; --i) {
31  const FieldT old_el = vec[i];
32  FieldT::mul(vec[i], acc_inverse, prod[i]);
33  FieldT::mul(acc_inverse, acc_inverse, old_el);
34  }
35 }
36 
37 } // namespace libff
38 #endif // FIELD_UTILS_TCC_