intel / pailliercryptolib

Intel Paillier Cryptosystem Library is an open-source library which provides accelerated performance of a partial homomorphic encryption (HE), named Paillier cryptosystem, by utilizing Intel® IPP-Crypto on AVX512IFMA instructions. Intel Paillier Cryptosystem Library is certified for ISO compliance.
Apache License 2.0
73 stars 18 forks source link

Does ipcl support encrypt a negative value? #41

Closed xhuan28 closed 1 year ago

xhuan28 commented 2 years ago

I tried to encrypt a negative value and then decrypt it. However, inconsistent values are obtained. See the code snippet as follows:

  ipcl::keyPair kp = ipcl::generateKeypair(1024, true);
  BigNumber bn(-123);
  auto pt = ipcl::PlainText(bn);
  ipcl::CipherText ct = kp.pub_key->encrypt(pt);
  ipcl::PlainText res = kp.priv_key->decrypt(ct);
  std::cout << pt.getElement(0) << std::endl;
  std::cout << res.getElement(0) << std::endl;

The output is:

-0x7b 0xaf3965135bf1a73acdf13bdd56deb3879746b648a64f79c692d7d21ddf0a177de13b9531347714969405cd62c9849e9a94841f4c4c9fa29cb206c6131eb978761b131742820ccdf40b63a8e3b0763c3727e729c9d0704df0f37119505bcfcb3b988aedd7a552a23a7169c737a148806422a53aa4c62785ff7b3694b6a600e504

So my question is, does ipcl support encrypt a negative value?

justalittlenoob commented 2 years ago

No. Paillier encryption is ONLY defined for NON-negative integers.
(btw, the integer should less than n)

xhuan28 commented 2 years ago

Well, if that is the case, I need to find a way to handle negative integers on higher level. Currently, what I am doing is,

ipcl::PlainText res = kp.priv_key->decrypt(ct);
if (res > n / 2) {
  res  -= n;
}

So far, it works fine. It would be great if negative values can be handled in IPCL in future.

justalittlenoob commented 2 years ago

If you want to do CT - CT, you can do something like this: image

output: image