microsoft / SEAL

Microsoft SEAL is an easy-to-use and powerful homomorphic encryption library.
https://www.microsoft.com/en-us/research/group/cryptography-research/
MIT License
3.61k stars 709 forks source link

output out of range when I add and multiply #90

Closed Zeng1998 closed 4 years ago

Zeng1998 commented 4 years ago

I want to calculate the dot product of a vector, but find that the sum overflow becomes negative during the calculation. The maximum value is about 20,000. But when I took this part alone for testing, I used the same method to multiply and accumulate random numbers, and found that it would not overflow, and the addition of the int ciphertext was normal. I lack the theoretical basis of homomorphic encryption, and I want to know if I need to set certain parameters or call certain methods.

Zeng1998 commented 4 years ago
for(int i=1;i<=M;i++){
        Ciphertext c_sum=zero_cr;
        for(int j=1;j<=M;j++){
            string name="/home/keane/CLionProjects/sim/sim_"+to_string(i)+"_"+to_string(j)+".data";
            read_by_file(name,cr,context);
            evaluator.multiply_inplace(cr,user[j]);
            evaluator.add_inplace(c_sum,cr);
            decryptor.decrypt(c_sum,pr);
        }
        decryptor.decrypt(c_sum,pr);
        auto val=encoder.decode_uint64(pr);
        cout << i << val << endl;
    }

This is wrong

for(int i=1;i<=M;i++){
        Ciphertext c_sum=zero_cr;
        for(int j=1;j<=M;j++){
           int vv=666;
           encryptor.encrypt(encoder.encode(vv),cr);
           evaluator.multiply_inplace(cr,cr);
            evaluator.add_inplace(c_sum,cr);
            decryptor.decrypt(c_sum,pr);
        }
        decryptor.decrypt(c_sum,pr);
        auto val=encoder.decode_uint64(pr);
        cout << i << val << endl;
    }

And this is right