Exemplo n.º 1
0
def test_prng_performance_permutation(permutation, state_size):
    print "Testing time to generate 1024 * 1024 bytes... "
    start = timestamp()
    output = _hash_prng(permutation, state_size, 1024 * 1024)
    end = timestamp()
    print("Time required to generate {} bytes: {}".format(
        1024 * 1024, end - start))
Exemplo n.º 2
0
def test_prng_performance_hash(hash_function):
    print "Testing time to generate 1024 * 1024 bytes... "
    start = timestamp()
    output = _hash_prng(hash_function, len(hash_function('\x00')), 1024 * 1024)
    end = timestamp()
    print("Time required to generate {} bytes: {}".format(
        1024 * 1024, end - start))
Exemplo n.º 3
0
def test_compression_performance(hash_function, test_message="\x00" * 2 ** 16):    
    print "Time testing compression function; Compressing {} bytes 10 times... ".format(len(test_message)) 
    values = []
    for round in range(10):
        sys.stdout.write("Round: {}\r".format(round))
        start = timestamp()
        hash_function(test_message)
        end = timestamp()
        values.append(end - start)
    print("Time required to compress {} bytes: {}".format(len(test_message) * 10, sum(values) / 10))
Exemplo n.º 4
0
def test_sign_verify():
    import sys
    test_size = 1024
    print("Testing correctness of signatures...")
    for count in range(1, test_size + 1):
        public, private = generate_keypair()
        r = "unit test"
        signature = sign(private, r)
        assert verify(public, r, signature)

        sys.stdout.write('\b' * 79)
        sys.stdout.flush()
        progress = (100 * (count / float(test_size)), count, test_size)
        sys.stdout.write("{}% ({}/{})".format(*progress))
        sys.stdout.flush()

    print("\nTesting performance of signatures...")
    from timeit import default_timer as timestamp
    before = timestamp()
    for count in range(test_size):
        assert verify(public, r, signature)
    after = timestamp()
    verify_time = after - before

    before = timestamp()
    for count in range(test_size):
        sign(private, r)
    after = timestamp()
    sign_time = after - before

    import parameters
    N = parameters.N
    q_size = parameters.PARAMETERS["q_size"]
    print("sign/verify test complete")
    private_size = q_size * len(private) * N
    compressed_size = q_size * len(private)
    public_size = q_size * len(public)
    sign_size = (q_size * N) + public_size
    messages = [
        "Time taken to produce {} signatures: {} seconds",
        "Time taken to verify  {} signatures: {} seconds",
        "Public key size : {} bits ({} bytes)",
        "Private key size: {} bits ({} bytes) (compressed)",
        "Private key size: {} bits ({} bytes) (uncompressed)",
        "Signature size  : {} bits ({} bytes)"
    ]
    inserts = [(test_size, sign_time), (test_size, verify_time),
               (public_size, public_size / 8),
               (compressed_size, compressed_size / 8),
               (private_size, private_size / 8), (sign_size, sign_size / 8)]
    for message, inserts in zip(messages, inserts):
        print(message.format(*inserts))
Exemplo n.º 5
0
def test_compression_performance(hash_function, test_message="\x00" * 2**16):
    print "Time testing compression function; Compressing {} bytes 10 times... ".format(
        len(test_message))
    values = []
    for round in range(10):
        sys.stdout.write("Round: {}\r".format(round))
        start = timestamp()
        hash_function(test_message)
        end = timestamp()
        values.append(end - start)
    print("Time required to compress {} bytes: {}".format(
        len(test_message) * 10,
        sum(values) / 10))
Exemplo n.º 6
0
def test_encrypt_decrypt2():
    data = "Testing!"
    iv = "\x00" * 16
    key = "\x00" * 16
    work_factor1 = 2
    work_factor2 = 1
    hash_function = lambda data: tunable_hash(data, key, work_factor2)
    start = timestamp()
    ciphertext = encrypt2(data, iv, key, work_factor1, hash_function)
    time_required = timestamp() - start
    print("Testing slow encrypt/fast decrypt: ")
    print( "Plaintext size: {}".format(len(data)))
    print("Ciphertext size: {}".format(len(ciphertext)))    
    print("Time taken to encrypt plaintext:  {}".format(time_required))
    
    start = timestamp()
    plaintext = decrypt2(ciphertext, iv, key, work_factor1, hash_function)
    print "Time taken to decrypt ciphertext: {}".format(timestamp() - start)    
    assert plaintext == data
Exemplo n.º 7
0
def test_encrypt_decrypt2():
    data = "Testing!"
    iv = "\x00" * 16
    key = "\x00" * 16
    work_factor1 = 2
    work_factor2 = 1
    hash_function = lambda data: tunable_hash(data, key, work_factor2)
    start = timestamp()
    ciphertext = encrypt2(data, iv, key, work_factor1, hash_function)
    time_required = timestamp() - start
    print("Testing slow encrypt/fast decrypt: ")
    print("Plaintext size: {}".format(len(data)))
    print("Ciphertext size: {}".format(len(ciphertext)))
    print("Time taken to encrypt plaintext:  {}".format(time_required))

    start = timestamp()
    plaintext = decrypt2(ciphertext, iv, key, work_factor1, hash_function)
    print "Time taken to decrypt ciphertext: {}".format(timestamp() - start)
    assert plaintext == data
Exemplo n.º 8
0
def test_cipher_performance(performance_test_sizes, encrypt_method, key, seed): 
    data_amount = 1024 * 128
    amount_string = "({} B {} KB {} MB)"
    test_message = "Testing time to generate " + amount_string + " in {} byte chunks:"
    rate_message = amount_string + "/s: {}"
    for increment_size in performance_test_sizes:
        print test_message.format(data_amount, data_amount / 1024.0, data_amount / (1024 * 1024.0), increment_size)
        size = (data_amount) / increment_size
        times = []
        
        for round in range(10):
            sys.stdout.write("{}{}%\r".format("=" * (7 * round), 10 * round))
            sys.stdout.flush()
            start = timestamp()  
            for chunk in range(size):                
                encrypt_method("\x00" * increment_size, key, seed)                                    
            end = timestamp()
            times.append(end - start)
        sys.stdout.write("{}100%\r".format("=" * 76))
        print rate_message.format(data_amount, data_amount / 1024.0, data_amount / (1024 * 1024.0), 1 / (sum(times) / float(len(times))))
Exemplo n.º 9
0
def test_encrypt_decrypt():
    message = "Testing!"
    key = "\x00" * 16
    iv = "\x00" * 16    
    work_factor = 2 
    work_factor2 = 1
    hash_function = lambda data: tunable_hash(data, key, work_factor2)
    
    start = timestamp()
    ciphertext = encrypt(message, key, iv, work_factor, hash_function)
    encryption_time = timestamp() - start
    
    print "Testing fast encrypt/slow decrypt: "
    print "Plaintext size: ", len(message)
    print "Ciphertext size: ", len(ciphertext)
    
    start = timestamp()
    plaintext = decrypt(ciphertext, key, iv, work_factor, hash_function)
    decryption_time = timestamp() - start
    assert plaintext == message
    
    print "Encryption time: ", encryption_time
    print "Decryption time: ", decryption_time
Exemplo n.º 10
0
def test_encrypt_decrypt():
    message = "Testing!"
    key = "\x00" * 16
    iv = "\x00" * 16
    work_factor = 2
    work_factor2 = 1
    hash_function = lambda data: tunable_hash(data, key, work_factor2)

    start = timestamp()
    ciphertext = encrypt(message, key, iv, work_factor, hash_function)
    encryption_time = timestamp() - start

    print "Testing fast encrypt/slow decrypt: "
    print "Plaintext size: ", len(message)
    print "Ciphertext size: ", len(ciphertext)

    start = timestamp()
    plaintext = decrypt(ciphertext, key, iv, work_factor, hash_function)
    decryption_time = timestamp() - start
    assert plaintext == message

    print "Encryption time: ", encryption_time
    print "Decryption time: ", decryption_time
Exemplo n.º 11
0
def test_cipher_performance(performance_test_sizes, encrypt_method, key, seed):
    data_amount = 1024 * 128
    amount_string = "({} B {} KB {} MB)"
    test_message = "Testing time to generate " + amount_string + " in {} byte chunks:"
    rate_message = amount_string + "/s: {}"
    for increment_size in performance_test_sizes:
        print test_message.format(data_amount, data_amount / 1024.0,
                                  data_amount / (1024 * 1024.0),
                                  increment_size)
        size = (data_amount) / increment_size
        times = []

        for round in range(10):
            sys.stdout.write("{}{}%\r".format("=" * (7 * round), 10 * round))
            sys.stdout.flush()
            start = timestamp()
            for chunk in range(size):
                encrypt_method("\x00" * increment_size, key, seed)
            end = timestamp()
            times.append(end - start)
        sys.stdout.write("{}100%\r".format("=" * 76))
        print rate_message.format(data_amount, data_amount / 1024.0,
                                  data_amount / (1024 * 1024.0),
                                  1 / (sum(times) / float(len(times))))
Exemplo n.º 12
0
    help=
    'Portion of the largest variance of all features that is added to variances for calculation stability.'
)

args = parser.parse_args()

X = np.load(args.X)
y = np.load(args.y)

X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=args.test_size, random_state=args.random_state)

for run in ['cpu', 'fpga']:
    if run == 'cpu':
        from sklearn.naive_bayes import GaussianNB
    if run == 'fpga':
        from inaccel.sklearn.naive_bayes import GaussianNB

    model = GaussianNB(var_smoothing=args.var_smoothing).fit(X_train, y_train)

    print('---')
    print('Naive Bayes predict on "{}":'.format(run))

    start = timestamp()
    predictions = model.predict(X_test)
    stop = timestamp()

    print('time=%.3f' % (stop - start))

    print('accuracy=%.3f' % accuracy_score(y_test, predictions))
Exemplo n.º 13
0
    # set up experiment
    server = QPAPIServer('127.0.0.1', 8080)
    experiment_category = 'demo'
    experiment_name = 'pricer'
    experiment = QPExperiment(server, experiment_category, experiment_name)
    experiment.get()

    date_time = datetime.now().strftime("%Y/%m/%d:%S")
    ### QP CODE ###
    # start with doing analytic prices
    run_name = 'analytic ' + date_time
    print('running ' + run_name)
    # reading the last message that was sent
    with experiment.new_run(run_name, offset_forwards=False, offset=1) as run:
        # start timer
        start = timestamp()

        # read data in from 1 back
        input = run.get_input()
        input_message = input.read()
        input_message_string = input_message.value.decode('utf-8')
        input_data = json.loads(input_message_string)

        # do the pricing
        bsm_process = read_data_and_get_process(input_data)
        bs_price = analytic_price(bsm_process)

        # stop timer
        end = timestamp()
        time_taken = end - start
Exemplo n.º 14
0
def test_prng_performance_permutation(permutation, state_size):
    print "Testing time to generate 1024 * 1024 bytes... "
    start = timestamp()
    output = _hash_prng(permutation, state_size, 1024 * 1024)
    end = timestamp()
    print("Time required to generate {} bytes: {}".format(1024 * 1024, end - start))
Exemplo n.º 15
0
def test_prng_performance_hash(hash_function):    
    print "Testing time to generate 1024 * 1024 bytes... "
    start = timestamp()
    output = _hash_prng(hash_function, len(hash_function('\x00')), 1024 * 1024)
    end = timestamp()
    print("Time required to generate {} bytes: {}".format(1024 * 1024, end - start))