def test_payload_lift_drop_once(self): plaintext_space = 5 public, private = generate_keypair(128, plaintext_space) data = Payload(1337, public, plaintext_space, plaintext_space) encrypted = data.lift_by(1) decrypted = encrypted.drop_by(1, private) self.assertEqual(decrypted.payload, 1337)
def select_block(self, bucket_ids, select_vector): max_onion_layers = max([self.__onions(x) for x in bucket_ids]) max_onion_layers += self.root_plain_space selectors = [] for i in range(len(bucket_ids)): bucket_id = bucket_ids[i] for j in range(self.blocks_per_bucket): assert select_vector[i][j] in [0, 1] if self.is_dummy(bucket_id, j): continue p = Payload(select_vector[i][j], self.public_key, max_onion_layers, max_onion_layers).lift_once() selectors.append(p) selected_chunks = [] for c in range(self.chunks_per_block): payloads = [] for i in range(len(bucket_ids)): bucket_id = bucket_ids[i] onion_layers = self.__onions(bucket_id) for j in range(self.blocks_per_bucket): if self.is_dummy(bucket_id, j): continue data = self.server.buckets[bucket_id].blocks[j].chunks[c] chunk = Payload(data, self.public_key, self.root_plain_space, self.root_plain_space + onion_layers) payloads.append(chunk) decrypted = homomorphic_select(payloads, selectors).get_plaintext( self.private_key).payload selected_chunks.append(decrypted) return copy.deepcopy(selected_chunks)
def test_homomorphic_payload_add(self): pspace = 10 public, private = generate_keypair(128, pspace) a = Payload(12851, public, pspace, pspace).lift_once() b = Payload(21585, public, pspace, pspace).lift_once() c = homomorphic_add(a, b).get_plaintext(private) self.assertEqual(c.payload, 12851 + 21585)
def test_homomorphic_scalar_multiply_zero(self): base_level = 3 onion_level = 4 public, private = generate_keypair(128, onion_level) hidden = Payload(444, public, base_level, base_level).lift_by(onion_level) selector = Payload(0, public, onion_level + base_level, onion_level + base_level).lift_by(1) res = homomorphic_scalar_multiply(hidden, selector) self.assertEqual(res.get_plaintext(private).payload, 0)
def get_addresses(self, target): buckets_, addresses_ = self.server.get_addresses(target) buckets = copy.deepcopy(buckets_) addresses = copy.deepcopy(addresses_) for i in range(len(buckets)): for j in range(self.blocks_per_bucket): if addresses[i][j] < 0: continue p = Payload(addresses[i][j], self.public_key, 1, 2) addresses[i][j] = p.get_plaintext(self.private_key).payload return buckets, addresses
def test_payload_lift_drop_multiple_times(self): pspace = 5 public, private = generate_keypair(128, pspace) for _ in range(4): num = random.randint(0, 100000) data = Payload(num, public, pspace, pspace) self.assertEqual(data.current_space, pspace) self.assertEqual(data.plaintext_space, pspace) by = random.randint(0, 10) encrypted = data.lift_by(by) self.assertEqual(encrypted.current_space, pspace + by) self.assertEqual(encrypted.plaintext_space, pspace) decrypted = encrypted.drop_by(by, private) self.assertEqual(decrypted.current_space, pspace) self.assertEqual(decrypted.plaintext_space, pspace) self.assertEqual(decrypted.payload, num)
def set_addresses(self, buckets, addresses): for i in range(len(buckets)): for j in range(self.blocks_per_bucket): if addresses[i][j] < 0: continue p = Payload(addresses[i][j], self.public_key, 1, 1).lift_once() addresses[i][j] = p.payload self.server.set_addresses(buckets, addresses)
def get_block(self, bucket_id, block_id): block = copy.deepcopy(self.server.buckets[bucket_id].blocks[block_id]) metadata = self.get_metadata(bucket_id, block_id) # decrypt the metadata of this block block.address = metadata[0] block.bucket_leaf_target = metadata[1] block.chunks_per_block = metadata[2] onion_layers = self.__onions(bucket_id) # decrypt all chunks of this block for c in range(block.chunks_per_block): p = Payload(block.chunks[c], self.public_key, self.root_plain_space, self.root_plain_space + onion_layers) block.chunks[c] = p.get_plaintext(self.private_key).payload return block
def test_homomorphic_select(self): base_level = 2 onion_layers = 6 public, private = generate_keypair(128, base_level) max_onion_layers = onion_layers + base_level nums = [6969, 333, 1337, 3512] for i in range(len(nums)): enc = [ Payload(x, public, base_level, base_level).lift_by(onion_layers) for x in nums ] select = [0] * len(nums) select[i] = 1 selector = [] for j in range(len(nums)): selector.append( Payload(select[j], public, max_onion_layers, max_onion_layers).lift_once()) res = homomorphic_select(enc, selector) self.assertEqual(res.get_plaintext(private).payload, nums[i])
def set_block(self, bucket_id, block_id, block_): block = copy.deepcopy(block_) def add_layer(data): return Payload(data, self.public_key, 1, 1).lift_once().payload # encrypt the metadata of this block block.address = add_layer(block.address) block.bucket_leaf_target = add_layer(block.bucket_leaf_target) onion_layers = self.__onions(bucket_id) # encrypt all chunks of this block for c in range(block.chunks_per_block): p = Payload(block.chunks[c], self.public_key, self.root_plain_space, self.root_plain_space).lift_by(onion_layers) block.chunks[c] = p.payload self.server.buckets[bucket_id].blocks[block_id] = block
def add_layer(data): return Payload(data, self.public_key, 1, 1).lift_once().payload
def remove_layer(data): return Payload(data, self.public_key, 1, 2).get_plaintext(self.private_key).payload