From d23b47e9d6c09f6cf0d2c5baade854add4d4c8b2 Mon Sep 17 00:00:00 2001 From: MITSUNARI Shigeo Date: Wed, 7 Nov 2018 10:49:41 +0900 Subject: [PATCH] add Fp2::mul_xi for bls12 --- src/fp_generator.hpp | 81 +++++++++++++++++++++++++++----------------- 1 file changed, 50 insertions(+), 31 deletions(-) diff --git a/src/fp_generator.hpp b/src/fp_generator.hpp index 8c0c3b7..0855ad6 100644 --- a/src/fp_generator.hpp +++ b/src/fp_generator.hpp @@ -3579,16 +3579,6 @@ private: gen_raw_fp_sub(sf.p[0], sf.p[1], sf.p[2], sf.t, false); gen_raw_fp_sub(sf.p[0] + FpByte_, sf.p[1] + FpByte_, sf.p[2] + FpByte_, sf.t, false); } - void2u gen_fp2_mul_xi() - { - align(16); - void2u func = getCurr(); - if (pn_ == 4 && !isFullBit_) { - gen_fp2_mul_xi4(); - return func; - } - return 0; - } /* for only xi_a = 1 y.a = a - b @@ -3597,14 +3587,17 @@ private: void gen_fp2_mul_xi4() { assert(!isFullBit_); -#if 0 - StackFrame sf(this, 2, 10 | UseRDX | UseRCX); + StackFrame sf(this, 2, 11 | UseRDX); + const Reg64& py = sf.p[0]; + const Reg64& px = sf.p[1]; Pack a = sf.t.sub(0, 4); Pack b = sf.t.sub(4, 4); - Pack t(rdx, rcx, sf.t[8], sf.t[9]); - load_rm(a, sf.p[1]); - load_rm(b, sf.p[1] + FpByte_); - for (int i = 0; i < 4; i++) { + Pack t = sf.t.sub(8); + t.append(rdx); + assert(t.size() == 4); + load_rm(a, px); + load_rm(b, px + FpByte_); + for (int i = 0; i < pn_; i++) { mov(t[i], a[i]); if (i == 0) { add(t[i], b[i]); @@ -3613,30 +3606,56 @@ private: } } sub_rr(a, b); - mov(rax, (size_t)p_); + mov(rax, pL_); load_rm(b, rax); sbb(rax, rax); - for (int i = 0; i < 4; i++) { + for (int i = 0; i < pn_; i++) { and_(b[i], rax); } add_rr(a, b); - store_mr(sf.p[0], a); - mov(rax, (size_t)p_); + store_mr(py, a); + mov(rax, pL_); mov_rr(a, t); sub_rm(t, rax); - for (int i = 0; i < 4; i++) { - cmovc(t[i], a[i]); + cmovc_rr(t, a); + store_mr(py + FpByte_, t); + } + void gen_fp2_mul_xi6() + { + assert(!isFullBit_); + StackFrame sf(this, 2, 12); + const Reg64& py = sf.p[0]; + const Reg64& px = sf.p[1]; + Pack a = sf.t.sub(0, 6); + Pack b = sf.t.sub(6); + load_rm(a, px); + mov_rr(b, a); + add_rm(b, px + FpByte_); + sub_rm(a, px + FpByte_); + mov(rax, pL_); + jnc("@f"); + add_rm(a, rax); + L("@@"); + store_mr(py, a); + mov_rr(a, b); + sub_rm(b, rax); + cmovc_rr(b, a); + store_mr(py + FpByte_, b); + } + void2u gen_fp2_mul_xi() + { + if (isFullBit_) return 0; + align(16); + void2u func = getCurr(); + if (pn_ == 4) { + gen_fp2_mul_xi4(); + return func; } - store_mr(sf.p[0] + FpByte_, t); -#else - StackFrame sf(this, 2, 8, 8 * 4); - gen_raw_fp_add(rsp, sf.p[1], sf.p[1] + FpByte_, sf.t, false); - gen_raw_fp_sub(sf.p[0], sf.p[1], sf.p[1] + FpByte_, sf.t, false); - for (int i = 0; i < 4; i++) { - mov(rax, ptr [rsp + i * 8]); - mov(ptr[sf.p[0] + FpByte_ + i * 8], rax); + if (pn_ == 6) { + gen_fp2_mul_xi6(); + return func; } -#endif + return 0; } void2u gen_fp2_neg() {