[she] add {Enc,Mul}IntVec

pull/2/head
MITSUNARI Shigeo 6 years ago
parent c371ee97fa
commit 53ac29fa9a
  1. 22
      include/mcl/she.h
  2. 3
      include/mcl/she.hpp
  3. 89
      src/she_c_impl.hpp
  4. 34
      test/she_c_test.hpp

@ -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 shePrecomputedPublicKeyEncG2(sheCipherTextG2 *c, const shePrecomputedPublicKey *ppub, mclInt m);
MCLSHE_DLL_API int shePrecomputedPublicKeyEncGT(sheCipherTextGT *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 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 sheMulG1(sheCipherTextG1 *z, const sheCipherTextG1 *x, mclInt y);
MCLSHE_DLL_API int sheMulG2(sheCipherTextG2 *z, const sheCipherTextG2 *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); 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 // return 0 if success
// z = x * y // z = x * y

@ -1656,7 +1656,8 @@ public:
{ {
mul(z, x.c1_, y.c2_); mul(z, x.c1_, y.c2_);
} }
static void mul(CipherTextGT& z, const CipherTextGT& x, int64_t y) template<class INT>
static void mul(CipherTextGT& z, const CipherTextGT& x, const INT& y)
{ {
for (int i = 0; i < 4; i++) { for (int i = 0; i < 4; i++) {
GT::pow(z.g_[i], x.g_[i], y); GT::pow(z.g_[i], x.g_[i], y);

@ -274,6 +274,41 @@ int sheEncGT(sheCipherTextGT *c, const shePublicKey *pub, mclInt m)
return encT(c, pub, 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<class CT>
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<class CT, class PK> template<class CT, class PK>
int encWithZkpBinT(CT *c, sheZkpBin *zkp, const PK *pub, int m) int encWithZkpBinT(CT *c, sheZkpBin *zkp, const PK *pub, int m)
try try
@ -512,6 +547,33 @@ int sheMulGT(sheCipherTextGT *z, const sheCipherTextGT *x, mclInt y)
return mulT(*cast(z), *cast(x), y); return mulT(*cast(z), *cast(x), y);
} }
template<class CT>
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) int sheMul(sheCipherTextGT *z, const sheCipherTextG1 *x, const sheCipherTextG2 *y)
{ {
return mulT(*cast(z), *cast(x), *cast(y)); return mulT(*cast(z), *cast(x), *cast(y));
@ -627,6 +689,33 @@ int shePrecomputedPublicKeyEncGT(sheCipherTextGT *c, const shePrecomputedPublicK
return pEncT(c, pub, m); return pEncT(c, pub, m);
} }
template<class CT>
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<class PK, class CT> template<class PK, class CT>
int verifyT(const PK& pub, const CT& c, const ZkpBin& zkp) int verifyT(const PK& pub, const CT& c, const ZkpBin& zkp)
try try

@ -432,6 +432,40 @@ CYBOZU_TEST_AUTO(ZkpEq)
shePrecomputedPublicKeyDestroy(ppub); shePrecomputedPublicKeyDestroy(ppub);
} }
template<class CT, class ENC, class ENCV, class DEC, class SUB, class MUL>
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<sheCipherTextG1>(sec, pub, sheEncG1, sheEncIntVecG1, sheDecG1, sheSubG1, sheMulIntVecG1, buf, n);
IntVecTest<sheCipherTextG2>(sec, pub, sheEncG2, sheEncIntVecG2, sheDecG2, sheSubG2, sheMulIntVecG2, buf, n);
IntVecTest<sheCipherTextGT>(sec, pub, sheEncGT, sheEncIntVecGT, sheDecGT, sheSubGT, sheMulIntVecGT, buf, n);
}
CYBOZU_TEST_AUTO(finalExp) CYBOZU_TEST_AUTO(finalExp)
{ {
sheSecretKey sec; sheSecretKey sec;

Loading…
Cancel
Save