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

CKKS square and power functions #100

Merged
merged 13 commits into from
Jul 15, 2020
Merged

CKKS square and power functions #100

merged 13 commits into from
Jul 15, 2020

Conversation

philomath213
Copy link
Member

@philomath213 philomath213 commented Jul 13, 2020

Description

Implement square and power functions for CKKS vector with minimal multiplication depth.
Now it's possible to compute the power of a CKKS vector using .power method or ** operator.
The power function compute recursively the result by splitting the computation into two balanced tree, as a result we will have a result ciphertext with minimal multiplication depth.
There is a special case when the power is zero, in this case a new CKKS vector of ones is returned, while keeping the same size.

Checklist

@philomath213 philomath213 added the Type: New Feature ➕ Introduction of a completely new addition to the codebase label Jul 13, 2020
@philomath213 philomath213 requested review from bcebere and youben11 July 13, 2020 16:19
return *this;
}

CKKSVector CKKSVector::power(int power) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not unsigned int power?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, you are right, unsigned int will remove the overhead of checking the negative values of 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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't be better to cache this->power_inplace(closest_power_of_2 / 2) result? It seems that is called twice for the same vector.

Or this->power_inplace(closest_power_of_2 / 2).square() ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought it will require one more multiplication depth, but it won't.
in this case this->power_inplace(closest_power_of_2 / 2).square_inplace() would be better.

Comment on lines +94 to +108
CKKSVector& CKKSVector::square_inplace() {
this->context->evaluator->square_inplace(this->ciphertext);

if (this->context->auto_relin()) {
this->context->evaluator->relinearize_inplace(
this->ciphertext, *this->context->relin_keys());
}

if (this->context->auto_rescale()) {
this->context->evaluator->rescale_to_next_inplace(this->ciphertext);
this->ciphertext.scale() = this->init_scale;
}

return *this;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not calling mul_inplace which should take care of relin/rescale?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

calling mul_inplace requires coping the ciphertext (i.e. this->mul_inplace(*this)).

Comment on lines 138 to 146
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)
.mul_inplace(this->power(closest_power_of_2 / 2));
} else {
this->power_inplace(power).mul_inplace(this->power(closest_power_of_2));
}
return *this;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can use the compute_polynomial_term from utils, we can refactor the name, but calling it with the power, coeffcient as 1, and the precomputed squares of x should compute the power with the most efficient circuit possible.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

compute_polynomial_term is particular case of power which have extra overhead.
I think keeping power function as it is would be better for both efficiency and readability.

Comment on lines 47 to 57
"plain_vec, power",
[
([], 2),
([0], 3),
([0, 1, -1, 2, -2], 0),
([1, -1, 2, -2], 1),
([1, -1, 2, -2], 2),
([1, -1, 2, -2], 3),
([1, -2, 3, -4], 1),
([1, -2, 3, -4], 2),
([1, -2, 3, -4], 3),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

raising to the power of 4 should be also possible with the default parameters, we need a test-case for this to make sure the circuit is optimum. Same for the other tests

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this should work with power of 4 as well.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

still missing a testcase of 4 here

@philomath213 philomath213 requested a review from youben11 July 13, 2020 18:22
Copy link
Member

@bcebere bcebere left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Comment on lines 90 to 95
.def("square", &CKKSVector::square)
.def("square_", &CKKSVector::square_inplace)
.def("square_inplace", &CKKSVector::square_inplace)
.def("pow", &CKKSVector::power)
.def("pow_", &CKKSVector::power_inplace)
.def("pow_inplace", &CKKSVector::power_inplace)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The python API have have this inplace operations terminated by a _ (e.g. add_) only and we aren't providing function_name_inplace kind of methods.

@philomath213 philomath213 requested a review from youben11 July 14, 2020 05:30
// if the power is zero, return a new encrypted vector of ones
if (power == 0) {
vector<double> ones(this->size(), 1);
*this = CKKSVector(this->context, ones, this->init_scale);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can here just replace the ciphertext (ciphertext = this->encrypt(...)) instead of creating a whole new CKKSVector.

tenseal/tensors/ckksvector.cpp Outdated Show resolved Hide resolved
Comment on lines 47 to 57
"plain_vec, power",
[
([], 2),
([0], 3),
([0, 1, -1, 2, -2], 0),
([1, -1, 2, -2], 1),
([1, -1, 2, -2], 2),
([1, -1, 2, -2], 3),
([1, -2, 3, -4], 1),
([1, -2, 3, -4], 2),
([1, -2, 3, -4], 3),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

still missing a testcase of 4 here

@philomath213 philomath213 requested a review from youben11 July 15, 2020 18:52
@youben11 youben11 merged commit fdf1add into master Jul 15, 2020
@delete-merged-branch delete-merged-branch bot deleted the ckks-square-power branch July 15, 2020 18:54
pierreeliseeflory pushed a commit to pierreeliseeflory/TenSEAL that referenced this pull request Apr 27, 2022
* implement square and power functions for CKKS vector

* python binding for CKKS vector square and power functions

* tests for CKKS vector square and power functions

* replace x.mul_inplace(x) with x.square()

* zero power

* requested changes

* add more test-case with power of 4

* remove _inplace function from python api

* add more test-case with power of 4

* fix power_inplace

Co-authored-by: Ayoub Benaissa <ayouben9@gmail.com>

* more tests to cover all function branches

* more tests

* python linting

Co-authored-by: Ayoub Benaissa <ayouben9@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: New Feature ➕ Introduction of a completely new addition to the codebase
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants