diff options
Diffstat (limited to 'vendor/github.com/dexon-foundation/mcl/src/fp.cpp')
-rw-r--r-- | vendor/github.com/dexon-foundation/mcl/src/fp.cpp | 83 |
1 files changed, 34 insertions, 49 deletions
diff --git a/vendor/github.com/dexon-foundation/mcl/src/fp.cpp b/vendor/github.com/dexon-foundation/mcl/src/fp.cpp index bea1dff72..df72d6d07 100644 --- a/vendor/github.com/dexon-foundation/mcl/src/fp.cpp +++ b/vendor/github.com/dexon-foundation/mcl/src/fp.cpp @@ -1,10 +1,6 @@ #include <mcl/op.hpp> #include <mcl/util.hpp> -#ifdef MCL_DONT_USE_OPENSSL #include <cybozu/sha2.hpp> -#else -#include <cybozu/crypto.hpp> -#endif #include <cybozu/endian.hpp> #include <mcl/conversion.hpp> #ifdef MCL_USE_XBYAK @@ -120,44 +116,16 @@ bool isEnableJIT() #endif } -void getRandVal(Unit *out, RandGen& rg, const Unit *in, size_t bitSize) -{ - if (rg.isZero()) rg = RandGen::get(); - const size_t n = (bitSize + UnitBitSize - 1) / UnitBitSize; - const size_t rem = bitSize & (UnitBitSize - 1); - assert(n > 0); - for (;;) { - rg.read(out, n * sizeof(Unit)); - if (rem > 0) out[n - 1] &= (Unit(1) << rem) - 1; - if (isLessArray(out, in, n)) return; - } -} - uint32_t sha256(void *out, uint32_t maxOutSize, const void *msg, uint32_t msgSize) { - const uint32_t hashSize = 256 / 8; - if (maxOutSize < hashSize) return 0; -#ifdef MCL_DONT_USE_OPENSSL - cybozu::Sha256(msg, msgSize).get(out); -#else - cybozu::crypto::Hash::digest(out, cybozu::crypto::Hash::N_SHA256, msg, msgSize); -#endif - return hashSize; + return (uint32_t)cybozu::Sha256().digest(out, maxOutSize, msg, msgSize); } uint32_t sha512(void *out, uint32_t maxOutSize, const void *msg, uint32_t msgSize) { - const uint32_t hashSize = 512 / 8; - if (maxOutSize < hashSize) return 0; -#ifdef MCL_DONT_USE_OPENSSL - cybozu::Sha512(msg, msgSize).get(out); -#else - cybozu::crypto::Hash::digest(out, cybozu::crypto::Hash::N_SHA512, msg, msgSize); -#endif - return hashSize; + return (uint32_t)cybozu::Sha512().digest(out, maxOutSize, msg, msgSize); } - #ifndef MCL_USE_VINT static inline void set_mpz_t(mpz_t& z, const Unit* p, int n) { @@ -350,9 +318,9 @@ static bool initForMont(Op& op, const Unit *p, Mode mode) if (mode != FP_XBYAK) return true; #ifdef MCL_USE_XBYAK if (op.fg == 0) op.fg = Op::createFpGenerator(); - op.fg->init(op); + bool useXbyak = op.fg->init(op); - if (op.isMont && N <= 4) { + if (useXbyak && op.isMont && N <= 4) { op.fp_invOp = &invOpForMontC; initInvTbl(op); } @@ -360,7 +328,7 @@ static bool initForMont(Op& op, const Unit *p, Mode mode) return true; } -bool Op::init(const mpz_class& _p, size_t maxBitSize, Mode mode, size_t mclMaxBitSize) +bool Op::init(const mpz_class& _p, size_t maxBitSize, int _xi_a, Mode mode, size_t mclMaxBitSize) { if (mclMaxBitSize != MCL_MAX_BIT_SIZE) return false; #ifdef MCL_USE_VINT @@ -382,6 +350,7 @@ bool Op::init(const mpz_class& _p, size_t maxBitSize, Mode mode, size_t mclMaxBi mp = _p; bitSize = gmp::getBitSize(mp); pmod4 = gmp::getUnit(mp, 0) % 4; + this->xi_a = _xi_a; /* priority : MCL_USE_XBYAK > MCL_USE_LLVM > none Xbyak > llvm_mont > llvm > gmp_mont > gmp @@ -497,18 +466,18 @@ bool Op::init(const mpz_class& _p, size_t maxBitSize, Mode mode, size_t mclMaxBi fpDbl_mod = &mcl::vint::mcl_fpDbl_mod_SECP256K1; } #endif - if (!fp::initForMont(*this, p, mode)) return false; - { - bool b; - sq.set(&b, mp); - if (!b) return false; - } if (N * UnitBitSize <= 256) { hash = sha256; } else { hash = sha512; } - return true; + { + bool b; + sq.set(&b, mp); + if (!b) return false; + } + modp.init(mp); + return fp::initForMont(*this, p, mode); } void copyUnitToByteAsLE(uint8_t *dst, const Unit *src, size_t byteSize) @@ -560,6 +529,27 @@ int detectIoMode(int ioMode, const std::ios_base& ios) bool copyAndMask(Unit *y, const void *x, size_t xByteSize, const Op& op, MaskMode maskMode) { const size_t fpByteSize = sizeof(Unit) * op.N; + if (maskMode == Mod) { + if (xByteSize > fpByteSize * 2) return false; + mpz_class mx; + bool b; + gmp::setArray(&b, mx, (const char*)x, xByteSize); + if (!b) return false; +#ifdef MCL_USE_VINT + op.modp.modp(mx, mx); +#else + mx %= op.mp; +#endif + const Unit *pmx = gmp::getUnit(mx); + size_t i = 0; + for (const size_t n = gmp::getUnitSize(mx); i < n; i++) { + y[i] = pmx[i]; + } + for (; i < op.N; i++) { + y[i] = 0; + } + return true; + } if (xByteSize > fpByteSize) { if (maskMode == NoMask) return false; xByteSize = fpByteSize; @@ -652,10 +642,5 @@ int64_t getInt64(bool *pb, fp::Block& b, const fp::Op& op) #pragma warning(pop) #endif -void Op::initFp2(int _xi_a) -{ - this->xi_a = _xi_a; -} - } } // mcl::fp |