Clearmatics Libff  0.1
C++ library for Finite Fields and Elliptic Curves
bn128_pairing.cpp
Go to the documentation of this file.
1 
17 #include <sstream>
18 
19 namespace libff
20 {
21 
23 {
24  return (
25  this->P[0] == other.P[0] && this->P[1] == other.P[1] &&
26  this->P[2] == other.P[2]);
27 }
28 
29 std::ostream &operator<<(std::ostream &out, const bn128_ate_G1_precomp &prec_P)
30 {
31  for (size_t i = 0; i < 3; ++i) {
32 #ifndef BINARY_OUTPUT
33  out << prec_P.P[i] << "\n";
34 #else
35  out.write((char *)&prec_P.P[i], sizeof(prec_P.P[i]));
36 #endif
37  }
38  return out;
39 }
40 
41 std::istream &operator>>(std::istream &in, bn128_ate_G1_precomp &prec_P)
42 {
43  for (size_t i = 0; i < 3; ++i) {
44 #ifndef BINARY_OUTPUT
45  in >> prec_P.P[i];
46  consume_newline(in);
47 #else
48  in.read((char *)&prec_P.P[i], sizeof(prec_P.P[i]));
49 #endif
50  }
51  return in;
52 }
53 
55 {
56  if (!(this->Q[0] == other.Q[0] && this->Q[1] == other.Q[1] &&
57  this->Q[2] == other.Q[2] &&
58  this->coeffs.size() == other.coeffs.size())) {
59  return false;
60  }
61 
62  /* work around for upstream serialization bug */
63  for (size_t i = 0; i < this->coeffs.size(); ++i) {
64  std::stringstream this_ss, other_ss;
65  this_ss << this->coeffs[i];
66  other_ss << other.coeffs[i];
67  if (this_ss.str() != other_ss.str()) {
68  return false;
69  }
70  }
71 
72  return true;
73 }
74 
75 std::ostream &operator<<(std::ostream &out, const bn128_ate_G2_precomp &prec_Q)
76 {
77  for (size_t i = 0; i < 3; ++i) {
78 #ifndef BINARY_OUTPUT
79  out << prec_Q.Q[i].a_ << "\n";
80  out << prec_Q.Q[i].b_ << "\n";
81 #else
82  out.write((char *)&prec_Q.Q[i].a_, sizeof(prec_Q.Q[i].a_));
83  out.write((char *)&prec_Q.Q[i].b_, sizeof(prec_Q.Q[i].b_));
84 #endif
85  }
86 
87  out << prec_Q.coeffs.size() << "\n";
88 
89  for (size_t i = 0; i < prec_Q.coeffs.size(); ++i) {
90 #ifndef BINARY_OUTPUT
91  out << prec_Q.coeffs[i].a_.a_ << "\n";
92  out << prec_Q.coeffs[i].a_.b_ << "\n";
93  out << prec_Q.coeffs[i].b_.a_ << "\n";
94  out << prec_Q.coeffs[i].b_.b_ << "\n";
95  out << prec_Q.coeffs[i].c_.a_ << "\n";
96  out << prec_Q.coeffs[i].c_.b_ << "\n";
97 #else
98  out.write(
99  (char *)&prec_Q.coeffs[i].a_.a_, sizeof(prec_Q.coeffs[i].a_.a_));
100  out.write(
101  (char *)&prec_Q.coeffs[i].a_.b_, sizeof(prec_Q.coeffs[i].a_.b_));
102  out.write(
103  (char *)&prec_Q.coeffs[i].b_.a_, sizeof(prec_Q.coeffs[i].b_.a_));
104  out.write(
105  (char *)&prec_Q.coeffs[i].b_.b_, sizeof(prec_Q.coeffs[i].b_.b_));
106  out.write(
107  (char *)&prec_Q.coeffs[i].c_.a_, sizeof(prec_Q.coeffs[i].c_.a_));
108  out.write(
109  (char *)&prec_Q.coeffs[i].c_.b_, sizeof(prec_Q.coeffs[i].c_.b_));
110 #endif
111  }
112 
113  return out;
114 }
115 
116 std::istream &operator>>(std::istream &in, bn128_ate_G2_precomp &prec_Q)
117 {
118  for (size_t i = 0; i < 3; ++i) {
119 #ifndef BINARY_OUTPUT
120  in >> prec_Q.Q[i].a_;
121  consume_newline(in);
122  in >> prec_Q.Q[i].b_;
123  consume_newline(in);
124 #else
125  in.read((char *)&prec_Q.Q[i].a_, sizeof(prec_Q.Q[i].a_));
126  in.read((char *)&prec_Q.Q[i].b_, sizeof(prec_Q.Q[i].b_));
127 #endif
128  }
129 
130  size_t count;
131  in >> count;
132  consume_newline(in);
133  prec_Q.coeffs.resize(count);
134  for (size_t i = 0; i < count; ++i) {
135 #ifndef BINARY_OUTPUT
136  in >> prec_Q.coeffs[i].a_.a_;
137  consume_newline(in);
138  in >> prec_Q.coeffs[i].a_.b_;
139  consume_newline(in);
140  in >> prec_Q.coeffs[i].b_.a_;
141  consume_newline(in);
142  in >> prec_Q.coeffs[i].b_.b_;
143  consume_newline(in);
144  in >> prec_Q.coeffs[i].c_.a_;
145  consume_newline(in);
146  in >> prec_Q.coeffs[i].c_.b_;
147  consume_newline(in);
148 #else
149  in.read(
150  (char *)&prec_Q.coeffs[i].a_.a_, sizeof(prec_Q.coeffs[i].a_.a_));
151  in.read(
152  (char *)&prec_Q.coeffs[i].a_.b_, sizeof(prec_Q.coeffs[i].a_.b_));
153  in.read(
154  (char *)&prec_Q.coeffs[i].b_.a_, sizeof(prec_Q.coeffs[i].b_.a_));
155  in.read(
156  (char *)&prec_Q.coeffs[i].b_.b_, sizeof(prec_Q.coeffs[i].b_.b_));
157  in.read(
158  (char *)&prec_Q.coeffs[i].c_.a_, sizeof(prec_Q.coeffs[i].c_.a_));
159  in.read(
160  (char *)&prec_Q.coeffs[i].c_.b_, sizeof(prec_Q.coeffs[i].c_.b_));
161 #endif
162  }
163  return in;
164 }
165 
167 {
168  enter_block("Call to bn128_ate_precompute_G1");
169 
170  bn128_ate_G1_precomp result;
171  bn::Fp P_coord[3];
172  P.fill_coord(P_coord);
173  bn::ecop::NormalizeJac(result.P, P_coord);
174 
175  leave_block("Call to bn128_ate_precompute_G1");
176  return result;
177 }
178 
180 {
181  enter_block("Call to bn128_ate_precompute_G2");
182 
183  bn128_ate_G2_precomp result;
184  bn::Fp2 Q_coord[3];
185  Q.fill_coord(Q_coord);
186  bn::components::precomputeG2(result.coeffs, result.Q, Q_coord);
187 
188  leave_block("Call to bn128_ate_precompute_G2");
189  return result;
190 }
191 
193  const bn128_ate_G1_precomp &prec_P, const bn128_ate_G2_precomp &prec_Q)
194 {
195  bn128_Fq12 f;
196  bn::components::millerLoop(f.elem, prec_Q.coeffs, prec_P.P);
197  return f;
198 }
199 
201  const bn128_ate_G1_precomp &prec_P1,
202  const bn128_ate_G2_precomp &prec_Q1,
203  const bn128_ate_G1_precomp &prec_P2,
204  const bn128_ate_G2_precomp &prec_Q2)
205 {
206  bn128_Fq12 f;
207  bn::components::millerLoop2(
208  f.elem, prec_Q1.coeffs, prec_P1.P, prec_Q2.coeffs, prec_P2.P);
209  return f;
210 }
211 
213 {
214  enter_block("Call to bn128_final_exponentiation");
215  bn128_GT eltcopy = elt;
216  eltcopy.elem.final_exp();
217  leave_block("Call to bn128_final_exponentiation");
218  return eltcopy;
219 }
220 } // namespace libff
libff::bn128_GT
Definition: bn128_gt.hpp:23
libff::enter_block
void enter_block(const std::string &msg, const bool indent)
Definition: profiling.cpp:271
libff::bn128_ate_G1_precomp::P
bn::Fp P[3]
Definition: bn128_pairing.hpp:23
libff
Definition: ffi.cpp:8
libff::bn128_G1
Definition: bn128_g1.hpp:23
libff::bn128_G2::fill_coord
void fill_coord(bn::Fp2 coord[3]) const
Definition: bn128_g2.hpp:49
libff::operator>>
std::istream & operator>>(std::istream &in, alt_bn128_G1 &g)
Definition: alt_bn128_g1.cpp:446
libff::bn128_GT::elem
bn::Fp12 elem
Definition: bn128_gt.hpp:27
bn128_gt.hpp
libff::bn128_ate_G2_precomp
Definition: bn128_pairing.hpp:34
libff::bn128_ate_G2_precomp::coeffs
std::vector< bn128_ate_ell_coeffs > coeffs
Definition: bn128_pairing.hpp:36
libff::bn128_ate_G1_precomp
Definition: bn128_pairing.hpp:22
libff::bn128_ate_precompute_G1
bn128_ate_G1_precomp bn128_ate_precompute_G1(const bn128_G1 &P)
Definition: bn128_pairing.cpp:166
libff::bn128_ate_G2_precomp::operator==
bool operator==(const bn128_ate_G2_precomp &other) const
Definition: bn128_pairing.cpp:54
libff::bn128_ate_G1_precomp::operator==
bool operator==(const bn128_ate_G1_precomp &other) const
Definition: bn128_pairing.cpp:22
libff::bn128_ate_precompute_G2
bn128_ate_G2_precomp bn128_ate_precompute_G2(const bn128_G2 &Q)
Definition: bn128_pairing.cpp:179
bn128_pairing.hpp
libff::bn128_final_exponentiation
bn128_GT bn128_final_exponentiation(const bn128_Fq12 &elt)
Definition: bn128_pairing.cpp:212
bn128_g1.hpp
bn128_init.hpp
libff::bn128_double_ate_miller_loop
bn128_Fq12 bn128_double_ate_miller_loop(const bn128_ate_G1_precomp &prec_P1, const bn128_ate_G2_precomp &prec_Q1, const bn128_ate_G1_precomp &prec_P2, const bn128_ate_G2_precomp &prec_Q2)
Definition: bn128_pairing.cpp:200
libff::operator<<
std::ostream & operator<<(std::ostream &out, const alt_bn128_G1 &g)
Definition: alt_bn128_g1.cpp:436
libff::bn128_G1::fill_coord
void fill_coord(bn::Fp coord[3]) const
Definition: bn128_g1.hpp:48
libff::bn128_ate_G2_precomp::Q
bn::Fp2 Q[3]
Definition: bn128_pairing.hpp:35
profiling.hpp
libff::leave_block
void leave_block(const std::string &msg, const bool indent)
Definition: profiling.cpp:305
libff::bn128_ate_miller_loop
bn128_Fq12 bn128_ate_miller_loop(const bn128_ate_G1_precomp &prec_P, const bn128_ate_G2_precomp &prec_Q)
Definition: bn128_pairing.cpp:192
bn128_g2.hpp
libff::consume_newline
void consume_newline(std::istream &in)
libff::bn128_G2
Definition: bn128_g2.hpp:24