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

Implement Relinearization operation of BFV Scheme #3977

Merged
merged 3 commits into from
Aug 13, 2020

Conversation

rav1kantsingh
Copy link
Member

Description

Closes #3662 #3791
Implementing Relin Key and Relinearization operation for the FV scheme. So that we can decrement the size of ciphertexts if needed. Mainly required after multiplication operation.

Affected Dependencies

None

How has this been tested?

Added tests for the relinearization operation.

Checklist

@rav1kantsingh rav1kantsingh added the Type: New Feature ➕ Introduction of a completely new addition to the codebase label Aug 10, 2020
@rav1kantsingh rav1kantsingh added this to the BFV Tensor in Python milestone Aug 10, 2020
@rav1kantsingh rav1kantsingh requested a review from a team as a code owner August 10, 2020 11:27
@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 Aug 10, 2020

Codecov Report

Merging #3977 into master will decrease coverage by 0.12%.
The diff coverage is 95.79%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #3977      +/-   ##
==========================================
- Coverage   94.88%   94.76%   -0.13%     
==========================================
  Files         202      203       +1     
  Lines       20853    21038     +185     
==========================================
+ Hits        19786    19936     +150     
- Misses       1067     1102      +35     
Impacted Files Coverage Δ
syft/frameworks/torch/he/fv/key_generator.py 90.62% <77.77%> (-1.05%) ⬇️
syft/frameworks/torch/he/fv/evaluator.py 96.11% <94.00%> (+0.60%) ⬆️
syft/frameworks/torch/he/fv/relin_keys.py 100.00% <100.00%> (ø)
test/torch/tensors/test_fv.py 100.00% <100.00%> (ø)
syft/frameworks/torch/mpc/fss.py 84.14% <0.00%> (-7.52%) ⬇️
syft/execution/plan.py 94.33% <0.00%> (-0.57%) ⬇️
...ft/frameworks/torch/tensors/interpreters/native.py 91.27% <0.00%> (-0.22%) ⬇️
...frameworks/torch/tensors/interpreters/precision.py 96.96% <0.00%> (-0.14%) ⬇️
...orks/torch/tensors/interpreters/additive_shared.py 91.94% <0.00%> (-0.14%) ⬇️
test/torch/tensors/test_precision.py 100.00% <0.00%> (ø)
... and 5 more

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.

Amazing to see the BFV scheme now working, looking forward to the tensor type!

temp_poly_ptr, encrypted_ptr[j], key_mod[j], self.poly_modulus
)

return CipherText(ct[:-1], param_id)
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 the current algorithm only apply for length 3 ciphertexts, right? So we can explicitly specify ct[0:2] to make it clear here

Comment on lines +39 to +45
def _get_sk_power_2(self, sk):
sk_power_2 = []
for i in range(len(self._coeff_modulus)):
sk_power_2.append(
poly_mul_mod(sk[i], sk[i], self._coeff_modulus[i], self._poly_modulus)
)
return sk_power_2
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 I've seen this method in quite some places, can we factorize it and provide it in utils maybe?

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, it is used in decrypter and here but I modified it from the other one. it only gives the power of 2 and the decryptors calculate any power given to it. I can get this done later because merging both to the same function will require changing indexes at various positions.😅

Comment on lines 681 to 697
@pytest.mark.parametrize(
"val", [(0), (1), (-1), (100), (1000), (-1000), (-99)],
)
def test_fv_relin(val):
ctx = Context(EncryptionParams(128, CoeffModulus().create(128, [40, 40]), 64))
keygenerator = KeyGenerator(ctx)
keys = keygenerator.keygen()
relin_key = keygenerator.get_relin_keys()
encoder = IntegerEncoder(ctx)
encryptor = Encryptor(ctx, keys[1]) # keys[1] = public_key
decryptor = Decryptor(ctx, keys[0]) # keys[0] = secret_key
evaluator = Evaluator(ctx)

op = encryptor.encrypt(encoder.encode(val))
temp_prod = evaluator.mul(op, op)
relin_prod = evaluator.relin(temp_prod, relin_key)
assert len(temp_prod.data) - 1 == len(relin_prod.data)
Copy link
Member

Choose a reason for hiding this comment

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

Can we add more tests, ones that do multiple mul and relin, and others that check the failure for len 2 and >3 ciphertexts?

Comment on lines +701 to +705
with pytest.raises(Warning):
evaluator.relin(op1, relin_key) # Ciphertext size 2

with pytest.raises(Exception):
evaluator.relin(evaluator.mul(temp_prod, val1), relin_key) # Ciphertext size 4
Copy link
Member

Choose a reason for hiding this comment

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

We doesn't need to run this on every values, I think you can separate this into a different test with a single value

@youben11 youben11 merged commit 00d7080 into OpenMined:master Aug 13, 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 Relinearization keys for FV scheme
2 participants