rename vars in mapto

update-fork
MITSUNARI Shigeo 4 years ago
parent 2488fa3f6d
commit 7333742229
  1. 46
      include/mcl/mapto_wb19.hpp
  2. 32
      test/mapto_wb19_test.cpp

@ -70,10 +70,11 @@ template<class F> int PointT<F>::specialA_;
template<class Fp, class G1, class Fp2, class G2> template<class Fp, class G1, class Fp2, class G2>
struct MapTo_WB19 { struct MapTo_WB19 {
typedef local::PointT<Fp2> Point; typedef local::PointT<Fp> E1;
typedef local::PointT<Fp2> E2;
mpz_class sqrtConst; // (p^2 - 9) / 16 mpz_class sqrtConst; // (p^2 - 9) / 16
Fp2 Ep_a; Fp2 g2A;
Fp2 Ep_b; Fp2 g2B;
Fp2 root4[4]; Fp2 root4[4];
Fp2 etas[4]; Fp2 etas[4];
Fp2 xnum[4]; Fp2 xnum[4];
@ -94,14 +95,17 @@ struct MapTo_WB19 {
void init() void init()
{ {
bool b; bool b;
Ep_a.a = 0; g2A.a = 0;
Ep_a.b = 240; g2A.b = 240;
Ep_b.a = 1012; g2B.a = 1012;
Ep_b.b = 1012; g2B.b = 1012;
Point::a_.clear(); E1::a_.clear();
Point::b_.a = 4; E1::b_ = 4;
Point::b_.b = 4; E1::specialA_ = ec::Zero;
Point::specialA_ = ec::Zero; E2::a_.clear();
E2::b_.a = 4;
E2::b_.b = 4;
E2::specialA_ = ec::Zero;
sqrtConst = Fp::getOp().mp; sqrtConst = Fp::getOp().mp;
sqrtConst *= sqrtConst; sqrtConst *= sqrtConst;
sqrtConst -= 9; sqrtConst -= 9;
@ -297,7 +301,7 @@ struct MapTo_WB19 {
} }
} }
// refer (xnum, xden, ynum, yden) // refer (xnum, xden, ynum, yden)
void iso3(G2& Q, const Point& P) const void iso3(G2& Q, const E2& P) const
{ {
Fp2 zpows[3]; Fp2 zpows[3];
Fp2::sqr(zpows[0], P.z); Fp2::sqr(zpows[0], P.z);
@ -321,7 +325,7 @@ struct MapTo_WB19 {
Q.y *= t; Q.y *= t;
} }
// refer (g1xnum, g1xden, g1ynum, g1yden) // refer (g1xnum, g1xden, g1ynum, g1yden)
void iso11(G1& Q, const Point& P) const void iso11(G1& Q, const E2& P) const
{ {
Fp2 zpows[3]; Fp2 zpows[3];
Fp2::sqr(zpows[0], P.z); Fp2::sqr(zpows[0], P.z);
@ -440,7 +444,7 @@ struct MapTo_WB19 {
pt[1] *= y; pt[1] *= y;
} }
// https://github.com/algorand/bls_sigs_ref // https://github.com/algorand/bls_sigs_ref
void osswu2_help(Point& P, const Fp2& t) const void osswu2_help(E2& P, const Fp2& t) const
{ {
Fp2 t2, t2xi; Fp2 t2, t2xi;
Fp2::sqr(t2, t); Fp2::sqr(t2, t);
@ -452,20 +456,20 @@ struct MapTo_WB19 {
den += den2; den += den2;
Fp2 x0_num, x0_den; Fp2 x0_num, x0_den;
Fp2::add(x0_num, den, 1); Fp2::add(x0_num, den, 1);
x0_num *= Ep_b; x0_num *= g2B;
if (den.isZero()) { if (den.isZero()) {
mul_xi(x0_den, Ep_a); mul_xi(x0_den, g2A);
} else { } else {
Fp2::mul(x0_den, -Ep_a, den); Fp2::mul(x0_den, -g2A, den);
} }
Fp2 x0_den2, x0_den3, gx0_den, gx0_num; Fp2 x0_den2, x0_den3, gx0_den, gx0_num;
Fp2::sqr(x0_den2, x0_den); Fp2::sqr(x0_den2, x0_den);
Fp2::mul(x0_den3, x0_den2, x0_den); Fp2::mul(x0_den3, x0_den2, x0_den);
gx0_den = x0_den3; gx0_den = x0_den3;
Fp2::mul(gx0_num, Ep_b, gx0_den); Fp2::mul(gx0_num, g2B, gx0_den);
Fp2 tmp, tmp1, tmp2; Fp2 tmp, tmp1, tmp2;
Fp2::mul(tmp, Ep_a, x0_num); Fp2::mul(tmp, g2A, x0_num);
tmp *= x0_den2; tmp *= x0_den2;
gx0_num += tmp; gx0_num += tmp;
Fp2::sqr(tmp, x0_num); Fp2::sqr(tmp, x0_num);
@ -538,10 +542,10 @@ struct MapTo_WB19 {
} }
void opt_swu2_map(G2& P, const Fp2& t, const Fp2 *t2 = 0) const void opt_swu2_map(G2& P, const Fp2& t, const Fp2 *t2 = 0) const
{ {
Point Pp; E2 Pp;
osswu2_help(Pp, t); osswu2_help(Pp, t);
if (t2) { if (t2) {
Point P2; E2 P2;
osswu2_help(P2, *t2); osswu2_help(P2, *t2);
ec::addJacobi(Pp, Pp, P2); ec::addJacobi(Pp, Pp, P2);
} }

@ -12,7 +12,7 @@ using namespace mcl;
using namespace mcl::bn; using namespace mcl::bn;
typedef mcl::MapTo_WB19<Fp, G1, Fp2, G2> MapTo; typedef mcl::MapTo_WB19<Fp, G1, Fp2, G2> MapTo;
typedef MapTo::Point Point; typedef MapTo::E2 E2;
void dump(const void *msg, size_t msgSize) void dump(const void *msg, size_t msgSize)
{ {
@ -72,8 +72,8 @@ void set(Fp2& x, const Fp2Str& s)
x.b.setStr(s.b); x.b.setStr(s.b);
} }
template<class Point> template<class E2>
void set(Point& P, const PointStr& s) void set(E2& P, const PointStr& s)
{ {
set(P.x, s.x); set(P.x, s.x);
set(P.y, s.y); set(P.y, s.y);
@ -128,7 +128,7 @@ bool sqr_div(const MapTo& mapto, Fp2& z, const Fp2& u, const Fp2& v)
} }
// Proj // Proj
void py_ecc_iso_map_G2(const MapTo& mapto, G2& Q, const Point& P) void py_ecc_iso_map_G2(const MapTo& mapto, G2& Q, const E2& P)
{ {
Fp2 zpows[3]; Fp2 zpows[3];
zpows[0] = P.z; zpows[0] = P.z;
@ -148,7 +148,7 @@ void py_ecc_iso_map_G2(const MapTo& mapto, G2& Q, const Point& P)
} }
// https://github.com/ethereum/py_ecc // https://github.com/ethereum/py_ecc
void py_ecc_optimized_swu_G2(const MapTo& mapto, Point& P, const Fp2& t) void py_ecc_optimized_swu_G2(const MapTo& mapto, E2& P, const Fp2& t)
{ {
Fp2 t2, t2xi, t2xi2; Fp2 t2, t2xi, t2xi2;
Fp2::sqr(t2, t); Fp2::sqr(t2, t);
@ -158,11 +158,11 @@ void py_ecc_optimized_swu_G2(const MapTo& mapto, Point& P, const Fp2& t)
// (t^2 * xi)^2 + (t^2 * xi) // (t^2 * xi)^2 + (t^2 * xi)
Fp2::add(deno, t2xi2, t2xi); Fp2::add(deno, t2xi2, t2xi);
Fp2::add(nume, deno, 1); Fp2::add(nume, deno, 1);
nume *= mapto.Ep_b; nume *= mapto.g2B;
if (deno.isZero()) { if (deno.isZero()) {
mapto.mul_xi(deno, mapto.Ep_a); mapto.mul_xi(deno, mapto.g2A);
} else { } else {
deno *= -mapto.Ep_a; deno *= -mapto.g2A;
} }
Fp2 u, v; Fp2 u, v;
{ {
@ -170,8 +170,8 @@ void py_ecc_optimized_swu_G2(const MapTo& mapto, Point& P, const Fp2& t)
Fp2::sqr(deno2, deno); Fp2::sqr(deno2, deno);
Fp2::mul(v, deno2, deno); Fp2::mul(v, deno2, deno);
Fp2::mul(u, mapto.Ep_b, v); Fp2::mul(u, mapto.g2B, v);
Fp2::mul(tmp, mapto.Ep_a, nume); Fp2::mul(tmp, mapto.g2A, nume);
tmp *= deno2; tmp *= deno2;
u += tmp; u += tmp;
Fp2::sqr(tmp, nume); Fp2::sqr(tmp, nume);
@ -210,7 +210,7 @@ void py_ecc_optimized_swu_G2(const MapTo& mapto, Point& P, const Fp2& t)
// Proj // Proj
void py_ecc_map_to_curve_G2(const MapTo& mapto, G2& out, const Fp2& t) void py_ecc_map_to_curve_G2(const MapTo& mapto, G2& out, const Fp2& t)
{ {
Point P; E2 P;
py_ecc_optimized_swu_G2(mapto, P, t); py_ecc_optimized_swu_G2(mapto, P, t);
py_ecc_iso_map_G2(mapto, out, P); py_ecc_iso_map_G2(mapto, out, P);
} }
@ -521,7 +521,7 @@ void osswu2_helpTest(const T& mapto)
}; };
for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(tbl); i++) { for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(tbl); i++) {
Fp2 t, x, y, z; Fp2 t, x, y, z;
Point P; E2 P;
set(t, tbl[i].t); set(t, tbl[i].t);
set(x, tbl[i].x); set(x, tbl[i].x);
set(y, tbl[i].y); set(y, tbl[i].y);
@ -587,11 +587,11 @@ void addTest()
}, },
}; };
for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(tbl); i++) { for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(tbl); i++) {
Point P, Q, R; E2 P, Q, R;
set(P, tbl[i].P); set(P, tbl[i].P);
set(Q, tbl[i].Q); set(Q, tbl[i].Q);
set(R, tbl[i].R); set(R, tbl[i].R);
Point E; E2 E;
ec::addJacobi(E, P, Q); ec::addJacobi(E, P, Q);
CYBOZU_TEST_ASSERT(R.isEqual(E)); CYBOZU_TEST_ASSERT(R.isEqual(E));
} }
@ -642,7 +642,7 @@ void iso3Test(const T& mapto)
"0xb7b36b9b1bbcf801d21ca5164aa9a0e71df2b4710c67dc0cd275b786800935fc29defbdf9c7e23dc84e26af13ba761d", "0xb7b36b9b1bbcf801d21ca5164aa9a0e71df2b4710c67dc0cd275b786800935fc29defbdf9c7e23dc84e26af13ba761d",
} }
}; };
typename T::Point P; typename T::E2 P;
G2 Q1, Q2; G2 Q1, Q2;
set(P, Ps); set(P, Ps);
set(Q1, Qs); set(Q1, Qs);
@ -748,7 +748,7 @@ void py_eccTest2(const T& mapto)
}; };
Fp2 t; Fp2 t;
set(t, ts); set(t, ts);
Point p, q; E2 p, q;
py_ecc_optimized_swu_G2(mapto, p, t); py_ecc_optimized_swu_G2(mapto, p, t);
set(q, out1s); set(q, out1s);
CYBOZU_TEST_EQUAL(p.x, q.x); CYBOZU_TEST_EQUAL(p.x, q.x);

Loading…
Cancel
Save