HElib icon indicating copy to clipboard operation
HElib copied to clipboard

Doesn't Helib support negative number calculation

Open bear1988520 opened this issue 6 years ago • 5 comments

Technical Lead: Expected Effort (Days, Weeks or Months): Expected Start Date: People or Skills Needed:

Brief Description:

I use Helib for data encryption and calculation, but it seems that it gives a wrong answer when I set a=-2 and b=3. Have I used the wrong method? The code is as below:

publicKey.Encrypt(ctx1, to_ZZX(-2)); // Encrypt the value 2 publicKey.Encrypt(ctx2, to_ZZX(-3)); // Encrypt the value 3

    Ctxt ctSum = ctx1;                   // Create a ciphertext to hold the sum and initialize it with Enc(2)
    ctSum += ctx2;                       // Perform Enc(2) + Enc(3)

    ZZX ptSum;                           //	Create a plaintext to hold the plaintext of the sum
    secretKey.Decrypt(ptSum, ctSum);	 // Decrypt the ciphertext ctSum into the plaintext ptSum using secretKey

    cout << "-2 + -3 = " << ptSum[0] << endl;

bear1988520 avatar Jan 07 '19 08:01 bear1988520

It should return (-5) mod p where p is the plaintext base.

quanhanyu avatar Jan 07 '19 08:01 quanhanyu

It should return (-5) mod p where p is the plaintext base.

however, it return a wrong answer lx_clip1546859449460_lx

bear1988520 avatar Jan 07 '19 11:01 bear1988520

It should return (-5) mod p where p is the plaintext base.

and where p is 1021 in my code. THX very much.

bear1988520 avatar Jan 07 '19 11:01 bear1988520

-5 mod 1021 == 1016 When you decrypt you should add -1021 to the result if it's larger than 510.

ssmiler avatar Jan 07 '19 12:01 ssmiler

-5 mod 1021 == 1016 When you decrypt you should add -1021 to the result if it's larger than 510.

THX

bear1988520 avatar Jan 09 '19 06:01 bear1988520