[she] change C api of sheDec

dev
MITSUNARI Shigeo 7 years ago
parent 9d1a2d754d
commit 07e8a0dc95
  1. 20
      include/mcl/she.h
  2. 144
      src/she_c_impl.hpp
  3. 28
      test/she_c_test.hpp

@ -73,7 +73,7 @@ MCLSHE_DLL_API size_t sheCipherTextG1Serialize(void *buf, size_t maxBufSize, con
MCLSHE_DLL_API size_t sheCipherTextG2Serialize(void *buf, size_t maxBufSize, const sheCipherTextG2 *c);
MCLSHE_DLL_API size_t sheCipherTextGTSerialize(void *buf, size_t maxBufSize, const sheCipherTextGT *c);
// return 0 if success else -1
// return 0 if success
MCLSHE_DLL_API int sheSecretKeyDeserialize(sheSecretKey* sec, const void *buf, size_t bufSize);
MCLSHE_DLL_API int shePublicKeyDeserialize(shePublicKey* pub, const void *buf, size_t bufSize);
MCLSHE_DLL_API int sheCipherTextG1Deserialize(sheCipherTextG1* c, const void *buf, size_t bufSize);
@ -82,7 +82,7 @@ MCLSHE_DLL_API int sheCipherTextGTDeserialize(sheCipherTextGT* c, const void *bu
/*
set secretKey if system has /dev/urandom or CryptGenRandom
return 0 if success else -1
return 0 if success
*/
MCLSHE_DLL_API int sheSecretKeySetByCSPRNG(sheSecretKey *sec);
@ -90,21 +90,21 @@ MCLSHE_DLL_API void sheGetPublicKey(shePublicKey *pub, const sheSecretKey *sec);
/*
make table to decode DLP
return 0 if success else -1
return 0 if success
*/
MCLSHE_DLL_API int sheSetRangeForDLP(size_t hashSize, size_t tryNum);
// return 0 if success else -1
// return 0 if success
MCLSHE_DLL_API int sheEncG1(sheCipherTextG1 *c, const shePublicKey *pub, int64_t m);
MCLSHE_DLL_API int sheEncG2(sheCipherTextG2 *c, const shePublicKey *pub, int64_t m);
MCLSHE_DLL_API int sheEncGT(sheCipherTextGT *c, const shePublicKey *pub, int64_t m);
#define MCLSHE_ERR_DECODE 0x7fffffffffffffffll
// return MCLSHE_ERR_DECODE if error
MCLSHE_DLL_API int64_t sheDecG1(const sheSecretKey *sec, const sheCipherTextG1 *c);
//MCLSHE_DLL_API int64_t sheDecG2(const sheSecretKey *sec, const sheCipherTextG2 *c);
MCLSHE_DLL_API int64_t sheDecGT(const sheSecretKey *sec, const sheCipherTextGT *c);
/*
decode c and set m[2]. plaintext is int64_t(m[1] << 32) + m[0]
return 0 if success
*/
MCLSHE_DLL_API int sheDecG1(uint32_t m[2], const sheSecretKey *sec, const sheCipherTextG1 *c);
MCLSHE_DLL_API int sheDecGT(uint32_t m[2], const sheSecretKey *sec, const sheCipherTextGT *c);
// return 0 if success
// z = x + y

@ -190,7 +190,8 @@ int sheSetRangeForDLP(size_t hashSize, size_t tryNum)
return -1;
}
int sheEncG1(sheCipherTextG1 *c, const shePublicKey *pub, int64_t m)
template<class CT>
int encT(CT *c, const shePublicKey *pub, int64_t m)
try
{
cast(pub)->enc(*cast(c), m);
@ -200,67 +201,75 @@ int sheEncG1(sheCipherTextG1 *c, const shePublicKey *pub, int64_t m)
return -1;
}
int sheEncG1(sheCipherTextG1 *c, const shePublicKey *pub, int64_t m)
{
return encT(c, pub, m);
}
int sheEncG2(sheCipherTextG2 *c, const shePublicKey *pub, int64_t m)
try
{
cast(pub)->enc(*cast(c), m);
return 0;
} catch (std::exception& e) {
printf("err %s\n", e.what());
return -1;
return encT(c, pub, m);
}
int sheEncGT(sheCipherTextGT *c, const shePublicKey *pub, int64_t m)
{
return encT(c, pub, m);
}
template<class CT>
int decT(uint32_t m[2], const sheSecretKey *sec, const CT *c)
try
{
cast(pub)->enc(*cast(c), m);
uint64_t dec = uint64_t(cast(sec)->dec(*cast(c)));
m[0] = uint32_t(dec);
m[1] = uint32_t(dec >> 32);
return 0;
} catch (std::exception& e) {
printf("err %s\n", e.what());
return -1;
}
int64_t sheDecG1(const sheSecretKey *sec, const sheCipherTextG1 *c)
try
int sheDecG1(uint32_t m[2], const sheSecretKey *sec, const sheCipherTextG1 *c)
{
return cast(sec)->dec(*cast(c));
} catch (std::exception& e) {
printf("err %s\n", e.what());
return MCLSHE_ERR_DECODE;
return decT(m, sec, c);
}
int64_t sheDecGT(const sheSecretKey *sec, const sheCipherTextGT *c)
try
int sheDecGT(uint32_t m[2], const sheSecretKey *sec, const sheCipherTextGT *c)
{
return cast(sec)->dec(*cast(c));
} catch (std::exception& e) {
printf("err %s\n", e.what());
return MCLSHE_ERR_DECODE;
return decT(m, sec, c);
}
int sheAddG1(sheCipherTextG1 *z, const sheCipherTextG1 *x, const sheCipherTextG1 *y)
template<class CT>
int addT(CT& z, const CT& x, const CT& y)
try
{
CipherTextG1::add(*cast(z), *cast(x), *cast(y));
CT::add(z, x, y);
return 0;
} catch (std::exception& e) {
printf("err %s\n", e.what());
return -1;
}
int sheAddG1(sheCipherTextG1 *z, const sheCipherTextG1 *x, const sheCipherTextG1 *y)
{
return addT(*cast(z), *cast(x), *cast(y));
}
int sheAddG2(sheCipherTextG2 *z, const sheCipherTextG2 *x, const sheCipherTextG2 *y)
try
{
CipherTextG2::add(*cast(z), *cast(x), *cast(y));
return 0;
} catch (std::exception& e) {
printf("err %s\n", e.what());
return -1;
return addT(*cast(z), *cast(x), *cast(y));
}
int sheAddGT(sheCipherTextGT *z, const sheCipherTextGT *x, const sheCipherTextGT *y)
{
return addT(*cast(z), *cast(x), *cast(y));
}
template<class CT>
int subT(CT& z, const CT& x, const CT& y)
try
{
CipherTextGT::add(*cast(z), *cast(x), *cast(y));
CT::sub(z, x, y);
return 0;
} catch (std::exception& e) {
printf("err %s\n", e.what());
@ -268,28 +277,25 @@ int sheAddGT(sheCipherTextGT *z, const sheCipherTextGT *x, const sheCipherTextGT
}
int sheSubG1(sheCipherTextG1 *z, const sheCipherTextG1 *x, const sheCipherTextG1 *y)
try
{
CipherTextG1::sub(*cast(z), *cast(x), *cast(y));
return 0;
} catch (std::exception& e) {
printf("err %s\n", e.what());
return -1;
return subT(*cast(z), *cast(x), *cast(y));
}
int sheSubG2(sheCipherTextG2 *z, const sheCipherTextG2 *x, const sheCipherTextG2 *y)
try
{
CipherTextG2::sub(*cast(z), *cast(x), *cast(y));
return 0;
} catch (std::exception& e) {
printf("err %s\n", e.what());
return -1;
return subT(*cast(z), *cast(x), *cast(y));
}
int sheSubGT(sheCipherTextGT *z, const sheCipherTextGT *x, const sheCipherTextGT *y)
{
return subT(*cast(z), *cast(x), *cast(y));
}
template<class CT1, class CT2, class CT3>
int mulT(CT1& z, const CT2& x, const CT3& y)
try
{
CipherTextGT::sub(*cast(z), *cast(x), *cast(y));
CT1::mul(z, x, y);
return 0;
} catch (std::exception& e) {
printf("err %s\n", e.what());
@ -297,38 +303,30 @@ int sheSubGT(sheCipherTextGT *z, const sheCipherTextGT *x, const sheCipherTextGT
}
int sheMulG1(sheCipherTextG1 *z, const sheCipherTextG1 *x, int64_t y)
try
{
CipherTextG1::mul(*cast(z), *cast(x), y);
return 0;
} catch (std::exception& e) {
printf("err %s\n", e.what());
return -1;
return mulT(*cast(z), *cast(x), y);
}
int sheMulG2(sheCipherTextG2 *z, const sheCipherTextG2 *x, int64_t y)
try
{
CipherTextG2::mul(*cast(z), *cast(x), y);
return 0;
} catch (std::exception& e) {
printf("err %s\n", e.what());
return -1;
return mulT(*cast(z), *cast(x), y);
}
int sheMulGT(sheCipherTextGT *z, const sheCipherTextGT *x, int64_t y)
try
{
CipherTextGT::mul(*cast(z), *cast(x), y);
return 0;
} catch (std::exception& e) {
printf("err %s\n", e.what());
return -1;
return mulT(*cast(z), *cast(x), y);
}
int sheMul(sheCipherTextGT *z, const sheCipherTextG1 *x, const sheCipherTextG2 *y)
{
return mulT(*cast(z), *cast(x), *cast(y));
}
template<class CT>
int reRandT(CT& c, const shePublicKey *pub)
try
{
CipherTextGT::mul(*cast(z), *cast(x), *cast(y));
cast(pub)->reRand(c);
return 0;
} catch (std::exception& e) {
printf("err %s\n", e.what());
@ -336,29 +334,17 @@ int sheMul(sheCipherTextGT *z, const sheCipherTextG1 *x, const sheCipherTextG2 *
}
int sheReRandG1(sheCipherTextG1 *c, const shePublicKey *pub)
try
{
cast(pub)->reRand(*cast(c));
return 0;
} catch (std::exception& e) {
printf("err %s\n", e.what());
return -1;
return reRandT(*cast(c), pub);
}
int sheReRandG2(sheCipherTextG2 *c, const shePublicKey *pub)
try
{
cast(pub)->reRand(*cast(c));
return 0;
} catch (std::exception& e) {
printf("err %s\n", e.what());
return -1;
return reRandT(*cast(c), pub);
}
int sheReRandGT(sheCipherTextGT *c, const shePublicKey *pub)
try
{
cast(pub)->reRand(*cast(c));
return 0;
} catch (std::exception& e) {
printf("err %s\n", e.what());
return -1;
return reRandT(*cast(c), pub);
}

@ -16,9 +16,14 @@ CYBOZU_TEST_AUTO(init)
#endif
int ret;
ret = sheInit(curve, MCLBN_FP_UNIT_SIZE);
CYBOZU_TEST_ASSERT(ret == 0);
CYBOZU_TEST_EQUAL(ret, 0);
ret = sheSetRangeForDLP(hashSize, tryNum);
CYBOZU_TEST_ASSERT(ret == 0);
CYBOZU_TEST_EQUAL(ret, 0);
}
int64_t toInt(const uint32_t m[2])
{
return m[0] + (int64_t(m[1]) << 32);
}
CYBOZU_TEST_AUTO(encDec)
@ -34,8 +39,11 @@ CYBOZU_TEST_AUTO(encDec)
sheEncG1(&c1, &pub, m);
sheEncGT(&ct, &pub, m);
CYBOZU_TEST_EQUAL(sheDecG1(&sec, &c1), m);
CYBOZU_TEST_EQUAL(sheDecGT(&sec, &ct), m);
uint32_t dec[2];
CYBOZU_TEST_EQUAL(sheDecG1(dec, &sec, &c1), 0);
CYBOZU_TEST_EQUAL(toInt(dec), m);
CYBOZU_TEST_EQUAL(sheDecGT(dec, &sec, &ct), 0);
CYBOZU_TEST_EQUAL(toInt(dec), m);
}
CYBOZU_TEST_AUTO(addMul)
@ -54,7 +62,9 @@ CYBOZU_TEST_AUTO(addMul)
sheEncG2(&c2, &pub, m2);
sheMul(&ct, &c1, &c2);
CYBOZU_TEST_EQUAL(sheDecGT(&sec, &ct), m1 * m2);
uint32_t dec[2];
CYBOZU_TEST_EQUAL(sheDecGT(dec, &sec, &ct), 0);
CYBOZU_TEST_EQUAL(toInt(dec), m1 * m2);
}
CYBOZU_TEST_AUTO(allOp)
@ -85,7 +95,9 @@ CYBOZU_TEST_AUTO(allOp)
sheMulGT(&ct, &ct, -4); // 160 * (m1 - m2) * (m3 - m4)
int64_t t = 160 * (m1 - m2) * (m3 - m4);
CYBOZU_TEST_EQUAL(sheDecGT(&sec, &ct), t);
uint32_t dec[2];
CYBOZU_TEST_EQUAL(sheDecGT(dec, &sec, &ct), 0);
CYBOZU_TEST_EQUAL(toInt(dec), t);
}
CYBOZU_TEST_AUTO(rerand)
@ -114,7 +126,9 @@ CYBOZU_TEST_AUTO(rerand)
sheReRandGT(&ct2, &pub);
sheAddGT(&ct1, &ct1, &ct2);
CYBOZU_TEST_EQUAL(sheDecGT(&sec, &ct1), m1 * m2 + m3);
uint32_t dec[2];
CYBOZU_TEST_EQUAL(sheDecGT(dec, &sec, &ct1), 0);
CYBOZU_TEST_EQUAL(toInt(dec), m1 * m2 + m3);
}
CYBOZU_TEST_AUTO(serialize)

Loading…
Cancel
Save