Skip to content

Instantly share code, notes, and snippets.

@snitzr
Last active January 29, 2018 08:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save snitzr/fd50032c5a950649e52a4e10237448f4 to your computer and use it in GitHub Desktop.
Save snitzr/fd50032c5a950649e52a4e10237448f4 to your computer and use it in GitHub Desktop.
Python version of Satoshi Nakamoto's blockchain formula originally written in C. Original function: https://bitcoin.org/bitcoin.pdf.
import math
import ast
def attacker_success_probability(q, z):
p = (1 - q)
lamb = (z * (q / p))
some = 1
k = 0
while (k <= z):
poisson = math.exp(-lamb)
i = 1
while (i <= k):
poisson *= (lamb / i)
i += 1
some -= (poisson * (1 - math.pow(q / p, z - k)))
k += 1
attacker_success_probability.some = some
q = input("What is q? ")
q = ast.literal_eval(q)
z = input("What is z? ")
z = ast.literal_eval(z)
attacker_success_probability(q, z)
print('\nq={}\nz={}\tp={}'.format(q, z, attacker_success_probability.some))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment