Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
bn254_if.hpp
Go to the documentation of this file.
1#pragma once
9#ifndef MIE_ATE_USE_GMP
10 #define MIE_ATE_USE_GMP
11#endif
12#include "bn.h"
13
14inline void SystemInit() throw(std::exception)
15{
17}
18
19class Fp2;
20class Fp12;
21class Ec1;
22class Ec2;
23
24class Mpz {
25 mpz_class self_;
26 friend class Fp;
27 friend class Fp2;
28 friend class Fp12;
29 friend class Ec1;
30 friend class Ec2;
31public:
32 Mpz() {}
33 Mpz(const Mpz& x) : self_(x.self_) {}
34 Mpz(int x) throw(std::exception) : self_(x) {}
35 Mpz(const std::string& str) throw(std::exception)
36 {
37 set(str);
38 }
39 void set(int x) throw(std::exception) { self_ = x; }
40 void set(const std::string& str) throw(std::exception)
41 {
42 self_.set_str(str, 0);
43 }
44 std::string toString() const throw(std::exception)
45 {
46 return self_.get_str();
47 }
48 bool equals(const Mpz& rhs) const { return self_ == rhs.self_; }
49 int compareTo(const Mpz& rhs) const { return mpz_cmp(self_.get_mpz_t(), rhs.self_.get_mpz_t()); }
50 void add(const Mpz& rhs) throw(std::exception) { self_ += rhs.self_; }
51 void sub(const Mpz& rhs) throw(std::exception) { self_ -= rhs.self_; }
52 void mul(const Mpz& rhs) throw(std::exception) { self_ *= rhs.self_; }
53 void mod(const Mpz& rhs) throw(std::exception) { self_ %= rhs.self_; }
54};
55
56class Fp {
57 ::bn::Fp self_;
58 friend class Fp2;
59 friend class Ec1;
60public:
61 Fp() {}
62 Fp(const Fp& x) : self_(x.self_) {}
63 Fp(int x) : self_(x) {}
64 Fp(const std::string& str) throw(std::exception)
65 {
66 self_.set(str);
67 }
68 void set(int x) { self_ = x; }
69 void set(const std::string& str) throw(std::exception)
70 {
71 self_.set(str);
72 }
73 std::string toString() const throw(std::exception)
74 {
75 return self_.toString();
76 }
77 bool equals(const Fp& rhs) const { return self_ == rhs.self_; }
78 void add(const Fp& rhs) throw(std::exception) { self_ += rhs.self_; }
79 void sub(const Fp& rhs) throw(std::exception) { self_ -= rhs.self_; }
80 void mul(const Fp& rhs) throw(std::exception) { self_ *= rhs.self_; }
81 void power(const Mpz& x)
82 {
83 self_ = mie::power(self_, x.self_);
84 }
85};
86
87class Fp2 {
88 ::bn::Fp2 self_;
89 friend class Ec2;
90public:
91 Fp2() {}
92 Fp2(const Fp2& x) : self_(x.self_) {}
93 Fp2(int a) : self_(a) {}
94 Fp2(int a, int b) : self_(a, b) {}
95 Fp2(const Fp& a, const Fp& b) throw(std::exception)
96 : self_(a.self_, b.self_)
97 {
98 }
99 Fp2(const std::string& a, const std::string& b) throw(std::exception)
100 : self_(Fp(a).self_, Fp(b).self_)
101 {
102 }
103 Fp& getA() { return *reinterpret_cast<Fp*>(&self_.a_); }
104 Fp& getB() { return *reinterpret_cast<Fp*>(&self_.b_); }
105 void set(const std::string& str) throw(std::exception)
106 {
107 self_.set(str);
108 }
109 std::string toString() const throw(std::exception)
110 {
111 return self_.toString();
112 }
113 bool equals(const Fp2& rhs) const { return self_ == rhs.self_; }
114 void add(const Fp2& rhs) throw(std::exception) { self_ += rhs.self_; }
115 void sub(const Fp2& rhs) throw(std::exception) { self_ -= rhs.self_; }
116 void mul(const Fp2& rhs) throw(std::exception) { self_ *= rhs.self_; }
117 void power(const Mpz& x)
118 {
119 self_ = mie::power(self_, x.self_);
120 }
121};
122
123class Fp12 {
124 ::bn::Fp12 self_;
125public:
126 Fp12() {}
127 Fp12(const Fp12& x) : self_(x.self_) {}
128 Fp12(int x) : self_(x) {}
129 void set(const std::string& str) throw(std::exception)
130 {
131 std::istringstream iss(str);
132 iss >> self_;
133 }
134 std::string toString() const throw(std::exception)
135 {
136 std::ostringstream oss;
137 oss << self_;
138 return oss.str();
139 }
140 bool equals(const Fp12& rhs) const { return self_ == rhs.self_; }
141 void add(const Fp12& rhs) throw(std::exception) { self_ += rhs.self_; }
142 void sub(const Fp12& rhs) throw(std::exception) { self_ -= rhs.self_; }
143 void mul(const Fp12& rhs) throw(std::exception) { self_ *= rhs.self_; }
144 void pairing(const Ec2& ec2, const Ec1& ec1);
145 void power(const Mpz& x)
146 {
147 self_ = mie::power(self_, x.self_);
148 }
149};
150
151class Ec1 {
152 ::bn::Ec1 self_;
153 friend class Fp12;
154public:
155 Ec1() { self_.clear(); }
156 Ec1(const Ec1& x) : self_(x.self_) {}
157 Ec1(const Fp& x, const Fp& y) throw(std::exception)
158 {
159 set(x, y);
160 }
161 Ec1(const Fp& x, const Fp& y, const Fp& z) throw(std::exception)
162 {
163 set(x, y, z);
164 }
165 bool isValid() const { return self_.isValid(); }
166 void set(const Fp& x, const Fp& y) throw(std::exception)
167 {
168 self_.set(x.self_, y.self_);
169 }
170 void set(const Fp& x, const Fp& y, const Fp& z) throw(std::exception)
171 {
172 self_.set(x.self_, y.self_, z.self_);
173 }
174 void set(const std::string& str) throw(std::exception)
175 {
176 std::istringstream iss(str);
177 iss >> self_;
178 }
179 std::string toString() const throw(std::exception)
180 {
181 std::ostringstream oss;
182 oss << self_;
183 return oss.str();
184 }
185 bool equals(const Ec1& rhs) const { return self_ == rhs.self_; }
186 bool isZero() const { return self_.isZero(); }
187 void clear() { self_.clear(); }
188 void dbl() { ::bn::Ec1::dbl(self_, self_); }
189 void neg() { ::bn::Ec1::neg(self_, self_); }
190 void add(const Ec1& rhs) { ::bn::Ec1::add(self_, self_, rhs.self_); }
191 void sub(const Ec1& rhs) { ::bn::Ec1::sub(self_, self_, rhs.self_); }
192 void mul(const Mpz& rhs) { ::bn::Ec1::mul(self_, self_, rhs.self_); }
193 Fp& getX() { return *reinterpret_cast<Fp*>(&self_.p[0]); }
194 Fp& getY() { return *reinterpret_cast<Fp*>(&self_.p[1]); }
195 Fp& getZ() { return *reinterpret_cast<Fp*>(&self_.p[2]); }
196};
197
198class Ec2 {
199 ::bn::Ec2 self_;
200 friend class Fp12;
201public:
202 Ec2() {}
203 Ec2(const Ec2& x) : self_(x.self_) {}
204 Ec2(const Fp2& x, const Fp2& y) throw(std::exception)
205 {
206 set(x, y);
207 }
208 Ec2(const Fp2& x, const Fp2& y, const Fp2& z) throw(std::exception)
209 {
210 set(x, y, z);
211 }
212 bool isValid() const { return self_.isValid(); }
213 void set(const Fp2& x, const Fp2& y) throw(std::exception)
214 {
215 self_.set(x.self_, y.self_);
216 }
217 void set(const Fp2& x, const Fp2& y, const Fp2& z) throw(std::exception)
218 {
219 self_.set(x.self_, y.self_, z.self_);
220 }
221 void set(const std::string& str) throw(std::exception)
222 {
223 std::istringstream iss(str);
224 iss >> self_;
225 }
226 std::string toString() const throw(std::exception)
227 {
228 std::ostringstream oss;
229 oss << self_;
230 return oss.str();
231 }
232 bool equals(const Ec2& rhs) const { return self_ == rhs.self_; }
233 bool isZero() const { return self_.isZero(); }
234 void clear() { self_.clear(); }
235 void dbl() { ::bn::Ec2::dbl(self_, self_); }
236 void neg() { ::bn::Ec2::neg(self_, self_); }
237 void add(const Ec2& rhs) { ::bn::Ec2::add(self_, self_, rhs.self_); }
238 void sub(const Ec2& rhs) { ::bn::Ec2::sub(self_, self_, rhs.self_); }
239 void mul(const Mpz& rhs) { ::bn::Ec2::mul(self_, self_, rhs.self_); }
240 Fp2& getX() { return *reinterpret_cast<Fp2*>(&self_.p[0]); }
241 Fp2& getY() { return *reinterpret_cast<Fp2*>(&self_.p[1]); }
242 Fp2& getZ() { return *reinterpret_cast<Fp2*>(&self_.p[2]); }
243};
244
245void Fp12::pairing(const Ec2& ec2, const Ec1& ec1)
246{
247 ::bn::opt_atePairing(self_, ec2.self_, ec1.self_);
248}
249
250inline const Mpz& GetParamR()
251{
252 static Mpz r("16798108731015832284940804142231733909759579603404752749028378864165570215949");
253 return r;
254}
255
256#ifdef _MSC_VER
257#if _MSC_VER == 1900
258#ifdef _DEBUG
259#pragma comment(lib, "14/mpird.lib")
260#pragma comment(lib, "14/mpirxxd.lib")
261#else
262#pragma comment(lib, "14/mpir.lib")
263#pragma comment(lib, "14/mpirxx.lib")
264#endif
265#elif _MSC_VER == 1800
266#ifdef _DEBUG
267#pragma comment(lib, "12/mpird.lib")
268#pragma comment(lib, "12/mpirxxd.lib")
269#else
270#pragma comment(lib, "12/mpir.lib")
271#pragma comment(lib, "12/mpirxx.lib")
272#endif
273#else
274#ifdef _DEBUG
275#pragma comment(lib, "mpird.lib")
276#pragma comment(lib, "mpirxxd.lib")
277#else
278#pragma comment(lib, "mpir.lib")
279#pragma comment(lib, "mpirxx.lib")
280#endif
281#endif
282#endif
const Mpz & GetParamR()
Definition bn254_if.hpp:250
void SystemInit()
Definition bn254_if.hpp:14
const mie::Vuint & r
Definition bn.cpp:28
BN parameter.
void set(const Fp &x, const Fp &y)
Definition bn254_if.hpp:166
void clear()
Definition bn254_if.hpp:187
void sub(const Ec1 &rhs)
Definition bn254_if.hpp:191
void neg()
Definition bn254_if.hpp:189
Ec1(const Fp &x, const Fp &y)
Definition bn254_if.hpp:157
std::string toString() const
Definition bn254_if.hpp:179
bool equals(const Ec1 &rhs) const
Definition bn254_if.hpp:185
void dbl()
Definition bn254_if.hpp:188
Ec1()
Definition bn254_if.hpp:155
bool isZero() const
Definition bn254_if.hpp:186
bool isValid() const
Definition bn254_if.hpp:165
Fp & getZ()
Definition bn254_if.hpp:195
Ec1(const Fp &x, const Fp &y, const Fp &z)
Definition bn254_if.hpp:161
void mul(const Mpz &rhs)
Definition bn254_if.hpp:192
void set(const std::string &str)
Definition bn254_if.hpp:174
Ec1(const Ec1 &x)
Definition bn254_if.hpp:156
void add(const Ec1 &rhs)
Definition bn254_if.hpp:190
Fp & getX()
Definition bn254_if.hpp:193
void set(const Fp &x, const Fp &y, const Fp &z)
Definition bn254_if.hpp:170
Fp & getY()
Definition bn254_if.hpp:194
void sub(const Ec2 &rhs)
Definition bn254_if.hpp:238
Ec2(const Fp2 &x, const Fp2 &y)
Definition bn254_if.hpp:204
bool equals(const Ec2 &rhs) const
Definition bn254_if.hpp:232
void neg()
Definition bn254_if.hpp:236
bool isValid() const
Definition bn254_if.hpp:212
std::string toString() const
Definition bn254_if.hpp:226
void add(const Ec2 &rhs)
Definition bn254_if.hpp:237
Fp2 & getZ()
Definition bn254_if.hpp:242
void set(const std::string &str)
Definition bn254_if.hpp:221
void set(const Fp2 &x, const Fp2 &y)
Definition bn254_if.hpp:213
void dbl()
Definition bn254_if.hpp:235
Ec2()
Definition bn254_if.hpp:202
void set(const Fp2 &x, const Fp2 &y, const Fp2 &z)
Definition bn254_if.hpp:217
Fp2 & getX()
Definition bn254_if.hpp:240
Ec2(const Ec2 &x)
Definition bn254_if.hpp:203
void mul(const Mpz &rhs)
Definition bn254_if.hpp:239
bool isZero() const
Definition bn254_if.hpp:233
Fp2 & getY()
Definition bn254_if.hpp:241
void clear()
Definition bn254_if.hpp:234
Ec2(const Fp2 &x, const Fp2 &y, const Fp2 &z)
Definition bn254_if.hpp:208
void mul(const Fp12 &rhs)
Definition bn254_if.hpp:143
bool equals(const Fp12 &rhs) const
Definition bn254_if.hpp:140
Fp12(int x)
Definition bn254_if.hpp:128
void sub(const Fp12 &rhs)
Definition bn254_if.hpp:142
void pairing(const Ec2 &ec2, const Ec1 &ec1)
Definition bn254_if.hpp:245
Fp12(const Fp12 &x)
Definition bn254_if.hpp:127
void power(const Mpz &x)
Definition bn254_if.hpp:145
void set(const std::string &str)
Definition bn254_if.hpp:129
std::string toString() const
Definition bn254_if.hpp:134
void add(const Fp12 &rhs)
Definition bn254_if.hpp:141
std::string toString() const
Definition bn254_if.hpp:109
Fp & getB()
Definition bn254_if.hpp:104
Fp & getA()
Definition bn254_if.hpp:103
bool equals(const Fp2 &rhs) const
Definition bn254_if.hpp:113
void mul(const Fp2 &rhs)
Definition bn254_if.hpp:116
void power(const Mpz &x)
Definition bn254_if.hpp:117
Fp2(int a, int b)
Definition bn254_if.hpp:94
void add(const Fp2 &rhs)
Definition bn254_if.hpp:114
Fp2(const std::string &a, const std::string &b)
Definition bn254_if.hpp:99
Fp2(const Fp2 &x)
Definition bn254_if.hpp:92
Fp2()
Definition bn254_if.hpp:91
void set(const std::string &str)
Definition bn254_if.hpp:105
Fp2(int a)
Definition bn254_if.hpp:93
Fp2(const Fp &a, const Fp &b)
Definition bn254_if.hpp:95
void sub(const Fp2 &rhs)
Definition bn254_if.hpp:115
Fp(const std::string &str)
Definition bn254_if.hpp:64
void power(const Mpz &x)
Definition bn254_if.hpp:81
Fp(int x)
Definition bn254_if.hpp:63
void set(const std::string &str)
Definition bn254_if.hpp:69
bool equals(const Fp &rhs) const
Definition bn254_if.hpp:77
void set(int x)
Definition bn254_if.hpp:68
std::string toString() const
Definition bn254_if.hpp:73
void mul(const Fp &rhs)
Definition bn254_if.hpp:80
Fp(const Fp &x)
Definition bn254_if.hpp:62
void sub(const Fp &rhs)
Definition bn254_if.hpp:79
Fp()
Definition bn254_if.hpp:61
void add(const Fp &rhs)
Definition bn254_if.hpp:78
std::string toString() const
Definition bn254_if.hpp:44
void sub(const Mpz &rhs)
Definition bn254_if.hpp:51
Mpz(const std::string &str)
Definition bn254_if.hpp:35
void mod(const Mpz &rhs)
Definition bn254_if.hpp:53
void add(const Mpz &rhs)
Definition bn254_if.hpp:50
void set(int x)
Definition bn254_if.hpp:39
int compareTo(const Mpz &rhs) const
Definition bn254_if.hpp:49
Mpz()
Definition bn254_if.hpp:32
void mul(const Mpz &rhs)
Definition bn254_if.hpp:52
Mpz(const Mpz &x)
Definition bn254_if.hpp:33
void set(const std::string &str)
Definition bn254_if.hpp:40
Mpz(int x)
Definition bn254_if.hpp:34
bool equals(const Mpz &rhs) const
Definition bn254_if.hpp:48
Definition bn.h:2815
T p[3]
Definition bn.h:2817
bool isValid() const
bool isZero() const
Definition bn.h:2911
void clear()
Definition bn.h:2860
static void dbl(EcT &R, const EcT &P)
Definition bn.h:2867
static void sub(EcT &R, const EcT &P, const EcT &Q)
Definition bn.h:2875
static void add(EcT &R, const EcT &P, const EcT &Q)
Definition bn.h:2871
static void neg(EcT &R, const EcT &P)
Definition bn.h:2881
static void mul(EcT &R, const EcT &P, const N &y)
Definition bn.h:2888
void set(const T &x, const T &y, bool verify=true)
Definition bn.h:2842
Definition zm2.h:18
void set(int x)
Definition zm2.h:47
std::string toString(int base=10) const
Definition zm2.h:250
void opt_atePairing(Fp12T< Fp6T< Fp2T< Fp > > > &f, const Fp2T< Fp > Q[2], const Fp P[2])
Definition bn.h:2720
T power(const T &x, const S &y)
Definition zm.h:1389
Definition name.hpp:106
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition pointer.h:1181
Definition bn.h:348
static void init(const CurveParam &cp, int mode=-1, bool useMulx=true)
Definition bn.h:206
bool set