def test_homomorphic_otp(): SputnikParser = Parser('tests/otp.sputnik') proggy = SputnikParser.get_program() sputnik = Sputnik(proggy, None) rng = numpy.random.RandomState() secret_key, bootstrap_key = nufhe.make_key_pair(sputnik.thr, rng, transform_type='NTT') size = 32 plain = numpy.array([ False, False, False, False, False, False, False, False, True, True, True, True, True, True, True, True, False, False, False, False, False, False, False, False, True, True, True, True, True, True, True, True ]) pad = rng.randint(0, 2, size=size).astype(numpy.bool) enc_plain = nufhe.encrypt(sputnik.thr, rng, secret_key, plain) enc_pad = nufhe.encrypt(sputnik.thr, rng, secret_key, pad) enc_otp = sputnik.execute_program(plain=enc_plain, pad=enc_pad, test_key=bootstrap_key) assert plain is not enc_plain assert enc_otp is not enc_plain dec_otp = nufhe.decrypt(sputnik.thr, secret_key, enc_otp) dec_pad = nufhe.decrypt(sputnik.thr, secret_key, enc_pad) assert dec_otp is not plain assert (plain ^ pad).all() == dec_otp.all()
def calc(age): #2 # Setup Sputnik and deploy vyper contract SputnikParser = Parser('contracts/contract.sputnik') proggy = SputnikParser.get_program() sputnik = Sputnik(proggy, None) with open('contracts/hasheater.vy', 'r') as f: contract_code = f.read() #3 # Setup numpy (insecure, but it's a hackathon...) rng = numpy.random.RandomState() #4 # Setup NuFHE secret_key, bootstrap_key = nufhe.make_key_pair(sputnik.thr, rng, transform_type='NTT') size = 32 #5 # Setup our plaintext and pad, then encrypt them mask16 = bin_array(16, 32) mask32 = bin_array(32, 32) mask64 = bin_array(64, 32) mask128 = bin_array(128, 32) enc_mask16 = nufhe.encrypt(sputnik.thr, rng, secret_key, mask16) enc_mask32 = nufhe.encrypt(sputnik.thr, rng, secret_key, mask32) enc_mask64 = nufhe.encrypt(sputnik.thr, rng, secret_key, mask64) enc_mask128 = nufhe.encrypt(sputnik.thr, rng, secret_key, mask128) enc_plain = nufhe.encrypt(sputnik.thr, rng, secret_key, bin_array(age, 32)) # Execute the homomorphic contract contract_state_out, merkle_tree = sputnik.execute_program(plain=enc_plain, mask16=enc_mask16, mask32=enc_mask32, mask64=enc_mask64, mask128=enc_mask128, test_key=bootstrap_key) #7 Show the reference vs the homomorphic contract output # reference = plain + ~pad dec_fhe_ref = nufhe.decrypt(sputnik.thr, secret_key, contract_state_out) allow = True for bit in dec_fhe_ref: if bit: allow = False break print(f"Refect at {age}? {allow}") print(dec_fhe_ref)
def age_restriction(self, payload): print(payload) # return payload['birthdate'] rng = numpy.random.RandomState(42) rngB = numpy.random.RandomState(43) SputnikParser = Parser('contracts/contract.sputnik') proggy = SputnikParser.get_program() sputnik = Sputnik(proggy, None) secret_key, bootstrap_key = nufhe.make_key_pair(sputnik.thr, rng, transform_type='NTT') secret_key_b, bootstrap_key_b = nufhe.make_key_pair( sputnik.thr, rngB, transform_type='NTT') plain = bin_array(payload['birthdate'], SIZE) mask16 = bin_array(payload['constraits'][0], SIZE) mask32 = bin_array(payload['constraits'][1], SIZE) mask64 = bin_array(payload['constraits'][2], SIZE) mask128 = bin_array(payload['constraits'][3], SIZE) expected = bin_array(0, SIZE) enc_expected = nufhe.encrypt(sputnik.thr, rng, secret_key, expected) enc_mask16 = nufhe.encrypt(sputnik.thr, rng, secret_key, mask16) enc_mask32 = nufhe.encrypt(sputnik.thr, rng, secret_key, mask32) enc_mask64 = nufhe.encrypt(sputnik.thr, rng, secret_key, mask64) enc_mask128 = nufhe.encrypt(sputnik.thr, rng, secret_key, mask128) enc_plain = nufhe.encrypt(sputnik.thr, rng, secret_key, plain) contract_state_out, merkle_tree = sputnik.execute_program( plain=enc_plain, mask16=enc_mask16, mask32=enc_mask32, mask64=enc_mask64, mask128=enc_mask128, test_key=bootstrap_key) dec_out = nufhe.decrypt(sputnik.thr, secret_key, contract_state_out) dec_trash = nufhe.decrypt(sputnik.thr, secret_key_b, contract_state_out) enc_trash = nufhe.encrypt(sputnik.thr, secret_key_b, dec_trash) dec_trash_to_orginal = nufhe.decrypt(sputnik.thr, secret_key, enc_trash) print(dec_trash) out = False for i in range(dec_out.shape[0]): if dec_out[i] is plain[i]: out = True break print(dec_out) return dict(out=out)
def encrypt(self, data, base): rng = numpy.random.RandomState(42) SputnikParser = Parser('contracts/contract.sputnik') proggy = SputnikParser.get_program() sputnik = Sputnik(proggy, None) secret_key, bootstrap_key = nufhe.make_key_pair(sputnik.thr, rng, transform_type='NTT') result = dict() flat = flatten(data) for key, value in flat.items(): if isinstance(value, (int, float)): # result[key] = value result[key] = serialize( nufhe.encrypt(sputnik.thr, rng, secret_key, bin_array(value, SIZE))) # print(result['country']) # for key, value in flatten(result).items(): # print(f'type of {key} is {type(value)}') return result
import random import numpy import nufhe from reikna.cluda import any_api 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)] thr = any_api().Thread.create(interactive=True) rng = nufhe.DeterministicRNG() secret_key, cloud_key = nufhe.make_key_pair(thr, rng) ciphertext1 = nufhe.encrypt(thr, rng, secret_key, bits1) ciphertext2 = nufhe.encrypt(thr, rng, secret_key, bits2) result = nufhe.empty_ciphertext(thr, cloud_key.params, ciphertext1.shape) nufhe.gate_nand(thr, cloud_key, result, ciphertext1, ciphertext2) result_bits = nufhe.decrypt(thr, secret_key, result) assert all(result_bits == reference)
import numpy import nufhe from reikna.cluda import any_api thr = any_api().Thread.create(interactive=True) rng = numpy.random.RandomState() private_key, public_key = nufhe.make_key_pair(thr, rng) size = 32 bits1 = rng.randint(0, 2, size=size).astype(numpy.bool) bits2 = rng.randint(0, 2, size=size).astype(numpy.bool) ciphertext1 = nufhe.encrypt(thr, rng, private_key, bits1) ciphertext2 = nufhe.encrypt(thr, rng, private_key, bits2) reference = ~(bits1 * bits2) result = nufhe.empty_ciphertext(thr, public_key.params, ciphertext1.shape) nufhe.gate_nand(thr, public_key, result, ciphertext1, ciphertext2) result_bits = nufhe.decrypt(thr, private_key, result) assert (result_bits == reference).all()
# Setup NuFHE secret_key, bootstrap_key = nufhe.make_key_pair(sputnik.thr, rng, transform_type='NTT') size = 32 #5 # Setup our plaintext and pad, then encrypt them plain = numpy.array([ False, False, False, False, False, False, False, False, True, True, True, True, True, True, True, True, False, False, False, False, False, False, False, False, True, True, True, True, True, True, True, True ]) pad = rng.randint(0, 2, size=size).astype(numpy.bool) enc_plain = nufhe.encrypt(sputnik.thr, rng, secret_key, plain) enc_pad = nufhe.encrypt(sputnik.thr, rng, secret_key, pad) #6 # Execute the homomorphic contract contract_state_out, merkle_tree = sputnik.execute_program( plain=enc_plain, pad=enc_pad, test_key=bootstrap_key) #7 Show the reference vs the homomorphic contract output reference = plain ^ pad dec_fhe_ref = nufhe.decrypt(sputnik.thr, secret_key, contract_state_out) #8 ## Commit the root to the blockchain and print it root = merkle_tree.get_merkle_root() h1 = contract.functions.add(root).transact()