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 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 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 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)
value2 = [int(x) for x in (value1)] # print(value1) # value1 = [1,2,3,4,5] # value2 = [1,2,3,4,5] for i in range(len(value1), 10): start = time.process_time() public_key, private_key = paillier.generate_paillier_keypair() encrp_data1 = [public_key.encrypt(x).ciphertext() for x in value1] encrp_data2 = [public_key.encrypt(x).ciphertext() for x in value2] data = {'n': public_key.n, 'cipher_texts': [encrp_data1, encrp_data2]} jsonData = json.dumps(data) # sending request res = requests.post("http://127.0.0.1:8000/getdata/", json=jsonData).json() ret_value = [] # evaluating response for i in res['sum_cipher_text']: sum_cipher_text = EncryptedNumber(public_key, i) ret_value.append(private_key.decrypt(sum_cipher_text)) time_taken = time.process_time() - start print("Sum of " + str(value1) + " and " + str(value2) + " is " + str(ret_value)) print(time_taken)
import json import requests from phe import paillier from phe.paillier import PaillierPublicKey, EncryptedNumber value1 = 1 value2 = 2 public_key, private_key = paillier.generate_paillier_keypair() encrp_data1 = public_key.encrypt(value1) encrp_data2 = public_key.encrypt(value2) data = { 'n': public_key.n, 'cipher_texts': [encrp_data1.ciphertext(), encrp_data2.ciphertext()] } jsonData = json.dumps(data) # sending request res = requests.post("http://127.0.0.1:8000/getdata/", json=jsonData).json() # evaluating response sum_cipher_text = EncryptedNumber(public_key, res['sum_cipher_text']) ret_value = private_key.decrypt(sum_cipher_text) print("Sum of " + str(value1) + " and " + str(value2) + " is " + str(ret_value))