dht_common.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. from random import SystemRandom
  2. import hashlib
  3. defaultcrypto = SystemRandom()
  4. # For now, just defaulting to SHA2 (not SHA3 due to version issues)
  5. def compute_document_ID(document):
  6. return hashlib.sha256(document).digest()
  7. # The maximum ID possible when using SHA3_256
  8. MAX_ID = 2**256 - 1
  9. # The size of hashes in bytes when using SHA3_256
  10. SIZE_OF_HASH = 16
  11. # "Between" in the circular sense; helps determine what node a document belongs to
  12. def between(test, a, b):
  13. if a <= test and test < b:
  14. return True
  15. if a <= test and b < a:
  16. return True
  17. if b < a and test < b:
  18. return True
  19. return False
  20. # Just generates some random bytes of a given size
  21. def generate_file(size, cryptogen=defaultcrypto):
  22. return (''.join([chr(cryptogen.randrange(128)) for i in range(size)])).encode('utf-8')
  23. # This comes from Backes et al., which states that what you'll actually do OT for is
  24. # an AES key to unlock a specific row from the table (and that key will be 256 bits)
  25. # This is a consequence of the Naor and Pinkas construction Backes et al. recommends
  26. SIZE_OF_OT_VALUE = int(256 / 8)
  27. # signature and key sizes taken from https://github.com/poanetwork/threshold_crypto/, which implements BLS signatures and the extension to threshold signatures by Boldyreva
  28. # extensions of BLS signatures to threshold cases are specifically cited in RCP as how they anticipate their threshold signatures to be implemented
  29. # (as far as I can tell, this signature size applies regardless of threshold/size of message, which is not surprising to me)
  30. SIZE_OF_KEY = 48
  31. SIZE_OF_SIGNATURE = 96
  32. # For now, simulating IPv4
  33. SIZE_OF_IP_ADDRESS = 4
  34. # Assuming a timestamp is 32 bits (could easily be updated for Y2k38 problem)
  35. SIZE_OF_TIMESTAMP = 4
  36. # A variable used in test_harness; shouldn't make much of a difference what it's set to,
  37. # as long as it's a positive integer less than the number of nodes in the test
  38. KNOWN_NODE = 0
  39. # 150 ms; assuming wired but transatlantic, this is a conservative estimate
  40. AVERAGE_RTT_ESTIMATE = 0.15 # in s
  41. # 50 Mb/s; global average bandwidth
  42. AVERAGE_CLIENT_BANDWIDTH_ESTIMATE = 50.0 * 1024 * 1024 / 8 # in B/s
  43. # 150 Mb/s; servers estimated with more bandwith
  44. AVERAGE_SERVER_BANDWIDTH_ESTIMATE = 50.0 * 1024 * 1024 / 8 # in B/s
  45. # ~1 GB/s; approximated from 3 cycles / B to encrypt and a 3 GHz machine
  46. ENCRYPTION_SPEED_ESTIMATE = 1000 * 1000 * 1000 # in B/s (yes, 1000, not 1024, because 1GHz is 1000000000 Hz, not 1024^3 Hz)
  47. ##
  48. # 4 GB/s; approximated from math Ian and Stan did during a meeting for Shamir PIR
  49. # (more precisely, we calculated it as 250ms/GB, but that's the same value of course)
  50. PIR_SPEED_ESTIMATE = 1024 * 1024 * 1024 / 0.25 # in B/s
  51. # Just what we used in the simulations
  52. SIZE_OF_CHUNK = 1024 # in B
  53. # 99% CI for the error bars
  54. Z_STAR = 2.576