From 106f0ad15feb05cb5d7e7c996f99d38496ec45e8 Mon Sep 17 00:00:00 2001 From: MITSUNARI Shigeo Date: Mon, 1 Jun 2015 11:16:38 +0900 Subject: [PATCH] FixedFp is a simple function --- include/mcl/fp.hpp | 21 +--- include/mcl/fp_base.hpp | 255 ++++++++++++++++++++-------------------- 2 files changed, 130 insertions(+), 146 deletions(-) diff --git a/include/mcl/fp.hpp b/include/mcl/fp.hpp index 0f1f0a7..eec16ee 100644 --- a/include/mcl/fp.hpp +++ b/include/mcl/fp.hpp @@ -88,7 +88,6 @@ public: op_.sub = subG; op_.mul = mulG; const Unit *p = op_.p; -#if 1 #ifdef USE_MONT_FP if (bitLen <= 128) { fp::MontFp::init(op_, p); } #if CYBOZU_OS_BIT == 32 @@ -108,25 +107,7 @@ public: #endif else { static fp::MontFp f; f.init(op_, p); } #else - if (bitLen <= 128) { fp::FixedFp::init(op_, p); } -#if CYBOZU_OS_BIT == 32 - else if (bitLen <= 160) { static fp::FixedFp f; f.init(op_, p); } -#endif - else if (bitLen <= 192) { static fp::FixedFp f; f.init(op_, p); } -#if CYBOZU_OS_BIT == 32 - else if (bitLen <= 224) { static fp::FixedFp f; f.init(op_, p); } -#endif - else if (bitLen <= 256) { static fp::FixedFp f; f.init(op_, p); } - else if (bitLen <= 384) { static fp::FixedFp f; f.init(op_, p); } - else if (bitLen <= 448) { static fp::FixedFp f; f.init(op_, p); } -#if CYBOZU_OS_BIT == 32 - else if (bitLen <= 544) { static fp::FixedFp f; f.init(op_, p); } -#else - else if (bitLen <= 576) { static fp::FixedFp f; f.init(op_, p); } -#endif - else { static fp::FixedFp f; f.init(op_, p); } -#endif - assert(op_.N <= maxUnitN); + initOp(op_, bitLen, p); #endif op_.bitLen = bitLen; std::cout << "QQQ : mp=" << op_.mp << std::endl; diff --git a/include/mcl/fp_base.hpp b/include/mcl/fp_base.hpp index d39c3f1..feba4ed 100644 --- a/include/mcl/fp_base.hpp +++ b/include/mcl/fp_base.hpp @@ -300,159 +300,162 @@ struct OpeFunc { } }; -template -struct FixedFp { +inline void initOp(Op& op, size_t bitN, const fp::Unit*) +{ typedef fp::Unit Unit; - static const size_t N = (bitN + sizeof(Unit) * 8 - 1) / (sizeof(Unit) * 8); - static inline void init(Op& op, const Unit*) - { - assert(N >= 2); - assert(sizeof(mp_limb_t) == sizeof(Unit)); - - op.N = N; - if (bitN <= 128) { - op.isZero = OpeFunc<128>::isZeroC; - op.clear = OpeFunc<128>::clearC; - op.copy = OpeFunc<128>::copyC; - op.negG = OpeFunc<128>::negC; - op.invG = OpeFunc<128>::invC; - op.addG = OpeFunc<128>::addC; - op.subG = OpeFunc<128>::subC; - op.mulPreG = OpeFunc<128>::mulPreC; - op.modG = OpeFunc<128>::modC; + assert(sizeof(mp_limb_t) == sizeof(Unit)); + const size_t UnitBitN = sizeof(Unit) * 8; + + if (bitN <= 128) { + op.N = 128 / UnitBitN; + op.isZero = OpeFunc<128>::isZeroC; + op.clear = OpeFunc<128>::clearC; + op.copy = OpeFunc<128>::copyC; + op.negG = OpeFunc<128>::negC; + op.invG = OpeFunc<128>::invC; + op.addG = OpeFunc<128>::addC; + op.subG = OpeFunc<128>::subC; + op.mulPreG = OpeFunc<128>::mulPreC; + op.modG = OpeFunc<128>::modC; #ifdef MCL_USE_LLVM - op.addG = mcl_fp_add128S; - op.subG = mcl_fp_sub128S; - op.mulPreG = mcl_fp_mulPre128; + op.addG = mcl_fp_add128S; + op.subG = mcl_fp_sub128S; + op.mulPreG = mcl_fp_mulPre128; #endif - } else + } else #if CYBOZU_OS_BIT == 32 - if (bitN <= 160) { - op.isZero = OpeFunc<160>::isZeroC; - op.clear = OpeFunc<160>::clearC; - op.copy = OpeFunc<160>::copyC; - op.negG = OpeFunc<160>::negC; - op.invG = OpeFunc<160>::invC; - op.addG = OpeFunc<160>::addC; - op.subG = OpeFunc<160>::subC; - op.mulPreG = OpeFunc<160>::mulPreC; - op.modG = OpeFunc<160>::modC; + if (bitN <= 160) { + op.N = 160 / UnitBitN; + op.isZero = OpeFunc<160>::isZeroC; + op.clear = OpeFunc<160>::clearC; + op.copy = OpeFunc<160>::copyC; + op.negG = OpeFunc<160>::negC; + op.invG = OpeFunc<160>::invC; + op.addG = OpeFunc<160>::addC; + op.subG = OpeFunc<160>::subC; + op.mulPreG = OpeFunc<160>::mulPreC; + op.modG = OpeFunc<160>::modC; #ifdef MCL_USE_LLVM - op.addG = mcl_fp_add160S; - op.subG = mcl_fp_sub160S; - op.mulPreG = mcl_fp_mulPre160; + op.addG = mcl_fp_add160S; + op.subG = mcl_fp_sub160S; + op.mulPreG = mcl_fp_mulPre160; #endif - } else + } else #endif - if (bitN <= 192) { - op.isZero = OpeFunc<192>::isZeroC; - op.clear = OpeFunc<192>::clearC; - op.copy = OpeFunc<192>::copyC; - op.negG = OpeFunc<192>::negC; - op.invG = OpeFunc<192>::invC; - op.addG = OpeFunc<192>::addC; - op.subG = OpeFunc<192>::subC; - op.mulPreG = OpeFunc<192>::mulPreC; - op.modG = OpeFunc<192>::modC; + if (bitN <= 192) { + op.N = 192 / UnitBitN; + op.isZero = OpeFunc<192>::isZeroC; + op.clear = OpeFunc<192>::clearC; + op.copy = OpeFunc<192>::copyC; + op.negG = OpeFunc<192>::negC; + op.invG = OpeFunc<192>::invC; + op.addG = OpeFunc<192>::addC; + op.subG = OpeFunc<192>::subC; + op.mulPreG = OpeFunc<192>::mulPreC; + op.modG = OpeFunc<192>::modC; #ifdef MCL_USE_LLVM - op.addG = mcl_fp_add192S; - op.subG = mcl_fp_sub192S; - op.mulPreG = mcl_fp_mulPre192; + op.addG = mcl_fp_add192S; + op.subG = mcl_fp_sub192S; + op.mulPreG = mcl_fp_mulPre192; #endif - } else + } else #if CYBOZU_OS_BIT == 32 - if (bitN <= 224) { - op.isZero = OpeFunc<224>::isZeroC; - op.clear = OpeFunc<224>::clearC; - op.copy = OpeFunc<224>::copyC; - op.negG = OpeFunc<224>::negC; - op.invG = OpeFunc<224>::invC; - op.addG = OpeFunc<224>::addC; - op.subG = OpeFunc<224>::subC; - op.mulPreG = OpeFunc<224>::mulPreC; - op.modG = OpeFunc<224>::modC; + if (bitN <= 224) { + op.N = 224 / UnitBitN; + op.isZero = OpeFunc<224>::isZeroC; + op.clear = OpeFunc<224>::clearC; + op.copy = OpeFunc<224>::copyC; + op.negG = OpeFunc<224>::negC; + op.invG = OpeFunc<224>::invC; + op.addG = OpeFunc<224>::addC; + op.subG = OpeFunc<224>::subC; + op.mulPreG = OpeFunc<224>::mulPreC; + op.modG = OpeFunc<224>::modC; #ifdef MCL_USE_LLVM - op.addG = mcl_fp_add224S; - op.subG = mcl_fp_sub224S; - op.mulPreG = mcl_fp_mulPre224; + op.addG = mcl_fp_add224S; + op.subG = mcl_fp_sub224S; + op.mulPreG = mcl_fp_mulPre224; #endif - } else + } else #endif - if (bitN <= 256) { - op.isZero = OpeFunc<256>::isZeroC; - op.clear = OpeFunc<256>::clearC; - op.copy = OpeFunc<256>::copyC; - op.negG = OpeFunc<256>::negC; - op.invG = OpeFunc<256>::invC; - op.addG = OpeFunc<256>::addC; - op.subG = OpeFunc<256>::subC; - op.mulPreG = OpeFunc<256>::mulPreC; - op.modG = OpeFunc<256>::modC; + if (bitN <= 256) { + op.N = 256 / UnitBitN; + op.isZero = OpeFunc<256>::isZeroC; + op.clear = OpeFunc<256>::clearC; + op.copy = OpeFunc<256>::copyC; + op.negG = OpeFunc<256>::negC; + op.invG = OpeFunc<256>::invC; + op.addG = OpeFunc<256>::addC; + op.subG = OpeFunc<256>::subC; + op.mulPreG = OpeFunc<256>::mulPreC; + op.modG = OpeFunc<256>::modC; #ifdef MCL_USE_LLVM - op.addG = mcl_fp_add256S; - op.subG = mcl_fp_sub256S; - op.mulPreG = mcl_fp_mulPre256; + op.addG = mcl_fp_add256S; + op.subG = mcl_fp_sub256S; + op.mulPreG = mcl_fp_mulPre256; #endif - } else - if (bitN <= 384) { - op.isZero = OpeFunc<384>::isZeroC; - op.clear = OpeFunc<384>::clearC; - op.copy = OpeFunc<384>::copyC; - op.negG = OpeFunc<384>::negC; - op.invG = OpeFunc<384>::invC; - op.addG = OpeFunc<384>::addC; - op.subG = OpeFunc<384>::subC; - op.mulPreG = OpeFunc<384>::mulPreC; - op.modG = OpeFunc<384>::modC; + } else + if (bitN <= 384) { + op.N = 384 / UnitBitN; + op.isZero = OpeFunc<384>::isZeroC; + op.clear = OpeFunc<384>::clearC; + op.copy = OpeFunc<384>::copyC; + op.negG = OpeFunc<384>::negC; + op.invG = OpeFunc<384>::invC; + op.addG = OpeFunc<384>::addC; + op.subG = OpeFunc<384>::subC; + op.mulPreG = OpeFunc<384>::mulPreC; + op.modG = OpeFunc<384>::modC; #ifdef MCL_USE_LLVM - op.addG = mcl_fp_add384S; - op.subG = mcl_fp_sub384S; - op.mulPreG = mcl_fp_mulPre384; + op.addG = mcl_fp_add384S; + op.subG = mcl_fp_sub384S; + op.mulPreG = mcl_fp_mulPre384; #endif - } else + } else #if CYBOZU_OS_BIT == 64 - if (bitN <= 576) { - op.isZero = OpeFunc<576>::isZeroC; - op.clear = OpeFunc<576>::clearC; - op.copy = OpeFunc<576>::copyC; - op.negG = OpeFunc<576>::negC; - op.invG = OpeFunc<576>::invC; - op.addG = OpeFunc<576>::addC; - op.subG = OpeFunc<576>::subC; - op.mulPreG = OpeFunc<576>::mulPreC; - op.modG = OpeFunc<576>::modC; + if (bitN <= 576) { + op.N = 576 / UnitBitN; + op.isZero = OpeFunc<576>::isZeroC; + op.clear = OpeFunc<576>::clearC; + op.copy = OpeFunc<576>::copyC; + op.negG = OpeFunc<576>::negC; + op.invG = OpeFunc<576>::invC; + op.addG = OpeFunc<576>::addC; + op.subG = OpeFunc<576>::subC; + op.mulPreG = OpeFunc<576>::mulPreC; + op.modG = OpeFunc<576>::modC; #ifdef MCL_USE_LLVM - op.addG = mcl_fp_add576S; - op.subG = mcl_fp_sub576S; - op.mulPreG = mcl_fp_mulPre576; + op.addG = mcl_fp_add576S; + op.subG = mcl_fp_sub576S; + op.mulPreG = mcl_fp_mulPre576; #endif - } + } #else - if (bitN <= 544) { - op.isZero = OpeFunc<544>::isZeroC; - op.clear = OpeFunc<544>::clearC; - op.copy = OpeFunc<544>::copyC; - op.negG = OpeFunc<544>::negC; - op.invG = OpeFunc<544>::invC; - op.addG = OpeFunc<544>::addC; - op.subG = OpeFunc<544>::subC; - op.mulPreG = OpeFunc<544>::mulPreC; - op.modG = OpeFunc<544>::modC; + if (bitN <= 544) { + op.N = 544 / UnitBitN; + op.isZero = OpeFunc<544>::isZeroC; + op.clear = OpeFunc<544>::clearC; + op.copy = OpeFunc<544>::copyC; + op.negG = OpeFunc<544>::negC; + op.invG = OpeFunc<544>::invC; + op.addG = OpeFunc<544>::addC; + op.subG = OpeFunc<544>::subC; + op.mulPreG = OpeFunc<544>::mulPreC; + op.modG = OpeFunc<544>::modC; #ifdef MCL_USE_LLVM - op.addG = mcl_fp_add544S; - op.subG = mcl_fp_sub544S; - op.mulPreG = mcl_fp_mulPre544; + op.addG = mcl_fp_add544S; + op.subG = mcl_fp_sub544S; + op.mulPreG = mcl_fp_mulPre544; #endif - } + } #endif #ifdef MCL_USE_LLVM - if (op.mp == mpz_class("0xfffffffffffffffffffffffffffffffeffffffffffffffff")) { - op.mul = &mcl_fp_mul_NIST_P192; // slower than MontFp192 - } -#endif + if (op.mp == mpz_class("0xfffffffffffffffffffffffffffffffeffffffffffffffff")) { + op.mul = &mcl_fp_mul_NIST_P192; // slower than MontFp192 } -}; +#endif +} #ifdef USE_MONT_FP