Skip to content

Commit

Permalink
SEALContext is no longer wrapper in shared_ptr and is now copyble and
Browse files Browse the repository at this point in the history
assignable
  • Loading branch information
kimlaine committed Jul 5, 2020
1 parent b7210f5 commit a94374d
Show file tree
Hide file tree
Showing 68 changed files with 1,058 additions and 1,192 deletions.
8 changes: 4 additions & 4 deletions native/examples/1_bfv_basics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ void example_bfv_basics()
object. This is a heavy class that checks the validity and properties of the
parameters we just set.
*/
auto context = SEALContext::Create(parms);
SEALContext context(parms);

/*
Print the parameters that we have chosen.
Expand All @@ -137,7 +137,7 @@ void example_bfv_basics()
When parameters are used to create SEALContext, Microsoft SEAL will first
validate those parameters. The parameters chosen here are valid.
*/
cout << "Parameter validation (success): " << context->parameter_error_message() << endl;
cout << "Parameter validation (success): " << context.parameter_error_message() << endl;

cout << endl;
cout << "~~~~~~ A naive way to calculate 4(x^2+1)(x+1)^2. ~~~~~~" << endl;
Expand Down Expand Up @@ -418,9 +418,9 @@ void example_bfv_basics()
print_line(__LINE__);
cout << "An example of invalid parameters" << endl;
parms.set_poly_modulus_degree(2048);
context = SEALContext::Create(parms);
context = SEALContext(parms);
print_parameters(context);
cout << "Parameter validation (failed): " << context->parameter_error_message() << endl << endl;
cout << "Parameter validation (failed): " << context.parameter_error_message() << endl << endl;

/*
This information is helpful to fix invalid encryption parameters.
Expand Down
8 changes: 4 additions & 4 deletions native/examples/2_encoders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ void example_integer_encoder()
will be incorrect.
*/
parms.set_plain_modulus(512);
auto context = SEALContext::Create(parms);
SEALContext context(parms);
print_parameters(context);
cout << endl;

Expand Down Expand Up @@ -182,15 +182,15 @@ void example_batch_encoder()
*/
parms.set_plain_modulus(PlainModulus::Batching(poly_modulus_degree, 20));

auto context = SEALContext::Create(parms);
SEALContext context(parms);
print_parameters(context);
cout << endl;

/*
We can verify that batching is indeed enabled by looking at the encryption
parameter qualifiers created by SEALContext.
*/
auto qualifiers = context->first_context_data()->qualifiers();
auto qualifiers = context.first_context_data()->qualifiers();
cout << "Batching enabled: " << boolalpha << qualifiers.using_batching << endl;

KeyGenerator keygen(context);
Expand Down Expand Up @@ -349,7 +349,7 @@ void example_ckks_encoder()
/*
We create the SEALContext as usual and print the parameters.
*/
auto context = SEALContext::Create(parms);
SEALContext context(parms);
print_parameters(context);
cout << endl;

Expand Down
18 changes: 9 additions & 9 deletions native/examples/3_levels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ void example_levels()
*/
parms.set_plain_modulus(PlainModulus::Batching(poly_modulus_degree, 20));

auto context = SEALContext::Create(parms);
SEALContext context(parms);
print_parameters(context);
cout << endl;

Expand All @@ -109,7 +109,7 @@ void example_levels()
/*
First print the key level parameter information.
*/
auto context_data = context->key_context_data();
auto context_data = context.key_context_data();
cout << "----> Level (chain index): " << context_data->chain_index();
cout << " ...... key_context_data()" << endl;
cout << " parms_id: " << context_data->parms_id() << endl;
Expand All @@ -126,15 +126,15 @@ void example_levels()
/*
Next iterate over the remaining (data) levels.
*/
context_data = context->first_context_data();
context_data = context.first_context_data();
while (context_data)
{
cout << " Level (chain index): " << context_data->chain_index();
if (context_data->parms_id() == context->first_parms_id())
if (context_data->parms_id() == context.first_parms_id())
{
cout << " ...... first_context_data()" << endl;
}
else if (context_data->parms_id() == context->last_parms_id())
else if (context_data->parms_id() == context.last_parms_id())
{
cout << " ...... last_context_data()" << endl;
}
Expand Down Expand Up @@ -206,7 +206,7 @@ void example_levels()
*/
print_line(__LINE__);
cout << "Perform modulus switching on encrypted and print." << endl;
context_data = context->first_context_data();
context_data = context.first_context_data();
cout << "---->";
while (context_data->next_context_data())
{
Expand Down Expand Up @@ -300,9 +300,9 @@ void example_levels()
/*
In BFV modulus switching is not necessary and in some cases the user might
not want to create the modulus switching chain, except for the highest two
levels. This can be done by passing a bool `false' to SEALContext::Create.
levels. This can be done by passing a bool `false' to SEALContext constructor.
*/
context = SEALContext::Create(parms, false);
context = SEALContext(parms, false);

/*
We can check that indeed the modulus switching chain has been created only
Expand All @@ -313,7 +313,7 @@ void example_levels()
print_line(__LINE__);
cout << "Print the modulus switching chain." << endl;
cout << "---->";
for (context_data = context->key_context_data(); context_data; context_data = context_data->next_context_data())
for (context_data = context.key_context_data(); context_data; context_data = context_data->next_context_data())
{
cout << " Level (chain index): " << context_data->chain_index() << endl;
cout << " parms_id: " << context_data->parms_id() << endl;
Expand Down
8 changes: 4 additions & 4 deletions native/examples/4_ckks_basics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ void example_ckks_basics()
*/
double scale = pow(2.0, 40);

auto context = SEALContext::Create(parms);
SEALContext context(parms);
print_parameters(context);
cout << endl;

Expand Down Expand Up @@ -204,11 +204,11 @@ void example_ckks_basics()
print_line(__LINE__);
cout << "Parameters used by all three terms are different." << endl;
cout << " + Modulus chain index for x3_encrypted: "
<< context->get_context_data(x3_encrypted.parms_id())->chain_index() << endl;
<< context.get_context_data(x3_encrypted.parms_id())->chain_index() << endl;
cout << " + Modulus chain index for x1_encrypted: "
<< context->get_context_data(x1_encrypted.parms_id())->chain_index() << endl;
<< context.get_context_data(x1_encrypted.parms_id())->chain_index() << endl;
cout << " + Modulus chain index for plain_coeff0: "
<< context->get_context_data(plain_coeff0.parms_id())->chain_index() << endl;
<< context.get_context_data(plain_coeff0.parms_id())->chain_index() << endl;
cout << endl;

/*
Expand Down
4 changes: 2 additions & 2 deletions native/examples/5_rotation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ void example_rotation_bfv()
parms.set_coeff_modulus(CoeffModulus::BFVDefault(poly_modulus_degree));
parms.set_plain_modulus(PlainModulus::Batching(poly_modulus_degree, 20));

auto context = SEALContext::Create(parms);
SEALContext context(parms);
print_parameters(context);
cout << endl;

Expand Down Expand Up @@ -134,7 +134,7 @@ void example_rotation_ckks()
parms.set_poly_modulus_degree(poly_modulus_degree);
parms.set_coeff_modulus(CoeffModulus::Create(poly_modulus_degree, { 40, 40, 40, 40, 40 }));

auto context = SEALContext::Create(parms);
SEALContext context(parms);
print_parameters(context);
cout << endl;

Expand Down
6 changes: 3 additions & 3 deletions native/examples/6_serialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ void example_serialization()
*/
parms_stream.seekg(0, parms_stream.beg);

auto context = SEALContext::Create(parms);
SEALContext context(parms);

KeyGenerator keygen(context);
auto sk = keygen.secret_key();
Expand Down Expand Up @@ -298,7 +298,7 @@ void example_serialization()
EncryptionParameters parms;
parms.load(parms_stream);
parms_stream.seekg(0, parms_stream.beg);
auto context = SEALContext::Create(parms);
SEALContext context(parms);

Evaluator evaluator(context);

Expand Down Expand Up @@ -347,7 +347,7 @@ void example_serialization()
EncryptionParameters parms;
parms.load(parms_stream);
parms_stream.seekg(0, parms_stream.beg);
auto context = SEALContext::Create(parms);
SEALContext context(parms);

/*
Load back the secret key from sk_stream.
Expand Down
44 changes: 22 additions & 22 deletions native/examples/7_performance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
using namespace std;
using namespace seal;

void bfv_performance_test(shared_ptr<SEALContext> context)
void bfv_performance_test(SEALContext context)
{
chrono::high_resolution_clock::time_point time_start, time_end;

print_parameters(context);
cout << endl;

auto &parms = context->first_context_data()->parms();
auto &parms = context.first_context_data()->parms();
auto &plain_modulus = parms.plain_modulus();
size_t poly_modulus_degree = parms.poly_modulus_degree();

Expand All @@ -27,7 +27,7 @@ void bfv_performance_test(shared_ptr<SEALContext> context)
RelinKeys relin_keys;
GaloisKeys gal_keys;
chrono::microseconds time_diff;
if (context->using_keyswitching())
if (context.using_keyswitching())
{
/*
Generate relinearization keys.
Expand All @@ -39,7 +39,7 @@ void bfv_performance_test(shared_ptr<SEALContext> context)
time_diff = chrono::duration_cast<chrono::microseconds>(time_end - time_start);
cout << "Done [" << time_diff.count() << " microseconds]" << endl;

if (!context->key_context_data()->qualifiers().using_batching)
if (!context.key_context_data()->qualifiers().using_batching)
{
cout << "Given encryption parameters do not support batching." << endl;
return;
Expand Down Expand Up @@ -201,7 +201,7 @@ void bfv_performance_test(shared_ptr<SEALContext> context)
time_end = chrono::high_resolution_clock::now();
time_square_sum += chrono::duration_cast<chrono::microseconds>(time_end - time_start);

if (context->using_keyswitching())
if (context.using_keyswitching())
{
/*
[Relinearize]
Expand Down Expand Up @@ -280,7 +280,7 @@ void bfv_performance_test(shared_ptr<SEALContext> context)
cout << "Average multiply: " << avg_multiply << " microseconds" << endl;
cout << "Average multiply plain: " << avg_multiply_plain << " microseconds" << endl;
cout << "Average square: " << avg_square << " microseconds" << endl;
if (context->using_keyswitching())
if (context.using_keyswitching())
{
cout << "Average relinearize: " << avg_relinearize << " microseconds" << endl;
cout << "Average rotate rows one step: " << avg_rotate_rows_one_step << " microseconds" << endl;
Expand All @@ -290,14 +290,14 @@ void bfv_performance_test(shared_ptr<SEALContext> context)
cout.flush();
}

void ckks_performance_test(shared_ptr<SEALContext> context)
void ckks_performance_test(SEALContext context)
{
chrono::high_resolution_clock::time_point time_start, time_end;

print_parameters(context);
cout << endl;

auto &parms = context->first_context_data()->parms();
auto &parms = context.first_context_data()->parms();
size_t poly_modulus_degree = parms.poly_modulus_degree();

cout << "Generating secret/public keys: ";
Expand All @@ -310,7 +310,7 @@ void ckks_performance_test(shared_ptr<SEALContext> context)
RelinKeys relin_keys;
GaloisKeys gal_keys;
chrono::microseconds time_diff;
if (context->using_keyswitching())
if (context.using_keyswitching())
{
cout << "Generating relinearization keys: ";
time_start = chrono::high_resolution_clock::now();
Expand All @@ -319,7 +319,7 @@ void ckks_performance_test(shared_ptr<SEALContext> context)
time_diff = chrono::duration_cast<chrono::microseconds>(time_end - time_start);
cout << "Done [" << time_diff.count() << " microseconds]" << endl;

if (!context->first_context_data()->qualifiers().using_batching)
if (!context.first_context_data()->qualifiers().using_batching)
{
cout << "Given encryption parameters do not support batching." << endl;
return;
Expand Down Expand Up @@ -453,7 +453,7 @@ void ckks_performance_test(shared_ptr<SEALContext> context)
time_end = chrono::high_resolution_clock::now();
time_square_sum += chrono::duration_cast<chrono::microseconds>(time_end - time_start);

if (context->using_keyswitching())
if (context.using_keyswitching())
{
/*
[Relinearize]
Expand Down Expand Up @@ -531,7 +531,7 @@ void ckks_performance_test(shared_ptr<SEALContext> context)
cout << "Average multiply: " << avg_multiply << " microseconds" << endl;
cout << "Average multiply plain: " << avg_multiply_plain << " microseconds" << endl;
cout << "Average square: " << avg_square << " microseconds" << endl;
if (context->using_keyswitching())
if (context.using_keyswitching())
{
cout << "Average relinearize: " << avg_relinearize << " microseconds" << endl;
cout << "Average rescale: " << avg_rescale << " microseconds" << endl;
Expand All @@ -551,21 +551,21 @@ void example_bfv_performance_default()
parms.set_poly_modulus_degree(poly_modulus_degree);
parms.set_coeff_modulus(CoeffModulus::BFVDefault(poly_modulus_degree));
parms.set_plain_modulus(786433);
bfv_performance_test(SEALContext::Create(parms));
bfv_performance_test(parms);

cout << endl;
poly_modulus_degree = 8192;
parms.set_poly_modulus_degree(poly_modulus_degree);
parms.set_coeff_modulus(CoeffModulus::BFVDefault(poly_modulus_degree));
parms.set_plain_modulus(786433);
bfv_performance_test(SEALContext::Create(parms));
bfv_performance_test(parms);

cout << endl;
poly_modulus_degree = 16384;
parms.set_poly_modulus_degree(poly_modulus_degree);
parms.set_coeff_modulus(CoeffModulus::BFVDefault(poly_modulus_degree));
parms.set_plain_modulus(786433);
bfv_performance_test(SEALContext::Create(parms));
bfv_performance_test(parms);

/*
Comment out the following to run the biggest example.
Expand All @@ -575,7 +575,7 @@ void example_bfv_performance_default()
// parms.set_poly_modulus_degree(poly_modulus_degree);
// parms.set_coeff_modulus(CoeffModulus::BFVDefault(poly_modulus_degree));
// parms.set_plain_modulus(786433);
// bfv_performance_test(SEALContext::Create(parms));
// bfv_performance_test(parms);
}

void example_bfv_performance_custom()
Expand Down Expand Up @@ -610,7 +610,7 @@ void example_bfv_performance_custom()
{
parms.set_plain_modulus(786433);
}
bfv_performance_test(SEALContext::Create(parms));
bfv_performance_test(parms);
}

void example_ckks_performance_default()
Expand All @@ -623,19 +623,19 @@ void example_ckks_performance_default()
size_t poly_modulus_degree = 4096;
parms.set_poly_modulus_degree(poly_modulus_degree);
parms.set_coeff_modulus(CoeffModulus::BFVDefault(poly_modulus_degree));
ckks_performance_test(SEALContext::Create(parms));
ckks_performance_test(parms);

cout << endl;
poly_modulus_degree = 8192;
parms.set_poly_modulus_degree(poly_modulus_degree);
parms.set_coeff_modulus(CoeffModulus::BFVDefault(poly_modulus_degree));
ckks_performance_test(SEALContext::Create(parms));
ckks_performance_test(parms);

cout << endl;
poly_modulus_degree = 16384;
parms.set_poly_modulus_degree(poly_modulus_degree);
parms.set_coeff_modulus(CoeffModulus::BFVDefault(poly_modulus_degree));
ckks_performance_test(SEALContext::Create(parms));
ckks_performance_test(parms);

/*
Comment out the following to run the biggest example.
Expand All @@ -644,7 +644,7 @@ void example_ckks_performance_default()
// poly_modulus_degree = 32768;
// parms.set_poly_modulus_degree(poly_modulus_degree);
// parms.set_coeff_modulus(CoeffModulus::BFVDefault(poly_modulus_degree));
// ckks_performance_test(SEALContext::Create(parms));
// ckks_performance_test(parms);
}

void example_ckks_performance_custom()
Expand All @@ -671,7 +671,7 @@ void example_ckks_performance_custom()
EncryptionParameters parms(scheme_type::CKKS);
parms.set_poly_modulus_degree(poly_modulus_degree);
parms.set_coeff_modulus(CoeffModulus::BFVDefault(poly_modulus_degree));
ckks_performance_test(SEALContext::Create(parms));
ckks_performance_test(parms);
}

/*
Expand Down
Loading

0 comments on commit a94374d

Please sign in to comment.