def test_Pyfhel_5c_save_restore_all(self): pyfhel = Pyfhel() pyfhel.contextGen(p=1964769281, m=8192, base=2, sec=192, flagBatching=True) pyfhel.keyGen() pyfhel.rotateKeyGen(60) pyfhel.relinKeyGen(60, 4) # save all keys into temporary directory tmp_dir = tempfile.TemporaryDirectory() pyfhel.saveContext(tmp_dir.name + "/context") pyfhel.savepublicKey(tmp_dir.name + "/pub.key") pyfhel.savesecretKey(tmp_dir.name + "/sec.key") pyfhel.saverelinKey(tmp_dir.name + "/relin.key") pyfhel.saverotateKey(tmp_dir.name + "/rotate.key") # restore all keys pyfhel2 = Pyfhel() pyfhel.contextGen(p=1964769281, m=8192, base=2, sec=192, flagBatching=True) pyfhel2.restoreContext(tmp_dir.name + "/context") pyfhel2.restorepublicKey(tmp_dir.name + "/pub.key") pyfhel2.restoresecretKey(tmp_dir.name + "/sec.key") pyfhel2.restorerelinKey(tmp_dir.name + "/relin.key") pyfhel2.restorerotateKey(tmp_dir.name + "/rotate.key") # test encryption decryption ctxt1 = pyfhel.encryptBatch([42]) self.assertEqual( pyfhel2.decryptBatch(ctxt1)[0], 42, "decrypting with restored keys should work", ) try: pyfhel2.rotate(ctxt1, -1) self.assertEqual( pyfhel2.decryptBatch(ctxt1)[1], 42, "decrypting with restored keys should work", ) except Exception as err: self.fail("PyPtxt() creation failed unexpectedly: ", err) # test ciphertext storing ctxt2 = pyfhel.encryptInt(42) ctxt2.save(tmp_dir.name + "/ctxt2") ctxt_restored = PyCtxt() ctxt_restored.load(tmp_dir.name + "/ctxt2", "int") self.assertEqual(pyfhel2.decryptInt(ctxt_restored), 42, "decrypting ciphertext should work") tmp_dir.cleanup()
def predict(self, test_labels): if self.verbosity: print("Computing Prediction") print("==================================") fc_folder = self.enclayers_dir + "/fullyconnected" out_folder = fc_folder + "/output" if not path.exists(out_folder): raise Exception( "You need to compute the fully connected layer before.") print(test_labels[0]) # Only q predictions are done simultaneously # for i in range(self.n) el = [] start = timeit.default_timer() for i in range(test_labels.shape[1]): file = out_folder + "/fc_" + str(i) p = PyCtxt() p.load(file, 'batch') ptxt = self.py.decrypt(p) ptxt = self.py.decodeBatch(ptxt) ptxt = self.decode_tensor(ptxt, self.t, self.precision) if (el.__len__() <= i): el.append([]) for j in range(ptxt.__len__()): if (el.__len__() <= j): el.append([ptxt[j]]) else: el[j].append(ptxt[j]) el = np.array(el) print(el.shape) print(el[0]) pos = 0 for i in range(el.shape[0]): mp = np.argmax(el[i]) ml = np.argmax(test_labels[i]) if (mp == ml): pos += 1 stop = timeit.default_timer() print("Computation time: " + str(stop - start) + " s.") print("Positive prediction: " + str(pos)) print("Negative prediction: " + str(self.n - pos)) acc = (pos / self.n) * 100 print("Model Accurancy:" + str(acc) + "%")
def test_Pyfhel_5d_save_restore_batch(self): pyfhel = Pyfhel() pyfhel.contextGen(p=1964769281, m=8192, base=2, sec=192, flagBatching=True) pyfhel.keyGen() pyfhel.rotateKeyGen(60) pyfhel.relinKeyGen(60, 4) # encrypt something ctxt = pyfhel.encryptBatch([1, 2, 3, 4]) # save to temporary file tmp = tempfile.NamedTemporaryFile() ctxt.save(tmp.name) # load from temporary file loaded = PyCtxt() loaded.load(tmp.name, "batch") self.assertEqual(pyfhel.decryptBatch(loaded)[:4], [1, 2, 3, 4])
def post(): print("Received Request!") # Read all bytestrings HE_server = Pyfhel() HE_server.from_bytes_context(request.json.get('context').encode('cp437')) HE_server.from_bytes_public_key(request.json.get('pk').encode('cp437')) HE_server.from_bytes_relin_key(request.json.get('rlk').encode('cp437')) HE_server.from_bytes_rotate_key(request.json.get('rtk').encode('cp437')) cx = PyCtxt(pyfhel=HE_server, bytestring=request.json.get('cx').encode('cp437')) print(f"[Server] received HE_server={HE_server} and cx={cx}") # Encode weights in plaintext ptxt_w = HE_server.encode(w) # Compute weighted average c_mean = (cx * ptxt_w) c_mean /= 4 # 4 c_mean += (c_mean >> 1) # cumulative sum c_mean += (c_mean >> 2) # element [3] contains the result print(f"[Server] Average computed! Responding: c_mean={c_mean}") # Serialize encrypted result and answer it back return c_mean.to_bytes().decode('cp437')
def test_Pyfhel_5f_save_restore_batch(self): pyfhel = Pyfhel() pyfhel.contextGen(p=1964769281, m=8192, base=2, sec=192, flagBatching=True) pyfhel.keyGen() pyfhel.rotateKeyGen(60) pyfhel.relinKeyGen(60, 4) # encrypt something ctxt = pyfhel.encryptBatch([1, 2, 3, 4]) # save to temporary file tmp_dir = tempfile.TemporaryDirectory() tmp_file = os.path.join(tmp_dir.name, "ctxt") ctxt.save(tmp_file) # load from temporary file loaded = PyCtxt() loaded.load(tmp_file, "batch") self.assertEqual(pyfhel.decryptBatch(loaded)[:4], [1, 2, 3, 4]) tmp_dir.cleanup()
def retrieve(self, folder, shape): if not self.is_stored_before(folder): raise Exception("Required files not foud in folder " + folder) to_populate = np.empty(shape=shape) to_populate = to_populate.flatten() l = [] for i in range(to_populate.__len__()): p = PyCtxt() fname = folder + "/enc_" + str(i) if not path.exists(fname): raise Exception("File ", fname, "not exists") p.load(fname, 'batch') l.append(p) return np.reshape(l, shape)
def test_Pyfhel_3a_encrypt_decrypt_int(self): pyfhel = Pyfhel() pyfhel.contextGen(p=65537) pyfhel.keyGen() ctxt = pyfhel.encryptInt(127) self.assertEqual(pyfhel.decryptInt(ctxt), 127) ctxt2 = PyCtxt(ctxt) pyfhel.encryptInt(-2, ctxt) self.assertEqual(pyfhel.decryptInt(ctxt), -2) self.assertEqual(pyfhel.decryptInt(ctxt), -2) self.assertEqual(pyfhel.decryptInt(ctxt2), 127)
def test_PyCtxt_creation_deletion(self): try: self.ctxt = PyCtxt() self.ctxt2 = PyCtxt(other_ctxt=self.ctxt) self.pyfhel = Pyfhel() self.ctxt3 = PyCtxt(pyfhel=self.pyfhel) self.ctxt4 = PyCtxt(other_ctxt=self.ctxt3) except Exception as err: self.fail("PyCtxt() creation failed unexpectedly: ", err) self.assertEqual(self.ctxt.size(), 2) self.assertEqual(self.ctxt._encoding, ENCODING_t.UNDEFINED) self.ctxt._encoding = ENCODING_t.FRACTIONAL self.assertEqual(self.ctxt._encoding, ENCODING_t.FRACTIONAL) del (self.ctxt._encoding) self.assertEqual(self.ctxt._encoding, ENCODING_t.UNDEFINED) self.assertEqual(self.ctxt.size(), 2) self.ctxt._pyfhel = self.pyfhel self.ctxt2._pyfhel = self.ctxt._pyfhel try: del (self.ctxt) except Exception as err: self.fail("PyCtxt() deletion failed unexpectedly: ", err)
def _enc_arr_(self, arr, file_name=None): if not self.py.getflagBatch(): raise Exception("You need to initialize Batch for this context.") if file_name != None: if path.exists(file_name): ct = PyCtxt() ct.load(file_name, 'batch') return ct res = [] for x in range(self.n): res.append(arr[x]) res = np.array(res) encoded = self.py.encodeBatch(res) encrypted = self.py.encryptPtxt(encoded) if file_name != None: encrypted.save(file_name) return encrypted
def get_results(self, test_labels): dense_folder = self.enclayers_dir + "/fullyconnected" out_folder = dense_folder + "/output" el = [] for i in range(test_labels.shape[1]): file = out_folder + "/fc_" + str(i) p = PyCtxt() p.load(file, 'batch') ptxt = self.py.decrypt(p) ptxt = self.py.decodeBatch(ptxt) if (el.__len__() <= i): el.append([]) for j in range(ptxt.__len__()): if (el.__len__() <= j): el.append([ptxt[j]]) else: el[j].append(ptxt[j]) return np.array(el)
print("[Client] server initialized...") # %% # 3. Launch a request to the server # ---------------------------------------- # We map the bytes into strings based on https://stackoverflow.com/a/27527728 if(USE_REAL_SERVER): r = requests.post('http://127.0.0.1:5000/fhe_mse', json={ 'context': s_context.decode('cp437'), 'pk': s_public_key.decode('cp437'), 'rlk':s_relin_key.decode('cp437'), 'rtk':s_rotate_key.decode('cp437'), 'cx': s_cx.decode('cp437'), }) c_res = PyCtxt(pyfhel=HE_client, bytestring=r.text.encode('cp437')) else: # Mocking server code (from Demo_5bis_CS_Server.py) # Read all bytestrings HE_server = Pyfhel() HE_server.from_bytes_context(s_context) HE_server.from_bytes_public_key(s_public_key) HE_server.from_bytes_relin_key(s_relin_key) HE_server.from_bytes_rotate_key(s_rotate_key) cx = PyCtxt(pyfhel=HE_server, bytestring=s_cx) print(f"[Server] received HE_server={HE_server} and cx={cx}") # Encode weights in plaintext w = np.array([0.5, -1.5, 4, 5]) ptxt_w = HE_server.encode(w) # Compute weighted average
HE.savepublicKey(tmp_dir.name + "/pub.key") HE.savesecretKey(tmp_dir.name + "/sec.key") HE.saverelinKey(tmp_dir.name + "/relin.key") HE.saverotateKey(tmp_dir.name + "/rotate.key") print("3. Restore all keys") HE2 = Pyfhel() HE2.restoreContext(tmp_dir.name + "/context") HE2.restorepublicKey(tmp_dir.name + "/pub.key") HE2.restoresecretKey(tmp_dir.name + "/sec.key") HE2.restorerelinKey(tmp_dir.name + "/relin.key") HE2.restorerotateKey(tmp_dir.name + "/rotate.key") print("4. Testing encryption decryption:") ctxt1 = HE.encryptBatch([42]) assert HE2.decryptBatch( ctxt1)[0] == 42, "decrypting with restored keys should work" HE2.rotate(ctxt1, -1) assert HE2.decryptBatch( ctxt1)[1] == 42, "decrypting with restored keys should work" print("5. Testing ciphertext storing:") ctxt2 = HE.encryptInt(42) ctxt2.save(tmp_dir.name + "/ctxt2") ctxt_restored = PyCtxt() ctxt_restored.load(tmp_dir.name + "/ctxt2", int) assert HE2.decryptInt(ctxt_restored) == 42, "decrypting ciphertext should work" # Cleaning up secure channel tmp_dir.cleanup()
class PyfhelTestCase(unittest.TestCase): def setUp(self): self.t0 = time.time() def tearDown(self): sys.stderr.write('({}s) ...'.format(round(time.time() - self.t0, 3))) def test_PyPtxt_PyCtxt(self): pass def test_PyPtxt_creation_deletion(self): try: self.ptxt = PyPtxt() self.ptxt2 = PyPtxt(other_ptxt=self.ptxt) self.pyfhel = Pyfhel() self.ptxt3 = PyPtxt(pyfhel=self.pyfhel) self.ptxt4 = PyPtxt(other_ptxt=self.ptxt3) except Exception as err: self.fail("PyPtxt() creation failed unexpectedly: ", err) self.assertEqual(self.ptxt._encoding, ENCODING_t.UNDEFINED) self.ptxt._encoding = ENCODING_t.INTEGER self.assertEqual(self.ptxt._encoding, ENCODING_t.INTEGER) del (self.ptxt._encoding) self.assertEqual(self.ptxt._encoding, ENCODING_t.UNDEFINED) self.ptxt._pyfhel = self.pyfhel self.ptxt2._pyfhel = self.ptxt._pyfhel try: del (self.ptxt) except Exception as err: self.fail("PyPtxt() deletion failed unexpectedly: ", err) def test_PyCtxt_creation_deletion(self): try: self.ctxt = PyCtxt() self.ctxt2 = PyCtxt(other_ctxt=self.ctxt) self.pyfhel = Pyfhel() self.ctxt3 = PyCtxt(pyfhel=self.pyfhel) self.ctxt4 = PyCtxt(other_ctxt=self.ctxt3) except Exception as err: self.fail("PyCtxt() creation failed unexpectedly: ", err) self.assertEqual(self.ctxt.size(), 2) self.assertEqual(self.ctxt._encoding, ENCODING_t.UNDEFINED) self.ctxt._encoding = ENCODING_t.FRACTIONAL self.assertEqual(self.ctxt._encoding, ENCODING_t.FRACTIONAL) del (self.ctxt._encoding) self.assertEqual(self.ctxt._encoding, ENCODING_t.UNDEFINED) self.assertEqual(self.ctxt.size(), 2) self.ctxt._pyfhel = self.pyfhel self.ctxt2._pyfhel = self.ctxt._pyfhel try: del (self.ctxt) except Exception as err: self.fail("PyCtxt() deletion failed unexpectedly: ", err) def test_Pyfhel_1_GENERATION(self): pass def test_Pyfhel_1a_creation_deletion(self): try: self.pyfhel = Pyfhel() except Exception as err: self.fail("Pyfhel() creation failed unexpectedly: ", err) try: del (self.pyfhel) except Exception as err: self.fail("Pyfhel() deletion failed unexpectedly: ", err) def test_Pyfhel_1b_context_n_key_generation(self): self.pyfhel = Pyfhel() self.pyfhel.contextGen(65537) self.pyfhel.keyGen() def test_Pyfhel_1c_rotate_key_generation(self): self.pyfhel = Pyfhel() self.pyfhel.contextGen(65537) self.pyfhel.keyGen() self.pyfhel.rotateKeyGen(30) self.pyfhel.rotateKeyGen(1) self.pyfhel.rotateKeyGen(60) self.assertRaises(SystemError, lambda: self.pyfhel.rotateKeyGen(61)) self.assertRaises(SystemError, lambda: self.pyfhel.rotateKeyGen(0)) def test_Pyfhel_1d_relin_key_generation(self): self.pyfhel = Pyfhel() self.pyfhel.contextGen(65537) self.pyfhel.keyGen() self.pyfhel.relinKeyGen(30, 5) self.pyfhel.relinKeyGen(1, 5) self.pyfhel.relinKeyGen(60, 5) self.assertRaises(SystemError, lambda: self.pyfhel.relinKeyGen(61, 5)) self.assertRaises(SystemError, lambda: self.pyfhel.relinKeyGen(0, 5)) def test_Pyfhel_2_ENCODING(self): pass def test_Pyfhel_2a_encode_decode_int(self): self.pyfhel = Pyfhel() self.pyfhel.contextGen(p=65537) self.pyfhel.keyGen() self.ptxt = self.pyfhel.encodeInt(127) self.assertEqual(self.ptxt.to_string(), b'1x^6 + 1x^5 + 1x^4 + 1x^3 + 1x^2 + 1x^1 + 1') self.assertEqual(self.pyfhel.decodeInt(self.ptxt), 127) self.ptxt2 = PyPtxt(self.ptxt) self.pyfhel.encodeInt(-2, self.ptxt) self.assertEqual(self.ptxt.to_string(), b'10000x^1') self.assertEqual(self.pyfhel.decodeInt(self.ptxt), -2) self.assertEqual(self.pyfhel.decodeInt(self.ptxt2), 127) def test_Pyfhel_2b_encode_decode_float(self): self.pyfhel = Pyfhel() self.pyfhel.contextGen(p=65537, m=8192, base=2, intDigits=80, fracDigits=20) self.pyfhel.keyGen() self.ptxt = self.pyfhel.encodeFrac(19.30) self.assertTrue(self.ptxt.to_string(), b'9x^8190 + 1x^4 + 1x^1 + 1') self.assertEqual(round(self.pyfhel.decodeFrac(self.ptxt), 2), 19.30) self.pyfhel.encodeFrac(-2.25, self.ptxt) self.assertEqual(self.ptxt.to_string(), b'1x^8190 + 10000x^1') self.assertEqual(round(self.pyfhel.decodeFrac(self.ptxt), 2), -2.25) def test_Pyfhel_2c_encode_decode_batch(self): self.pyfhel = Pyfhel() self.pyfhel.contextGen(p=1964769281, m=8192, base=2, sec=192, flagBatching=True) self.pyfhel.keyGen() self.assertTrue(self.pyfhel.batchEnabled()) self.ptxt = self.pyfhel.encodeBatch([1, 2, 3, 4, 5, 6]) self.assertEqual(self.pyfhel.getnSlots(), 8192) self.assertEqual( self.pyfhel.decodeBatch(self.ptxt)[:6], [1, 2, 3, 4, 5, 6]) #print(self.ptxt.to_string()) def test_Pyfhel_2d_encode_decode_array(self): self.pyfhel = Pyfhel() self.pyfhel.contextGen(p=1964769281, m=8192, base=2, sec=192, flagBatching=True) self.pyfhel.keyGen() self.assertTrue(self.pyfhel.batchEnabled()) self.ptxt = self.pyfhel.encodeArray(np.array([1, 2, 3, 4, 5, 6])) self.assertEqual(self.pyfhel.getnSlots(), 8192) self.assertTrue( np.alltrue( self.pyfhel.decodeArray(self.ptxt)[:6] == np.array( [1, 2, 3, 4, 5, 6]))) def test_Pyfhel_3_ENCRYPTING(self): pass def test_Pyfhel_3a_encrypt_decrypt_int(self): self.pyfhel = Pyfhel() self.pyfhel.contextGen(p=65537) self.pyfhel.keyGen() self.ctxt = self.pyfhel.encryptInt(127) self.assertEqual(self.pyfhel.decryptInt(self.ctxt), 127) self.ctxt2 = PyCtxt(self.ctxt) self.pyfhel.encryptInt(-2, self.ctxt) self.assertEqual(self.pyfhel.decryptInt(self.ctxt), -2) self.assertEqual(self.pyfhel.decryptInt(self.ctxt), -2) self.assertEqual(self.pyfhel.decryptInt(self.ctxt2), 127) def test_Pyfhel_3b_encrypt_decrypt_float(self): self.pyfhel = Pyfhel() self.pyfhel.contextGen(p=65537, m=8192, base=2, intDigits=80, fracDigits=20) self.pyfhel.keyGen() self.ctxt = self.pyfhel.encryptFrac(19.30) self.assertEqual(round(self.pyfhel.decryptFrac(self.ctxt), 2), 19.30) self.pyfhel.encryptFrac(-2.25, self.ctxt) self.assertEqual(round(self.pyfhel.decryptFrac(self.ctxt), 2), -2.25) def test_Pyfhel_3c_encrypt_decrypt_batch(self): self.pyfhel = Pyfhel() self.pyfhel.contextGen(p=1964769281, m=8192, base=2, sec=192, flagBatching=True) self.pyfhel.keyGen() self.assertTrue(self.pyfhel.batchEnabled()) self.ctxt = self.pyfhel.encryptBatch([1, 2, 3, 4, 5, 6]) self.assertEqual(self.pyfhel.getnSlots(), 8192) self.assertEqual( self.pyfhel.decryptBatch(self.ctxt)[:6], [1, 2, 3, 4, 5, 6]) #print(self.ptxt.to_string()) def test_Pyfhel_3d_encrypt_decrypt_array(self): self.pyfhel = Pyfhel() self.pyfhel.contextGen(p=1964769281, m=8192, base=2, sec=192, flagBatching=True) self.pyfhel.keyGen() self.assertTrue(self.pyfhel.batchEnabled()) self.ctxt = self.pyfhel.encryptArray(np.array([1, 2, 3, 4, 5, 6])) self.assertEqual(self.pyfhel.getnSlots(), 8192) self.assertTrue( np.alltrue( self.pyfhel.decryptArray(self.ctxt)[:6] == np.array( [1, 2, 3, 4, 5, 6]))) def test_Pyfhel_4_OPERATIONS(self): pass def test_Pyfhel_4a_operations_integer(self): self.pyfhel = Pyfhel() self.pyfhel.contextGen(p=1964769281, m=8192, base=3, sec=192) self.pyfhel.keyGen() #self.pyfhel.rotateKeyGen(60) #self.pyfhel.relinKeyGen(60) self.ctxti = self.pyfhel.encryptInt(127) self.ctxti2 = self.pyfhel.encryptInt(-2) self.ptxti = self.pyfhel.encodeInt(3) self.ctxt_add = self.pyfhel.add(self.ctxti, self.ctxti2, in_new_ctxt=True) self.ctxt_add2 = self.pyfhel.add_plain(self.ctxti, self.ptxti, in_new_ctxt=True) self.ctxt_sub = self.pyfhel.sub(self.ctxti, self.ctxti2, in_new_ctxt=True) self.ctxt_sub2 = self.pyfhel.sub_plain(self.ctxti, self.ptxti, in_new_ctxt=True) self.ctxt_mult = self.pyfhel.multiply(self.ctxti, self.ctxti2, in_new_ctxt=True) self.ctxt_mult2 = self.pyfhel.multiply_plain(self.ctxti, self.ptxti, in_new_ctxt=True) #self.ctxt_rotate = self.pyfhel.rotate(self.ctxti, 2) #self.ctxt_expon = self.pyfhel.power(self.ctxti, 3) #self.ctxt_expon2 = self.pyfhel.power(self.ctxti2, 3) #self.ctxt_polyEval = self.pyfhel.polyEval(self.ctxti, [1, 2, 1], in_new_ctxt=True) self.assertEqual(self.pyfhel.decryptInt(self.ctxt_add), 125) self.assertEqual(self.pyfhel.decryptInt(self.ctxt_add2), 130) self.assertEqual(self.pyfhel.decryptInt(self.ctxt_sub), 129) self.assertEqual(self.pyfhel.decryptInt(self.ctxt_sub2), 124) self.assertEqual(self.pyfhel.decryptInt(self.ctxt_mult), -254) self.assertEqual(self.pyfhel.decryptInt(self.ctxt_mult2), 381) #self.assertEqual(self.pyfhel.decryptInt(self.ctxt_expon), 2048383) #self.assertEqual(self.pyfhel.decryptInt(self.ctxt_expon2), -8) #self.assertEqual(self.pyfhel.decryptInt(self.ctxt_polyEval), 16510) def test_Pyfhel_4b_operations_frac(self): self.pyfhel = Pyfhel() self.pyfhel.contextGen(p=1964769281, m=8192, base=3, sec=192) self.pyfhel.keyGen() #self.pyfhel.rotateKeyGen(60) #self.pyfhel.relinKeyGen(60) self.ctxti = self.pyfhel.encryptFrac(19.37) self.ctxti2 = self.pyfhel.encryptFrac(-2.25) self.ptxti = self.pyfhel.encodeFrac(3.12) self.ctxt_add = self.pyfhel.add(self.ctxti, self.ctxti2, in_new_ctxt=True) self.ctxt_add2 = self.pyfhel.add_plain(self.ctxti, self.ptxti, in_new_ctxt=True) self.ctxt_sub = self.pyfhel.sub(self.ctxti, self.ctxti2, in_new_ctxt=True) self.ctxt_sub2 = self.pyfhel.sub_plain(self.ctxti, self.ptxti, in_new_ctxt=True) self.ctxt_mult = self.pyfhel.multiply(self.ctxti, self.ctxti2, in_new_ctxt=True) self.ctxt_mult2 = self.pyfhel.multiply_plain(self.ctxti, self.ptxti, in_new_ctxt=True) self.assertEqual(round(self.pyfhel.decryptFrac(self.ctxt_add), 2), 17.12) self.assertEqual(round(self.pyfhel.decryptFrac(self.ctxt_add2), 2), 22.49) self.assertEqual(round(self.pyfhel.decryptFrac(self.ctxt_sub), 2), 21.62) self.assertEqual(round(self.pyfhel.decryptFrac(self.ctxt_sub2), 2), 16.25) self.assertEqual(round(self.pyfhel.decryptFrac(self.ctxt_mult), 2), -43.58) self.assertEqual(round(self.pyfhel.decryptFrac(self.ctxt_mult2), 2), 60.43) def test_Pyfhel_4c_operations_batch_array(self): self.pyfhel = Pyfhel() self.pyfhel.contextGen(p=1964769281, m=8192, base=2, sec=192, flagBatching=True) self.pyfhel.keyGen() self.pyfhel.rotateKeyGen(60) self.ctxti = self.pyfhel.encryptBatch([1, 2, 3, 4, 5, 6]) self.ctxti2 = self.pyfhel.encryptArray( np.array([-6, -5, -4, -3, -2, -1])) self.ptxti = self.pyfhel.encodeArray(np.array([12, 15, 18, 21, 24, 27])) self.ctxt_add = self.pyfhel.add(self.ctxti, self.ctxti2, in_new_ctxt=True) self.ctxt_add2 = self.pyfhel.add_plain(self.ctxti, self.ptxti, in_new_ctxt=True) self.ctxt_sub = self.pyfhel.sub(self.ctxti, self.ctxti2, in_new_ctxt=True) self.ctxt_sub2 = self.pyfhel.sub_plain(self.ctxti, self.ptxti, in_new_ctxt=True) self.ctxt_mult = self.pyfhel.multiply(self.ctxti, self.ctxti2, in_new_ctxt=True) self.ctxt_mult2 = self.pyfhel.multiply_plain(self.ctxti, self.ptxti, in_new_ctxt=True) self.ctxt_rotate = self.pyfhel.rotate(self.ctxti, -2, in_new_ctxt=True) self.ctxt_rotate2 = self.pyfhel.rotate(self.ctxti, 2, in_new_ctxt=True) #self.ctxt_expon = self.pyfhel.power(self.ctxti, 3) #self.ctxt_expon2 = self.pyfhel.power(self.ctxti2, 3) #self.ctxt_polyEval = self.pyfhel.polyEval(self.ctxti, [1, 2, 1], in_new_ctxt=True) self.assertEqual( self.pyfhel.decryptBatch(self.ctxt_add)[:6], [-5, -3, -1, 1, 3, 5]) self.assertEqual( self.pyfhel.decryptBatch(self.ctxt_add2)[:6], [13, 17, 21, 25, 29, 33]) self.assertEqual( self.pyfhel.decryptBatch(self.ctxt_sub)[:6], [7, 7, 7, 7, 7, 7]) self.assertEqual( self.pyfhel.decryptBatch(self.ctxt_sub2)[:6], [-11, -13, -15, -17, -19, -21]) self.assertEqual( self.pyfhel.decryptBatch(self.ctxt_mult)[:6], [-6, -10, -12, -12, -10, -6]) self.assertEqual( self.pyfhel.decryptBatch(self.ctxt_mult2)[:6], [12, 30, 54, 84, 120, 162]) self.assertEqual( self.pyfhel.decryptBatch(self.ctxt_rotate)[:6], [0, 0, 1, 2, 3, 4]) self.assertEqual( self.pyfhel.decryptBatch(self.ctxt_rotate2)[:6], [3, 4, 5, 6, 0, 0]) def test_Pyfhel_5_IO_SAVE_RESTORE(self): pass def test_Pyfhel_5a_save_objects(self): self.pyfhel = Pyfhel() self.pyfhel.contextGen(p=1964769281, m=8192, base=2, sec=192, flagBatching=True) self.pyfhel.keyGen() self.pyfhel.rotateKeyGen(60) #self.pyfhel.relinKeyGen(60) self.assertTrue(self.pyfhel.saveContext(b"context.pycon")) self.assertTrue(self.pyfhel.savepublicKey(b"public_k.pypk")) self.assertTrue(self.pyfhel.savesecretKey(b"secret_k.pysk")) #self.assertTrue(self.pyfhel.saverelinKey(b"relin_k.pyrlk")) self.assertTrue(self.pyfhel.saverotateKey(b"rotate_k.pyrok")) def test_Pyfhel_5b_restore_objects(self): self.pyfhel = Pyfhel() self.assertTrue(self.pyfhel.restoreContext(b"context.pycon")) self.assertTrue(self.pyfhel.restoresecretKey(b"secret_k.pysk")) self.assertTrue(self.pyfhel.restorepublicKey(b"public_k.pypk")) #self.assertTrue(self.pyfhel.restorerelinKey(b"relin_k.pyrlk")) self.assertTrue(self.pyfhel.restorerotateKey(b"rotate_k.pyrok")) os.remove(b"context.pycon") os.remove(b"secret_k.pysk") os.remove(b"public_k.pypk") os.remove(b"rotate_k.pyrok")
HE.keyGen() # Generates public and private key # Save context and public key only HE.save_public_key("mypk.pk") HE.save_context("mycontext.con") # Encrypt and save inputs ctxt_a = HE.encrypt(15) # implicit encoding ctxt_b = HE.encrypt(25) ctxt_a.save("ctxt_a.ctxt") ctxt_b.save("ctxt_b.ctxt") ##### SERVER HE_server = Pyfhel() HE_server.load_context("mycontext.con") HE_server.load_public_key("mypk.pk") # no secret key # Load ciphertexts ca = PyCtxt(pyfhel=HE_server, fileName="ctxt_a.ctxt") cb = PyCtxt(pyfhel=HE_server, fileName="ctxt_b.ctxt") # Compute homomorphically and send result cr = (ca + cb) * 2 cr.save("cr.ctxt") ##### CLIENT # Load and decrypt result c_res = PyCtxt(pyfhel=HE, fileName="cr.ctxt") print("-----------------------------------------------------") print("Client_server result:", c_res.decrypt()) for f in ["mypk.pk", "mycontext.con", "ctxt_a.ctxt", "ctxt_b.ctxt", "cr.ctxt"]: os.remove(f)
from Pyfhel import Pyfhel,PyCtxt,PyPtxt import numpy as np import time he=Pyfhel() he.contextGen(p=1179649,m=32768,flagBatching=True,fracDigits=20) he.keyGen() a=PyCtxt() b=PyCtxt() q=0 cc=0 #加密文件 def PPctxt(name): fil=open(name) lines=fil.readlines() strr=''.join(lines) global l l=len(strr) l=l-1 arr=[] for line in strr: for ind in line: arr.append(ind) #arr.remove('\n') brr=[] for i in arr: brr.append(ord(i)) crr=np.array(brr) #列表数据转数组 err=he.encryptBatch(crr) err.save(name)
b = 2.5 ca = HE.encryptFrac(a) cb = HE.encryptFrac(b) ca.to_file(sec_con / "ca.ctxt") cb.to_file(sec_con / "cb.ctxt") ##### SEMI-HONEST CLOUD # Generating a second HE, acting as the honest-but-curious Cloud provider, # that will perform the operations and try to decrypt everything HE_Cl = Pyfhel() HE_Cl.restoreContext(contx_file) HE_Cl.restorepublicKey(pk_file) # loading the two ciphertexts. There is clearly potential for improvement here c2a = PyCtxt(pyfhel=HE_Cl, fileName=sec_con / "ca.ctxt", encoding=float) c2b = PyCtxt(pyfhel=HE_Cl, fileName=sec_con / "cb.ctxt", encoding=float) # Attempting to decrypt results raises an error (missing secret key) #> --------------------------------------------------------------------------- #> RuntimeError Traceback (most recent call last) #> Pyfhel/Pyfhel.pyx in Pyfhel.Pyfhel.Pyfhel.decryptFrac() #> RuntimeError: Missing a Private Key [...] try: print(HE_Cl.decrypt(c2a)) raise Exception("This should not be reached!") except RuntimeError: print("The cloud tried to decrypt, but couldn't!") # The cloud operates with the ciphertexts: c_mean = (c2a + c2b) / 2
def fully_connected(self): if self.verbosity: print("Computing Fully Connected") print("==================================") input_folder = self.enclayers_dir + "/dense2/output" fc_folder = self.enclayers_dir + "/fullyconnected" out_folder = fc_folder + "/output" wfile = "storage/layers/preprocessed/precision_" + str( self.precision) + "/pre_4_dense_11.npy" bfile = "storage/layers/preprocessed/precision_" + str( self.precision) + "/pre_bias_4_dense_11.npy" if not path.exists(fc_folder): createDir(fc_folder) if path.exists(out_folder): print("Processed before. You can found it in " + out_folder + " folder.") print("") elif not path.exists(wfile) or not path.exists(bfile): raise Exception( "Fully connected layer weights and biases need to be preprocessed before (with precision " + str(self.precision) + ").") elif not path.exists(input_folder): raise Exception( "Second dense output required. Please run Encryption.dense2(...) before." ) else: createDir(out_folder) w = np.load(wfile) b = np.load(bfile) if w.shape[1] != b.shape[0]: raise Exception("Preprocessed weights " + str(w.shape) + " and biases " + str(b.shape) + "are incopatible.") if self.verbosity: print("Fully Connected: output processing...") print("0%") start = timeit.default_timer() for x in range(w.shape[1]): local_sum = None for i in range(w.shape[0]): fname = input_folder + "/square_" + str(i) p = PyCtxt() p.load(fname, 'batch') encw = self.get_map(w[i][x]) el = self.py.multiply_plain(p, encw, True) if (local_sum == None): local_sum = el else: local_sum = self.py.add(local_sum, el) enc_b = self.get_map(b[x]) ts = self.py.add_plain(local_sum, enc_b, True) out_name = out_folder + "/fc_" + str(x) ts.save(out_name) if self.verbosity: perc = int(((x + 1) / w.shape[1]) * 100) print( str(perc) + "% (" + str(x + 1) + "/" + str(w.shape[1]) + ")") stop = timeit.default_timer() if self.verbosity: print("Fully Connected: output processed in " + str(stop - start) + " s.") print("")
def dense1(self, input_shape): if self.verbosity: print("Computing First Dense (square)") print("==================================") dense_folder = self.enclayers_dir + "/dense1" out_folder = dense_folder + "/output" conv_folder = self.enclayers_dir + "/conv" out_conv = conv_folder + "/output" wfile = "storage/layers/preprocessed/precision_" + str( self.precision) + "/pre_2_dense_9.npy" bfile = "storage/layers/preprocessed/precision_" + str( self.precision) + "/pre_bias_2_dense_9.npy" if not path.exists(dense_folder): createDir(dense_folder) if path.exists(out_folder): print("Processed before. You can found it in " + out_folder + " folder.") print("") elif not path.exists(wfile) or not path.exists(bfile): raise Exception( "First dense layer weights and biases need to be preprocessed before (with precision " + str(self.precision) + ").") elif not path.exists(out_conv): raise Exception( "Convolution output required. Please run Encryption.convolution(...) before." ) else: createDir(out_folder) w = np.load(wfile) b = np.load(bfile) start = timeit.default_timer() per = input_shape[0] * input_shape[1] filters = input_shape[2] flat = per * filters if flat != w.shape[0]: raise Exception("Input shape " + str(input_shape) + " is not compatible with preprocessed input " + str(w.shape)) if w.shape[1] != b.shape[0]: raise Exception("Preprocessed weights " + str(w.shape) + " and biases " + str(b.shape) + "are incopatible.") if self.verbosity: print("First Dense: output processing...") print("0%") for x in range(w.shape[1]): local_sum = None for i in range(per): for j in range(filters): fname = out_conv + "/" + str(i) + "_filter" + str(j) p = PyCtxt() p.load(fname, 'batch') row = (i * filters + j) encw = self.get_map(w[row][x]) el = self.py.multiply_plain(p, encw, True) if (local_sum == None): local_sum = el else: local_sum = self.py.add(local_sum, el) enc_b = self.get_map(b[x]) ts = self.py.add_plain(local_sum, enc_b, True) ts = self.py.square(ts) out_name = out_folder + "/square_" + str(x) ts.save(out_name) if self.verbosity: perc = int(((x + 1) / w.shape[1]) * 100) print( str(perc) + "% (" + str(x + 1) + "/" + str(w.shape[1]) + ")") stop = timeit.default_timer() if self.verbosity: print("First Dense: output processed in " + str(stop - start) + " s.") print("")
def convolution(self, size, kernel, stride): if self.verbosity: print("Computing Convolution") print("==================================") conv_folder = self.enclayers_dir + "/conv" pre_conv = conv_folder + "/pre" out_conv = conv_folder + "/output" if not path.exists(conv_folder): createDir(conv_folder) conv_w = self.preprocess_dir + "precision_" + str( self.precision) + "/pre_0_conv2d_3.npy" conv_b = self.preprocess_dir + "precision_" + str( self.precision) + "/pre_bias_0_conv2d_3.npy" if path.exists(pre_conv): print("(Pre)processed before. You can found it in " + pre_conv + " folder.") elif not path.exists(conv_w): print( "Convolution weights need to be preprocessed before (with precision " + str(self.precision) + ").") print("") else: createDir(pre_conv) filters = np.load(conv_w) start = timeit.default_timer() fshape = filters.shape f = filters.reshape((fshape[0] * fshape[1], fshape[2])) conv_map = self.get_conv_map(size, kernel, stride) if (conv_map.shape[0] != f.shape[0]): raise Exception( "Convolution map and filter shapes must match.") if self.verbosity: print("Convolution: output preprocessing...") print("0%") for x in range(f.shape[0]): for y in range(f.shape[1]): w_filter = self.get_map(f[x, y]) for k in range(conv_map.shape[1]): enc_pixel = self.getEncryptedPixel(conv_map[x, k]) # computing |self.n| dot products at time res = self.py.multiply_plain(enc_pixel, w_filter, True) f_name = pre_conv + "/pixel" + str( conv_map[x, k]) + "_filter" + str(y) res.save(f_name) if self.verbosity: perc = int(((x + 1) / f.shape[0]) * 100) print( str(perc) + "% (" + str(x + 1) + "/" + str(f.shape[0]) + ")") stop = timeit.default_timer() if self.verbosity: print("Convolution: output preprocessed in " + str(stop - start) + " s.") if path.exists(out_conv): print("Processed before. You can found it in " + out_conv + " folder.") print("") elif not path.exists(conv_b): print( "Convolution biases need to be preprocessed before (with precision " + str(self.precision) + ").") print("") else: createDir(out_conv) biases = np.load(conv_b) start = timeit.default_timer() bshape = biases.shape windows = self.get_conv_windows(size, kernel, stride) wshape = windows.shape if self.verbosity: print("Convolution: output processing...") print("0%") for x in range(bshape[0]): encoded_bias = self.get_map(biases[x]) for y in range(wshape[0]): local_sum = None for k in range(wshape[1]): f_name = pre_conv + "/pixel" + str( windows[y, k]) + "_filter" + str(x) p = PyCtxt() p.load(f_name, 'batch') if (local_sum == None): local_sum = p else: local_sum = self.py.add(local_sum, p) local_sum = self.py.add_plain(local_sum, encoded_bias) file_name = out_conv + "/" + str(y) + "_filter" + str(x) local_sum.save(file_name) if self.verbosity: perc = int(((x + 1) / bshape[0]) * 100) print( str(perc) + "% (" + str(x + 1) + "/" + str(bshape[0]) + ")") stop = timeit.default_timer() if self.verbosity: print("Convolution: output processed in " + str(stop - start) + " s.") print("") return out_conv
def put(self, func): todos[func] = "1" a = request.form['a'] b = request.form['b'] c = request.form['c'] d = request.form['d'] HE = Pyfhel() #Import context con = base64.b64decode(bytes(a, "utf-8")) with open('context.pycon', "wb") as pk_fw: pk_fw.write(con) HE.restoreContext("context.pycon") # Import public key pk = base64.b64decode(bytes(b, "utf-8")) with open('public_k.pypk', "wb") as pk_fw: pk_fw.write(pk) HE.restorepublicKey("public_k.pypk") # Import Ciphertext 1 c1 = base64.b64decode(bytes(c, "utf-8")) with open('ctxt.c1', "wb") as c1_fw: c1_fw.write(c1) ctxt1 = PyCtxt() ctxt1.load("ctxt.c1") ctxt1._encoding = ENCODING_t.INTEGER # Import Ciphertext 2 c2 = base64.b64decode(bytes(d, "utf-8")) with open('ctxt.c2', "wb") as c2_fw: c2_fw.write(c2) ctxt2 = PyCtxt() ctxt2.load("ctxt.c2") ctxt2._encoding = ENCODING_t.INTEGER #密文同态运算 ctxtSum = HE.add(ctxt1, ctxt2, True) ctxtSub = HE.sub(ctxt1, ctxt2, True) ctxtMul = HE.multiply(ctxt1, ctxt2, True) #Ciphertext homomorphism operation ctxtSum.save("sum") ctxtSub.save("sub") ctxtMul.save("mul") with open("sum", "rb") as f_sum: sum_e = str(base64.b64encode(f_sum.read()), "utf-8") with open("sub", "rb") as f_sub: sub_e = str(base64.b64encode(f_sub.read()), "utf-8") with open("mul", "rb") as f_mul: mul_e = str(base64.b64encode(f_mul.read()), "utf-8") if func == "add": todos[func] = sum_e elif func == "sub": todos[func] = sub_e elif func == "mul": todos[func] = mul_e return todos[func]
#Assuming that ciphertext is received, verify reliability sum_d = base64.b64decode(bytes(sum_e, "utf-8")) sub_d = base64.b64decode(bytes(sub_e, "utf-8")) mul_d = base64.b64decode(bytes(mul_e, "utf-8")) with open("txt.c1", "wb") as t1_f: t1_f.write(sum_d) with open("txt.c2", "wb") as t2_f: t2_f.write(sub_d) with open("txt.c3", "wb") as t3_f: t3_f.write(mul_d) sum = PyCtxt() sum.load("txt.c1") sum._encoding = ENCODING_t.INTEGER sub = PyCtxt() sub.load("txt.c2") sub._encoding = ENCODING_t.INTEGER mul = PyCtxt() mul.load("txt.c3") mul._encoding = ENCODING_t.INTEGER HE.restoresecretKey("secret_k.pysk") print(" addition: decrypt(ctxt1 + ctxt2) = ", HE.decryptInt(sum)) print(" substraction: decrypt(ctxt1 - ctxt2) = ", HE.decryptInt(sub)) print(" multiplication: decrypt(ctxt1 * ctxt2) = ", HE.decryptInt(mul))
def getEncryptedPixel(self, index): pixel_file = self.enclayers_dir + "/input/pixel_" + str( index) + ".pyctxt" p = PyCtxt() p.load(pixel_file, 'batch') return p
print("==============================================================") print("===================== Pyfhel ENCRYPTING ======================") print("==============================================================") print("1. Creating Context and KeyGen in a Pyfhel Object ") HE = Pyfhel() # Creating empty Pyfhel object HE.contextGen(p=65537, m=1024, flagBatching=True) # Generating context. # The values of p and m are chosen to enable batching (see Demo_Batching_SIMD.py) HE.keyGen() # Key Generation. print("2. Encrypting integers with encryptInt") integer1 = 94 integer2 = -235 ctxt_i1 = HE.encryptInt( integer1) # Encrypting integer1 in a new PyCtxt with encryptInt ctxt_i2 = PyCtxt() # Empty ciphertexts have no encoding type. print(" Empty created ctxt_i2: ", str(ctxt_i2)) HE.encryptInt(integer2, ctxt_i2) # Encrypting integer2 in an existing PyCtxt print(" int ", integer1, '-> ctxt_i1 ', str(ctxt_i1)) print(" int ", integer2, '-> ctxt_i2 ', str(ctxt_i2)) print("3. Encrypting floating point values with encryptFrac") float1 = 3.5 float2 = -7.8 ctxt_f1 = HE.encryptInt( float1) # Encrypting float1 in a new PyCtxt with encryptFrac ctxt_f2 = PyCtxt() HE.encryptFrac(float2, ctxt_f2) # Encrypting float2 in an existing PyCtxt print(" float ", float1, '-> ctxt_f1 ', str(ctxt_f1)) print(" float ", float2, '-> ctxt_f2 ', str(ctxt_f2))
HE.save_rotate_key(tmp_dir_name + "/rotate.key") c.save(tmp_dir_name + "/c.ctxt") p.save(tmp_dir_name + "/p.ptxt") print("2a. Saving everything into files. Let's check the temporary dir:") print("\n\t".join(os.listdir(tmp_dir_name))) # Now we restore everything and quickly check if it works. # Note! make sure to set the `pyfhel` parameter in PyCtxt/PyPtxt creation! HE_f = Pyfhel() # Empty creation HE_f.load_context(tmp_dir_name + "/context") HE_f.load_public_key(tmp_dir_name + "/pub.key") HE_f.load_secret_key(tmp_dir_name + "/sec.key") HE_f.load_relin_key(tmp_dir_name + "/relin.key") HE_f.load_rotate_key(tmp_dir_name + "/rotate.key") c_f = PyCtxt(pyfhel=HE_f, fileName=tmp_dir_name + "/c.ctxt") p_f = PyPtxt(pyfhel=HE_f, fileName=tmp_dir_name + "/p.ptxt", scheme='bfv') print("2b. Loading everything from files into a new environment.") # Some checks assert HE_f.decryptInt(HE_f.encryptInt(np.array([42])))[0]==42, "Incorrect encryption" assert HE_f.decryptInt(c_f)[0]==42, "Incorrect decryption/ciphertext" assert HE_f.decodeInt(p_f)[0]==-1, "Incorrect decoding" assert HE_f.decryptInt(c_f >> 1)[1]==42, "Incorrect Rotation" c_relin = c_f**2 ~c_relin assert c_relin.size()==2, "Incorrect relinearization" print(" All checks passed! Loaded from files correctly") # Cleaning up temporary directory tmp_dir.cleanup()