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 test_sputnik():
    SputnikParser = Parser('tests/engine.sputnik')
    proggy = SputnikParser.get_program()

    sputnik = Sputnik(proggy, None)
    sputnik.execute_program(test='yes')
    assert sputnik.program.variables['test'] == 'yes'
    assert sputnik.program.state == 'yes'
    assert sputnik.program.state == sputnik.program.variables['new_var']
示例#3
0
    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)
示例#4
0
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)
示例#5
0
def run(sputnik_filepath, **kwargs):
    """
    Executes the Sputnik program file at the given path.
    """
    click.echo("Executing Sputnik program...")
    # TODO: Bootstrapping key input

    SputnikParser = Parser(sputnik_filepath)
    program = SputnikParser.get_program()

    sputnik_execution_engine = Sputnik(program, None)
    output = sputnik_execution_engine.execute_program(**kwargs)

    click.echo("Execution complete! Final Status:")
    click.echo("Execution killed?  {}".format(program.is_killed))
    click.echo("Execution halted?  {}\n\n".format(program.is_halted))
    click.echo("Final State or State Machine Output:")
    click.echo(output)
示例#6
0
    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
def test_parser_read_testfile():
    SputnikParser = Parser('tests/parser.sputnik')

    assert SputnikParser.raw_data is not ''
    assert len(SputnikParser.lines) == 6
    assert len(SputnikParser.operations) == 3

    # Test Operations lengths
    bootstrap = SputnikParser.operations[0]
    assert len(bootstrap) == 3

    xor = SputnikParser.operations[1]
    assert len(xor) == 5

    end = SputnikParser.operations[2]
    assert len(end) == 1
示例#8
0
#1
import nufhe
import numpy
import web3
import pickle
from web3.auto.gethdev import w3
from binascii import unhexlify
from sputnik.engine import Sputnik
from sputnik.parser import Parser
from vyper import compiler

if __name__ == '__main__':
    #2
    # Setup Sputnik and deploy vyper contract
    SputnikParser = Parser('contracts/otp.sputnik')
    proggy = SputnikParser.get_program()
    sputnik = Sputnik(proggy, None)

    with open('contracts/hasheater.vy', 'r') as f:
        contract_code = f.read()

    contract_bytecode = compiler.compile(contract_code).hex()
    contract_abi = compiler.mk_full_signature(contract_code)
    w3.eth.defaultAccount = w3.eth.accounts[0]

    Contract = w3.eth.contract(abi=contract_abi, bytecode=contract_bytecode)
    tx_hash = Contract.constructor().transact()
    tx_receipt = w3.eth.waitForTransactionReceipt(tx_hash)
    contract = w3.eth.contract(address=tx_receipt.contractAddress,
                               abi=contract_abi)
def test_program():
    SputnikParser = Parser('tests/engine.sputnik')

    proggy = SputnikParser.get_program()
    assert len(proggy.operations) == 4
示例#10
0
def pair(op, boot, enc_left, enc_right):
    SputnikParser = Parser(f'contracts/{op}.sputnik')
    contract_state_out, merkle_tree = sputnik.execute_program(left=enc_left, right=enc_right, test_key=boot)

    return (contract_state_out, merkle_tree)