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