diff --git a/include/mcl/she.h b/include/mcl/she.h index 60b399c..d474216 100644 --- a/include/mcl/she.h +++ b/include/mcl/she.h @@ -163,6 +163,19 @@ MCLSHE_DLL_API int shePrecomputedPublicKeyEncG1(sheCipherTextG1 *c, const shePre MCLSHE_DLL_API int shePrecomputedPublicKeyEncG2(sheCipherTextG2 *c, const shePrecomputedPublicKey *ppub, mclInt m); MCLSHE_DLL_API int shePrecomputedPublicKeyEncGT(sheCipherTextGT *c, const shePrecomputedPublicKey *ppub, mclInt m); +/* + enc large integer + buf[bufSize] is little endian + bufSize <= (FrBitSize + 63) & ~63 + return 0 if success +*/ +MCLSHE_DLL_API int sheEncIntVecG1(sheCipherTextG1 *c, const shePublicKey *pub, const void *buf, mclSize bufSize); +MCLSHE_DLL_API int sheEncIntVecG2(sheCipherTextG2 *c, const shePublicKey *pub, const void *buf, mclSize bufSize); +MCLSHE_DLL_API int sheEncIntVecGT(sheCipherTextGT *c, const shePublicKey *pub, const void *buf, mclSize bufSize); +MCLSHE_DLL_API int shePrecomputedPublicKeyEncIntVecG1(sheCipherTextG1 *c, const shePrecomputedPublicKey *ppub, const void *buf, mclSize bufSize); +MCLSHE_DLL_API int shePrecomputedPublicKeyEncIntVecG2(sheCipherTextG2 *c, const shePrecomputedPublicKey *ppub, const void *buf, mclSize bufSize); +MCLSHE_DLL_API int shePrecomputedPublicKeyEncIntVecGT(sheCipherTextGT *c, const shePrecomputedPublicKey *ppub, const void *buf, mclSize bufSize); + /* m must be 0 or 1 */ @@ -235,6 +248,15 @@ MCLSHE_DLL_API int sheSubGT(sheCipherTextGT *z, const sheCipherTextGT *x, const MCLSHE_DLL_API int sheMulG1(sheCipherTextG1 *z, const sheCipherTextG1 *x, mclInt y); MCLSHE_DLL_API int sheMulG2(sheCipherTextG2 *z, const sheCipherTextG2 *x, mclInt y); MCLSHE_DLL_API int sheMulGT(sheCipherTextGT *z, const sheCipherTextGT *x, mclInt y); +/* + mul large integer + buf[bufSize] is little endian + bufSize <= (FrBitSize + 63) & ~63 + return 0 if success +*/ +MCLSHE_DLL_API int sheMulIntVecG1(sheCipherTextG1 *z, const sheCipherTextG1 *x, const void *buf, mclSize bufSize); +MCLSHE_DLL_API int sheMulIntVecG2(sheCipherTextG2 *z, const sheCipherTextG2 *x, const void *buf, mclSize bufSize); +MCLSHE_DLL_API int sheMulIntVecGT(sheCipherTextGT *z, const sheCipherTextGT *x, const void *buf, mclSize bufSize); // return 0 if success // z = x * y diff --git a/include/mcl/she.hpp b/include/mcl/she.hpp index 3ce3614..47e8796 100644 --- a/include/mcl/she.hpp +++ b/include/mcl/she.hpp @@ -1656,7 +1656,8 @@ public: { mul(z, x.c1_, y.c2_); } - static void mul(CipherTextGT& z, const CipherTextGT& x, int64_t y) + template + static void mul(CipherTextGT& z, const CipherTextGT& x, const INT& y) { for (int i = 0; i < 4; i++) { GT::pow(z.g_[i], x.g_[i], y); diff --git a/src/she_c_impl.hpp b/src/she_c_impl.hpp index 073bc2b..8cfa6d0 100644 --- a/src/she_c_impl.hpp +++ b/src/she_c_impl.hpp @@ -274,6 +274,41 @@ int sheEncGT(sheCipherTextGT *c, const shePublicKey *pub, mclInt m) return encT(c, pub, m); } +bool setArray(mpz_class& m, const void *buf, mclSize bufSize) +{ + if (bufSize > Fr::getUnitSize() * sizeof(mcl::fp::Unit)) return false; + bool b; + mcl::gmp::setArray(&b, m, (const uint8_t*)buf, bufSize); + return b; +} + +template +int encIntVecT(CT *c, const shePublicKey *pub, const void *buf, mclSize bufSize) + try +{ + mpz_class m; + if (!setArray(m, buf, bufSize)) return -1; + cast(pub)->enc(*cast(c), m); + return 0; +} catch (std::exception&) { + return -1; +} + +int sheEncIntVecG1(sheCipherTextG1 *c, const shePublicKey *pub, const void *buf, mclSize bufSize) +{ + return encIntVecT(c, pub, buf, bufSize); +} + +int sheEncIntVecG2(sheCipherTextG2 *c, const shePublicKey *pub, const void *buf, mclSize bufSize) +{ + return encIntVecT(c, pub, buf, bufSize); +} + +int sheEncIntVecGT(sheCipherTextGT *c, const shePublicKey *pub, const void *buf, mclSize bufSize) +{ + return encIntVecT(c, pub, buf, bufSize); +} + template int encWithZkpBinT(CT *c, sheZkpBin *zkp, const PK *pub, int m) try @@ -512,6 +547,33 @@ int sheMulGT(sheCipherTextGT *z, const sheCipherTextGT *x, mclInt y) return mulT(*cast(z), *cast(x), y); } +template +int mulIntVecT(CT& z, const CT& x, const void *buf, mclSize bufSize) + try +{ + mpz_class m; + if (!setArray(m, buf, bufSize)) return -1; + CT::mul(z, x, m); + return 0; +} catch (std::exception&) { + return -1; +} + +int sheMulIntVecG1(sheCipherTextG1 *z, const sheCipherTextG1 *x, const void *buf, mclSize bufSize) +{ + return mulIntVecT(*cast(z), *cast(x), buf, bufSize); +} + +int sheMulIntVecG2(sheCipherTextG2 *z, const sheCipherTextG2 *x, const void *buf, mclSize bufSize) +{ + return mulIntVecT(*cast(z), *cast(x), buf, bufSize); +} + +int sheMulIntVecGT(sheCipherTextGT *z, const sheCipherTextGT *x, const void *buf, mclSize bufSize) +{ + return mulIntVecT(*cast(z), *cast(x), buf, bufSize); +} + int sheMul(sheCipherTextGT *z, const sheCipherTextG1 *x, const sheCipherTextG2 *y) { return mulT(*cast(z), *cast(x), *cast(y)); @@ -627,6 +689,33 @@ int shePrecomputedPublicKeyEncGT(sheCipherTextGT *c, const shePrecomputedPublicK return pEncT(c, pub, m); } +template +int pEncIntVecT(CT *c, const shePrecomputedPublicKey *pub, const void *buf, mclSize bufSize) + try +{ + mpz_class m; + if (!setArray(m, buf, bufSize)) return -1; + cast(pub)->enc(*cast(c), m); + return 0; +} catch (std::exception&) { + return -1; +} + +int shePrecomputedPublicKeyEncIntVecG1(sheCipherTextG1 *c, const shePrecomputedPublicKey *pub, const void *buf, mclSize bufSize) +{ + return pEncIntVecT(c, pub, buf, bufSize); +} + +int shePrecomputedPublicKeyEncIntVecG2(sheCipherTextG2 *c, const shePrecomputedPublicKey *pub, const void *buf, mclSize bufSize) +{ + return pEncIntVecT(c, pub, buf, bufSize); +} + +int shePrecomputedPublicKeyEncIntVecGT(sheCipherTextGT *c, const shePrecomputedPublicKey *pub, const void *buf, mclSize bufSize) +{ + return pEncIntVecT(c, pub, buf, bufSize); +} + template int verifyT(const PK& pub, const CT& c, const ZkpBin& zkp) try diff --git a/test/she_c_test.hpp b/test/she_c_test.hpp index 8287c0e..f5516d3 100644 --- a/test/she_c_test.hpp +++ b/test/she_c_test.hpp @@ -432,6 +432,40 @@ CYBOZU_TEST_AUTO(ZkpEq) shePrecomputedPublicKeyDestroy(ppub); } +template +void IntVecTest(const sheSecretKey& sec, const shePublicKey& pub, const ENC& enc, const ENCV& encv, const DEC& dec, const SUB& sub, const MUL& mul, uint8_t *buf, size_t bufSize) +{ + CT c1, c2; + int ret; + ret = encv(&c1, &pub, buf, bufSize); + CYBOZU_TEST_EQUAL(ret, 0); + buf[0] += 5; + enc(&c2, &pub, 1); + ret = mul(&c2, &c2, buf, bufSize); + CYBOZU_TEST_EQUAL(ret, 0); + sub(&c2, &c2, &c1); + int64_t d; + ret = dec(&d, &sec, &c2); + CYBOZU_TEST_EQUAL(ret, 0); + CYBOZU_TEST_EQUAL(d, 5); +} + +CYBOZU_TEST_AUTO(IntVec) +{ + sheSecretKey sec; + sheSecretKeySetByCSPRNG(&sec); + shePublicKey pub; + sheGetPublicKey(&pub, &sec); + uint8_t buf[48]; + size_t n = 32; + for (size_t i = 0; i < sizeof(buf); i++) { + buf[i] = uint8_t(i + 5); + } + IntVecTest(sec, pub, sheEncG1, sheEncIntVecG1, sheDecG1, sheSubG1, sheMulIntVecG1, buf, n); + IntVecTest(sec, pub, sheEncG2, sheEncIntVecG2, sheDecG2, sheSubG2, sheMulIntVecG2, buf, n); + IntVecTest(sec, pub, sheEncGT, sheEncIntVecGT, sheDecGT, sheSubGT, sheMulIntVecGT, buf, n); +} + CYBOZU_TEST_AUTO(finalExp) { sheSecretKey sec;