def client_prepare(): size = 32 bits1 = [random.choice([False, True]) for i in range(size)] bits2 = [random.choice([False, True]) for i in range(size)] reference = [not (b1 and b2) for b1, b2 in zip(bits1, bits2)] ctx = nufhe.Context() secret_key, cloud_key = ctx.make_key_pair() ciphertext1 = ctx.encrypt(secret_key, bits1) ciphertext2 = ctx.encrypt(secret_key, bits2) with open('secret_key', 'wb') as f: secret_key.dump(f) with open('cloud_key', 'wb') as f: cloud_key.dump(f) with open('ciphertext1', 'wb') as f: ciphertext1.dump(f) with open('ciphertext2', 'wb') as f: ciphertext2.dump(f) return reference
def test_all(): context = nufhe.Context() secret_key, cloud_key = context.make_key_pair() vm = context.make_virtual_machine(cloud_key) test_secure_subset_testing(vm, secret_key, context) test_somewhat_secure_subset_testing(vm, secret_key, context) test_secure_count(vm, secret_key, context) test_secure_compare(vm, secret_key, context) test_freq_itemset_mining(vm, secret_key, context)
def client_verify(reference): ctx = nufhe.Context() with open('secret_key', 'rb') as f: secret_key = ctx.load_secret_key(f) with open('result', 'rb') as f: result = ctx.load_ciphertext(f) result_bits = ctx.decrypt(secret_key, result) assert all(result_bits == reference)
def run(): ctx = nufhe.Context() secret_key, cloud_key = ctx.make_key_pair() vm = ctx.make_virtual_machine(cloud_key) # Create tape # Create VM # Execute instruction # Get output tape encrypted # Decrypt tape to get execution results tape = Tape() # Create tape of size n n = 2 indices = [] # Encrypt indices before feeding into VM for x in range(n): tape.add_cell(Number.from_plaintext(0, ctx, secret_key)) indices.append(Number.from_plaintext(x, ctx, secret_key)) utils.logic = vm utils.flag = Number.from_plaintext(1, ctx, secret_key, size=1) # Flag for conditions utils.one = Number.from_plaintext(1, ctx, secret_key, size=1) # A one utils.zero = Number.from_plaintext(0, ctx, secret_key, size=1) # A zero utils.data_ptr = Number.from_plaintext(0, ctx, secret_key) # Cell to perform op on blind_machine = VirtualMachine(tape, indices) # Add 1 instruction """for x in range(3): inc_data_ptr = Number.from_plaintext(0, ctx, secret_key) inc_data_cell = Number.from_plaintext(1, ctx, secret_key) blind_machine.step(inc_data_ptr, inc_data_cell) """ #print(utils.one + utils.one) A = 129 B = 5 sum = Number.from_plaintext(A, ctx, secret_key) + Number.from_plaintext( B, ctx, secret_key) print(sum.decrypt(ctx, secret_key)) print(A, "+", B, "=", sum.decrypt(ctx, secret_key, decimal=True))
def test_serialize_secret_key(to_file): ctx = nufhe.Context() secret_key = ctx.make_secret_key() if to_file: file_obj = io.BytesIO() secret_key.dump(file_obj) file_obj.seek(0) secret_key_loaded = ctx.load_secret_key(file_obj) else: s = secret_key.dumps() secret_key_loaded = ctx.load_secret_key(s) assert secret_key_loaded == secret_key
def test_serialize_cloud_key(to_file): ctx = nufhe.Context() secret_key, cloud_key = ctx.make_key_pair() if to_file: file_obj = io.BytesIO() cloud_key.dump(file_obj) file_obj.seek(0) cloud_key_loaded = ctx.load_cloud_key(file_obj) else: s = cloud_key.dumps() cloud_key_loaded = ctx.load_cloud_key(s) assert cloud_key_loaded == cloud_key
def cloud_process(): ctx = nufhe.Context() with open('cloud_key', 'rb') as f: cloud_key = ctx.load_cloud_key(f) vm = ctx.make_virtual_machine(cloud_key) with open('ciphertext1', 'rb') as f: ciphertext1 = vm.load_ciphertext(f) with open('ciphertext2', 'rb') as f: ciphertext2 = vm.load_ciphertext(f) result = vm.gate_nand(ciphertext1, ciphertext2) with open('result', 'wb') as f: result.dump(f)
def test_serialize_ciphertext(to_file): ctx = nufhe.Context() secret_key, cloud_key = ctx.make_key_pair() size = 32 bits = [random.choice([False, True]) for i in range(size)] ciphertext = ctx.encrypt(secret_key, bits) if to_file: file_obj = io.BytesIO() ciphertext.dump(file_obj) file_obj.seek(0) ciphertext_loaded = ctx.load_ciphertext(file_obj) else: s = ciphertext.dumps() ciphertext_loaded = ctx.load_ciphertext(s) assert ciphertext_loaded == ciphertext
def test_rngs(rng_cls): size = 32 bits1 = [random.choice([False, True]) for i in range(size)] bits2 = [random.choice([False, True]) for i in range(size)] reference = [not (b1 and b2) for b1, b2 in zip(bits1, bits2)] rng = rng_cls() ctx = nufhe.Context(rng=rng) secret_key, cloud_key = ctx.make_key_pair() ciphertext1 = ctx.encrypt(secret_key, bits1) ciphertext2 = ctx.encrypt(secret_key, bits2) vm = ctx.make_virtual_machine(cloud_key) result = vm.gate_nand(ciphertext1, ciphertext2) result_bits = ctx.decrypt(secret_key, result) assert all(result_bits == reference)
def worker(device_id, cloud_key_cpu, ciphertext1_cpu, ciphertext2_cpu): """ The thread worker function. Runs a NAND gate over two provided ciphertexts and returns the serialized result. """ print("Running a thread with", device_id) ctx = nufhe.Context(device_id=device_id) cloud_key = ctx.load_cloud_key(cloud_key_cpu) ciphertext1 = ctx.load_ciphertext(ciphertext1_cpu) ciphertext2 = ctx.load_ciphertext(ciphertext2_cpu) vm = ctx.make_virtual_machine(cloud_key) result = vm.gate_nand(ciphertext1, ciphertext2) result_cpu = result.dumps() print("Done") return result_cpu
def test(): f = open("rust-example.wat", "r") compute_wasm = f.read() ctx = nufhe.Context() secret_key, cloud_key = ctx.make_key_pair() vm = ctx.make_virtual_machine(cloud_key) bits1 = [ 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ] ciphertext1 = ctx.encrypt(secret_key, bits1) result = perform_computation(vm, compute_wasm, ciphertext1) result_bits = list( [1 if ctx.decrypt(secret_key, a)[0] else 0 for a in result]) print(result_bits) sum = 0 for b in result_bits: sum = (sum << 1) | b assert sum == 5 # test()
def main(api, device_id, ## Mode bulletin_board, crypto_provider, ## Web uri, port): devices = nufhe.find_devices(api=api) ctx = nufhe.Context(device_id=devices[device_id]) if bulletin_board: board = BulletinBoard(size=5, provider_uri=uri, ctx=ctx) web_controller = board.make_web_controller() if crypto_provider: provider = CryptoProvider(ctx) web_controller = provider.make_web_controller() web_controller.run(port=port)
def main(): context = nufhe.Context() secret_key, cloud_key = context.make_key_pair() vm = context.make_virtual_machine(cloud_key) file_name = "dataset_chess.txt" min_supp = 0.8 running_times = 5 trans_nums = [i for i in range(100, 1100, 100)] items_nums = [i for i in range(10, 110, 10)] for m in trans_nums: n = 20 k = math.floor(math.log2(m)) + 1 print("Trans Number:", m, ", Items Number:", n) data_matrix = user.load_data(file_name, n, m) ctxt_data_matrix = user.encrypt_data(data_matrix, context, secret_key) ctxt_min_supp_count = user.ctxt_min_supp_count(min_supp, m, k, context, secret_key) query = miner.plaintext_query(n) ctxt_query = miner.ciphertext_query(n, context, secret_key) time_records_p1 = [] time_records_p2 = [] for i in range(running_times): start = time.time() cloud.freq_itemset_mining(ctxt_data_matrix, ctxt_min_supp_count, True, ctxt_query, vm) finish = time.time() time_records_p1.append(finish-start) start = time.time() cloud.freq_itemset_mining(ctxt_data_matrix, ctxt_min_supp_count, False, query, vm) finish = time.time() time_records_p2.append(finish-start) print("Protocol 1 time used: ", time_records_p1, "average time:", sum(time_records_p1)/len(time_records_p1), "seconds") print("Protocol 2 time used: ", time_records_p2, "average time:", sum(time_records_p2)/len(time_records_p2), "seconds\n") for n in items_nums: m = 1000 k = math.floor(math.log2(m)) + 1 print("Trans Number:", m, ", Items Number:", n) data_matrix = user.load_data(file_name, n, m) ctxt_data_matrix = user.encrypt_data(data_matrix, context, secret_key) ctxt_min_supp_count = user.ctxt_min_supp_count(min_supp, m, k, context, secret_key) query = miner.plaintext_query(n) ctxt_query = miner.ciphertext_query(n, context, secret_key) time_records_p1 = [] time_records_p2 = [] for i in range(running_times): start = time.time() cloud.freq_itemset_mining(ctxt_data_matrix, ctxt_min_supp_count, True, ctxt_query, vm) finish = time.time() time_records_p1.append(finish-start) start = time.time() cloud.freq_itemset_mining(ctxt_data_matrix, ctxt_min_supp_count, False, query, vm) finish = time.time() time_records_p2.append(finish-start) print("Protocol 1 time used: ", time_records_p1, "average time:", sum(time_records_p1)/len(time_records_p1), "seconds") print("Protocol 2 time used: ", time_records_p2, "average time:", sum(time_records_p2)/len(time_records_p2), "seconds\n")
import nufhe context = nufhe.Context() result_dump = open('encrypted_compute', 'rb').read() secret_key_dump = open('secret_key', 'rb').read() results = context.load_ciphertext(result_dump) secret_key = context.load_secret_key(secret_key_dump) result_bits = context.decrypt(secret_key, results) passed = not any(result_bits) print(passed)
import random import nufhe import time # thr = any_api().Thread.create(interactive=True) ctx = nufhe.Context() secret_key, cloud_key = ctx.make_key_pair() vm = ctx.make_virtual_machine(cloud_key) def addBits(r, a, b, carry): # Xor(t1[0], a, carry[0]) t1 = vm.gate_xor(a, carry) # Xor(t2[0], b, carry[0]) t2 = vm.gate_xor(b, carry) # Xor(r[0], a, t2[0]) r[0] = vm.gate_xor(a, t2) # And(t1[0], t1[0], t2[0]) t1 = vm.gate_and(t1, t2) # Xor(r[1], carry[0], t1[0]) r[1] = vm.gate_xor(carry, t1) return r def addNumbers(ctA, ctB, nBits): ctRes = [[vm.empty_ciphertext((1, ))] for i in range(nBits)]
def generate_context(self): return nufhe.Context()
from phe import paillier import nufhe import numpy as np import requests import json import pickle import base64 from utils import EncUInt8 size = 5 with open('phe_keys.pickle', 'rb') as f: public_key, private_key = pickle.load(f) devices = nufhe.find_devices(api="OpenCL") ctx = nufhe.Context(device_id=devices[1]) with open('fhe_keys.pickle', 'rb') as f: secret_key_bytes, cloud_key_bytes = pickle.load(f) secret_key = ctx.load_secret_key(secret_key_bytes) cloud_key = ctx.load_cloud_key(cloud_key_bytes) vm = ctx.make_virtual_machine(cloud_key) URL = 'http://127.0.0.1:5001' headers = {'content-type': 'application/json'} ## publish rating values = np.random.randint(1, 5, size=size, dtype=np.uint8) values = [int(x) for x in values] print(values)