-
-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
316fcc0
commit b75cce4
Showing
6 changed files
with
133 additions
and
3 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import numpy as np | ||
|
||
from ...manager import BaseManager | ||
from .torch_wrapper import PaillierTensor | ||
|
||
|
||
def attach_paillier_to_client_for_encrypted_grad(cls, pk, sk): | ||
class PaillierClientWrapper(cls): | ||
def __init__(self, *args, **kwargs): | ||
super(PaillierClientWrapper, self).__init__(*args, **kwargs) | ||
|
||
def upload_gradients(self): | ||
pt_grads = super().upload_gradients() | ||
print("do enc") | ||
return [ | ||
PaillierTensor( | ||
np.vectorize(lambda x: pk.encrypt(x.detach().numpy()))(grad) | ||
) | ||
for grad in pt_grads | ||
] | ||
|
||
def download(self, model_parameters): | ||
decrypted_params = {} | ||
for key, param in model_parameters.items(): | ||
if type(param) == PaillierTensor: | ||
decrypted_params[key] = param.decrypt2float(sk) | ||
else: | ||
decrypted_params[key] = param | ||
return super().download(decrypted_params) | ||
|
||
return PaillierClientWrapper | ||
|
||
|
||
class PaillierGradientClientManager(BaseManager): | ||
def __init__(self, *args, **kwargs): | ||
self.args = args | ||
self.kwargs = kwargs | ||
|
||
def attach(self, cls): | ||
return attach_paillier_to_client_for_encrypted_grad( | ||
cls, *self.args, **self.kwargs | ||
) |
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