From f474245e874f386b279899afbee762290f91b49b Mon Sep 17 00:00:00 2001 From: MITSUNARI Shigeo Date: Mon, 10 May 2021 12:24:45 +0900 Subject: [PATCH] avoid cast in Fp2::inv --- include/mcl/fp_tower.hpp | 39 ++++++++++++++++++--------------------- include/mcl/op.hpp | 2 -- 2 files changed, 18 insertions(+), 23 deletions(-) diff --git a/include/mcl/fp_tower.hpp b/include/mcl/fp_tower.hpp index f75b4c2..247f7e9 100644 --- a/include/mcl/fp_tower.hpp +++ b/include/mcl/fp_tower.hpp @@ -275,8 +275,25 @@ public: { Fp::op_.fp2_mul_xiA_(y.a.v_, x.a.v_); } + /* + x = a + bi + 1 / x = (a - bi) / (a^2 + b^2) + */ + static void inv(Fp2T& y, const Fp2T& x) + { + assert(!x.isZero()); + const Fp& a = x.a; + const Fp& b = x.b; + Fp aa, bb; + Fp::sqr(aa, a); + Fp::sqr(bb, b); + aa += bb; + Fp::inv(aa, aa); // aa = 1 / (a^2 + b^2) + Fp::mul(y.a, a, aa); + Fp::mul(y.b, b, aa); + Fp::neg(y.b, y.b); + } static void addPre(Fp2T& z, const Fp2T& x, const Fp2T& y) { Fp::addPre(z.a, x.a, y.a); Fp::addPre(z.b, x.b, y.b); } - static void inv(Fp2T& y, const Fp2T& x) { Fp::op_.fp2_inv(y.a.v_, x.a.v_); } static void divBy2(Fp2T& y, const Fp2T& x) { Fp::divBy2(y.a, x.a); @@ -446,7 +463,6 @@ public: op.fp2_mul_xiA_ = fp2_mul_xiA; } } - op.fp2_inv = fp2_invW; FpDblT::init(); Fp2DblT::init(); // call init before Fp2::pow because FpDbl is used in Fp2T @@ -630,25 +646,6 @@ private: Fp::sub(y.a, a, b); y.b = t; } - /* - x = a + bi - 1 / x = (a - bi) / (a^2 + b^2) - */ - static void fp2_invW(Unit *y, const Unit *x) - { - const Fp *px = reinterpret_cast(x); - Fp *py = reinterpret_cast(y); - const Fp& a = px[0]; - const Fp& b = px[1]; - Fp aa, bb; - Fp::sqr(aa, a); - Fp::sqr(bb, b); - aa += bb; - Fp::inv(aa, aa); // aa = 1 / (a^2 + b^2) - Fp::mul(py[0], a, aa); - Fp::mul(py[1], b, aa); - Fp::neg(py[1], py[1]); - } }; template diff --git a/include/mcl/op.hpp b/include/mcl/op.hpp index b1085da..25d6bce 100644 --- a/include/mcl/op.hpp +++ b/include/mcl/op.hpp @@ -258,7 +258,6 @@ struct Op { */ int xi_a; // xi = xi_a + u void4u fp2_mulNF; - void2u fp2_inv; void2u fp2_mul_xiA_; uint32_t (*hash)(void *out, uint32_t maxOutSize, const void *msg, uint32_t msgSize); @@ -345,7 +344,6 @@ struct Op { xi_a = 0; fp2_mulNF = 0; - fp2_inv = 0; fp2_mul_xiA_ = 0; hash = 0;