edits
[iotcloud.git] / PyORAM / examples / path_oram_sftp.py
1 #
2 # This example measures the performance of Path ORAM when
3 # storage is accessed through an SSH client using the Secure
4 # File Transfer Protocol (SFTP).
5 #
6 # In order to run this example, you must provide a host
7 # (server) address along with valid login credentials
8 #
9
10 import os
11 import random
12 import time
13
14 import pyoram
15 from pyoram.util.misc import MemorySize
16 from pyoram.oblivious_storage.tree.path_oram import \
17     PathORAM
18
19 import paramiko
20 import tqdm
21
22 pyoram.config.SHOW_PROGRESS_BAR = True
23
24 # Set SSH login credentials here
25 # (by default, we pull these from the environment
26 # for testing purposes)
27 ssh_host = os.environ.get('PYORAM_SSH_TEST_HOST')
28 ssh_username = os.environ.get('PYORAM_SSH_TEST_USERNAME')
29 ssh_password = os.environ.get('PYORAM_SSH_TEST_PASSWORD')
30
31 # Set the storage location and size
32 storage_name = "heap.bin"
33 # 4KB block size
34 block_size = 4000
35 # one block per bucket in the
36 # storage heap of height 8
37 block_count = 2**(8+1)-1
38
39 def main():
40
41     print("Storage Name: %s" % (storage_name))
42     print("Block Count: %s" % (block_count))
43     print("Block Size: %s" % (MemorySize(block_size)))
44     print("Total Memory: %s"
45           % (MemorySize(block_size*block_count)))
46     print("Actual Storage Required: %s"
47           % (MemorySize(
48               PathORAM.compute_storage_size(
49                   block_size,
50                   block_count,
51                   storage_type='sftp'))))
52     print("")
53
54     # Start an SSH client using paramiko
55     print("Starting SSH Client")
56     with paramiko.SSHClient() as ssh:
57         ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
58         ssh.load_system_host_keys()
59         ssh.connect(ssh_host,
60                     username=ssh_username,
61                     password=ssh_password)
62
63         print("Setting Up Path ORAM Storage")
64         setup_start = time.time()
65         with PathORAM.setup(storage_name,
66                             block_size,
67                             block_count,
68                             storage_type='sftp',
69                             sshclient=ssh,
70                             ignore_existing=True) as f:
71             print("Total Setup Time: %.2f s"
72                   % (time.time()-setup_start))
73             print("Current Stash Size: %s"
74                   % len(f.stash))
75             print("Total Data Transmission: %s"
76                   % (MemorySize(f.bytes_sent + f.bytes_received)))
77             print("")
78
79         # We close the device and reopen it after
80         # setup to reset the bytes sent and bytes
81         # received stats.
82         with PathORAM(storage_name,
83                       f.stash,
84                       f.position_map,
85                       key=f.key,
86                       storage_type='sftp',
87                       sshclient=ssh) as f:
88
89             test_count = 100
90             start_time = time.time()
91             for t in tqdm.tqdm(list(range(test_count)),
92                                desc="Running I/O Performance Test"):
93                 f.read_block(random.randint(0,f.block_count-1))
94             stop_time = time.time()
95             print("Current Stash Size: %s"
96                   % len(f.stash))
97             print("Access Block Avg. Data Transmitted: %s (%.3fx)"
98                   % (MemorySize((f.bytes_sent + f.bytes_received)/float(test_count)),
99                      (f.bytes_sent + f.bytes_received)/float(test_count)/float(block_size)))
100             print("Fetch Block Avg. Latency: %.2f ms"
101                   % ((stop_time-start_time)/float(test_count)*1000))
102             print("")
103
104 if __name__ == "__main__":
105     main()                                             # pragma: no cover