def test_big_ranges(): in_range = ValueRange(2**32, 2**33) out_range = ValueRange(2**48, 2**49) ope = OPE(b'test-big-ranges', in_range, out_range) plaintext = in_range.start while plaintext <= in_range.end: assert ope.encrypt(plaintext) plaintext += 2**24
def test_encrypt_small_out_range_issue(): """Regression test for this issue: https://github.com/tonyo/pyope/issues/13""" cipher = OPE(b'fresh key', in_range=ValueRange(0, 2), out_range=ValueRange(2, 5)) assert cipher.encrypt(0) assert cipher.encrypt(1) assert cipher.encrypt(2)
def test_dense_range(): """Equal ranges must yield 1-to-1 mapping""" range_start = 0 range_end = 2**15 in_range = ValueRange(range_start, range_end) out_range = ValueRange(range_start, range_end) key = b'123' cipher = OPE(key, in_range, out_range) values = [0, 10, 20, 50, 100, 1000, 2**10, 2**15] for v in values: assert cipher.encrypt(v) == v assert cipher.decrypt(v) == v with pytest.raises(Exception): OPE(key, ValueRange(0, 10), ValueRange(1, 2))
def test_ope_encrypt_decrypt(): """Encrypt and then decrypt""" values = [-1000, -100, -20, -1, 0, 1, 10, 100, 314, 1337, 1338, 10000] key = b'key' in_range = ValueRange(-1000, 2**20) out_range = ValueRange(-10000, 2**32) # Client encrypts values cipher = OPE(key, in_range, out_range) encrypted_values = [cipher.encrypt(value) for value in values] # Decryption at the peer side cipher_dec = OPE(key, in_range, out_range) for value, encrypted in zip(values, encrypted_values): decrypted = cipher_dec.decrypt(encrypted) assert value == decrypted, "Dec(Enc(P)) != P"
def test_range_simple(): start = 2 end = 1000 r = ValueRange(start, end) assert r.size() == 999 for i in range(start, end + 1): assert r.contains(i) assert not r.contains(start - 1) assert not r.contains(end + 1) assert r.range_bit_size() == 10
def test_uniform(): # Short ranges value = 10 unit_range = ValueRange(value, value) assert sample_uniform(unit_range, []) == value short_range = ValueRange(value, value + 1) assert sample_uniform(short_range, [0]) == value assert sample_uniform(short_range, [1]) == value + 1 assert sample_uniform(short_range, [0, 0, 1, 0, 'llama']) == value, "More bits yield no effect" with pytest.raises(Exception): sample_uniform(short_range, []) # Medium ranges start_range = 20 end_range = start_range + 15 range1 = ValueRange(start_range, start_range + 15) assert sample_uniform(range1, [0, 0, 0, 0]) == start_range assert sample_uniform(range1, [0, 0, 0, 1]) == start_range + 1 assert sample_uniform(range1, [1, 1, 1, 1]) == end_range # Test with a generator object assert sample_uniform(range1, itertools.repeat(0, 10)) == start_range # Negative range start_range = -32 end_range = -17 range = ValueRange(start_range, end_range) assert sample_uniform(range, [0] * 5) == start_range assert sample_uniform(range, [1] * 5) == end_range # Mixed range start_range = -32 end_range = 31 range = ValueRange(start_range, end_range) assert sample_uniform(range, [0] * 6) == start_range assert sample_uniform(range, [1] * 6) == end_range
import socket import sys from pyope.ope import OPE from pyope.ope import ValueRange # Create a TCP/IP socket sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Bind the socket to the port server_address = ('localhost', int(sys.argv[1])) print >> sys.stderr, 'starting up on %s port %s' % server_address sock.bind(server_address) # store key key = str(sys.argv[2]) inrange = ValueRange(0, 2**34 - 1) outrange = ValueRange(0, 2**50 - 1) cipher = OPE(key, inrange, outrange) # Listen for incoming connections sock.listen(1) while True: # Wait for a connection print >> sys.stderr, 'waiting for a connection' connection, client_address = sock.accept() try: print 'got a connection' # Receive the data in small chunks and retransmit it while True:
def test_huge_output_range(): """Regression test for https://github.com/tonyo/pyope/pull/16""" cipher = OPE(b'key11', in_range=ValueRange(0, 0), out_range=ValueRange(0, 2**65)) assert cipher.encrypt(0)
def instantiate_ope_cipher(key): return OPE(key, in_range=ValueRange(-100000000000, 100000000000), out_range=ValueRange(-214748364800, 214748364700))
import sys from pyope.ope import OPE, ValueRange cipher = OPE(b'key goes here' * 2, in_range=ValueRange(-2**31, 2**31-1), out_range=ValueRange(-2**53, 2**53-1)) input = list(sys.argv[1]) for c in input: print cipher.encrypt(ord(c))
def test_range_repr(): a = ValueRange(1, 10) assert eval(repr(a)) == a
# -*- coding: utf-8 -*- import sys from pyope.ope import OPE, ValueRange cipher = OPE(b'key goes here' * 2, in_range=ValueRange(-2**31, 2**31 - 1), out_range=ValueRange(-2**53, 2**53 - 1)) input = sys.argv[1] if " " in input: for i in input.split(): print cipher.decrypt(int(i)) print 0 else: print cipher.decrypt(int(input))
def test_invalid_range_ends(self, start, end): with pytest.raises(InvalidRangeLimitsError): ValueRange(start, end)