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

I get an error when I rotate the ciphertext vector #97

Closed Zeng1998 closed 4 years ago

Zeng1998 commented 4 years ago

I want to rotate the product vector of two ciphertext vectors, but I get an error and I don't know how I should try to solve it. It is correct when I rotate a ciphertext vector, but when this ciphertext vector is a product, an error occurs.

int main() {
    EncryptionParameters parms(scheme_type::BFV);

    parms.set_poly_modulus_degree(poly_modulus_degree);
    parms.set_coeff_modulus(CoeffModulus::BFVDefault(poly_modulus_degree));
    parms.set_plain_modulus(PlainModulus::Batching(poly_modulus_degree, 20));
    auto context = SEALContext::Create(parms);
    KeyGenerator keygen(context);
    PublicKey psk=keygen.public_key();
    SecretKey sk=keygen.secret_key();
    RelinKeys rlk=keygen.relin_keys();
  GaloisKeys glk=keygen.galois_keys();
  Encryptor encryptor(context, psk);
  Decryptor decryptor(context, sk);
  Evaluator evaluator(context);
  BatchEncoder batch_encoder(context);

  size_t slot_count = batch_encoder.slot_count();
  size_t row_size = slot_count / 2;

  mt19937 rnd(time(NULL));
  vector<uint64_t> v(slot_count,0ull);
  for(int i=0;i<1000;i++){
      v[i]=rnd()%1000;
  }
  print_matrix(v,row_size);
  Plaintext pr;
  Ciphertext cr;
  batch_encoder.encode(v,pr);
  encryptor.encrypt(pr,cr);
  evaluator.multiply_inplace(cr,cr);
  decryptor.decrypt(cr,pr);
  batch_encoder.decode(pr,v);
  print_matrix(v,row_size);
  //error::  what():  encrypted size must be 2
  evaluator.rotate_rows(cr,pow(2,0),glk,cr);
  return 0;
Zeng1998 commented 4 years ago

I solved it, the size of the ciphertext will change from 2 to 3 after multiplication, and I need to use relinearize to change the size to 2, and then I can rotate it. Using batch_encode and rotate can make calculating the inner product of the ciphertext much more efficient