From cf4c1b2fedc972e7fd49ffda9cb1ca7ff9f84341 Mon Sep 17 00:00:00 2001 From: MITSUNARI Shigeo Date: Mon, 18 Apr 2016 16:04:42 +0900 Subject: [PATCH] make operator.hpp and move power into it --- include/mcl/fp.hpp | 49 ++----------------------- include/mcl/operator.hpp | 77 ++++++++++++++++++++++++++++++++++++++++ include/mcl/util.hpp | 31 ---------------- 3 files changed, 80 insertions(+), 77 deletions(-) create mode 100644 include/mcl/operator.hpp diff --git a/include/mcl/fp.hpp b/include/mcl/fp.hpp index d5b1354..18fb75e 100644 --- a/include/mcl/fp.hpp +++ b/include/mcl/fp.hpp @@ -27,6 +27,7 @@ #include #include #include +#include namespace mcl { @@ -52,8 +53,9 @@ int64_t getInt64(bool *pb, fp::Block& b, const fp::Op& op); } // mcl::fp template -class FpT { +class FpT : public fp::Operator > { typedef fp::Unit Unit; + typedef fp::Operator > Operator; static const size_t maxSize = (maxBitSize + fp::UnitBitSize - 1) / fp::UnitBitSize; static fp::Op op_; template friend class FpT; @@ -311,28 +313,6 @@ public: static inline void inv(FpT& y, const FpT& x) { op_.fp_invOp(y.v_, x.v_, op_); } static inline void neg(FpT& y, const FpT& x) { op_.fp_neg(y.v_, x.v_); } static inline void sqr(FpT& y, const FpT& x) { op_.fp_sqr(y.v_, x.v_); } - static inline void div(FpT& z, const FpT& x, const FpT& y) - { - FpT rev; - inv(rev, y); - mul(z, x, rev); - } - template - static inline void power(FpT& z, const FpT& x, const FpT& y) - { - fp::Block b; - y.getBlock(b); - powerArray(z, x, b.p, b.n, false); - } - static inline void power(FpT& z, const FpT& x, int y) - { - const Unit u = abs(y); - powerArray(z, x, &u, 1, y < 0); - } - static inline void power(FpT& z, const FpT& x, const mpz_class& y) - { - powerArray(z, x, Gmp::getUnit(y), abs(y.get_mpz_t()->_mp_size), y < 0); - } bool isZero() const { return op_.fp_isZero(v_); } bool isOne() const { return fp::isEqualArray(v_, op_.oneRep, op_.N); } /* @@ -365,15 +345,6 @@ public: static inline size_t getModBitLen() { return op_.bitSize; } bool operator==(const FpT& rhs) const { return fp::isEqualArray(v_, rhs.v_, op_.N); } bool operator!=(const FpT& rhs) const { return !operator==(rhs); } - inline friend FpT operator+(const FpT& x, const FpT& y) { FpT z; add(z, x, y); return z; } - inline friend FpT operator-(const FpT& x, const FpT& y) { FpT z; sub(z, x, y); return z; } - inline friend FpT operator*(const FpT& x, const FpT& y) { FpT z; mul(z, x, y); return z; } - inline friend FpT operator/(const FpT& x, const FpT& y) { FpT z; div(z, x, y); return z; } - FpT& operator+=(const FpT& x) { add(*this, *this, x); return *this; } - FpT& operator-=(const FpT& x) { sub(*this, *this, x); return *this; } - FpT& operator*=(const FpT& x) { mul(*this, *this, x); return *this; } - FpT& operator/=(const FpT& x) { div(*this, *this, x); return *this; } - FpT operator-() const { FpT x; neg(x, *this); return x; } friend inline std::ostream& operator<<(std::ostream& os, const FpT& self) { const std::ios_base::fmtflags f = os.flags(); @@ -430,20 +401,6 @@ public: } void normalize() {} // dummy method private: - static inline void powerArray(FpT& z, const FpT& x, const Unit *y, size_t yn, bool isNegative) - { - FpT tmp; - const FpT *px = &x; - if (&z == &x) { - tmp = x; - px = &tmp; - } - z = 1; - fp::powerGeneric(z, *px, y, yn, mul, sqr); - if (isNegative) { - FpT::inv(z, z); - } - } /* wrapper function for generic p add(z, x, y) diff --git a/include/mcl/operator.hpp b/include/mcl/operator.hpp new file mode 100644 index 0000000..8dcec14 --- /dev/null +++ b/include/mcl/operator.hpp @@ -0,0 +1,77 @@ +#pragma once +/** + @file + @brief operator class + @author MITSUNARI Shigeo(@herumi) + @license modified new BSD license + http://opensource.org/licenses/BSD-3-Clause +*/ +#include +#include +#ifdef _MSC_VER + #ifndef MCL_FORCE_INLINE + #define MCL_FORCE_INLINE __forceinline + #endif + #pragma warning(push) + #pragma warning(disable : 4714) +#else + #ifndef MCL_FORCE_INLINE + #define MCL_FORCE_INLINE __attribute__((always_inline)) + #endif +#endif + +namespace mcl { namespace fp { + +template +struct Empty {}; + +/* + T must have add, sub, mul, inv, neg +*/ +template > +struct Operator : E { + template MCL_FORCE_INLINE T& operator+=(const S& rhs) { T::add(static_cast(*this), static_cast(*this), rhs); return static_cast(*this); } + template MCL_FORCE_INLINE T& operator-=(const S& rhs) { T::sub(static_cast(*this), static_cast(*this), rhs); return static_cast(*this); } + template friend MCL_FORCE_INLINE T operator+(const T& a, const S& b) { T c; T::add(c, a, b); return c; } + template friend MCL_FORCE_INLINE T operator-(const T& a, const S& b) { T c; T::sub(c, a, b); return c; } + template MCL_FORCE_INLINE T& operator*=(const S& rhs) { T::mul(static_cast(*this), static_cast(*this), rhs); return static_cast(*this); } + template friend MCL_FORCE_INLINE T operator*(const T& a, const S& b) { T c; T::mul(c, a, b); return c; } + MCL_FORCE_INLINE T& operator/=(const T& rhs) { T c; T::inv(c, rhs); T::mul(static_cast(*this), static_cast(*this), c); return static_cast(*this); } + friend MCL_FORCE_INLINE void div(const T& z, const T& x, const T& y) { T t; T::inv(t, y); T::mul(z, x, t); } + friend MCL_FORCE_INLINE T operator/(const T& a, const T& b) { T c; T::inv(c, b); T::mul(c, c, a); return c; } + MCL_FORCE_INLINE T operator-() const { T c; T::neg(c, static_cast(*this)); return c; } + template class FpT> + static inline void power(T& z, const T& x, const FpT& y) + { + fp::Block b; + y.getBlock(b); + powerArray(z, x, b.p, b.n, false); + } + static inline void power(T& z, const T& x, int y) + { + const Unit u = abs(y); + powerArray(z, x, &u, 1, y < 0); + } + static inline void power(T& z, const T& x, const mpz_class& y) + { + powerArray(z, x, Gmp::getUnit(y), abs(y.get_mpz_t()->_mp_size), y < 0); + } +private: + static inline void powerArray(T& z, const T& x, const Unit *y, size_t yn, bool isNegative) + { + T tmp; + const T *px = &x; + if (&z == &x) { + tmp = x; + px = &tmp; + } + z = 1; + fp::powerGeneric(z, *px, y, yn, T::mul, T::sqr); + if (isNegative) { + T::inv(z, z); + } + } +}; + +} } // mcl::fp + diff --git a/include/mcl/util.hpp b/include/mcl/util.hpp index 86d7306..001a6ad 100644 --- a/include/mcl/util.hpp +++ b/include/mcl/util.hpp @@ -14,17 +14,6 @@ #pragma warning(disable : 4456) #pragma warning(disable : 4459) #endif -#ifdef _MSC_VER - #ifndef MCL_FORCE_INLINE - #define MCL_FORCE_INLINE __forceinline - #endif - #pragma warning(push) - #pragma warning(disable : 4714) -#else - #ifndef MCL_FORCE_INLINE - #define MCL_FORCE_INLINE __attribute__((always_inline)) - #endif -#endif namespace mcl { namespace fp { @@ -267,26 +256,6 @@ void powerGeneric(G& out, const G& x, const T *y, size_t n, void mul(G&, const G #endif } -template -struct Empty {}; - -/* - T must have add, sub, mul, inv, neg -*/ -template > -struct Operator : E { - template MCL_FORCE_INLINE T& operator+=(const S& rhs) { T::add(static_cast(*this), static_cast(*this), rhs); return static_cast(*this); } - template MCL_FORCE_INLINE T& operator-=(const S& rhs) { T::sub(static_cast(*this), static_cast(*this), rhs); return static_cast(*this); } - template friend MCL_FORCE_INLINE T operator+(const T& a, const S& b) { T c; T::add(c, a, b); return c; } - template friend MCL_FORCE_INLINE T operator-(const T& a, const S& b) { T c; T::sub(c, a, b); return c; } - template MCL_FORCE_INLINE T& operator*=(const S& rhs) { T::mul(static_cast(*this), static_cast(*this), rhs); return static_cast(*this); } - template friend MCL_FORCE_INLINE T operator*(const T& a, const S& b) { T c; T::mul(c, a, b); return c; } - MCL_FORCE_INLINE T& operator/=(const T& rhs) { T c; T::inv(c, rhs); T::mul(static_cast(*this), static_cast(*this), c); return static_cast(*this); } - friend MCL_FORCE_INLINE void div(const T& z, const T& x, const T& y) { T t; T::inv(t, y); T::mul(z, x, t); } - friend MCL_FORCE_INLINE T operator/(const T& a, const T& b) { T c; T::inv(c, b); T::mul(c, c, a); return c; } - MCL_FORCE_INLINE T operator-() const { T c; T::neg(c, static_cast(*this)); return c; } -}; - } } // mcl::fp #ifdef _MSC_VER