diff options
author | MITSUNARI Shigeo <herumi@nifty.com> | 2018-05-27 23:37:40 +0800 |
---|---|---|
committer | MITSUNARI Shigeo <herumi@nifty.com> | 2018-05-27 23:37:40 +0800 |
commit | 76c73cb0da746fd2026117394b6570b6bb3f09be (patch) | |
tree | 1f025111fdcf3c9c00b49201d4261cb0bee969a2 | |
parent | d82f2944a8d52001eff0c1828dca203cc8b9861f (diff) | |
download | dexon-mcl-76c73cb0da746fd2026117394b6570b6bb3f09be.tar.gz dexon-mcl-76c73cb0da746fd2026117394b6570b6bb3f09be.tar.zst dexon-mcl-76c73cb0da746fd2026117394b6570b6bb3f09be.zip |
reduce including standard headers
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | include/mcl/bn.hpp | 12 | ||||
-rw-r--r-- | include/mcl/ec.hpp | 16 | ||||
-rw-r--r-- | include/mcl/fp.hpp | 22 | ||||
-rw-r--r-- | include/mcl/fp_tower.hpp | 4 | ||||
-rw-r--r-- | include/mcl/gmp_util.hpp | 1 | ||||
-rw-r--r-- | include/mcl/op.hpp | 6 | ||||
-rw-r--r-- | include/mcl/operator.hpp | 2 | ||||
-rw-r--r-- | include/mcl/util.hpp | 19 | ||||
-rw-r--r-- | include/mcl/vint.hpp | 51 | ||||
-rw-r--r-- | src/fp.cpp | 2 |
11 files changed, 85 insertions, 52 deletions
@@ -261,7 +261,7 @@ endif emcc -o $@ src/fp.cpp src/she_c384.cpp $(EMCC_OPT) -DMCL_MAX_BIT_SIZE=384 -s TOTAL_MEMORY=67108864 -s DISABLE_EXCEPTION_CATCHING=0 ../mcl-wasm/mcl_c.js: src/bn_c256.cpp $(MCL_C_DEP) - emcc -o $@ src/fp.cpp src/bn_c256.cpp $(EMCC_OPT) -DMCL_MAX_BIT_SIZE=256 -DMCL_USE_WEB_CRYPTO_API -s DISABLE_EXCEPTION_CATCHING=1 -DCYBOZU_DONT_USE_EXCEPTION -DCYBOZU_DONT_USE_STRING -fno-exceptions + emcc -o $@ src/fp.cpp src/bn_c256.cpp $(EMCC_OPT) -DMCL_MAX_BIT_SIZE=256 -DMCL_USE_WEB_CRYPTO_API -s DISABLE_EXCEPTION_CATCHING=1 -DCYBOZU_DONT_USE_EXCEPTION -DCYBOZU_DONT_USE_STRING -fno-exceptions -MD -MP -MF obj/mcl_c.d ../mcl-wasm/mcl_c512.js: src/bn_c512.cpp $(MCL_C_DEP) emcc -o $@ src/fp.cpp src/bn_c512.cpp $(EMCC_OPT) -DMCL_MAX_BIT_SIZE=512 -DMCL_USE_WEB_CRYPTO_API -s DISABLE_EXCEPTION_CATCHING=1 -DCYBOZU_DONT_USE_EXCEPTION -DCYBOZU_DONT_USE_STRING -fno-exceptions diff --git a/include/mcl/bn.hpp b/include/mcl/bn.hpp index d70cd3c..4e5d413 100644 --- a/include/mcl/bn.hpp +++ b/include/mcl/bn.hpp @@ -10,7 +10,9 @@ #include <mcl/ec.hpp> #include <mcl/curve_type.h> #include <assert.h> +#ifndef CYBOZU_DONT_USE_EXCEPTION #include <vector> +#endif /* set bit size of Fp and Fr @@ -622,7 +624,7 @@ struct GLV1 { mcl::gmp::getArray(&b, w[i], maxUnit, u[i]); assert(b); bitTbl[i] = (int)mcl::gmp::getBitSize(u[i]); - maxBit = std::max(maxBit, bitTbl[i]); + maxBit = fp::max_(maxBit, bitTbl[i]); } assert(maxBit > 0); maxBit--; @@ -816,7 +818,7 @@ struct GLV2 { mcl::gmp::getArray(&b, w[i], maxUnit, u[i]); assert(b); bitTbl[i] = (int)mcl::gmp::getBitSize(u[i]); - maxBit = std::max(maxBit, bitTbl[i]); + maxBit = fp::max_(maxBit, bitTbl[i]); } maxBit--; /* @@ -1730,11 +1732,13 @@ inline void precomputeG2(Fp6 *Qcoeff, const G2& Q_) precomputeG2(Qcoeff, Q); precomputedMillerLoop(e, P, Qcoeff); */ +#ifndef CYBOZU_DONT_USE_EXCEPTION inline void precomputeG2(std::vector<Fp6>& Qcoeff, const G2& Q) { Qcoeff.resize(BN::param.precomputedQcoeffSize); precomputeG2(Qcoeff.data(), Q); } +#endif inline bool precomputeG2(mcl::Array<Fp6>& Qcoeff, const G2& Q) { bool b = Qcoeff.resize(BN::param.precomputedQcoeffSize); @@ -1778,10 +1782,12 @@ inline void precomputedMillerLoop(Fp12& f, const G1& P_, const Fp6* Qcoeff) mulSparse2(ft, d, e); f *= ft; } +#ifndef CYBOZU_DONT_USE_EXCEPTION inline void precomputedMillerLoop(Fp12& f, const G1& P, const std::vector<Fp6>& Qcoeff) { precomputedMillerLoop(f, P, Qcoeff.data()); } +#endif inline void precomputedMillerLoop(Fp12& f, const G1& P, const mcl::Array<Fp6>& Qcoeff) { precomputedMillerLoop(f, P, Qcoeff.data()); @@ -1840,10 +1846,12 @@ inline void precomputedMillerLoop2(Fp12& f, const G1& P1_, const Fp6* Q1coeff, c f *= f1; f *= f2; } +#ifndef CYBOZU_DONT_USE_EXCEPTION inline void precomputedMillerLoop2(Fp12& f, const G1& P1, const std::vector<Fp6>& Q1coeff, const G1& P2, const std::vector<Fp6>& Q2coeff) { precomputedMillerLoop2(f, P1, Q1coeff.data(), P2, Q2coeff.data()); } +#endif inline void precomputedMillerLoop2(Fp12& f, const G1& P1, const mcl::Array<Fp6>& Q1coeff, const G1& P2, const mcl::Array<Fp6>& Q2coeff) { precomputedMillerLoop2(f, P1, Q1coeff.data(), P2, Q2coeff.data()); diff --git a/include/mcl/ec.hpp b/include/mcl/ec.hpp index a42b207..d41ac48 100644 --- a/include/mcl/ec.hpp +++ b/include/mcl/ec.hpp @@ -6,7 +6,6 @@ @license modified new BSD license http://opensource.org/licenses/BSD-3-Clause */ -#include <sstream> #include <stdlib.h> #include <cybozu/exception.hpp> #include <mcl/op.hpp> @@ -194,11 +193,6 @@ public: { mulArrayGLV = f; } - // backward compatilibity - static inline void setParam(const std::string& astr, const std::string& bstr, int mode = ec::Jacobi) - { - init(astr, bstr, mode); - } static inline void init(bool *pb, const char *astr, const char *bstr, int mode = ec::Jacobi) { Fp a, b; @@ -549,7 +543,7 @@ public: const EcT *pP = &P0; const EcT *pQ = &Q0; if (pP->z.isOne()) { - std::swap(pP, pQ); + fp::swap_(pP, pQ); } const EcT& P(*pP); const EcT& Q(*pQ); @@ -592,7 +586,7 @@ public: } static inline void mul(EcT& z, const EcT& x, int64_t y) { - const uint64_t u = std::abs(y); + const uint64_t u = fp::abs_(y); #if MCL_SIZEOF_UNIT == 8 mulArray(z, x, &u, 1, y < 0); #else @@ -696,7 +690,6 @@ public: const size_t n = Fp::getByteSize(); const size_t adj = isMSBserialize() ? 0 : 1; char buf[sizeof(Fp) + 1]; - std::string str; if (isZero()) { memset(buf, 0, n + adj); } else { @@ -927,6 +920,11 @@ public: } #endif #ifndef CYBOZU_DONT_USE_STRING + // backward compatilibity + static inline void setParam(const std::string& astr, const std::string& bstr, int mode = ec::Jacobi) + { + init(astr, bstr, mode); + } friend inline std::istream& operator>>(std::istream& is, EcT& self) { self.load(is, fp::detectIoMode(getIoMode(), is)); diff --git a/include/mcl/fp.hpp b/include/mcl/fp.hpp index 96fbe45..0faa181 100644 --- a/include/mcl/fp.hpp +++ b/include/mcl/fp.hpp @@ -6,7 +6,9 @@ @license modified new BSD license http://opensource.org/licenses/BSD-3-Clause */ +#ifndef CYBOZU_DONT_USE_STRING #include <iosfwd> +#endif #ifdef _MSC_VER #pragma warning(push) #pragma warning(disable : 4127) @@ -404,16 +406,6 @@ public: } bool operator==(const FpT& rhs) const { return fp::isEqualArray(v_, rhs.v_, op_.N); } bool operator!=(const FpT& rhs) const { return !operator==(rhs); } - friend inline std::ostream& operator<<(std::ostream& os, const FpT& self) - { - self.save(os, fp::detectIoMode(getIoMode(), os)); - return os; - } - friend inline std::istream& operator>>(std::istream& is, FpT& self) - { - self.load(is, fp::detectIoMode(getIoMode(), is)); - return is; - } /* @note this compare functions is slow because of calling mul if isMont is true. @@ -485,6 +477,16 @@ public: { init(mstr, mode); } + friend inline std::ostream& operator<<(std::ostream& os, const FpT& self) + { + self.save(os, fp::detectIoMode(getIoMode(), os)); + return os; + } + friend inline std::istream& operator>>(std::istream& is, FpT& self) + { + self.load(is, fp::detectIoMode(getIoMode(), is)); + return is; + } #endif #ifndef CYBOZU_DONT_USE_EXCEPTION static inline void init(const mpz_class& _p, fp::Mode mode = fp::FP_AUTO) diff --git a/include/mcl/fp_tower.hpp b/include/mcl/fp_tower.hpp index 0575c6e..b6a5229 100644 --- a/include/mcl/fp_tower.hpp +++ b/include/mcl/fp_tower.hpp @@ -175,7 +175,6 @@ public: Fp2T(int64_t a) : a(a), b(0) { } Fp2T(const Fp& a, const Fp& b) : a(a), b(b) { } Fp2T(int64_t a, int64_t b) : a(a), b(b) { } - Fp2T(const std::string& a, const std::string& b, int base = 0) : a(a, base), b(b, base) {} Fp* getFp0() { return &a; } const Fp* getFp0() const { return &a; } const Unit* getUnit() const { return a.getUnit(); } @@ -400,6 +399,7 @@ public: } #endif #ifndef CYBOZU_DONT_USE_STRING + Fp2T(const std::string& a, const std::string& b, int base = 0) : a(a, base), b(b, base) {} friend std::istream& operator>>(std::istream& is, Fp2T& self) { self.load(is, fp::detectIoMode(Fp::BaseFp::getIoMode(), is)); @@ -698,10 +698,12 @@ struct Fp2DblT { FpDbl::mod(y.a, x.a); FpDbl::mod(y.b, x.b); } +#ifndef CYBOZU_DONT_USE_STRING friend std::ostream& operator<<(std::ostream& os, const Fp2DblT& x) { return os << x.a << ' ' << x.b; } +#endif void operator+=(const Fp2DblT& x) { add(*this, *this, x); } void operator-=(const Fp2DblT& x) { sub(*this, *this, x); } }; diff --git a/include/mcl/gmp_util.hpp b/include/mcl/gmp_util.hpp index a59ccb3..6146b45 100644 --- a/include/mcl/gmp_util.hpp +++ b/include/mcl/gmp_util.hpp @@ -1,5 +1,4 @@ #pragma once -#include <iostream> /** @file @brief util function for gmp diff --git a/include/mcl/op.hpp b/include/mcl/op.hpp index e2f52e9..7e27821 100644 --- a/include/mcl/op.hpp +++ b/include/mcl/op.hpp @@ -326,8 +326,6 @@ inline const char* getIoSeparator(int ioMode) return (ioMode & (IoArray | IoArrayRaw | IoSerialize)) ? "" : " "; } -int detectIoMode(int ioMode, const std::ios_base& ios); - inline void dump(const char *s, size_t n) { for (size_t i = 0; i < n; i++) { @@ -336,9 +334,13 @@ inline void dump(const char *s, size_t n) printf("\n"); } +#ifndef CYBOZU_DONT_USE_STRING +int detectIoMode(int ioMode, const std::ios_base& ios); + inline void dump(const std::string& s) { dump(s.c_str(), s.size()); } +#endif } } // mcl::fp diff --git a/include/mcl/operator.hpp b/include/mcl/operator.hpp index 29a66f5..e9bc506 100644 --- a/include/mcl/operator.hpp +++ b/include/mcl/operator.hpp @@ -63,7 +63,7 @@ struct Operator : public E { } static void pow(T& z, const T& x, int64_t y) { - const uint64_t u = std::abs(y); + const uint64_t u = fp::abs_(y); #if MCL_SIZEOF_UNIT == 8 powArray(z, x, &u, 1, y < 0, false); #else diff --git a/include/mcl/util.hpp b/include/mcl/util.hpp index 452420b..edef971 100644 --- a/include/mcl/util.hpp +++ b/include/mcl/util.hpp @@ -16,6 +16,25 @@ namespace mcl { namespace fp { +template<class T> +T abs_(T x) { return x < 0 ? -x : x; } + +template<class T> +T min_(T x, T y) { return x < y ? x : y; } + +template<class T> +T max_(T x, T y) { return x < y ? y : x; } + +template<class T> +void swap_(T& x, T& y) +{ + T t; + t = x; + x = y; + y = t; +} + + /* get pp such that p * pp = -1 mod M, where p is prime and M = 1 << 64(or 32). diff --git a/include/mcl/vint.hpp b/include/mcl/vint.hpp index c1ab44b..c787981 100644 --- a/include/mcl/vint.hpp +++ b/include/mcl/vint.hpp @@ -5,8 +5,9 @@ #include <cybozu/exception.hpp> #include <cybozu/bit_operation.hpp> #include <assert.h> -#include <cmath> +#ifndef CYBOZU_DONT_USE_STRING #include <iostream> +#endif #include <mcl/array.hpp> #include <mcl/util.hpp> #include <mcl/randgen.hpp> @@ -248,8 +249,8 @@ template<class T> T addNM(T *z, const T *x, size_t xn, const T *y, size_t yn) { if (yn > xn) { - std::swap(xn, yn); - std::swap(x, y); + fp::swap_(xn, yn); + fp::swap_(x, y); } assert(xn >= yn); size_t max = xn; @@ -367,8 +368,8 @@ static inline void mulNM(T *z, const T *x, size_t xn, const T *y, size_t yn) { assert(xn > 0 && yn > 0); if (yn > xn) { - std::swap(yn, xn); - std::swap(x, y); + fp::swap_(yn, xn); + fp::swap_(x, y); } assert(xn >= yn); if (z == x) { @@ -598,7 +599,7 @@ void divNM(T *q, size_t qn, T *r, const T *x, size_t xn, const T *y, size_t yn) continue; } assert(xb > yb + 1); - size_t w = std::min(unitBitSize, xb - yb); + size_t w = fp::min_(unitBitSize, xb - yb); vint::shrN(t, rr, xn, xb - w); T q0 = t[0]; t[yn] = vint::mulu1(t, y, yn, q0); @@ -649,8 +650,8 @@ public: noexcept #endif { - std::swap(allocSize_, rhs.allocSize_); - std::swap(ptr_, rhs.ptr_); + fp::swap_(allocSize_, rhs.allocSize_); + fp::swap_(ptr_, rhs.ptr_); } void clear() { @@ -742,16 +743,16 @@ public: FixedBuffer *p1 = this; FixedBuffer *p2 = &rhs; if (p1->size_ < p2->size_) { - std::swap(p1, p2); + fp::swap_(p1, p2); } assert(p1->size_ >= p2->size_); for (size_t i = 0; i < p2->size_; i++) { - std::swap(p1->v_[i], p2->v_[i]); + fp::swap_(p1->v_[i], p2->v_[i]); } for (size_t i = p2->size_; i < p1->size_; i++) { p2->v_[i] = p1->v_[i]; } - std::swap(p1->size_, p2->size_); + fp::swap_(p1->size_, p2->size_); } // to avoid warning of gcc void verify(size_t n) const @@ -850,7 +851,7 @@ private: } static void uadd(VintT& z, const Buffer& x, size_t xn, const Buffer& y, size_t yn) { - size_t zn = std::max(xn, yn) + 1; + size_t zn = fp::max_(xn, yn) + 1; bool b; z.buf_.alloc(&b, zn); assert(b); (void)b; @@ -1020,7 +1021,7 @@ public: bool b; buf_.alloc(&b, 1); assert(b); (void)b; - buf_[0] = std::abs(x); + buf_[0] = fp::abs_(x); size_ = 1; return *this; } @@ -1061,9 +1062,9 @@ public: noexcept #endif { - std::swap(buf_, rhs.buf_); - std::swap(size_, rhs.size_); - std::swap(isNeg_, rhs.isNeg_); + fp::swap_(buf_, rhs.buf_); + fp::swap_(size_, rhs.size_); + fp::swap_(isNeg_, rhs.isNeg_); } void dump(const char *msg = "") const { @@ -1224,7 +1225,7 @@ public: return x.isNeg_ ? -1 : 1; } else { // same sign - Unit y0 = std::abs(y); + Unit y0 = fp::abs_(y); int c = vint::compareNM(&x.buf_[0], x.size(), &y0, 1); if (x.isNeg_) { return -c; @@ -1296,17 +1297,17 @@ public: static void adds1(VintT& z, const VintT& x, int y) { assert(y != invalidVar); - _adds1(z, x, std::abs(y), y < 0); + _adds1(z, x, fp::abs_(y), y < 0); } static void subs1(VintT& z, const VintT& x, int y) { assert(y != invalidVar); - _adds1(z, x, std::abs(y), !(y < 0)); + _adds1(z, x, fp::abs_(y), !(y < 0)); } static void muls1(VintT& z, const VintT& x, int y) { assert(y != invalidVar); - mulu1(z, x, std::abs(y)); + mulu1(z, x, fp::abs_(y)); z.isNeg_ ^= (y < 0); } /* @@ -1320,7 +1321,7 @@ public: assert(y != invalidVar); bool xNeg = x.isNeg_; bool yNeg = y < 0; - Unit absY = std::abs(y); + Unit absY = fp::abs_(y); size_t xn = x.size(); int r; if (q) { @@ -1471,7 +1472,7 @@ public: assert(!x.isNeg_ && !y.isNeg_); const VintT *px = &x, *py = &y; if (x.size() < y.size()) { - std::swap(px, py); + fp::swap_(px, py); } size_t xn = px->size(); size_t yn = py->size(); @@ -1490,7 +1491,7 @@ public: assert(!x.isNeg_ && !y.isNeg_); const VintT *px = &x, *py = &y; if (x.size() < y.size()) { - std::swap(px, py); + fp::swap_(px, py); } size_t yn = py->size(); assert(px->size() >= yn); @@ -1537,10 +1538,10 @@ public: const VintT xx = x; z = 1; #if MCL_SIZEOF_UNIT == 8 - Unit ua = std::abs(y); + Unit ua = fp::abs_(y); mcl::fp::powGeneric(z, xx, &ua, 1, mul, sqr, (void (*)(VintT&, const VintT&))0); #else - uint64_t ua = std::abs(y); + uint64_t ua = fp::abs_(y); Unit u[2] = { uint32_t(ua), uint32_t(ua >> 32) }; size_t un = u[1] ? 2 : 1; mcl::fp::powGeneric(z, xx, u, un, mul, sqr, (void (*)(VintT&, const VintT&))0); @@ -537,6 +537,7 @@ void copyByteToUnitAsLE(Unit *dst, const uint8_t *src, size_t byteSize) *dst = x; } +#ifndef CYBOZU_DONT_USE_STRING int detectIoMode(int ioMode, const std::ios_base& ios) { if (ioMode & ~IoPrefix) return ioMode; @@ -549,6 +550,7 @@ int detectIoMode(int ioMode, const std::ios_base& ios) } return ioMode; } +#endif bool copyAndMask(Unit *y, const void *x, size_t xByteSize, const Op& op, MaskMode maskMode) { |