diff options
author | MITSUNARI Shigeo <herumi@nifty.com> | 2017-12-13 21:15:05 +0800 |
---|---|---|
committer | MITSUNARI Shigeo <herumi@nifty.com> | 2017-12-13 21:15:10 +0800 |
commit | d80d10da3934624503e45173bd51adc0669457ff (patch) | |
tree | ad77706e8d53f59f3dc137dff1bd2e2bc1d0c690 | |
parent | 65562d873d72c279b4e93383f1e6d8dd2d63d769 (diff) | |
download | tangerine-mcl-d80d10da3934624503e45173bd51adc0669457ff.tar.gz tangerine-mcl-d80d10da3934624503e45173bd51adc0669457ff.tar.zst tangerine-mcl-d80d10da3934624503e45173bd51adc0669457ff.zip |
[she] add PrecomputedPublicKey api for c
-rw-r--r-- | include/mcl/she.h | 9 | ||||
-rw-r--r-- | src/she_c_impl.hpp | 51 | ||||
-rw-r--r-- | test/she_c_test.hpp | 27 |
3 files changed, 87 insertions, 0 deletions
diff --git a/include/mcl/she.h b/include/mcl/she.h index 1467adc..9b6866a 100644 --- a/include/mcl/she.h +++ b/include/mcl/she.h @@ -154,6 +154,15 @@ MCLSHE_DLL_API int sheReRandGT(sheCipherTextGT *c, const shePublicKey *pub); MCLSHE_DLL_API int sheConvertG1(sheCipherTextGT *y, const shePublicKey *pub, const sheCipherTextG1 *x); MCLSHE_DLL_API int sheConvertG2(sheCipherTextGT *y, const shePublicKey *pub, const sheCipherTextG2 *x); +struct shePrecomputedPublicKey; +MCLSHE_DLL_API shePrecomputedPublicKey *shePrecomputedPublicKeyCreate(); +MCLSHE_DLL_API void shePrecomputedPublicKeyDestroy(shePrecomputedPublicKey *ppub); +// return 0 if success +MCLSHE_DLL_API int shePrecomputedPublicKeyInit(shePrecomputedPublicKey *ppub, const shePublicKey *pub); +MCLSHE_DLL_API int shePrecomputedPublicKeyEncG1(sheCipherTextG1 *c, const shePrecomputedPublicKey *ppub, mclInt m); +MCLSHE_DLL_API int shePrecomputedPublicKeyEncG2(sheCipherTextG2 *c, const shePrecomputedPublicKey *ppub, mclInt m); +MCLSHE_DLL_API int shePrecomputedPublicKeyEncGT(sheCipherTextGT *c, const shePrecomputedPublicKey *ppub, mclInt m); + #ifdef __cplusplus } #endif diff --git a/src/she_c_impl.hpp b/src/she_c_impl.hpp index 0000795..db87659 100644 --- a/src/she_c_impl.hpp +++ b/src/she_c_impl.hpp @@ -27,6 +27,9 @@ static const SecretKey *cast(const sheSecretKey *p) { return reinterpret_cast<co static PublicKey *cast(shePublicKey *p) { return reinterpret_cast<PublicKey*>(p); } static const PublicKey *cast(const shePublicKey *p) { return reinterpret_cast<const PublicKey*>(p); } +static PrecomputedPublicKey *cast(shePrecomputedPublicKey *p) { return reinterpret_cast<PrecomputedPublicKey*>(p); } +static const PrecomputedPublicKey *cast(const shePrecomputedPublicKey *p) { return reinterpret_cast<const PrecomputedPublicKey*>(p); } + static CipherTextG1 *cast(sheCipherTextG1 *p) { return reinterpret_cast<CipherTextG1*>(p); } static const CipherTextG1 *cast(const sheCipherTextG1 *p) { return reinterpret_cast<const CipherTextG1*>(p); } @@ -396,3 +399,51 @@ int sheConvertG2(sheCipherTextGT *y, const shePublicKey *pub, const sheCipherTex { return convert(y, pub, x); } + +shePrecomputedPublicKey *shePrecomputedPublicKeyCreate() + try +{ + return reinterpret_cast<shePrecomputedPublicKey*>(new PrecomputedPublicKey()); +} catch (...) { + return 0; +} + +void shePrecomputedPublicKeyDestroy(shePrecomputedPublicKey *ppub) +{ + delete cast(ppub); +} + +int shePrecomputedPublicKeyInit(shePrecomputedPublicKey *ppub, const shePublicKey *pub) + try +{ + cast(ppub)->init(*cast(pub)); + return 0; +} catch (...) { + return 1; +} + +template<class CT> +int pEncT(CT *c, const shePrecomputedPublicKey *pub, mclInt m) + try +{ + cast(pub)->enc(*cast(c), m); + return 0; +} catch (std::exception& e) { + fprintf(stderr, "err %s\n", e.what()); + return -1; +} + +int shePrecomputedPublicKeyEncG1(sheCipherTextG1 *c, const shePrecomputedPublicKey *pub, mclInt m) +{ + return pEncT(c, pub, m); +} + +int shePrecomputedPublicKeyEncG2(sheCipherTextG2 *c, const shePrecomputedPublicKey *pub, mclInt m) +{ + return pEncT(c, pub, m); +} + +int shePrecomputedPublicKeyEncGT(sheCipherTextGT *c, const shePrecomputedPublicKey *pub, mclInt m) +{ + return pEncT(c, pub, m); +} diff --git a/test/she_c_test.hpp b/test/she_c_test.hpp index 425b600..7bc3256 100644 --- a/test/she_c_test.hpp +++ b/test/she_c_test.hpp @@ -235,3 +235,30 @@ CYBOZU_TEST_AUTO(convert) CYBOZU_TEST_EQUAL(dec, 123); } +CYBOZU_TEST_AUTO(precomputed) +{ + sheSecretKey sec; + sheSecretKeySetByCSPRNG(&sec); + shePublicKey pub; + sheGetPublicKey(&pub, &sec); + shePrecomputedPublicKey *ppub = shePrecomputedPublicKeyCreate(); + CYBOZU_TEST_EQUAL(shePrecomputedPublicKeyInit(ppub, &pub), 0); + const int64_t m = 152; + sheCipherTextG1 c1; + sheCipherTextG2 c2; + sheCipherTextGT ct; + int64_t dec = 0; + CYBOZU_TEST_EQUAL(shePrecomputedPublicKeyEncG1(&c1, ppub, m), 0); + CYBOZU_TEST_EQUAL(sheDecG1(&dec, &sec, &c1), 0); + CYBOZU_TEST_EQUAL(dec, m); + dec = 0; + CYBOZU_TEST_EQUAL(shePrecomputedPublicKeyEncG2(&c2, ppub, m), 0); + CYBOZU_TEST_EQUAL(sheDecG2(&dec, &sec, &c2), 0); + CYBOZU_TEST_EQUAL(dec, m); + dec = 0; + CYBOZU_TEST_EQUAL(shePrecomputedPublicKeyEncGT(&ct, ppub, m), 0); + CYBOZU_TEST_EQUAL(sheDecGT(&dec, &sec, &ct), 0); + CYBOZU_TEST_EQUAL(dec, m); + + shePrecomputedPublicKeyDestroy(ppub); +} |