Как использовать пакетную технику CRT в Microsoft SEAL 3.1? - PullRequest
0 голосов
/ 17 июня 2020

Подскажите, пожалуйста, поддерживает ли SEAL 3.1 класс PolyCRTBuilder? Я пытаюсь запустить следующую программу, но потерпел неудачу, поскольку класс не объявлен в этой области.

/ ** Предположим, у меня есть два массива x = [1,2,3,4,5] и xMean = [3,3,3,3,3]. Я составил и зашифровал два массива с помощью PolyCRTBuilder (xCiphertext и xMeanCiphertext). Если я вычту два зашифрованных текста (xCiphertext MINUS xMeanCiphertext), я должен получить xResult = [-2, -1, 0, 1, 2], но после вычитания homomorphi c я получаю xResultDecrypted = [40959, 40960, 0 ,1, 2]. Я могу связать результат переполнения с простым набором модулей, но есть ли способ решить эту проблему. Вот код: * /

#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;

}

1 Ответ

1 голос
/ 18 июня 2020

PolyCRTBuilder был переименован в BatchEncoder. Взгляните на каталог src/examples в SEAL v3.1 (или native/examples в более новой версии), и вы увидите множество примеров.

Относится к вашему вопросу: coeff_modulus_128 функция давно не существовала в SEAL; такую ​​же функциональность предоставляет функция CoeffModulus::BFVDefault. С этими изменениями ваш код может работать даже в SEAL 3.5.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...