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

Addition operation on FV scheme #3674

Merged
merged 8 commits into from
Jun 11, 2020
Merged

Conversation

rav1kantsingh
Copy link
Member

@rav1kantsingh rav1kantsingh commented Jun 6, 2020

Description

Closes #3656
Addition operation on FV scheme.

Implemented:

  • Support Ciphertext Ciphertext addition
  • Support Ciphertext Plaintext addition
  • Support Plaintext Plaintext addition

How has this been tested?

Added tests for every private method and simultaneously tested the public method add function.

Checklist

@rav1kantsingh rav1kantsingh added Type: New Feature ➕ Introduction of a completely new addition to the codebase Crypto Team labels Jun 6, 2020
@rav1kantsingh rav1kantsingh added this to the BFV Tensor in Python milestone Jun 6, 2020
@rav1kantsingh rav1kantsingh requested review from youben11 and a team June 6, 2020 11:30
@review-notebook-app
Copy link

Check out this pull request on  ReviewNB

Review Jupyter notebook visual diffs & provide feedback on notebooks.


Powered by ReviewNB

@codecov
Copy link

codecov bot commented Jun 6, 2020

Codecov Report

Merging #3674 into master will increase coverage by 0.01%.
The diff coverage is 98.46%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #3674      +/-   ##
==========================================
+ Coverage   94.79%   94.80%   +0.01%     
==========================================
  Files         184      185       +1     
  Lines       18227    18292      +65     
==========================================
+ Hits        17278    17342      +64     
- Misses        949      950       +1     
Impacted Files Coverage Δ
syft/frameworks/torch/he/fv/evaluator.py 96.96% <96.96%> (ø)
syft/frameworks/torch/he/fv/util/operations.py 95.14% <100.00%> (+0.19%) ⬆️
test/torch/tensors/test_fv.py 100.00% <100.00%> (ø)

@rav1kantsingh rav1kantsingh changed the title [WIP] Add FV Addition operation Addition operation for FV scheme Jun 6, 2020
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.

Good job so far, just some minor issues and some questions.

syft/frameworks/torch/he/fv/evaluator.py Show resolved Hide resolved
syft/frameworks/torch/he/fv/evaluator.py Outdated Show resolved Hide resolved
syft/frameworks/torch/he/fv/evaluator.py Outdated Show resolved Hide resolved
syft/frameworks/torch/he/fv/evaluator.py Outdated Show resolved Hide resolved
syft/frameworks/torch/he/fv/evaluator.py Outdated Show resolved Hide resolved
syft/frameworks/torch/he/fv/evaluator.py Outdated Show resolved Hide resolved
A Plaintext object with value equivalent to result of addition of two provided
arguments.
"""
pt1, pt2 = copy.deepcopy(pt1), copy.deepcopy(pt2)
Copy link
Member

Choose a reason for hiding this comment

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

same question regarding deepcopy

Copy link
Member Author

Choose a reason for hiding this comment

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

In the case of Plaintext, it is not required, I removed it. Thanks!

arguments.
"""
pt1, pt2 = copy.deepcopy(pt1), copy.deepcopy(pt2)
encoder = IntegerEncoder(self.context)
Copy link
Member

Choose a reason for hiding this comment

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

Will IntergerEncoder be the only supported encoder?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, IntegerEncoder converts integers to plaintext and vice-versa and I don't have any other encoder for that.

syft/frameworks/torch/he/fv/evaluator.py Outdated Show resolved Hide resolved
assert (
int1 + int2
== encoder.decode(decryptor.decrypt(evaluator._add_plain_cipher(op1, op2)))
== encoder.decode(decryptor.decrypt(evaluator.add(op1, op2)))
Copy link
Member

Choose a reason for hiding this comment

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

Maybe
== encoder.decode(decryptor.decrypt(evaluator.add(op2, op1)))

too?

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!

ct1, ct2 = copy.deepcopy(ct1.data), copy.deepcopy(ct2.data)
result = ct2 if len(ct2) > len(ct1) else ct1

for i in range(min(len(ct1), len(ct2))):
Copy link
Member

Choose a reason for hiding this comment

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

Q: does this mean that if we have two ciphertext c1=(c1[0], c1[1]) and c2=(c2[0], c2[1], c2[2]), the result would be c=(c1[0] + c2[0], c1[1] + c2[1])?

Copy link
Member Author

Choose a reason for hiding this comment

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

extra c2[2] will be already present in the result by the:
result = ct2 if len(ct2) > len(ct1) else ct1

"""
encoder = IntegerEncoder(self.context)

return encoder.encode(encoder.decode(pt1) + encoder.decode(pt2))
Copy link
Member

Choose a reason for hiding this comment

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

why should we require decoding, add and re-encode, can't we add in the plaintext space?

Copy link
Member Author

Choose a reason for hiding this comment

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

After @bcebere suggestion on this method I tried to do:
return PlainText(poly_add_mod(pt1.data, pt2.data, self.plain_modulus))
but it did not return correct result. Any suggestions

Copy link
Member

@youben11 youben11 left a comment

Choose a reason for hiding this comment

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

LGTM overall, I just have some questions and remarks

@rav1kantsingh rav1kantsingh self-assigned this Jun 9, 2020
@youben11 youben11 merged commit 98db4bb into OpenMined:master Jun 11, 2020
@rav1kantsingh rav1kantsingh changed the title Addition operation for FV scheme Addition operation on FV scheme Aug 25, 2020
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.

Implement Addition operation for FV HE Scheme
3 participants