use sha2.hpp instead of crypto.hpp

dev
MITSUNARI Shigeo 6 years ago
parent 07372576fe
commit a7efe8a6fe
  1. 16
      include/cybozu/sha2.hpp
  2. 18
      include/mcl/elgamal.hpp
  3. 7
      sample/vote.cpp
  4. 24
      test/elgamal_test.cpp
  5. 18
      test/fp_test.cpp

@ -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);

@ -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;
}
/*

@ -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);

@ -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");
}
}

@ -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]);

Loading…
Cancel
Save