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

Cpp library improvements #167

Merged
merged 38 commits into from
Nov 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
ab26966
WIP: use shared_ptr for tensor API
bcebere Oct 18, 2020
398c94f
WIP: use shared_ptr for tensors API
bcebere Oct 18, 2020
43ee4ec
WIP: use shared_ptr in tensor API
bcebere Oct 18, 2020
32a49f7
update some tests
bcebere Oct 18, 2020
b6e88a6
update tests
bcebere Oct 19, 2020
51de7b4
add tenseal.h library header
bcebere Oct 20, 2020
ee07c16
initial tensor interface
bcebere Oct 21, 2020
a0047f8
update tests
bcebere Oct 21, 2020
6f5fca2
move common logic to tensor.cpp
bcebere Oct 21, 2020
200ca6b
update cmake file
bcebere Oct 21, 2020
797c873
port bfvvector to encrypted tensor too
bcebere Oct 21, 2020
603f057
move encrypt to cpp
bcebere Oct 21, 2020
364bf9b
update tests
bcebere Oct 22, 2020
4541722
update ctx settings for BFV
bcebere Oct 22, 2020
70515a8
update pack_vectors
bcebere Oct 22, 2020
0b685d2
add protected fields
bcebere Oct 22, 2020
4a6e40a
tensor -> vector
bcebere Oct 22, 2020
ba6dbda
update cpp tests
bcebere Oct 22, 2020
450ec15
templated encrypted vector
bcebere Oct 22, 2020
976ee04
rework Create flow
bcebere Oct 23, 2020
0a4b0fc
cleanup create flow
bcebere Oct 23, 2020
01db3c0
rename files
bcebere Oct 23, 2020
2324bd6
lint
bcebere Oct 23, 2020
48ce2ca
add encrypted tensor interface
bcebere Oct 26, 2020
9e8dbdd
cleanup encrypted_vector interface, every inplace op is implemented i…
bcebere Oct 27, 2020
8d58e85
cleanup interfaces
bcebere Oct 27, 2020
2075aec
API cleanup
bcebere Oct 27, 2020
898a6b1
add plain_tensor
bcebere Oct 28, 2020
1909573
remove unecessary copies
bcebere Oct 28, 2020
9fe5cd7
update some docs
bcebere Oct 28, 2020
c6799bb
docs + cleanup
bcebere Oct 29, 2020
f2a644f
use const ref everywhere
bcebere Oct 30, 2020
3c99570
some cleanup
bcebere Oct 30, 2020
a12c287
Merge branch 'master' of https://github.com/OpenMined/TenSEAL into cp…
bcebere Nov 22, 2020
13a29d9
Merge branch 'master' of https://github.com/OpenMined/TenSEAL into cp…
bcebere Nov 24, 2020
cdc4210
add iterator support for PlainTensor
bcebere Nov 24, 2020
a170040
remove duplicated decrypt code
bcebere Nov 24, 2020
970c3e1
use the new tensor
bcebere Nov 24, 2020
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
4 changes: 2 additions & 2 deletions tenseal/cpp/tensors/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ cc_library(
name = "tenseal_tensors_cc",
srcs = [
"bfvvector.cpp",
"ckksvector.cpp",
"ckkstensor.cpp",
"ckksvector.cpp",
"utils/utils.cpp",
"utils/utils.h",
],
hdrs = [
"api.h",
"bfvvector.h",
"ckksvector.h",
"ckkstensor.h",
"ckksvector.h",
"encrypted_tensor.h",
"encrypted_vector.h",
"plain_tensor.h",
Expand Down
10 changes: 0 additions & 10 deletions tenseal/cpp/tensors/bfvvector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,6 @@ Ciphertext BFVVector::encrypt(shared_ptr<TenSEALContext> context,
return ciphertext;
}

BFVVector::plain_t BFVVector::decrypt() const {
if (this->tenseal_context()->decryptor == NULL) {
// this->context was loaded with public keys only
throw invalid_argument(
"the current context of the vector doesn't hold a secret_key, "
"please provide one as argument");
}

return this->decrypt(this->tenseal_context()->secret_key());
}
BFVVector::plain_t BFVVector::decrypt(const shared_ptr<SecretKey>& sk) const {
Plaintext plaintext;
Decryptor decryptor =
Expand Down
3 changes: 2 additions & 1 deletion tenseal/cpp/tensors/bfvvector.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class BFVVector
public:
using encrypted_t = shared_ptr<BFVVector>;
using plain_t = PlainTensor<int64_t>;
using EncryptedVector<int64_t, shared_ptr<BFVVector>,
BatchEncoder>::decrypt;

template <typename... Args>
static encrypted_t Create(Args&&... args) {
Expand All @@ -31,7 +33,6 @@ class BFVVector
* Decrypts and returns the plaintext representation of the encrypted vector
*of integers using the secret-key.
**/
plain_t decrypt() const override;
plain_t decrypt(const shared_ptr<SecretKey>& sk) const override;
/**
* Compute the power of the BFVVector with minimal multiplication depth.
Expand Down
18 changes: 3 additions & 15 deletions tenseal/cpp/tensors/ckkstensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@ CKKSTensor::CKKSTensor(const shared_ptr<TenSEALContext>& ctx,
this->_init_scale = ctx->global_scale();
}

vector<double> plain_data = tensor.data();
for (int i = 0; i < plain_data.size(); i++)
_data.push_back(CKKSTensor::encrypt(ctx, this->_init_scale,
vector<double>({plain_data[i]})));
for (auto it = tensor.cbegin(); it != tensor.cend(); it++)
_data.push_back(
CKKSTensor::encrypt(ctx, this->_init_scale, vector<double>({*it})));
}

Ciphertext CKKSTensor::encrypt(const shared_ptr<TenSEALContext>& ctx,
Expand All @@ -42,17 +41,6 @@ Ciphertext CKKSTensor::encrypt(const shared_ptr<TenSEALContext>& ctx,
return ciphertext;
}

PlainTensor<double> CKKSTensor::decrypt() const {
if (this->tenseal_context()->decryptor == NULL) {
// this->context was loaded with public keys only
throw invalid_argument(
"the current context of the tensor doesn't hold a secret_key, "
"please provide one as argument");
}

return this->decrypt(this->tenseal_context()->secret_key());
}

PlainTensor<double> CKKSTensor::decrypt(const shared_ptr<SecretKey>& sk) const {
Plaintext plaintext;
Decryptor decryptor =
Expand Down
2 changes: 1 addition & 1 deletion tenseal/cpp/tensors/ckkstensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ using namespace std;
class CKKSTensor : public EncryptedTensor<double, shared_ptr<CKKSTensor>>,
public enable_shared_from_this<CKKSTensor> {
public:
using EncryptedTensor<double, shared_ptr<CKKSTensor>>::decrypt;
/**
* Create a new CKKSTensor from an 1D vector.
* @param[in] input vector.
Expand All @@ -24,7 +25,6 @@ class CKKSTensor : public EncryptedTensor<double, shared_ptr<CKKSTensor>>,
new CKKSTensor(std::forward<Args>(args)...));
}

PlainTensor<double> decrypt() const override;
PlainTensor<double> decrypt(const shared_ptr<SecretKey>& sk) const override;

shared_ptr<CKKSTensor> negate_inplace() override;
Expand Down
9 changes: 0 additions & 9 deletions tenseal/cpp/tensors/ckksvector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,7 @@ Ciphertext CKKSVector::encrypt(shared_ptr<TenSEALContext> context, double scale,

return ciphertext;
}
CKKSVector::plain_t CKKSVector::decrypt() const {
if (this->tenseal_context()->decryptor == NULL) {
// this->context was loaded with public keys only
throw invalid_argument(
"the current context of the vector doesn't hold a secret_key, "
"please provide one as argument");
}

return this->decrypt(this->tenseal_context()->secret_key());
}
CKKSVector::plain_t CKKSVector::decrypt(const shared_ptr<SecretKey>& sk) const {
Plaintext plaintext;
Decryptor decryptor =
Expand Down
2 changes: 1 addition & 1 deletion tenseal/cpp/tensors/ckksvector.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class CKKSVector
public:
using encrypted_t = shared_ptr<CKKSVector>;
using plain_t = PlainTensor<double>;
using EncryptedVector<double, shared_ptr<CKKSVector>, CKKSEncoder>::decrypt;

template <typename... Args>
static encrypted_t Create(Args&&... args) {
Expand All @@ -28,7 +29,6 @@ class CKKSVector
* Decrypts and returns the plaintext representation of the encrypted vector
*of real numbers using the secret-key.
**/
plain_t decrypt() const override;
plain_t decrypt(const shared_ptr<SecretKey>& sk) const override;

/**
Expand Down
10 changes: 9 additions & 1 deletion tenseal/cpp/tensors/encrypted_tensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,15 @@ class EncryptedTensor {
* Decrypts and returns the plaintext representation of the encrypted tensor
*of real numbers using the secret-key.
**/
virtual PlainTensor<plain_data_t> decrypt() const = 0;
PlainTensor<plain_data_t> decrypt() const {
if (this->tenseal_context()->decryptor == nullptr) {
// this->context was loaded with public keys only
throw invalid_argument(
"the current context of the tensor doesn't hold a secret_key, "
"please provide one as argument");
}
return this->decrypt(this->tenseal_context()->secret_key());
};
virtual PlainTensor<plain_data_t> decrypt(
const shared_ptr<SecretKey>& sk) const = 0;

Expand Down
4 changes: 2 additions & 2 deletions tenseal/cpp/tensors/encrypted_vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ using namespace std;
*
* The integrator must override the following inherited methods, along with the
*EncryptedVector pure methods:
* * vector<plain_t> EncryptedTensor::decrypt() const = 0;
* * vector<plain_t> EncryptedTensor::decrypt() = 0;
* * vector<plain_t> EncryptedTensor::decrypt(const shared_ptr<SecretKey>&)
*const = 0;
* * encrypted_t negate_inplace();
* * encrypted_t square_inplace();
* * encrypted_t add_inplace(encrypted_t to_add);
Expand Down
9 changes: 9 additions & 0 deletions tenseal/cpp/tensors/plain_tensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ template <typename plain_t>
class PlainTensor {
public:
using dtype = plain_t;
using iterator = typename vector<plain_t>::iterator;
using const_iterator = typename vector<plain_t>::const_iterator;
/**
* Create a new PlainTensor from an 1D vector.
* @param[in] input vector.
Expand Down Expand Up @@ -188,6 +190,13 @@ class PlainTensor {
* Casts the tensor to an 1D vector.
*/
operator vector<plain_t>() const { return _data; }
/**
* Iterator utils
**/
inline iterator begin() noexcept { return _data.begin(); }
inline const_iterator cbegin() const noexcept { return _data.cbegin(); }
inline iterator end() noexcept { return _data.end(); }
inline const_iterator cend() const noexcept { return _data.cend(); }
/**
* Replicates the internal representation for <times> elements.
*/
Expand Down