Skip to content
Snippets Groups Projects
Commit 60e0d352 authored by Lily Faris's avatar Lily Faris
Browse files

benchmarks and path oram

parent ad1ee8ff
No related branches found
No related tags found
No related merge requests found
Pipeline #617364 failed
import subprocess
import os
import pickle
import torch
import random
import time
import numpy as np
import pandas as pd
from cryptography.fernet import Fernet
# Import functions from model.py
from model import compile_oram, store, retrieve
# Simulate homomorphic encryption (for demonstration purposes)
def homomorphic_encrypt(data):
# Placeholder for homomorphic encryption
return data
def homomorphic_decrypt(data):
# Placeholder for homomorphic decryption
return data
def baseline_store(key, tensor):
"""Baseline storage without ORAM."""
tensor_bytes = pickle.dumps(tensor)
# Simulate storage (e.g., writing to a file or database)
file_name = f"baseline_{key}.bin"
with open(file_name, "wb") as f:
f.write(tensor_bytes)
return file_name
def baseline_retrieve(key):
"""Baseline retrieval without ORAM."""
file_name = f"baseline_{key}.bin"
with open(file_name, "rb") as f:
tensor_bytes = f.read()
return pickle.loads(tensor_bytes)
def homomorphic_store(key, tensor):
"""Store using homomorphic encryption."""
tensor_bytes = pickle.dumps(tensor)
encrypted_data = homomorphic_encrypt(tensor_bytes)
file_name = f"homomorphic_{key}.bin"
# Simulate storage
with open(file_name, "wb") as f:
f.write(encrypted_data)
return file_name
def homomorphic_retrieve(key):
"""Retrieve using homomorphic encryption."""
file_name = f"homomorphic_{key}.bin"
with open(file_name, "rb") as f:
encrypted_data = f.read()
tensor_bytes = homomorphic_decrypt(encrypted_data)
return pickle.loads(tensor_bytes)
def measure_latency_and_memory(store_func, retrieve_func, num_operations=100, is_oram=False):
"""Measures latency and memory overhead for storage and retrieval."""
latencies = []
memory_overhead = []
for _ in range(num_operations):
key = random.randint(0, 9)
tensor = torch.randn(4, 4)
# Measure storage latency
start_time = time.time()
if is_oram:
store_func(key, tensor)
# Estimate memory size for ORAM
memory_size = len(pickle.dumps(tensor))
else:
file_name = store_func(key, tensor)
memory_size = os.path.getsize(file_name)
store_latency = time.time() - start_time
# Measure retrieval latency
start_time = time.time()
retrieved_tensor = retrieve_func(key)
retrieve_latency = time.time() - start_time
latencies.append((store_latency, retrieve_latency))
memory_overhead.append(memory_size)
avg_store_latency = np.mean([lat[0] for lat in latencies])
avg_retrieve_latency = np.mean([lat[1] for lat in latencies])
avg_memory_overhead = np.mean(memory_overhead)
return avg_store_latency, avg_retrieve_latency, avg_memory_overhead
def main():
compile_oram() # Ensure ORAM is compiled
# Measure baseline
baseline_results = measure_latency_and_memory(baseline_store, baseline_retrieve)
# Measure Path ORAM
path_oram_results = measure_latency_and_memory(store, retrieve, is_oram=True)
# Measure Homomorphic Encryption
homomorphic_results = measure_latency_and_memory(homomorphic_store, homomorphic_retrieve)
# Compile results into a DataFrame
results_df = pd.DataFrame({
"Configuration": ["Baseline", "Path ORAM", "Homomorphic Encryption"],
"Avg Store Latency (s)": [baseline_results[0], path_oram_results[0], homomorphic_results[0]],
"Avg Retrieve Latency (s)": [baseline_results[1], path_oram_results[1], homomorphic_results[1]],
"Avg Memory Overhead (bytes)": [baseline_results[2], path_oram_results[2], homomorphic_results[2]]
})
print(results_df)
if __name__ == "__main__":
main()
\ No newline at end of file
import subprocess
import os
import pickle
import torch
import random
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from cryptography.fernet import Fernet
from scipy.stats import entropy
# Import functions from model.py
from model import compile_oram, store, retrieve
def calculate_entropy(data):
"""Calculates Shannon entropy of the data."""
value, counts = np.unique(data, return_counts=True)
return entropy(counts, base=2)
def calculate_kl_divergence(p, q):
"""Calculates KL divergence between two distributions."""
return entropy(p, q, base=2)
def simulate_access_patterns(num_accesses=100):
"""Simulates access patterns and calculates entropy and KL divergence."""
access_counts = np.zeros(10) # Assuming 10 possible keys for simplicity
for _ in range(num_accesses):
key = random.randint(0, 9)
store(key, torch.randn(4, 4)) # Store random data
access_counts[key] += 1
# Normalize access counts to get a probability distribution
access_distribution = access_counts / num_accesses
uniform_distribution = np.ones_like(access_distribution) / len(access_distribution)
shannon_entropy = calculate_entropy(access_distribution)
kl_divergence = calculate_kl_divergence(access_distribution, uniform_distribution)
return shannon_entropy, kl_divergence, access_distribution
def plot_results(shannon_entropy, kl_divergence, access_distribution):
"""Displays the results of the entropy and KL divergence analysis as a table."""
# Creating a DataFrame for tabular representation
metrics_data = {
"Metric": ["Shannon Entropy", "KL Divergence"],
"Value": [shannon_entropy, kl_divergence]
}
df_metrics = pd.DataFrame(metrics_data)
access_data = {
"Key": list(range(len(access_distribution))),
"Probability": access_distribution
}
df_access = pd.DataFrame(access_data)
# Print tables to console
print("\nEntropy and KL Divergence Metrics:")
print(df_metrics.to_string(index=False))
print("\nAccess Distribution:")
print(df_access.to_string(index=False))
# Optionally, display the table using matplotlib for a research-style presentation
fig, ax = plt.subplots(1, 2, figsize=(12, 4))
ax[0].axis('tight')
ax[0].axis('off')
ax[0].table(cellText=df_metrics.values, colLabels=df_metrics.columns, cellLoc='center', loc='center')
ax[1].axis('tight')
ax[1].axis('off')
ax[1].table(cellText=df_access.values, colLabels=df_access.columns, cellLoc='center', loc='center')
plt.suptitle("Research Results: Entropy, KL Divergence, and Access Distribution", fontsize=14, fontweight='bold')
plt.show()
def main():
compile_oram() # Ensure ORAM is compiled
shannon_entropy, kl_divergence, access_distribution = simulate_access_patterns()
print(f"Shannon Entropy: {shannon_entropy}")
print(f"KL Divergence: {kl_divergence}")
plot_results(shannon_entropy, kl_divergence, access_distribution)
if __name__ == "__main__":
main()
\ No newline at end of file
path_oram 0 → 100755
File added
cryptography==41.0.3
torch==2.0.1
langchain-openai==0.0.1
numpy==1.24.3
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment