def func(x): x_bits = binary(x, size=self.index_blength) # Take the XOR of the negated index bit and query bit gamma = [precomputed[1-bit][i] for bit, i in zip(x_bits, range(len(x_bits)))] # TODO optimize the AND step # After all, we keep ANDing the same set of bits and the order is not important. return reduce(_AND, gamma)
def test_retrieve(self): public_key, secret_key = generate_pair() index = 2 enc_index = public_key.encrypt(binary(index, size=self.store.index_blength)) enc_data = self.store.retrieve(enc_index, public_key) data = [secret_key.decrypt(bit) for bit in enc_data] assert_true(all(data == self.db[index]))
def test_retrieve(self): public_key, secret_key = generate_pair() index = 2 enc_index = public_key.encrypt( binary(index, size=self.store.index_blength)) enc_data = self.store.retrieve(enc_index, public_key) data = [secret_key.decrypt(bit) for bit in enc_data] assert_true(all(data == self.db[index]))
def func(x): x_bits = binary(x, size=self.index_blength) # Take the XOR of the negated index bit and query bit gamma = [ precomputed[1 - bit][i] for bit, i in zip(x_bits, range(len(x_bits))) ] # TODO optimize the AND step # After all, we keep ANDing the same set of bits and the order is not important. return reduce(_AND, gamma)
def test_set(self): index = 2 new_value = [0, 0, 0, 0, 1, 1, 1, 1] self.store.set(index, new_value) public_key, secret_key = generate_pair() enc_index = public_key.encrypt(binary(index, size=self.store.index_blength)) enc_data = self.store.retrieve(enc_index, public_key) data = [secret_key.decrypt(bit) for bit in enc_data] assert_equals(data, new_value)
def test_set(self): index = 2 new_value = [0, 0, 0, 0, 1, 1, 1, 1] self.store.set(index, new_value) public_key, secret_key = generate_pair() enc_index = public_key.encrypt( binary(index, size=self.store.index_blength)) enc_data = self.store.retrieve(enc_index, public_key) data = [secret_key.decrypt(bit) for bit in enc_data] assert_equals(data, new_value)
def retrieve(self, index): """ Retrieves a value from the Blindstore array. :param index: the index of the value to retrieve. :returns: the value stored at the given index, as a bit array. """ public_key, secret_key = generate_pair() enc_index = public_key.encrypt(binary(index, size=self.index_length), secret_key) data = {'PUBLIC_KEY': str(public_key), 'ENC_INDEX': str(enc_index)} r = requests.post(self.url + 'retrieve', data=data) enc_data = [EncryptedBit(public_key, str(s)) for s in json.loads(r.text)] return [secret_key.decrypt(bit) for bit in enc_data]
def __init__(self, record_size=3, record_count=5, database=None, fill=0): """ Creates a new private store. :param record_size: the size of each record, in bits. :param record_count: the number of records. :param database: numpy matrix of database values. :param fill: value to fill the database with. """ if database is None: array = None if fill == "random": array = [[random.randint(0, 1) for _ in range(record_size)] for _ in range(record_count)] else: array = [[fill] * record_size for _ in range(record_count)] database = np.array(array) self.record_count, self.record_size = database.shape self.database = database self.index_bits = index_bits(self.record_count) # precompute binary representation for index self.binary_index = [binary(x, size=self.index_bits) for x in range(self.record_count)]
def set(): index = int(request.form['INDEX']) data = int.from_bytes(base64.b64decode(request.form['DATA']), 'big') store.set(index, binary(data, store.record_blength)) return '', 200
from common.utils import binary _AND = lambda a, b: a & b # Get a store that has cached: # a) a bin array of the indices # b) encrypted(1) HM_XOR encrypted(1) # c) encrypted(0) HM_XOR encrypted(1) # We need to print and play with the lengths of: # a) encrypted(1) # b) encrypted(0) # c) encrypted(1) HM_XOR encrypted(1) (same for 0) # d) length of indices # e) num of indices pk, _ = generate_pair() index_to_find = binary(32, size=32) index_to_find_enc_bit_array = pk.encrypt(index_to_find) index_in_store = binary(32, size=32) # can be a whatever number. it is just for testing enc_one = pk.encrypt(0) enc_zero = pk.encrypt(1) cached_enc_one_one = enc_zero ^ enc_one cached_enc_zero_one = enc_one ^ enc_one cached_reverse_encryptions = [cached_enc_one_one, cached_enc_zero_one] def gamma(a_hats, index_bit_array): pre_gamma = [] for i, a_hat in enumerate(a_hats): pre_gamma.append( cached_reverse_encryptions[index_bit_array[i]] ^ a_hat )
import os import sys sys.path.append(os.path.dirname(os.path.abspath(__file__)) + "/..") from benchmarks.benchmark import benchmark from scarab import generate_pair from common.utils import binary from server import Store store = Store(record_size=20, record_count=20, fill='random') index = 2 pk, sk = generate_pair() eq = pk.encrypt(binary(index, size=store.index_bits), sk) def func(): list(store.retrieve(eq, pk)) benchmark(func, 10, verbose=True)
import sys import os.path sys.path.append(os.path.dirname(os.path.abspath(__file__)) + "/..") from benchmarks.benchmark import benchmark_precise from scarab import generate_pair from common.utils import binary pk, sk = generate_pair() index = binary(42, size=8) encrypted_one = pk.encrypt(1) def func(): sk.decrypt(encrypted_one) benchmark_precise(func, 10000, verbose=True)
import sys.path import os.path # Import from sibling directory sys.path.append(os.path.dirname(os.path.abspath(__file__)) + "/..") from scarab import generate_pair from benchmark import benchmark from common.utils import binary from server import Store store = Store(record_size=20, record_count=20, fill='random') index = 2 pk, sk = generate_pair() eq = pk.encrypt(binary(index, size=store.index_length), sk) def func(): list(store.retrieve(eq, pk)) benchmark(func, 10, verbose=True)
def func(x): x = binary(x, size=self.index_blength) x = public_key.encrypt(x) x = _gamma(cipher_query, x, cipher_one) return x
return x # TODO: make this parallel gammas = map(func, range(self.record_count)) gammas = np.array(list(gammas)) # TODO: make this parallel return map(lambda x: _R(gammas, self.database[:, x], public_key), range(self.record_blength)) def set(self, idx, value): """ Set a value in the array. :param idx: the unencrypted index to set. :param value: the unencrypted value. """ if len(value) < self.record_blength: padded_value = np.zeros(self.record_blength, dtype=np.int) padded_value[padded_value.size - len(value):] = value else: padded_value = value self.database[idx] = padded_value if __name__ == '__main__': store = Store(record_count=8, record_blength=8) pk, sk = generate_pair() index = 2 enc_data = store.retrieve(pk.encrypt(binary(index, size=store.index_blength)), pk) print([sk.decrypt(bit) for bit in enc_data])
# TODO: make this parallel gammas = map(func, range(self.record_count)) gammas = np.array(list(gammas)) # TODO: make this parallel return map(lambda x: _R(gammas, self.database[:, x], public_key), range(self.record_blength)) def set(self, idx, value): """ Set a value in the array. :param idx: the unencrypted index to set. :param value: the unencrypted value. """ if len(value) < self.record_blength: padded_value = np.zeros(self.record_blength, dtype=np.int) padded_value[padded_value.size - len(value):] = value else: padded_value = value self.database[idx] = padded_value if __name__ == '__main__': store = Store(record_count=8, record_blength=8) pk, sk = generate_pair() index = 2 enc_data = store.retrieve( pk.encrypt(binary(index, size=store.index_blength)), pk) print([sk.decrypt(bit) for bit in enc_data])