|
|
@ -11,7 +11,7 @@ typedef mcl::EcT<Fp> Ec; |
|
|
|
typedef mcl::ElgamalT<Ec, Zn> ElgamalEc; |
|
|
|
typedef mcl::ElgamalT<Ec, Zn> ElgamalEc; |
|
|
|
|
|
|
|
|
|
|
|
const mcl::EcParam& para = mcl::ecparam::secp192k1; |
|
|
|
const mcl::EcParam& para = mcl::ecparam::secp192k1; |
|
|
|
cybozu::RandomGenerator rg; |
|
|
|
cybozu::RandomGenerator g_rg; |
|
|
|
|
|
|
|
|
|
|
|
CYBOZU_TEST_AUTO(testEc) |
|
|
|
CYBOZU_TEST_AUTO(testEc) |
|
|
|
{ |
|
|
|
{ |
|
|
@ -26,15 +26,15 @@ CYBOZU_TEST_AUTO(testEc) |
|
|
|
Zn = <P> |
|
|
|
Zn = <P> |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
ElgamalEc::PrivateKey prv; |
|
|
|
ElgamalEc::PrivateKey prv; |
|
|
|
prv.init(P, bitSize, rg); |
|
|
|
prv.init(P, bitSize, g_rg); |
|
|
|
prv.setCache(0, 60000); |
|
|
|
prv.setCache(0, 60000); |
|
|
|
const ElgamalEc::PublicKey& pub = prv.getPublicKey(); |
|
|
|
const ElgamalEc::PublicKey& pub = prv.getPublicKey(); |
|
|
|
|
|
|
|
|
|
|
|
const int m1 = 12345; |
|
|
|
const int m1 = 12345; |
|
|
|
const int m2 = 17655; |
|
|
|
const int m2 = 17655; |
|
|
|
ElgamalEc::CipherText c1, c2; |
|
|
|
ElgamalEc::CipherText c1, c2; |
|
|
|
pub.enc(c1, m1, rg); |
|
|
|
pub.enc(c1, m1, g_rg); |
|
|
|
pub.enc(c2, m2, rg); |
|
|
|
pub.enc(c2, m2, g_rg); |
|
|
|
Zn dec1, dec2; |
|
|
|
Zn dec1, dec2; |
|
|
|
prv.dec(dec1, c1); |
|
|
|
prv.dec(dec1, c1); |
|
|
|
prv.dec(dec2, c2); |
|
|
|
prv.dec(dec2, c2); |
|
|
@ -69,7 +69,7 @@ CYBOZU_TEST_AUTO(testEc) |
|
|
|
ss << pub; |
|
|
|
ss << pub; |
|
|
|
ss >> pub2; |
|
|
|
ss >> pub2; |
|
|
|
} |
|
|
|
} |
|
|
|
pub2.enc(cc2, m2, rg); |
|
|
|
pub2.enc(cc2, m2, g_rg); |
|
|
|
prv.dec(d, cc2); |
|
|
|
prv.dec(d, cc2); |
|
|
|
CYBOZU_TEST_EQUAL(d, m2); |
|
|
|
CYBOZU_TEST_EQUAL(d, m2); |
|
|
|
} |
|
|
|
} |
|
|
@ -86,7 +86,7 @@ CYBOZU_TEST_AUTO(testEc) |
|
|
|
} |
|
|
|
} |
|
|
|
// rerandomize
|
|
|
|
// rerandomize
|
|
|
|
c1 = c2; |
|
|
|
c1 = c2; |
|
|
|
pub.rerandomize(c1, rg); |
|
|
|
pub.rerandomize(c1, g_rg); |
|
|
|
// verify c1 != c2
|
|
|
|
// verify c1 != c2
|
|
|
|
CYBOZU_TEST_ASSERT(c1.c1 != c2.c1); |
|
|
|
CYBOZU_TEST_ASSERT(c1.c1 != c2.c1); |
|
|
|
CYBOZU_TEST_ASSERT(c1.c2 != c2.c2); |
|
|
|
CYBOZU_TEST_ASSERT(c1.c2 != c2.c2); |
|
|
@ -98,7 +98,7 @@ CYBOZU_TEST_AUTO(testEc) |
|
|
|
{ |
|
|
|
{ |
|
|
|
ElgamalEc::CipherText c; |
|
|
|
ElgamalEc::CipherText c; |
|
|
|
Zn m = 1234; |
|
|
|
Zn m = 1234; |
|
|
|
pub.enc(c, m, rg); |
|
|
|
pub.enc(c, m, g_rg); |
|
|
|
c.neg(); |
|
|
|
c.neg(); |
|
|
|
Zn dec; |
|
|
|
Zn dec; |
|
|
|
prv.dec(dec, c); |
|
|
|
prv.dec(dec, c); |
|
|
@ -109,7 +109,7 @@ CYBOZU_TEST_AUTO(testEc) |
|
|
|
ElgamalEc::CipherText c; |
|
|
|
ElgamalEc::CipherText c; |
|
|
|
Zn m = 123; |
|
|
|
Zn m = 123; |
|
|
|
int x = 111; |
|
|
|
int x = 111; |
|
|
|
pub.enc(c, m, rg); |
|
|
|
pub.enc(c, m, g_rg); |
|
|
|
Zn dec; |
|
|
|
Zn dec; |
|
|
|
prv.dec(dec, c); |
|
|
|
prv.dec(dec, c); |
|
|
|
c.mul(x); |
|
|
|
c.mul(x); |
|
|
@ -122,7 +122,7 @@ CYBOZU_TEST_AUTO(testEc) |
|
|
|
for (int i = -10; i < 10; i++) { |
|
|
|
for (int i = -10; i < 10; i++) { |
|
|
|
ElgamalEc::CipherText c; |
|
|
|
ElgamalEc::CipherText c; |
|
|
|
const Zn mm = i; |
|
|
|
const Zn mm = i; |
|
|
|
pub.enc(c, mm, rg); |
|
|
|
pub.enc(c, mm, g_rg); |
|
|
|
Zn dec; |
|
|
|
Zn dec; |
|
|
|
prv.dec(dec, c, 1000); |
|
|
|
prv.dec(dec, c, 1000); |
|
|
|
CYBOZU_TEST_EQUAL(dec, mm); |
|
|
|
CYBOZU_TEST_EQUAL(dec, mm); |
|
|
@ -131,7 +131,7 @@ CYBOZU_TEST_AUTO(testEc) |
|
|
|
// isZeroMessage
|
|
|
|
// isZeroMessage
|
|
|
|
for (int m = 0; m < 10; m++) { |
|
|
|
for (int m = 0; m < 10; m++) { |
|
|
|
ElgamalEc::CipherText c0; |
|
|
|
ElgamalEc::CipherText c0; |
|
|
|
pub.enc(c0, m, rg); |
|
|
|
pub.enc(c0, m, g_rg); |
|
|
|
if (m == 0) { |
|
|
|
if (m == 0) { |
|
|
|
CYBOZU_TEST_ASSERT(prv.isZeroMessage(c0)); |
|
|
|
CYBOZU_TEST_ASSERT(prv.isZeroMessage(c0)); |
|
|
|
} else { |
|
|
|
} else { |
|
|
@ -142,14 +142,14 @@ CYBOZU_TEST_AUTO(testEc) |
|
|
|
{ |
|
|
|
{ |
|
|
|
ElgamalEc::Zkp zkp; |
|
|
|
ElgamalEc::Zkp zkp; |
|
|
|
ElgamalEc::CipherText c; |
|
|
|
ElgamalEc::CipherText c; |
|
|
|
pub.encWithZkp(c, zkp, 0, rg); |
|
|
|
pub.encWithZkp(c, zkp, 0, g_rg); |
|
|
|
CYBOZU_TEST_ASSERT(pub.verify(c, zkp)); |
|
|
|
CYBOZU_TEST_ASSERT(pub.verify(c, zkp)); |
|
|
|
zkp.s0 += 1; |
|
|
|
zkp.s0 += 1; |
|
|
|
CYBOZU_TEST_ASSERT(!pub.verify(c, zkp)); |
|
|
|
CYBOZU_TEST_ASSERT(!pub.verify(c, zkp)); |
|
|
|
pub.encWithZkp(c, zkp, 1, rg); |
|
|
|
pub.encWithZkp(c, zkp, 1, g_rg); |
|
|
|
CYBOZU_TEST_ASSERT(pub.verify(c, zkp)); |
|
|
|
CYBOZU_TEST_ASSERT(pub.verify(c, zkp)); |
|
|
|
zkp.s0 += 1; |
|
|
|
zkp.s0 += 1; |
|
|
|
CYBOZU_TEST_ASSERT(!pub.verify(c, zkp)); |
|
|
|
CYBOZU_TEST_ASSERT(!pub.verify(c, zkp)); |
|
|
|
CYBOZU_TEST_EXCEPTION_MESSAGE(pub.encWithZkp(c, zkp, 2, rg), cybozu::Exception, "encWithZkp"); |
|
|
|
CYBOZU_TEST_EXCEPTION_MESSAGE(pub.encWithZkp(c, zkp, 2, g_rg), cybozu::Exception, "encWithZkp"); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|