Skip to content

Commit

Permalink
add operation guard
Browse files Browse the repository at this point in the history
  • Loading branch information
bcebere committed Dec 21, 2020
1 parent ca564f3 commit b2533dc
Show file tree
Hide file tree
Showing 8 changed files with 353 additions and 229 deletions.
58 changes: 31 additions & 27 deletions tenseal/cpp/tensors/bfvvector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ BFVVector::plain_t BFVVector::decrypt(const shared_ptr<SecretKey>& sk) const {
return vector<int64_t>(result.cbegin(), result.cbegin() + this->size());
}

shared_ptr<BFVVector> BFVVector::power_inplace(unsigned int power) {
shared_ptr<BFVVector> BFVVector::power_inplace_impl(unsigned int power) {
// if the power is zero, return a new encrypted vector of ones
if (power == 0) {
vector<int64_t> ones(this->size(), 1);
Expand All @@ -97,29 +97,29 @@ shared_ptr<BFVVector> BFVVector::power_inplace(unsigned int power) {
int closest_power_of_2 = 1 << static_cast<int>(floor(log2(power)));
power -= closest_power_of_2;
if (power == 0) {
this->power_inplace(closest_power_of_2 / 2)->square_inplace();
this->power_inplace_impl(closest_power_of_2 / 2)->square_inplace_impl();
} else {
auto closest_pow2_vector = this->power(closest_power_of_2);
this->power_inplace(power)->mul_inplace(closest_pow2_vector);
this->power_inplace_impl(power)->mul_inplace_impl(closest_pow2_vector);
}

return shared_from_this();
}

shared_ptr<BFVVector> BFVVector::negate_inplace() {
shared_ptr<BFVVector> BFVVector::negate_inplace_impl() {
this->tenseal_context()->evaluator->negate_inplace(this->_ciphertext);

return shared_from_this();
}

shared_ptr<BFVVector> BFVVector::square_inplace() {
shared_ptr<BFVVector> BFVVector::square_inplace_impl() {
this->tenseal_context()->evaluator->square_inplace(this->_ciphertext);
this->auto_relin(_ciphertext);

return shared_from_this();
}

shared_ptr<BFVVector> BFVVector::add_inplace(
shared_ptr<BFVVector> BFVVector::add_inplace_impl(
const shared_ptr<BFVVector>& other) {
auto to_add = other->copy();
if (!this->tenseal_context()->equals(to_add->tenseal_context())) {
Expand All @@ -136,7 +136,7 @@ shared_ptr<BFVVector> BFVVector::add_inplace(
return shared_from_this();
}

shared_ptr<BFVVector> BFVVector::sub_inplace(
shared_ptr<BFVVector> BFVVector::sub_inplace_impl(
const shared_ptr<BFVVector>& other) {
auto to_sub = other->copy();
if (!this->tenseal_context()->equals(to_sub->tenseal_context())) {
Expand All @@ -153,7 +153,7 @@ shared_ptr<BFVVector> BFVVector::sub_inplace(
return shared_from_this();
}

shared_ptr<BFVVector> BFVVector::mul_inplace(
shared_ptr<BFVVector> BFVVector::mul_inplace_impl(
const shared_ptr<BFVVector>& other) {
auto to_mul = other->copy();
if (!this->tenseal_context()->equals(to_mul->tenseal_context())) {
Expand All @@ -171,34 +171,34 @@ shared_ptr<BFVVector> BFVVector::mul_inplace(
return shared_from_this();
}

shared_ptr<BFVVector> BFVVector::dot_product_inplace(
shared_ptr<BFVVector> BFVVector::dot_product_inplace_impl(
const shared_ptr<BFVVector>& to_mul) {
this->mul_inplace(to_mul);
this->sum_inplace();
this->mul_inplace_impl(to_mul);
this->sum_inplace_impl();

return shared_from_this();
}

shared_ptr<BFVVector> BFVVector::dot_product_plain_inplace(
shared_ptr<BFVVector> BFVVector::dot_product_plain_inplace_impl(
const BFVVector::plain_t& to_mul) {
this->mul_plain_inplace(to_mul);
this->sum_inplace();
this->mul_plain_inplace_impl(to_mul);
this->sum_inplace_impl();

return shared_from_this();
}

shared_ptr<BFVVector> BFVVector::sum_inplace(size_t /*axis=0*/) {
shared_ptr<BFVVector> BFVVector::sum_inplace_impl(size_t /*axis=0*/) {
sum_vector(this->tenseal_context(), this->_ciphertext, this->size());
this->_size = 1;
return shared_from_this();
}

shared_ptr<BFVVector> BFVVector::add_plain_inplace(
shared_ptr<BFVVector> BFVVector::add_plain_inplace_impl(
const plain_t::dtype& to_add) {
throw std::logic_error("not implemented");
}

shared_ptr<BFVVector> BFVVector::add_plain_inplace(
shared_ptr<BFVVector> BFVVector::add_plain_inplace_impl(
const BFVVector::plain_t& to_add) {
if (this->size() != to_add.size()) {
throw invalid_argument("can't add vectors of different sizes");
Expand All @@ -213,12 +213,12 @@ shared_ptr<BFVVector> BFVVector::add_plain_inplace(
return shared_from_this();
}

shared_ptr<BFVVector> BFVVector::sub_plain_inplace(
shared_ptr<BFVVector> BFVVector::sub_plain_inplace_impl(
const plain_t::dtype& to_sub) {
throw std::logic_error("not implemented");
}

shared_ptr<BFVVector> BFVVector::sub_plain_inplace(
shared_ptr<BFVVector> BFVVector::sub_plain_inplace_impl(
const BFVVector::plain_t& to_sub) {
if (this->size() != to_sub.size()) {
throw invalid_argument("can't sub vectors of different sizes");
Expand All @@ -233,12 +233,12 @@ shared_ptr<BFVVector> BFVVector::sub_plain_inplace(
return shared_from_this();
}

shared_ptr<BFVVector> BFVVector::mul_plain_inplace(
shared_ptr<BFVVector> BFVVector::mul_plain_inplace_impl(
const plain_t::dtype& to_sub) {
throw std::logic_error("not implemented");
}

shared_ptr<BFVVector> BFVVector::mul_plain_inplace(
shared_ptr<BFVVector> BFVVector::mul_plain_inplace_impl(
const BFVVector::plain_t& to_mul) {
if (this->size() != to_mul.size()) {
throw invalid_argument("can't multiply vectors of different sizes");
Expand All @@ -262,31 +262,31 @@ shared_ptr<BFVVector> BFVVector::mul_plain_inplace(
return shared_from_this();
}

shared_ptr<BFVVector> BFVVector::matmul_plain_inplace(
shared_ptr<BFVVector> BFVVector::matmul_plain_inplace_impl(
const BFVVector::plain_t& matrix, size_t n_jobs) {
throw std::logic_error("not implemented");
}

shared_ptr<BFVVector> BFVVector::polyval_inplace(
shared_ptr<BFVVector> BFVVector::polyval_inplace_impl(
const vector<double>& coefficients) {
throw std::logic_error("not implemented");
}

shared_ptr<BFVVector> BFVVector::conv2d_im2col_inplace(
shared_ptr<BFVVector> BFVVector::conv2d_im2col_inplace_impl(
const BFVVector::plain_t& kernel, const size_t windows_nb) {
throw std::logic_error("not implemented");
}

shared_ptr<BFVVector> BFVVector::enc_matmul_plain_inplace(
shared_ptr<BFVVector> BFVVector::enc_matmul_plain_inplace_impl(
const BFVVector::plain_t& plain_vec, const size_t rows_nb) {
throw std::logic_error("not implemented");
}

shared_ptr<BFVVector> BFVVector::replicate_first_slot_inplace(size_t n) {
shared_ptr<BFVVector> BFVVector::replicate_first_slot_inplace_impl(size_t n) {
// mask
vector<int64_t> mask(this->_size, 0);
mask[0] = 1;
this->mul_plain_inplace(mask);
this->mul_plain_inplace_impl(mask);

// replicate
Ciphertext tmp = this->_ciphertext;
Expand Down Expand Up @@ -353,4 +353,8 @@ shared_ptr<BFVVector> BFVVector::deepcopy() const {
BFVVectorProto vec = this->save_proto();
return BFVVector::Create(ctx, vec);
}

bool BFVVector::_check_operation_sanity(){
return true;
}
} // namespace tenseal
75 changes: 40 additions & 35 deletions tenseal/cpp/tensors/bfvvector.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,93 +28,98 @@ class BFVVector
return shared_ptr<BFVVector>(
new BFVVector(std::forward<Args>(args)...));
}

/**
* Decrypts and returns the plaintext representation of the encrypted vector
*of integers using the secret-key.
**/
plain_t decrypt(const shared_ptr<SecretKey>& sk) const override;
/**
* Load/Save the vector from/to a serialized protobuffer.
**/
void load(const string& vec) override;
string save() const override;
/**
*Recreates a new BFVVector from the current one, without any
*pointer/reference to this one.
* **/
encrypted_t copy() const override;
encrypted_t deepcopy() const override;
double scale() const override { throw logic_error("not implemented"); }

protected:
/**
* Compute the power of the BFVVector with minimal multiplication depth.
**/
encrypted_t power_inplace(unsigned int power) override;
encrypted_t power_inplace_impl(unsigned int power) override;
/**
* Negates a BFVVector.
**/
encrypted_t negate_inplace() override;
encrypted_t negate_inplace_impl() override;
/**
* Compute the square of the BFVVector.
**/
encrypted_t square_inplace() override;
encrypted_t square_inplace_impl() override;
/**
* Encrypted evaluation function operates on two encrypted vectors and
* returns a new BFVVector which is the result of either
*addition, substraction or multiplication in an element-wise fashion.
*in_place functions return a reference to the same object.
**/
encrypted_t add_inplace(const encrypted_t& to_add) override;
encrypted_t sub_inplace(const encrypted_t& to_sub) override;
encrypted_t mul_inplace(const encrypted_t& to_mul) override;
encrypted_t dot_product_inplace(const encrypted_t& to_mul) override;
encrypted_t dot_product_plain_inplace(const plain_t& to_mul) override;
encrypted_t sum_inplace(size_t axis = 0) override;
encrypted_t add_inplace_impl(const encrypted_t& to_add) override;
encrypted_t sub_inplace_impl(const encrypted_t& to_sub) override;
encrypted_t mul_inplace_impl(const encrypted_t& to_mul) override;
encrypted_t dot_product_inplace_impl(const encrypted_t& to_mul) override;
encrypted_t dot_product_plain_inplace_impl(const plain_t& to_mul) override;
encrypted_t sum_inplace_impl(size_t axis = 0) override;

/**
* Plain evaluation function operates on an encrypted vector and plaintext
* vector of integers and returns a new BFVVector which is the result of
* either addition, substraction or multiplication in an element-wise
*fashion. in_place functions return a reference to the same object.
**/
encrypted_t add_plain_inplace(const plain_t::dtype& to_add) override;
encrypted_t add_plain_inplace(const plain_t& to_add) override;
encrypted_t sub_plain_inplace(const plain_t::dtype& to_sub) override;
encrypted_t sub_plain_inplace(const plain_t& to_sub) override;
encrypted_t mul_plain_inplace(const plain_t::dtype& to_mul) override;
encrypted_t mul_plain_inplace(const plain_t& to_mul) override;
encrypted_t add_plain_inplace_impl(const plain_t::dtype& to_add) override;
encrypted_t add_plain_inplace_impl(const plain_t& to_add) override;
encrypted_t sub_plain_inplace_impl(const plain_t::dtype& to_sub) override;
encrypted_t sub_plain_inplace_impl(const plain_t& to_sub) override;
encrypted_t mul_plain_inplace_impl(const plain_t::dtype& to_mul) override;
encrypted_t mul_plain_inplace_impl(const plain_t& to_mul) override;
/**
* Encrypted Vector multiplication with plain matrix.
**/
encrypted_t matmul_plain_inplace(const plain_t& matrix,
size_t n_jobs = 0) override;
encrypted_t matmul_plain_inplace_impl(const plain_t& matrix,
size_t n_jobs = 0) override;

/**
* Encrypted Matrix multiplication with plain vector.
**/
encrypted_t enc_matmul_plain_inplace(const plain_t& plain_vec,
size_t row_size) override;
encrypted_t enc_matmul_plain_inplace_impl(const plain_t& plain_vec,
size_t row_size) override;

/**
* Polynomial evaluation with `this` as variable.
* p(x) = coefficients[0] + coefficients[1] * x + ... + coefficients[i] *
*x^i
**/
encrypted_t polyval_inplace(const vector<double>& coefficients) override;
encrypted_t polyval_inplace_impl(
const vector<double>& coefficients) override;

/*
* Image Block to Columns.
* The input matrix should be encoded in a vertical scan (column-major).
* The kernel vector should be padded with zeros to the next power of 2
*/
encrypted_t conv2d_im2col_inplace(const plain_t& kernel,
const size_t windows_nb) override;
encrypted_t conv2d_im2col_inplace_impl(const plain_t& kernel,
const size_t windows_nb) override;
/**
* Replicate the first slot of a ciphertext n times. Requires a
*multiplication.
**/
encrypted_t replicate_first_slot_inplace(size_t n) override;
/**
* Load/Save the vector from/to a serialized protobuffer.
**/
void load(const string& vec) override;
string save() const override;
encrypted_t replicate_first_slot_inplace_impl(size_t n) override;
/**
*Recreates a new BFVVector from the current one, without any
*pointer/reference to this one.
* Check tensor sanity
* **/
encrypted_t copy() const override;
encrypted_t deepcopy() const override;

double scale() const override { throw logic_error("not implemented"); }
bool _check_operation_sanity() override;

private:
BFVVector(const shared_ptr<TenSEALContext>& ctx, const plain_t& vec);
Expand Down
Loading

0 comments on commit b2533dc

Please sign in to comment.