def deserialize_paillier(struct, pub=None): # Case 1: dict recursion if isinstance(struct, dict): pub = PaillierPublicKey(n=int(struct['n'])) child = [ deserialize_paillier(substruct, pub) for substruct in struct['values'] ] # Building Paillier Tensor tensor = PaillierTensor() tensor.child = array(child) tensor.pubkey = pub return tensor.wrap() # Case 2: list recursion elif isinstance(struct, list): return [deserialize_paillier(substruct, pub) for substruct in struct] # Case 3: Tuple deserialization elif isinstance(struct, tuple): return EncryptedNumber(pub, int(struct[0]), int(struct[1])) # Case 4: Unknown type else: raise TypeError(type(struct))
def detail(worker: AbstractWorker, tensor_tuple: tuple) -> "PaillierTensor": """ This function reconstructs a LogTensor given it's attributes in form of a tuple. Args: worker: the worker doing the deserialization tensor_tuple: a tuple holding the attributes of the LogTensor Returns: PaillierTensor: a LogTensor Examples: logtensor = detail(data) """ obj_id, chain = tensor_tuple struct = sy.serde.msgpack.serde._detail(worker, chain) if isinstance(struct, dict) and chain is not None: tensor = PaillierTensor(owner=worker, id=obj_id) public_key = struct["public_key"] pub = PaillierPublicKey(n=int(public_key["n"])) if isinstance(struct["values"][0], list): values = [[ EncryptedNumber(pub, int(x[0]), int(x[1])) for x in y ] for y in struct["values"]] else: values = [ EncryptedNumber(pub, int(x[0]), int(x[1])) for x in struct["values"] ] tensor.child = np.array(values) syft_tensor = tensor.wrap() return syft_tensor else: raise TypeError(type(struct))
def getdata(): jsondata = request.get_json() data = json.loads(jsondata) print("Request recieved and started evaluating") # Use the data here public_key = PaillierPublicKey(data['n']) encrp_numb1 = EncryptedNumber(public_key, data['cipher_texts'][0]) encrp_numb2 = EncryptedNumber(public_key, data['cipher_texts'][1]) sum_cipher_text = encrp_numb1 + encrp_numb2 results = {'sum_cipher_text': sum_cipher_text.ciphertext()} return json.dumps(results)
def encrypt_logistic_regression_dataset(public_key:pl.PaillierPublicKey, dataset): ''' This functions is used for encrypting dataset using Paillier. Args: public_key: PaillierPublicKey object. dataset: a numpy array that stores feature values of every sample in the dataset. Returns: Numpy array that stores the encrypted dataset. ''' encrypted_dataset = np.empty(dataset.shape, dtype=object) for row in range(dataset.shape[0]): for col in range(dataset.shape[1]): encrypted_dataset[row, col] = public_key.encrypt(dataset[row, col]) return encrypted_dataset
def getdata(): jsondata = request.get_json() data = json.loads(jsondata) m = 10 # m is constant print("Request recieved and started evaluating") # Use the data here public_key = PaillierPublicKey(data['n']) sum_cipher_text = [] for i in range(0, len(data['cipher_texts'][0])): # operation : a+mb # a is EncryptedNumber1 # b is EncryptedNumber2 # and m is constant encrp_numb1 = EncryptedNumber(public_key, data['cipher_texts'][0][i]) encrp_numb2 = EncryptedNumber(public_key, data['cipher_texts'][1][i]) sum_cipher_text.append((encrp_numb1 + m * encrp_numb2).ciphertext()) results = {'sum_cipher_text': sum_cipher_text} return json.dumps(results)
def testPrivateKeyEquality(self): pk = PaillierPublicKey(2537) p1 = PaillierPrivateKey(pk, 43, 59) p2 = PaillierPrivateKey(pk, 59, 43) self.assertEqual(p1, p2, "These private keys should be equal")
'061766016057232162558670842672792786382176458853266584196452737'), int('1871982055803778392476802740345788758859696561585211055384725016851434368935520998885418713898088559' '8418663282389899893379025256053425211917863903290311897594491856834816788774485793876862964078700211' '5051595425860113579097652652381289438357115345852014278975683760722548127064999055942330089512549084' '3457104787310253898939023613930692662609946992238109343510216184595026872508990430490517011040610502' '496065494899574677755032259109437651483551873070754235640245607'), int('8890019373497156577506573854692757910861682878751666399881268812211327768889285942922917822427406171' '1374084535302327665620874750481886059863738167654613913302052224437095881661430248867369722480906681' '0293579420608981080797910712789594734623519152297342857449149250317462819509862326203278505821528945' '6448968243743443665102431436949177200582903936634957540698176693179452224130375241137422201752548805' '38484129753291039384174315026630418515764098957297791847701740'))) public_key = PaillierPublicKey( int('1484838658248464064259856757081154421171196989150622299748222043593445654503905553445414915359609687' '77035409413270882739651258158816563585214558468913443613231772079652558633998048113787675894701782805516' '041546348825706150790878817148626225214317380324006002845687798158170472767179664512573569011721121619210' ), int('1484838658248464064259856757081154421171196989150622299748222043593445654503905553445414915359609687' '7703540941327088273965125815881656358521455846891344361323177207965255863399804811378767589470178280' '5516041546348825706150790878817148626225214317380324006002845687798158170472767179664512573569011721' '121619209')) context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH) context.load_cert_chain(certfile="server-cert.pem", keyfile="server-key.pem") sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) print('Socket created') #Bind socket to local host and port try: sock.bind((HOST, PORT)) except socket.error as msg: