c++encryptioncryptographyseal

Vector dot product in Microsoft SEAL with CKKS


I am currently trying to implement matrix multiplication methods using the Microsoft SEAL library. I have created a vector<vector<double>> as input matrix and encoded it with CKKSEncoder. However the encoder packs an entire vector into a single Plaintext so I just have a vector<Plaintext> which makes me lose the 2D structure (and then of course I'll have a vector<Ciphertext> after encryption). Having a 1D vector allows me to access only the rows entirely but not the columns.

I managed to transpose the matrices before encoding. This allowed me to multiply component-wise the rows of the first matrix and columns (rows in transposed form) of the second matrix but I am unable to sum the elements of the resulting vector together since it's packed into a single Ciphertext. I just need to figure out how to make the vector dot product work in SEAL to perform matrix multiplication. Am I missing something or is my method wrong?


Solution

  • It has been suggested by KyoohyungHan in the issue: https://github.com/microsoft/SEAL/issues/138 that it is possible to solve the problem with rotations by rotating the output vector and summing it up repeatedly.

    For example:

    // my_output_vector is the Ciphertext output
    
    vector<Ciphertext> rotations_output(my_output_vector.size());
    
    for(int steps = 0; steps < my_output_vector.size(); steps++)
    {
        evaluator.rotate_vector(my_output_vector, steps, galois_keys, rotations_output[steps]);
    }
    
    Ciphertext sum_output;
    evaluator.add_many(rotations_output, sum_output);