Skip to content

Commit

Permalink
Add timing deviation stats to encrypt/decrypt tests
Browse files Browse the repository at this point in the history
  • Loading branch information
markqvist committed Feb 25, 2025
1 parent 77c0bee commit 08751a7
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 10 deletions.
3 changes: 2 additions & 1 deletion RNS/Cryptography/Provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
PROVIDER_INTERNAL = 0x01
PROVIDER_PYCA = 0x02

FORCE_INTERNAL = False
PROVIDER = PROVIDER_NONE

pyca_v = None
use_pyca = False

try:
if importlib.util.find_spec('cryptography') != None:
if not FORCE_INTERNAL and importlib.util.find_spec('cryptography') != None:
import cryptography
pyca_v = cryptography.__version__
v = pyca_v.split(".")
Expand Down
56 changes: 47 additions & 9 deletions tests/identity.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ def test_2_encrypt(self):
b = 0
e_t = 0
d_t = 0
e_times = []
d_times = []
for i in range(1, 500):
mlen = i % (RNS.Reticulum.MTU//2) + (RNS.Reticulum.MTU//2)
msg = os.urandom(mlen)
Expand All @@ -169,16 +171,34 @@ def test_2_encrypt(self):

e_start = time.time()
token = id2.encrypt(msg)
e_t += time.time() - e_start
e_now = time.time()
e_t += e_now - e_start
e_times.append(e_now - e_start)

d_start = time.time()
decrypted = id1.decrypt(token)
self.assertEqual(msg, decrypted)
d_t += time.time() - d_start
d_now = time.time()
d_t += d_now - d_start
d_times.append(d_now - d_start)

print("Encrypt chunks < MTU: "+self.size_str(b/e_t, "b")+"ps")
print("Decrypt chunks < MTU: "+self.size_str(b/d_t, "b")+"ps")
print("")
import statistics
e_tmin = min(e_times)*1000; d_tmin = min(d_times)*1000
e_tmax = max(e_times)*1000; d_tmax = max(d_times)*1000
e_tmean = (sum(e_times)/len(e_times))*1000; d_tmean = (sum(d_times)/len(d_times))*1000
e_tmed = statistics.median(e_times)*1000; d_tmed = statistics.median(d_times)*1000
e_tmdev = e_tmax - e_tmin; d_tmdev = d_tmax - d_tmin
e_mpct = (e_tmax/e_tmed)*100; d_mpct = (d_tmax/d_tmed)*100

print(" Encrypt chunks < MTU: "+self.size_str(b/e_t, "b")+"ps")
print(" Encryption timing min/avg/med/max/mdev: "+str(round(e_tmin, 3))+"/"+str(round(e_tmean, 3))+"/"+str(round(e_tmed, 3))+"/"+str(round(e_tmax, 3))+"/"+str(round(e_tmdev, 3)))
print(" Max deviation from median: "+str(round(e_mpct, 1))+"%")
print()

print(" Decrypt chunks < MTU: "+self.size_str(b/d_t, "b")+"ps")
print(" Decryption timing min/avg/med/max/mdev: "+str(round(d_tmin, 3))+"/"+str(round(d_tmean, 3))+"/"+str(round(d_tmed, 3))+"/"+str(round(d_tmax, 3))+"/"+str(round(d_tmdev, 3)))
print(" Max deviation from median: "+str(round(d_mpct, 1))+"%")
print()

# Test encrypt and decrypt of large chunks
print("Testing large chunk encrypt/decrypt")
Expand All @@ -197,14 +217,32 @@ def test_2_encrypt(self):

e_start = time.time()
token = id2.encrypt(msg)
e_t += time.time() - e_start
e_now = time.time()
e_t += e_now - e_start
e_times.append(e_now - e_start)

d_start = time.time()
self.assertEqual(msg, id1.decrypt(token))
d_t += time.time() - d_start
d_now = time.time()
d_t += d_now - d_start
d_times.append(d_now - d_start)

e_tmin = min(e_times)*1000; d_tmin = min(d_times)*1000
e_tmax = max(e_times)*1000; d_tmax = max(d_times)*1000
e_tmean = (sum(e_times)/len(e_times))*1000; d_tmean = (sum(d_times)/len(d_times))*1000
e_tmed = statistics.median(e_times)*1000; d_tmed = statistics.median(d_times)*1000
e_tmdev = e_tmax - e_tmin; d_tmdev = d_tmax - d_tmin
e_mpct = (e_tmax/e_tmed)*100; d_mpct = (d_tmax/d_tmed)*100

print(" Encrypt "+self.size_str(mlen)+" chunks: "+self.size_str(b/e_t, "b")+"ps")
print(" Encryption timing min/avg/med/max/mdev: "+str(round(e_tmin, 3))+"/"+str(round(e_tmean, 3))+"/"+str(round(e_tmed, 3))+"/"+str(round(e_tmax, 3))+"/"+str(round(e_tmdev, 3)))
print(" Max deviation from median: "+str(round(e_mpct, 1))+"%")
print()

print("Encrypt "+self.size_str(mlen)+" chunks: "+self.size_str(b/e_t, "b")+"ps")
print("Decrypt "+self.size_str(mlen)+" chunks: "+self.size_str(b/d_t, "b")+"ps")
print(" Decrypt "+self.size_str(mlen)+" chunks: "+self.size_str(b/d_t, "b")+"ps")
print(" Decryption timing min/avg/med/max/mdev: "+str(round(d_tmin, 3))+"/"+str(round(d_tmean, 3))+"/"+str(round(d_tmed, 3))+"/"+str(round(d_tmax, 3))+"/"+str(round(d_tmdev, 3)))
print(" Max deviation from median: "+str(round(d_mpct, 1))+"%")
print()

def size_str(self, num, suffix='B'):
units = ['','K','M','G','T','P','E','Z']
Expand Down

0 comments on commit 08751a7

Please sign in to comment.