Skip to content

Commit

Permalink
Second stage pir (#57)
Browse files Browse the repository at this point in the history
* manager: fix location of commas in test funcs

* evaluator: add ptx decomposition mult

* evaluator:add test for ptx embedding

* evaluator:add ntt transform for EmbeddedCiphertext

* server.cpp: minor change for debug mode
  • Loading branch information
elkanatovey authored Dec 11, 2022
1 parent 2f516d0 commit b75a7d2
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 15 deletions.
47 changes: 38 additions & 9 deletions src/math_utils/evaluator_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,23 +235,31 @@ namespace math_utils {
}
}

void EvaluatorWrapper::get_ptx_embedding(const seal::Ciphertext &ctx, const seal::RelinKeys& relin_keys, std::vector<seal::Plaintext> &ptx_decomposition) const {
seal::Ciphertext ctx_copy;
evaluator->relinearize(ctx, relin_keys, ctx_copy);
evaluator->mod_switch_to_inplace(ctx_copy, context.last_parms_id());
void EvaluatorWrapper::get_ptx_embedding(const seal::Ciphertext &ctx, EmbeddedCiphertext &ptx_decomposition) const {

ptx_decomposition = std::move(decompose_to_plaintexts(context.last_context_data()->parms(), ctx_copy));
ptx_decomposition = std::move(decompose_to_plaintexts(context.last_context_data()->parms(), ctx));

for(auto & ptx : ptx_decomposition){
evaluator->transform_to_ntt_inplace(ptx, context.first_parms_id());
}

}

void EvaluatorWrapper::compose_to_ctx(const std::vector<seal::Plaintext> &ptx_decomposition, seal::Ciphertext &decoded) const {
seal::Ciphertext ctx_copy(context, context.last_parms_id());
compose_to_ciphertext( context.last_context_data()->parms(), ptx_decomposition, ctx_copy);
decoded = ctx_copy;
decoded = std::move(ctx_copy);
}


void EvaluatorWrapper::mult_with_ptx_decomposition(const EmbeddedCiphertext &ptx_decomposition, const seal::Ciphertext &ctx, EncryptedEmbeddedCiphertext &result) const {
#ifdef DISTRIBICOM_DEBUG
assert(ptx_decomposition[0].is_ntt_form());
assert(ctx.is_ntt_form());
#endif

std::vector<seal::Ciphertext> result_copy(ptx_decomposition.size());
for(auto i=0; i<ptx_decomposition.size(); i++){
evaluator->multiply_plain(ctx, ptx_decomposition[i], result_copy[i]);
}
result = std::move(result_copy);
}

void EvaluatorWrapper::scalar_multiply(std::uint64_t scalar, const seal::Ciphertext &right,
Expand All @@ -268,4 +276,25 @@ namespace math_utils {
}


void EvaluatorWrapper::transform_to_ntt_inplace(EmbeddedCiphertext &encoded) const {
for(auto & ptx_piece : encoded) {
evaluator->transform_to_ntt_inplace(ptx_piece, context.first_parms_id());
}
}


void EvaluatorWrapper::add_embedded_ctxs(const EncryptedEmbeddedCiphertext &ctx_decomposition1, const EncryptedEmbeddedCiphertext &ctx_decomposition2, EncryptedEmbeddedCiphertext &result) const {
#ifdef DISTRIBICOM_DEBUG
assert(ctx_decomposition1[0].is_ntt_form());
assert(ctx_decomposition2[0].is_ntt_form());
assert(ctx_decomposition1.size()==ctx_decomposition2.size());
#endif

EncryptedEmbeddedCiphertext result_copy(ctx_decomposition1.size());
for(auto i=0; i<ctx_decomposition1.size(); i++){
evaluator->add(ctx_decomposition1[i], ctx_decomposition2[i], result_copy[i]);
}
result = std::move(result_copy);
}

}
22 changes: 20 additions & 2 deletions src/math_utils/evaluator_wrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ namespace math_utils {
*/
typedef std::vector<seal::Plaintext> SplitPlaintextNTTForm;


/***
* EmbeddedCiphertext represents a ciphertext that was embedded into plaintexts
*/
typedef std::vector<seal::Plaintext> EmbeddedCiphertext;

typedef std::vector<seal::Ciphertext> EncryptedEmbeddedCiphertext;

/***
* This class wraps and modifies the behaviour of seal::Evaluator to add wanted multiplication for distribicom.
*/
Expand Down Expand Up @@ -103,10 +111,20 @@ namespace math_utils {
void trivial_ciphertext(const seal::Plaintext &ptx, seal::Ciphertext &result) const;

/***
* writes ptx embedding of ctx to ptx_decomposition after relinearizing ctx and putting switching to last modulus
* writes ptx embedding of ctx to ptx_decompositio
*/
void get_ptx_embedding(const seal::Ciphertext &ctx, const seal::RelinKeys& relin_keys, std::vector<seal::Plaintext> &ptx_decomposition) const;
void get_ptx_embedding(const seal::Ciphertext &ctx, EmbeddedCiphertext &ptx_decomposition) const;

void compose_to_ctx(const std::vector<seal::Plaintext> &ptx_decomposition, seal::Ciphertext &decoded) const;

void
mult_with_ptx_decomposition(const EmbeddedCiphertext &ptx_decomposition, const seal::Ciphertext &ctx,
std::vector<seal::Ciphertext> &result) const;

void add_embedded_ctxs(const EncryptedEmbeddedCiphertext &ctx_decomposition1,
const EncryptedEmbeddedCiphertext &ctx_decomposition2,
EncryptedEmbeddedCiphertext &result) const;

void transform_to_ntt_inplace(EmbeddedCiphertext &encoded) const;
};
}
7 changes: 7 additions & 0 deletions src/math_utils/matrix_operations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,13 @@ namespace math_utils {
}
}

void MatrixOperations::to_ntt(std::vector<seal::Plaintext> &m) const {
std::for_each(std::execution::par_unseq, m.begin(), m.end(), [this](seal::Plaintext &ptx) {
w_evaluator->evaluator->transform_to_ntt_inplace(ptx, this->w_evaluator->context.first_parms_id());
});
}


void MatrixOperations::to_ntt(std::vector<seal::Ciphertext> &m) const {
std::for_each(std::execution::par_unseq, m.begin(), m.end(), [this](seal::Ciphertext &ctx) {
w_evaluator->evaluator->transform_to_ntt_inplace(ctx);
Expand Down
1 change: 1 addition & 0 deletions src/math_utils/matrix_operations.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ namespace math_utils {
const matrix<seal::Ciphertext> &b,
matrix<seal::Ciphertext> &result) const;

void to_ntt(std::vector<seal::Plaintext> &m) const;
};

}
Expand Down
4 changes: 2 additions & 2 deletions src/services/manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ namespace services {
std::shared_ptr<WorkDistributionLedger>
Manager::distribute_work(const math_utils::matrix<seal::Plaintext> &db,
const ClientDB &all_clients,
int rnd, int epoch,
int rnd, int epoch
#ifdef DISTRIBICOM_DEBUG
const seal::GaloisKeys &expansion_key
,const seal::GaloisKeys &expansion_key
#endif
) {

Expand Down
4 changes: 2 additions & 2 deletions src/services/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,9 @@ std::shared_ptr<services::WorkDistributionLedger> services::FullServer::distribu

// // todo: set specific round and handle.

ledger = manager.distribute_work(db_handle.mat, client_query_manager, 1, 1,
ledger = manager.distribute_work(db_handle.mat, client_query_manager, 1, 1
#ifdef DISTRIBICOM_DEBUG
client_query_manager.id_to_info.begin()->second->galois_keys
,client_query_manager.id_to_info.begin()->second->galois_keys
#endif
);
}
Expand Down
26 changes: 26 additions & 0 deletions test/math_utils/evaluator_wrapper_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@ void order_of_ops_test1();

void order_of_ops_test2(std::shared_ptr<TestUtils::CryptoObjects> all);

void ctx_composition_test();

int evaluator_wrapper_test(int, char *[]) {

ctx_composition_test();

mult_slow_vs_modified_test();

// commutative test.
Expand Down Expand Up @@ -211,4 +215,26 @@ void benchmark_mult() {
std::cout << "comparing mult_slow to both mult and mult_plain: " << std::endl;
std::cout << "mult_slow / mult: " << double(mult_slow_time) / double(mult_time) << std::endl;
std::cout << "mult_slow / mult_plain: " << double(mult_slow_time) / double(mult_plain_time) << std::endl;
}

void ctx_composition_test() {
auto all = TestUtils::setup(TestUtils::DEFAULT_SETUP_CONFIGS);

auto ptx = all->random_plaintext();
seal::Ciphertext enc_ptx;


all->encryptor.encrypt_symmetric(ptx, enc_ptx);

math_utils::EmbeddedCiphertext embedded_enc_ptx;

all->w_evaluator->get_ptx_embedding(enc_ptx, embedded_enc_ptx);

seal::Ciphertext unembedded_enc_ptx;
all->w_evaluator->compose_to_ctx(embedded_enc_ptx, unembedded_enc_ptx);

seal::Plaintext decrypted_unembedded_enc_ptx;
all->decryptor.decrypt(unembedded_enc_ptx, decrypted_unembedded_enc_ptx);

assert(decrypted_unembedded_enc_ptx.to_string() == ptx.to_string());
}

0 comments on commit b75a7d2

Please sign in to comment.