Skip to content

Commit

Permalink
Convert out of NTT for polyarrays from cipher/public key
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanorendorff committed Jul 31, 2023
1 parent c5425cf commit a650181
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
30 changes: 28 additions & 2 deletions native/src/seal/polyarray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,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 +31,15 @@ namespace seal
const auto data_ptr = ciphertext.data() + i * (poly_modulus_degree * coeff_modulus_size);
insert_polynomial(i, data_ptr);
}

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 @@ -36,12 +49,17 @@ namespace seal
) : PolynomialArray(pool) {

auto &ciphertext = public_key.data();
auto &parms = context.first_context_data()->parms();
auto &parms = context.key_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 = 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
Expand All @@ -50,6 +68,14 @@ namespace seal
const auto data_ptr = ciphertext.data() + i * (poly_modulus_degree * coeff_modulus_size);
insert_polynomial(i, data_ptr);
}

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

0 comments on commit a650181

Please sign in to comment.