Created
July 9, 2020 07:50
-
-
Save mydreambei-ai/07a8573b912899015f9b4a410f5a69f3 to your computer and use it in GitHub Desktop.
private set intersection
This file contains hidden or 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
''' | |
https://github.com/OpenMined/PyPSI | |
''' | |
import gmpy2 | |
import secrets | |
import hashlib | |
from Crypto.PublicKey import RSA | |
class PSI(object): | |
def __init__(self, rsa_size=1024): | |
self.private_key = RSA.generate(rsa_size) | |
self.public_key = RSA.construct((self.private_key.n, self.private_key.e)) | |
self.r = secrets.randbelow(self.public_key.n) | |
def YA(self, x): | |
return x * gmpy2.powmod(self.r, self.public_key.e, self.public_key.n) % self.public_key.n | |
def ZA(self, ya): | |
return gmpy2.powmod(ya, self.private_key.d, self.private_key.n) | |
def DA(self, za): | |
return za * gmpy2.invert(self.r, self.public_key.n) % self.public_key.n | |
def hash(self, x): | |
if isinstance(x, int): | |
return x | |
s = hashlib.sha1(x).hexdigest() | |
return int(s, 16) | |
def ZB(self, x): | |
return gmpy2.powmod(x, self.private_key.d, self.private_key.n) | |
def YA_set(self, x): | |
return np.array(x) * gmpy2.powmod(self.r, self.public_key.e, self.public_key.n) % self.public_key.n | |
def ZA_set(self, ya): | |
o = [] | |
for x in ya: | |
o.append(self.ZA(x)) | |
return o | |
def DA_set(self, za): | |
return np.array(za) * gmpy2.invert(self.r, self.public_key.n) % self.public_key.n | |
def test(self, xa, xb): | |
xa = self.hash(xa) | |
da = self.DA(self.ZA(self.YA(xa))) | |
xb = self.hash(xb) | |
zb = self.ZB(xb) | |
return da, zb , da==zb |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://fate.readthedocs.io/en/latest/federatedml/statistic/intersect/README.html#figure-1