add CipherText::neg

dev
MITSUNARI Shigeo 7 years ago
parent cfc6586a58
commit 09ee2e1ab2
  1. 6
      include/mcl/she.h
  2. 6
      include/mcl/she.hpp
  3. 27
      src/she_c_impl.hpp
  4. 15
      test/she_c_test.hpp

@ -179,6 +179,12 @@ MCLSHE_DLL_API int sheIsZeroG1(const sheSecretKey *sec, const sheCipherTextG1 *c
MCLSHE_DLL_API int sheIsZeroG2(const sheSecretKey *sec, const sheCipherTextG2 *c);
MCLSHE_DLL_API int sheIsZeroGT(const sheSecretKey *sec, const sheCipherTextGT *c);
// return 0 if success
// y = -x
MCLSHE_DLL_API int sheNegG1(sheCipherTextG1 *y, const sheCipherTextG1 *x);
MCLSHE_DLL_API int sheNegG2(sheCipherTextG2 *y, const sheCipherTextG2 *x);
MCLSHE_DLL_API int sheNegGT(sheCipherTextGT *y, const sheCipherTextGT *x);
// return 0 if success
// z = x + y
MCLSHE_DLL_API int sheAddG1(sheCipherTextG1 *z, const sheCipherTextG1 *x, const sheCipherTextG1 *y);

@ -1253,6 +1253,12 @@ public:
g_[i].setOne();
}
}
static void neg(CipherTextGT& y, const CipherTextGT& x)
{
for (int i = 0; i < 4; i++) {
GT::unitaryInv(y.g_[i], x.g_[i]);
}
}
static void add(CipherTextGT& z, const CipherTextGT& x, const CipherTextGT& y)
{
/*

@ -421,6 +421,33 @@ int sheIsZeroGT(const sheSecretKey *sec, const sheCipherTextGT *c)
return isZeroT(sec, c);
}
template<class CT>
int negT(CT& y, const CT& x)
try
{
CT::neg(y, x);
return 0;
} catch (std::exception& e) {
#ifndef NDEBUG
fprintf(stderr, "err %s\n", e.what());
#endif
return -1;
}
int sheNegG1(sheCipherTextG1 *y, const sheCipherTextG1 *x)
{
return negT(*cast(y), *cast(x));
}
int sheNegG2(sheCipherTextG2 *y, const sheCipherTextG2 *x)
{
return negT(*cast(y), *cast(x));
}
int sheNegGT(sheCipherTextGT *y, const sheCipherTextGT *x)
{
return negT(*cast(y), *cast(x));
}
template<class CT>
int addT(CT& z, const CT& x, const CT& y)

@ -97,15 +97,24 @@ CYBOZU_TEST_AUTO(allOp)
int64_t m2 = -9;
int64_t m3 = 12;
int64_t m4 = -9;
int64_t dec;
sheCipherTextG1 c11, c12;
sheCipherTextG2 c21, c22;
sheCipherTextGT ct;
sheEncG1(&c11, &pub, m1);
sheNegG1(&c12, &c11);
CYBOZU_TEST_EQUAL(sheDecG1(&dec, &sec, &c12), 0);
CYBOZU_TEST_EQUAL(dec, -m1);
sheEncG1(&c12, &pub, m2);
sheSubG1(&c11, &c11, &c12); // m1 - m2
sheMulG1(&c11, &c11, 4); // 4 * (m1 - m2)
sheEncG2(&c21, &pub, m3);
sheNegG2(&c22, &c21);
CYBOZU_TEST_EQUAL(sheDecG2(&dec, &sec, &c22), 0);
CYBOZU_TEST_EQUAL(dec, -m3);
sheEncG2(&c22, &pub, m4);
sheSubG2(&c21, &c21, &c22); // m3 - m4
sheMulG2(&c21, &c21, -5); // -5 * (m3 - m4)
@ -114,9 +123,13 @@ CYBOZU_TEST_AUTO(allOp)
sheMulGT(&ct, &ct, -4); // 160 * (m1 - m2) * (m3 - m4)
int64_t t = 160 * (m1 - m2) * (m3 - m4);
int64_t dec;
CYBOZU_TEST_EQUAL(sheDecGT(&dec, &sec, &ct), 0);
CYBOZU_TEST_EQUAL(dec, t);
sheCipherTextGT ct2;
sheNegGT(&ct2, &ct);
CYBOZU_TEST_EQUAL(sheDecGT(&dec, &sec, &ct2), 0);
CYBOZU_TEST_EQUAL(dec, -t);
}
CYBOZU_TEST_AUTO(rerand)

Loading…
Cancel
Save