2 # This example measures the performance of Path ORAM
3 # when storage is accessed through RAM.
11 from pyoram.util.misc import MemorySize
12 from pyoram.oblivious_storage.tree.path_oram import \
14 from pyoram.storage.block_storage_ram import \
19 pyoram.config.SHOW_PROGRESS_BAR = True
21 # Set the storage location and size
22 storage_name = "heap.bin"
25 # one block per bucket in the
26 # storage heap of height 8
27 block_count = 2**(8+1)-1
31 print("Storage Name: %s" % (storage_name))
32 print("Block Count: %s" % (block_count))
33 print("Block Size: %s" % (MemorySize(block_size)))
34 print("Total Memory: %s"
35 % (MemorySize(block_size*block_count)))
36 print("Actual Storage Required: %s"
38 PathORAM.compute_storage_size(
41 storage_type='ram'))))
44 print("Setting Up Path ORAM Storage")
45 setup_start = time.time()
46 with PathORAM.setup(storage_name, # RAM storage ignores this argument
50 ignore_existing=True) as f:
51 print("Total Setup Time: %2.f s"
52 % (time.time()-setup_start))
53 print("Current Stash Size: %s"
55 print("Total Data Transmission: %s"
56 % (MemorySize(f.bytes_sent + f.bytes_received)))
59 # This must be done after closing the file to ensure the lock flag
60 # is set to False in the saved data. The tofile method only exists
62 f.raw_storage.tofile(storage_name)
64 # We close the device and reopen it after
65 # setup to reset the bytes sent and bytes
67 with PathORAM(BlockStorageRAM.fromfile(storage_name),
73 start_time = time.time()
74 for t in tqdm.tqdm(list(range(test_count)),
75 desc="Running I/O Performance Test"):
76 f.read_block(random.randint(0,f.block_count-1))
77 stop_time = time.time()
78 print("Current Stash Size: %s"
80 print("Access Block Avg. Data Transmitted: %s (%.3fx)"
81 % (MemorySize((f.bytes_sent + f.bytes_received)/float(test_count)),
82 (f.bytes_sent + f.bytes_received)/float(test_count)/float(block_size)))
83 print("Access Block Avg. Latency: %.2f ms"
84 % ((stop_time-start_time)/float(test_count)*1000))
87 # cleanup because this is a test example
88 os.remove(storage_name)
90 if __name__ == "__main__":
91 main() # pragma: no cover