def assert_compiles_and_matches_reference(self, prog, inputs=None, config={}): if inputs == None: inputs = { name: [uniform(-2, 2) for _ in range(prog.vec_size)] for name in prog.inputs } config['warn_vec_size'] = 'false' reference = evaluate(prog, inputs) compiler = CKKSCompiler(config=config) compiled_prog, params, signature = compiler.compile(prog) reference_compiled = evaluate(compiled_prog, inputs) ref_mse = valuation_mse(reference, reference_compiled) self.assertTrue(ref_mse < 0.0000000001, f"Mean squared error was {ref_mse}") public_ctx, secret_ctx = generate_keys(params) encInputs = public_ctx.encrypt(inputs, signature) encOutputs = public_ctx.execute(compiled_prog, encInputs) outputs = secret_ctx.decrypt(encOutputs, signature) he_mse = valuation_mse(outputs, reference) self.assertTrue(he_mse < 0.01, f"Mean squared error was {he_mse}") return (compiled_prog, params, signature)
def judge_eva4(my_pick, el_pick, hero_to_use): hero_power = eva.get_hero_power() hero_my_choose = get_in_road_choose(hero_to_use, my_pick) hero_el_choose = get_in_road_choose(hero_to_use, el_pick) tmp = -100 for i in hero_my_choose: l_my1 = my_pick.copy() l_my1.append(i) c_my1 = get_in_road_choose(hero_my_choose, l_my1) for j in hero_el_choose: l_el1 = el_pick.copy() l_el1.append(j) c_el1 = get_in_road_choose(hero_el_choose, l_el1) for l in c_el1: l_el2 = l_el1.copy() l_el2.append(l) for k in c_my1: l_my2 = l_my1.copy() l_my2.append(k) evaluation = eva.evaluate(l_my2) - eva.evaluate(l_el2) if evaluation > tmp: tmp = evaluation hero = i power = 0 for i in list(hero_power[hero].values()): if i > power: power = i return hero, int(power * 100)
def judge_eva1(my_pick, el_pick, hero_to_use): hero_power = eva.get_hero_power() hero_my_choose = get_in_road_choose(hero_to_use, my_pick) tmp = 0 for i in hero_my_choose: l_my1 = my_pick.copy() l_my1.append(i) evaluation = eva.evaluate(l_my1) if evaluation > tmp: tmp = evaluation hero = i power = 0 for i in list(hero_power[hero].values()): if i > power: power = i return hero, int(power * 100)
image_data = [x / 255.0 for x in list(image.getdata())] return {'image': image_data} def write_output_image(outputs, tag): enc_result_image = Image.new('L', (w, h)) enc_result_image.putdata([x * 255.0 for x in outputs['image'][0:h * w]]) enc_result_image.save(f'baboon_{tag}.png', "PNG") if __name__ == "__main__": inputs = read_input_image() for prog in [sobel, harris]: print(f'Compiling {prog.name}') compiler = CKKSCompiler() compiled, params, signature = compiler.compile(prog) public_ctx, secret_ctx = generate_keys(params) enc_inputs = public_ctx.encrypt(inputs, signature) enc_outputs = public_ctx.execute(compiled, enc_inputs) outputs = secret_ctx.decrypt(enc_outputs, signature) write_output_image(outputs, f'{compiled.name}_encrypted') reference = evaluate(compiled, inputs) write_output_image(reference, f'{compiled.name}_reference') print('MSE', valuation_mse(outputs, reference)) print()
from train import train_linear from parseData import parseData, parseData_sliding from eva import evaluate from util import getTrainValidSets, normalize import numpy as np import sys [history, target, factors, trainLen] = parseData_sliding('./data/train.csv') # Get training and validation sets (trainData, testTrain) = getTrainValidSets(history, 1.1) (gndData, testGnd) = getTrainValidSets(target, 1.1) # trainData = history gndData = target # Feature normalization # trainData = normalize(trainData, axis=0) # testTrain = normalize(testTrain, axis=0) # trainData = np.divide(trainData, 2) # testTrain = np.divide(testTrain, 2) beta = train_linear(trainData, gndData, testTrain, testGnd, 5e-9, 1e-10, 1e-6, 500000, 0) print(factors) evaluate('./data/test_X.csv', beta, factors, trainLen, sys.argv[1])
public_ctx = load('poly.sealpublic') inputs = {'x': [i for i in range(signature.vec_size)]} encInputs = public_ctx.encrypt(inputs, signature) save(encInputs, 'poly_inputs.sealvals') ################################################# print('Runtime on server') poly = load('poly.eva') public_ctx = load('poly.sealpublic') encInputs = load('poly_inputs.sealvals') encOutputs = public_ctx.execute(poly, encInputs) save(encOutputs, 'poly_outputs.sealvals') ################################################# print('Back on client') secret_ctx = load('poly.sealsecret') encOutputs = load('poly_outputs.sealvals') outputs = secret_ctx.decrypt(encOutputs, signature) reference = evaluate(poly, inputs) print('Expected', reference) print('Got', outputs) print('MSE', valuation_mse(outputs, reference))