-
-
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
CKKSVector Polynomial Evaluation #99
Merged
Merged
Changes from 24 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
b45ac2b
make it work with tests
youben11 836dc2a
Merge branch 'master' into ckks-polyval
youben11 ffc75cd
log(n) mul depth for polynomial of degree n
youben11 3167218
fix some tests
youben11 b86f7b1
rename tests
youben11 1d0d4f9
some fixes and new tests
youben11 6360f20
case where poly is of degree < 2
youben11 f2a035d
more tests
youben11 c3ea803
rescale the encrypt_zero ciphertext
philomath213 65313f9
null polynomial tests
philomath213 43bf886
Merge branch 'ckks-polyval' of github.com:OpenMined/TenSEAL into ckks…
youben11 1240096
remove debug
youben11 943647c
fix _mul_plain_inplace, add some tests
youben11 d9d6084
optimize circuit considering coeffecient
youben11 ddb34a3
balancing multiplication circuit
philomath213 78ce95a
more tests, docs and TODO
youben11 c77957f
more tests
youben11 0250390
fix bugs, refactor and simplify algorithm
youben11 530ae62
simplify accumulator creation
youben11 e8be408
move compute_polynomial_term to utils
youben11 ad24085
remove inclue <map>
youben11 2ebc1cd
lint
youben11 8161d22
requested changes
youben11 d97e953
cast
youben11 8cb55ac
fall back to reserve
youben11 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -316,8 +316,11 @@ CKKSVector& CKKSVector::_mul_plain_inplace(const T& to_mul) { | |
this->context->evaluator->multiply_plain_inplace(this->ciphertext, | ||
plaintext); | ||
} catch (const std::logic_error& e) { // result ciphertext is transparent | ||
// TODO: chech if error e is exactly a "ciphertext is transparent" error | ||
// replace by encryption of zero | ||
this->context->encryptor->encrypt_zero(this->ciphertext); | ||
this->ciphertext.scale() = this->init_scale; | ||
return *this; | ||
} | ||
|
||
if (this->context->auto_rescale()) { | ||
|
@@ -412,4 +415,58 @@ CKKSVector& CKKSVector::replicate_first_slot_inplace(size_t n) { | |
return *this; | ||
} | ||
|
||
CKKSVector CKKSVector::polyval(const vector<double>& coefficients) { | ||
CKKSVector new_vector = *this; | ||
return new_vector.polyval_inplace(coefficients); | ||
} | ||
|
||
CKKSVector& CKKSVector::polyval_inplace(const vector<double>& coefficients) { | ||
if (coefficients.size() == 0) { | ||
throw invalid_argument( | ||
"the coefficients vector need to have at least one element"); | ||
} | ||
|
||
int degree = static_cast<int>(coefficients.size()) - 1; | ||
while (degree >= 0) { | ||
if (coefficients[degree] == 0.0) | ||
degree--; | ||
else | ||
break; | ||
} | ||
|
||
// null polynomial: output should be an encrypted 0 | ||
// we can multiply by 0, or return the encryption of zero | ||
if (degree == -1) { | ||
// we set the vector to the encryption of zero | ||
this->context->encryptor->encrypt_zero(this->ciphertext); | ||
this->ciphertext.scale() = this->init_scale; | ||
return *this; | ||
} | ||
|
||
// set result accumulator to the constant coefficient | ||
vector<double> cst_coeff(this->size(), coefficients[0]); | ||
CKKSVector result(this->context, cst_coeff, this->init_scale); | ||
|
||
// pre-compute squares of x | ||
CKKSVector x = *this; | ||
int max_square = static_cast<int>(floor(log2(degree))); | ||
vector<CKKSVector> x_squares(max_square + 1); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the build fails here now, because it tries to call CKKSVector with the default constructor.
doesn't work, ignore this, my bad. |
||
x_squares[0] = x; // x | ||
for (int i = 1; i <= max_square; i++) { | ||
// TODO: use square | ||
x.mul_inplace(x); | ||
x_squares[i] = x; // x^(2^(i+1)) | ||
} | ||
|
||
// coefficients[1] * x + ... + coefficients[degree] * x^(degree) | ||
for (int i = 1; i <= degree; i++) { | ||
if (coefficients[i] == 0.0) continue; | ||
x = compute_polynomial_term(i, coefficients[i], x_squares); | ||
result.add_inplace(x); | ||
} | ||
|
||
this->ciphertext = result.ciphertext; | ||
return *this; | ||
} | ||
|
||
} // namespace tenseal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
this creates a vector of size this->size() with all the values set to coefficients[0]. is that the expected behavior?
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.
yep, to encrypt it later and have operations done element-wise