简体   繁体   中英

How to use CRT batch technique in Microsoft SEAL 3.1?

Can you please tell me whether SEAL 3.1 supports PolyCRTBuilder class? I am trying to run the following program but failed because the class is not declared in this scope.

/** Suppose I have two arrays x = [1,2,3,4,5] and xMean = [3,3,3,3,3] . I composed and encrypted the two array using PolyCRTBuilder ( xCiphertext and xMeanCiphertext ). If I subtract the two ciphertexts ( xCiphertext MINUS xMeanCiphertext ), I should get xResult = [-2, -1, 0, 1, 2] but after the homomorphic subtraction I am getting xResultDecrypted = [40959, 40960, 0,1, 2] . I can relate the overflow result to the plain modulus set but is there a work around for this problem. Here is the code: */

#include <iostream>
#include "seal/seal.h"
using namespace std;
using namespace seal;

/*
Helper function: Prints the parameters in a SEALContext.
*/
void print_parameters(shared_ptr<SEALContext> context)
{
    // Verify parameters

    if (!context)
    {
        throw invalid_argument("context is not set");
    }
    auto &context_data = *context->context_data();

    /*
    Which scheme are we using?
    */
    string scheme_name;
    switch (context_data.parms().scheme())
    {
    case scheme_type::BFV:scheme_name = "BFV";
        break;
    case scheme_type::CKKS:scheme_name = "CKKS";
        break;
    default:
        throw invalid_argument("unsupported scheme");
    }

    cout << "/ Encryption parameters:" << endl;
    cout << "| scheme: " << scheme_name << endl;
    cout << "| poly_modulus_degree: " << context_data.parms().poly_modulus_degree() << endl;

    /*
    Print the size of the true (product) coefficient modulus.
    */
    cout << "| coeff_modulus size: " << context_data.
        total_coeff_modulus_bit_count() << " bits" << endl;

    /*
    For the BFV scheme print the plain_modulus parameter.
    */
    if (context_data.parms().scheme() == scheme_type::BFV)
    {
        cout << "| plain_modulus: " << context_data.
            parms().plain_modulus().value() << endl;
    }

    cout << "\\ noise_standard_deviation: " << context_data.
        parms().noise_standard_deviation() << endl;
    cout << endl;
}

int main(){
    cout << "\nTotal memory allocated from the current memory pool: "<< (MemoryManager::GetPool().alloc_byte_count() >> 20) << " MB" << endl;
    EncryptionParameters parms(scheme_type::BFV);
    //EncryptionParameters parms;
    parms.set_poly_modulus_degree(4096);
    parms.set_coeff_modulus(coeff_modulus_128(4096));
    parms.set_plain_modulus(40961); ////Make the coefficient modulus prime>2n to enable CRT batching

    auto context = SEALContext::Create(parms);
    print_parameters(context);
    IntegerEncoder encoder(parms.plain_modulus());

    KeyGenerator keygen(context);
    PublicKey public_key = keygen.public_key();
    SecretKey secret_key = keygen.secret_key();
    // SEALContext context(parms);

    // KeyGenerator keygen(context);
    // auto public_key = keygen.public_key();
    // auto secret_key = keygen.secret_key();

    Encryptor encryptor(context, public_key);
    Evaluator evaluator(context);
    Decryptor decryptor(context, secret_key);

    PolyCRTBuilder crtbuilder(context);

    int slot_count = crtbuilder.slot_count();
    int row_size = slot_count / 2;

    vector<uint64_t> x_pod_matrix(slot_count, 0);
    x_pod_matrix[0] = 1;
    x_pod_matrix[1] = 2;
    x_pod_matrix[2] = 3;
    x_pod_matrix[3] = 4;
    x_pod_matrix[4] = 5;

    Plaintext x_plain_matrix;
    crtbuilder.compose(x_pod_matrix, x_plain_matrix);

    Ciphertext x_encrypted_matrix;

    encryptor.encrypt(x_plain_matrix, x_encrypted_matrix);

    vector<uint64_t> x_mean_pod_matrix(slot_count, 0);
    x_mean_pod_matrix[0] = 3;
    x_mean_pod_matrix[1] = 3;
    x_mean_pod_matrix[2] = 3;
    x_mean_pod_matrix[3] = 3;
    x_mean_pod_matrix[4] = 3;

    Plaintext x_mean_plain_matrix;
    crtbuilder.compose(x_mean_pod_matrix, x_mean_plain_matrix);

    Ciphertext x_mean_encrypted_matrix;

    encryptor.encrypt(x_mean_plain_matrix, x_mean_encrypted_matrix);

    evaluator.sub_plain(x_encrypted_matrix, x_mean_encrypted_matrix);

    // Decrypt x_encrypted_matrix
    Plaintext x_plain_result;

    decryptor.decrypt(x_encrypted_matrix, x_plain_result);

    vector<uint64_t> pod_result;

    crtbuilder.decompose(x_plain_result, pod_result);

    for(int i = 0; i < 5; i++)  {

        std::cout << pod_result[i] << '\n';

    }

  return 0;

}

PolyCRTBuilder has been renamed to BatchEncoder . Take a look at the src/examples directory in SEAL v3.1 (or native/examples in a newer version) and you'll see plenty of examples.

Kind of related to your question: the coeff_modulus_128 function hasn't existed in SEAL for quite a while; the same functionality is provided by the CoeffModulus::BFVDefault function. With these changes your code might work in SEAL 3.5 even.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM