-
-
Notifications
You must be signed in to change notification settings - Fork 157
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
Conversation
tenseal/tensors/ckksvector.cpp
Outdated
return *this; | ||
} | ||
|
||
CKKSVector CKKSVector::power(int power) { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
tenseal/tensors/ckksvector.cpp
Outdated
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) |
There was a problem hiding this comment.
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() ?
There was a problem hiding this comment.
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.
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; | ||
} |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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)
).
tenseal/tensors/ckksvector.cpp
Outdated
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; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
tests/tensors/test_ckks_vector.py
Outdated
"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), |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
tenseal/binding.cpp
Outdated
.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) |
There was a problem hiding this comment.
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.
// 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); |
There was a problem hiding this comment.
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.
tests/tensors/test_ckks_vector.py
Outdated
"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), |
There was a problem hiding this comment.
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
Co-authored-by: Ayoub Benaissa <ayouben9@gmail.com>
* 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>
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