Skip to content

Commit

Permalink
BGV encryption
Browse files Browse the repository at this point in the history
- add size check
- refactor some codes
  • Loading branch information
居侯 authored and Wei Dai committed Mar 17, 2022
1 parent 6eee31f commit 5b6a321
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 21 deletions.
12 changes: 7 additions & 5 deletions native/src/seal/encryptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ namespace seal
get<0>(I), prev_context_data.small_ntt_tables(), pool);
}
// bfv switch-to-next
else if(parms.scheme() != scheme_type::bgv)
else if (parms.scheme() != scheme_type::bgv)
{
rns_tool->divide_and_round_q_last_inplace(get<0>(I), pool);
}
Expand Down Expand Up @@ -244,10 +244,12 @@ namespace seal
throw invalid_argument("plain cannot be in NTT form");
}
encrypt_zero_internal(context_.first_parms_id(), is_asymmetric, save_seed, destination, pool);
//c_{0} = pk_{0}*u + p*e_{0} + M
add_plain_without_scaling_variant(plain, *context_.first_context_data(),
RNSIter(destination.data(0), context_.first_context_data()->parms().poly_modulus_degree()));

auto context_data_ptr = context_.first_context_data();
auto &parms = context_data_ptr->parms();
size_t coeff_count = parms.poly_modulus_degree();
size_t coeff_modulus_size = parms.coeff_modulus().size();
// c_{0} = pk_{0}*u + p*e_{0} + M
add_plain_without_scaling_variant(plain, *context_data_ptr, RNSIter(destination.data(0), coeff_count));
}
else
{
Expand Down
55 changes: 39 additions & 16 deletions native/src/seal/util/scalingvariant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,26 @@ namespace seal
auto &parms = context_data.parms();
auto &coeff_modulus = parms.coeff_modulus();
auto &plain_modulus = parms.plain_modulus();
size_t coeff_count = plain.coeff_count();
size_t coeff_modulus_size = coeff_modulus.size();
const size_t coeff_count = plain.coeff_count();
const size_t coeff_modulus_size = coeff_modulus.size();
if (coeff_count > parms.poly_modulus_degree())
{
throw std::invalid_argument("add_plain_without_scaling_variant: invalid plaintext");
}

SEAL_ITERATE(iter(destination, coeff_modulus), coeff_modulus_size, [&](auto I){
for(size_t j = 0; j < coeff_count; ++j){
//This can be replaced with barrett reduction.
uint64_t plain_mod = modulo_uint(plain.data() + j, 1, plain_modulus);
modulo_uint_inplace(&plain_mod, 1, get<1>(I));
get<0>(I)[j] = add_uint_mod(get<0>(I)[j], plain_mod, get<1>(I));
}
if (destination.poly_modulus_degree() != parms.poly_modulus_degree())
{
throw std::invalid_argument("add_plain_without_scaling_variant: invalid destination iter");
}

SEAL_ITERATE(iter(destination, coeff_modulus), coeff_modulus_size, [&](auto I) {
const Modulus &cipher_modulus = get<1>(I);
std::transform(
plain.data(), plain.data() + coeff_count, get<0>(I), get<0>(I),
[&](uint64_t m, uint64_t c) -> uint64_t {
m = barrett_reduce_64(m, plain_modulus);
return add_uint_mod(c, m, cipher_modulus);
});
});
}

Expand All @@ -37,14 +47,27 @@ namespace seal
auto &parms = context_data.parms();
auto &coeff_modulus = parms.coeff_modulus();
auto &plain_modulus = parms.plain_modulus();
size_t coeff_count = plain.coeff_count();
size_t coeff_modulus_size = coeff_modulus.size();
const size_t coeff_count = plain.coeff_count();
const size_t coeff_modulus_size = coeff_modulus.size();

SEAL_ITERATE(iter(destination, coeff_modulus), coeff_modulus_size, [&](auto I){
for(size_t j = 0; j < coeff_count; ++j){
uint64_t plain_mod = modulo_uint(plain.data() + j, 1, plain_modulus);
get<0>(I)[j] = sub_uint_mod(get<0>(I)[j], plain_mod, get<1>(I));
}
if (coeff_count > parms.poly_modulus_degree())
{
throw std::invalid_argument("sub_plain_without_scaling_variant: invalid plaintext");
}

if (destination.poly_modulus_degree() != parms.poly_modulus_degree())
{
throw std::invalid_argument("sub_plain_without_scaling_variant: invalid destination iter");
}

SEAL_ITERATE(iter(destination, coeff_modulus), coeff_modulus_size, [&](auto I) {
const Modulus &cipher_modulus = get<1>(I);
std::transform(
plain.data(), plain.data() + coeff_count, get<0>(I), get<0>(I),
[&](uint64_t m, uint64_t c) -> uint64_t {
m = barrett_reduce_64(m, plain_modulus);
return sub_uint_mod(c, m, cipher_modulus);
});
});
}

Expand Down

0 comments on commit 5b6a321

Please sign in to comment.