[英]How to use CRT batch technique in Microsoft SEAL 3.1?
Can you please tell me whether SEAL 3.1 supports PolyCRTBuilder class?您能否告诉我 SEAL 3.1 是否支持 PolyCRTBuilder class? I am trying to run the following program but failed because the class is not declared in this scope.
我正在尝试运行以下程序但失败了,因为 scope 中未声明 class。
/** Suppose I have two arrays x = [1,2,3,4,5]
and xMean = [3,3,3,3,3]
. /** 假设我有两个 arrays
x = [1,2,3,4,5]
和xMean = [3,3,3,3,3]
。 I composed and encrypted the two array using PolyCRTBuilder ( xCiphertext and xMeanCiphertext ).我使用 PolyCRTBuilder( xCiphertext 和 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]
.如果我减去两个密文( xCiphertext MINUS xMeanCiphertext ),我应该得到
xResult = [-2, -1, 0, 1, 2]
但在同态减法之后我得到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
. PolyCRTBuilder
已重命名为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.查看 SEAL v3.1 中的
src/examples
目录(或较新版本中的native/examples
),您会看到大量示例。
Kind of related to your question: the coeff_modulus_128
function hasn't existed in SEAL for quite a while;与您的问题有关:
coeff_modulus_128
function 在 SEAL 中已经存在了很长一段时间了; the same functionality is provided by the CoeffModulus::BFVDefault
function. CoeffModulus::BFVDefault
function 提供了相同的功能。 With these changes your code might work in SEAL 3.5 even.通过这些更改,您的代码甚至可以在 SEAL 3.5 中运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.