aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMITSUNARI Shigeo <herumi@nifty.com>2018-02-04 18:17:03 +0800
committerMITSUNARI Shigeo <herumi@nifty.com>2018-02-04 18:17:03 +0800
commit90c2a423272004b77ca83c365e6f7993711add20 (patch)
treef4576aad91f7b3a392589a87cc375341a9225631 /src
parent81008ac6bec34eb7289a67516dd887f49517145a (diff)
downloadtangerine-mcl-90c2a423272004b77ca83c365e6f7993711add20.tar.gz
tangerine-mcl-90c2a423272004b77ca83c365e6f7993711add20.tar.zst
tangerine-mcl-90c2a423272004b77ca83c365e6f7993711add20.zip
add Fp::setHashFunc
Diffstat (limited to 'src')
-rw-r--r--src/fp.cpp36
1 files changed, 23 insertions, 13 deletions
diff --git a/src/fp.cpp b/src/fp.cpp
index 50131e4..078be1c 100644
--- a/src/fp.cpp
+++ b/src/fp.cpp
@@ -194,23 +194,28 @@ bool isEnableJIT()
#endif
}
-std::string hash(size_t bitSize, const void *msg, size_t msgSize)
+static 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
- if (bitSize <= 256) {
- return cybozu::Sha256(msg, msgSize).get();
- } else {
- return cybozu::Sha512(msg, msgSize).get();
- }
+ cybozu::Sha256(msg, msgSize).get(out);
#else
- cybozu::crypto::Hash::Name name;
- if (bitSize <= 256) {
- name = cybozu::crypto::Hash::N_SHA256;
- } else {
- name = cybozu::crypto::Hash::N_SHA512;
- }
- return cybozu::crypto::Hash::digest(name, (const char *)msg, msgSize);
+ cybozu::crypto::Hash::digest(out, cybozu::crypto::Hash::N_SHA256, msg, msgSize);
+#endif
+ return hashSize;
+}
+
+static 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;
}
#ifndef MCL_USE_VINT
@@ -530,6 +535,11 @@ void Op::init(const std::string& mstr, size_t maxBitSize, Mode mode, size_t mclM
#endif
fp::initForMont(*this, p, mode);
sq.set(mp);
+ if (N * UnitBitSize <= 256) {
+ hash = sha256;
+ } else {
+ hash = sha512;
+ }
}
void arrayToStr(std::string& str, const Unit *x, size_t n, int ioMode)