From 07e8a0dc95f601ff0cee94de92ce884f4d53858a Mon Sep 17 00:00:00 2001 From: MITSUNARI Shigeo Date: Sun, 1 Oct 2017 16:31:39 +0900 Subject: [PATCH] [she] change C api of sheDec --- include/mcl/she.h | 20 +++--- src/she_c_impl.hpp | 144 ++++++++++++++++++++------------------------ test/she_c_test.hpp | 28 ++++++--- 3 files changed, 96 insertions(+), 96 deletions(-) diff --git a/include/mcl/she.h b/include/mcl/she.h index 3d13962..6384b47 100644 --- a/include/mcl/she.h +++ b/include/mcl/she.h @@ -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 diff --git a/src/she_c_impl.hpp b/src/she_c_impl.hpp index 075ef43..885000f 100644 --- a/src/she_c_impl.hpp +++ b/src/she_c_impl.hpp @@ -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 +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 +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 +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 +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 +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 +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); } + diff --git a/test/she_c_test.hpp b/test/she_c_test.hpp index 1dd2165..66a8ec1 100644 --- a/test/she_c_test.hpp +++ b/test/she_c_test.hpp @@ -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)