diff options
author | MITSUNARI Shigeo <herumi@nifty.com> | 2018-11-27 14:17:31 +0800 |
---|---|---|
committer | MITSUNARI Shigeo <herumi@nifty.com> | 2018-11-27 14:17:31 +0800 |
commit | a7efe8a6fee268f2c12da379c8fed7ef81673e84 (patch) | |
tree | b5a2db3764e360ecec66a53ced3bf3d5b7a298d0 | |
parent | 07372576fe5e7afb89add4ff19e642143b2444d9 (diff) | |
download | tangerine-mcl-a7efe8a6fee268f2c12da379c8fed7ef81673e84.tar.gz tangerine-mcl-a7efe8a6fee268f2c12da379c8fed7ef81673e84.tar.zst tangerine-mcl-a7efe8a6fee268f2c12da379c8fed7ef81673e84.zip |
use sha2.hpp instead of crypto.hpp
-rw-r--r-- | include/cybozu/sha2.hpp | 16 | ||||
-rw-r--r-- | include/mcl/elgamal.hpp | 18 | ||||
-rw-r--r-- | sample/vote.cpp | 7 | ||||
-rw-r--r-- | test/elgamal_test.cpp | 24 | ||||
-rw-r--r-- | test/fp_test.cpp | 18 |
5 files changed, 32 insertions, 51 deletions
diff --git a/include/cybozu/sha2.hpp b/include/cybozu/sha2.hpp index b3fd459..1830936 100644 --- a/include/cybozu/sha2.hpp +++ b/include/cybozu/sha2.hpp @@ -57,6 +57,10 @@ public: { update(buf.c_str(), buf.size()); } + std::string digest(const std::string& buf) + { + return digest(buf.c_str(), buf.size()); + } std::string digest(const void *buf, size_t bufSize) { std::string md(SHA256_DIGEST_LENGTH, 0); @@ -93,6 +97,10 @@ public: { update(buf.c_str(), buf.size()); } + std::string digest(const std::string& buf) + { + return digest(buf.c_str(), buf.size()); + } std::string digest(const void *buf, size_t bufSize) { std::string md(SHA512_DIGEST_LENGTH, 0); @@ -300,6 +308,10 @@ public: { update(buf.c_str(), buf.size()); } + std::string digest(const std::string& buf) + { + return digest(buf.c_str(), buf.size()); + } std::string digest(const void *buf, size_t bufSize) { std::string md(outByteSize_, 0); @@ -437,6 +449,10 @@ public: { update(buf.c_str(), buf.size()); } + std::string digest(const std::string& buf) + { + return digest(buf.c_str(), buf.size()); + } std::string digest(const void *buf, size_t bufSize) { std::string md(outByteSize_, 0); diff --git a/include/mcl/elgamal.hpp b/include/mcl/elgamal.hpp index 8bc3104..4311485 100644 --- a/include/mcl/elgamal.hpp +++ b/include/mcl/elgamal.hpp @@ -244,8 +244,7 @@ struct ElgamalT { input : m = 0 or 1 output : c (c1, c2), zkp */ - template<class Hash> - void encWithZkp(CipherText& c, Zkp& zkp, int m, Hash& hash, fp::RandGen rg = fp::RandGen()) const + void encWithZkp(CipherText& c, Zkp& zkp, int m, fp::RandGen rg = fp::RandGen()) const { if (m != 0 && m != 1) { throw cybozu::Exception("elgamal:PublicKey:encWithZkp") << m; @@ -272,10 +271,8 @@ struct ElgamalT { mulH(R12, r1); std::ostringstream os; os << R01 << R02 << R11 << R12 << c.c1 << c.c2 << f << g << h; - hash.update(os.str()); - const std::string digest = hash.digest(); Zn cc; - cc.setArrayMask(digest.c_str(), digest.size()); + cc.setHashOf(os.str()); zkp.c1 = cc - zkp.c0; zkp.s1 = r1 + zkp.c1 * u; } else { @@ -296,10 +293,8 @@ struct ElgamalT { Ec::sub(R12, t1, t2); std::ostringstream os; os << R01 << R02 << R11 << R12 << c.c1 << c.c2 << f << g << h; - hash.update(os.str()); - const std::string digest = hash.digest(); Zn cc; - cc.setArrayMask(digest.c_str(), digest.size()); + cc.setHashOf(os.str()); zkp.c0 = cc - zkp.c1; zkp.s0 = r0 + zkp.c0 * u; } @@ -307,8 +302,7 @@ struct ElgamalT { /* verify cipher text with ZKP */ - template<class Hash> - bool verify(const CipherText& c, const Zkp& zkp, Hash& hash) const + bool verify(const CipherText& c, const Zkp& zkp) const { Ec R01, R02, R11, R12; Ec t1, t2; @@ -327,10 +321,8 @@ struct ElgamalT { Ec::sub(R12, t1, t2); std::ostringstream os; os << R01 << R02 << R11 << R12 << c.c1 << c.c2 << f << g << h; - hash.update(os.str()); - const std::string digest = hash.digest(); Zn cc; - cc.setArrayMask(digest.c_str(), digest.size()); + cc.setHashOf(os.str()); return cc == zkp.c0 + zkp.c1; } /* diff --git a/sample/vote.cpp b/sample/vote.cpp index ba09525..8813718 100644 --- a/sample/vote.cpp +++ b/sample/vote.cpp @@ -10,7 +10,6 @@ #include <fstream> #include <cybozu/random_generator.hpp> #include <cybozu/option.hpp> -#include <cybozu/crypto.hpp> #include <cybozu/itoa.hpp> #include <mcl/fp.hpp> #include <mcl/ec.hpp> @@ -107,8 +106,7 @@ struct CipherWithZkp { Elgamal::Zkp zkp; bool verify(const Elgamal::PublicKey& pub) const { - cybozu::crypto::Hash hash; - return pub.verify(c, zkp, hash); + return pub.verify(c, zkp); } }; @@ -134,8 +132,7 @@ void Vote(const std::string& voteList) puts("each voter votes"); for (size_t i = 0; i < voteList.size(); i++) { CipherWithZkp c; - cybozu::crypto::Hash hash; - pub.encWithZkp(c.c, c.zkp, voteList[i] - '0', hash, rg); + pub.encWithZkp(c.c, c.zkp, voteList[i] - '0', rg); const std::string sheetName = GetSheetName(idxTbl[i]); printf("make %s\n", sheetName.c_str()); Save(sheetName, c); diff --git a/test/elgamal_test.cpp b/test/elgamal_test.cpp index 225ccee..9532fc5 100644 --- a/test/elgamal_test.cpp +++ b/test/elgamal_test.cpp @@ -1,10 +1,5 @@ #include <cybozu/test.hpp> #include <cybozu/random_generator.hpp> -#ifdef MCL_DONT_USE_OPENSSL -#include <cybozu/sha1.hpp> -#else -#include <cybozu/crypto.hpp> -#endif #include <mcl/fp.hpp> #include <mcl/ecparam.hpp> #include <mcl/elgamal.hpp> @@ -147,19 +142,14 @@ CYBOZU_TEST_AUTO(testEc) { ElgamalEc::Zkp zkp; ElgamalEc::CipherText c; -#ifdef MCL_DONT_USE_OPENSSL - cybozu::Sha1 hash; -#else - cybozu::crypto::Hash hash(cybozu::crypto::Hash::N_SHA256); -#endif - pub.encWithZkp(c, zkp, 0, hash, rg); - CYBOZU_TEST_ASSERT(pub.verify(c, zkp, hash)); + pub.encWithZkp(c, zkp, 0, rg); + CYBOZU_TEST_ASSERT(pub.verify(c, zkp)); zkp.s0 += 1; - CYBOZU_TEST_ASSERT(!pub.verify(c, zkp, hash)); - pub.encWithZkp(c, zkp, 1, hash, rg); - CYBOZU_TEST_ASSERT(pub.verify(c, zkp, hash)); + CYBOZU_TEST_ASSERT(!pub.verify(c, zkp)); + pub.encWithZkp(c, zkp, 1, rg); + CYBOZU_TEST_ASSERT(pub.verify(c, zkp)); zkp.s0 += 1; - CYBOZU_TEST_ASSERT(!pub.verify(c, zkp, hash)); - CYBOZU_TEST_EXCEPTION_MESSAGE(pub.encWithZkp(c, zkp, 2, hash, rg), cybozu::Exception, "encWithZkp"); + CYBOZU_TEST_ASSERT(!pub.verify(c, zkp)); + CYBOZU_TEST_EXCEPTION_MESSAGE(pub.encWithZkp(c, zkp, 2, rg), cybozu::Exception, "encWithZkp"); } } diff --git a/test/fp_test.cpp b/test/fp_test.cpp index f883b24..d8b4742 100644 --- a/test/fp_test.cpp +++ b/test/fp_test.cpp @@ -7,11 +7,7 @@ #include <time.h> #include <cybozu/benchmark.hpp> #include <cybozu/option.hpp> -#ifdef MCL_DONT_USE_OPENSSL #include <cybozu/sha2.hpp> -#else -#include <cybozu/crypto.hpp> -#endif #ifdef _MSC_VER #pragma warning(disable: 4127) // const condition @@ -726,22 +722,12 @@ void setHashOfTest() }; for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(msgTbl); i++) { size_t bitSize = Fp::getBitSize(); -#ifdef MCL_DONT_USE_OPENSSL std::string digest; if (bitSize <= 256) { - digest = cybozu::Sha256(msgTbl[i].c_str(), msgTbl[i].size()).get(); + digest = cybozu::Sha256().digest(msgTbl[i]); } else { - digest = cybozu::Sha512(msgTbl[i].c_str(), msgTbl[i].size()).get(); + digest = cybozu::Sha512().digest(msgTbl[i]); } -#else - cybozu::crypto::Hash::Name name; - if (bitSize <= 256) { - name = cybozu::crypto::Hash::N_SHA256; - } else { - name = cybozu::crypto::Hash::N_SHA512; - } - std::string digest = cybozu::crypto::Hash::digest(name, msgTbl[i]); -#endif Fp x, y; x.setArrayMask(digest.c_str(), digest.size()); y.setHashOf(msgTbl[i]); |