Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Polyarrays created from ciphertexts/public keys are converted out of NTT on creation #4

Merged
merged 3 commits into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 57 additions & 3 deletions native/src/seal/polyarray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT license.

#include "seal/polyarray.h"
#include <algorithm>

using namespace seal::util;

Expand All @@ -12,13 +13,17 @@ namespace seal
const Ciphertext &ciphertext,
MemoryPoolHandle pool
) : PolynomialArray(pool) {

auto &parms = context.first_context_data()->parms();
auto &coeff_modulus = parms.coeff_modulus();
auto coeff_modulus_size = ciphertext.coeff_modulus_size();
auto poly_modulus_degree = ciphertext.poly_modulus_degree();
auto num_poly = ciphertext.size();

auto is_ntt_form = ciphertext.is_ntt_form();
size_t coeff_count = parms.poly_modulus_degree();
auto &context_data = *context.get_context_data(parms.parms_id());
auto ntt_tables = context_data.small_ntt_tables();

reserve(num_poly, poly_modulus_degree, coeff_modulus);

// The ciphertexts are stored in the same RNS format as the
Expand All @@ -27,6 +32,17 @@ namespace seal
const auto data_ptr = ciphertext.data() + i * (poly_modulus_degree * coeff_modulus_size);
insert_polynomial(i, data_ptr);
}

// Convert out of NTT form for each polynomial. For BFV, this should not
// be necessary.
if (is_ntt_form) {
for (size_t i = 0; i < coeff_modulus_size; i++) {
for (size_t j = 0; j < num_poly; j++) {
inverse_ntt_negacyclic_harvey(get_polynomial(j) + i * coeff_count, ntt_tables[i]);
}
}
}

}

PolynomialArray::PolynomialArray(
Expand All @@ -38,18 +54,32 @@ namespace seal
auto &ciphertext = public_key.data();
auto &parms = context.first_context_data()->parms();
auto &coeff_modulus = parms.coeff_modulus();
auto coeff_modulus_size = ciphertext.coeff_modulus_size();
auto coeff_modulus_size = coeff_modulus.size();
auto poly_modulus_degree = ciphertext.poly_modulus_degree();
auto num_poly = ciphertext.size();

auto is_ntt_form = public_key.is_ntt_form();
size_t coeff_count = parms.poly_modulus_degree();
auto &context_data = *context.get_context_data(parms.parms_id());
auto ntt_tables = context_data.small_ntt_tables();

reserve(num_poly, poly_modulus_degree, coeff_modulus);

// The ciphertexts are stored in the same RNS format as the
// other polynomial arrays.
for (int i = 0; i < num_poly; i++) {
const auto data_ptr = ciphertext.data() + i * (poly_modulus_degree * coeff_modulus_size);
const auto data_ptr = public_key.data().data(i);
insert_polynomial(i, data_ptr);
}

// Convert out of NTT form for each polynomial.
if (is_ntt_form) {
for (size_t i = 0; i < coeff_modulus_size; i++) {
for (size_t j = 0; j < num_poly; j++) {
inverse_ntt_negacyclic_harvey(get_polynomial(j) + i * coeff_count, ntt_tables[i]);
}
}
}
}

PolynomialArray::PolynomialArray(const PolynomialArray &copy): PolynomialArray(copy.pool_) {
Expand Down Expand Up @@ -123,4 +153,28 @@ namespace seal

is_rns_ = true;
}

/**
Switches the polynomial array down one modulus by dropping the last modulus
in the set.
*/
PolynomialArray PolynomialArray::drop() const {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently this method is not used. I was using it when I was converting the underlying public key ciphertext from a higher modulus to a lower modulus, but the current implementation (using first_context_data instead of key_context_data already does this drop for us). Leaving this here in case we need it later. I have a test in Rust that ensures it does the correct thing.

auto lower_modulus = rnsbase_.get()->drop();
auto new_coeff_modulus_size = lower_modulus.size();
std::vector<Modulus> lower_modulus_values(lower_modulus.size());

for (std::size_t i = 0; i < lower_modulus.size(); i++) {
lower_modulus_values[i] = lower_modulus[i];
}

auto new_len = poly_size_ * coeff_size_ * new_coeff_modulus_size;

PolynomialArray poly_array(pool_);
poly_array.reserve(poly_size_, coeff_size_, lower_modulus_values);

std::copy_n(data_.get(), new_len, poly_array.data_.get());
poly_array.polynomial_reserved_ = polynomial_reserved_;

return poly_array;
}
}
5 changes: 5 additions & 0 deletions native/src/seal/polyarray.h
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,11 @@ namespace seal
util::set_uint(data_.get(), len_, data);
}

/**
Switches the polynomial array down one modulus by dropping the
last modulus in the set.
*/
PolynomialArray drop() const;

private:
// We make an independent function instead of setting on
Expand Down
8 changes: 8 additions & 0 deletions native/src/seal/publickey.h
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,14 @@ namespace seal
return pk_.pool();
}

/**
Returns whether the ciphertext is in NTT form.
*/
SEAL_NODISCARD inline bool is_ntt_form() const noexcept
{
return pk_.is_ntt_form();
}

/**
Enables access to private members of seal::PublicKey for SEAL_C.
*/
Expand Down