main.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import math
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4. import random
  5. L = 28948022309329048855892746252171976963363056481941560715954676764349967630337
  6. # crypsinous original target function
  7. def target(f, rel_stake):
  8. T = L * (1 - (1-f)**rel_stake)
  9. return T
  10. # naive factorial
  11. def fact(n):
  12. assert (n>0)
  13. n = int(n)
  14. if n==1:
  15. return 1
  16. elif n==2:
  17. return 2
  18. else:
  19. return n * fact(n-1)
  20. # all inputs to this function are integers
  21. # sigmas are public
  22. # stake is private
  23. def approx_target_in_zk(sigmas, stake):
  24. # both sigma_1, sigma_2 are constants, if f is a constant.
  25. # if f is constant then sigma_12, sigma_2
  26. # this dictates that tuning need to be hardcoded,
  27. # secondly the reward, or at least the total stake in the network,
  28. # can't be anonymous, should be public.
  29. T = [sigma*stake**(i+1) for i, sigma in enumerate(sigmas)]
  30. return sum(T)
  31. # approximation of crypsinous targt
  32. def approx_target(c, stake, Sigma, k):
  33. sigmas = [int((c/Sigma)**i * (L/fact(i))) for i in range(1, k+1)]
  34. return -1*approx_target_in_zk(sigmas, stake)
  35. f = 0.5
  36. x = (1-f)
  37. c = math.log(x)
  38. # let's assume stakeholde having 1% of the stake, 1/100.
  39. # each iteration increases stake by value 1.
  40. TOTAL = 10000
  41. S = []
  42. stake = 0
  43. targets = []
  44. T = []
  45. T_approx_2term = []
  46. T_approx_3term = []
  47. T_approx_5term = []
  48. k=7
  49. for i in range(TOTAL):
  50. if random.random() >= 0.9:
  51. stake+=1
  52. S+=[(stake, i+1.0)]
  53. col = []
  54. t = target(f, stake/(i+1.0))
  55. col += [t]
  56. for j in range(1,k+1):
  57. t_approx = approx_target(c, stake, (i+1.0), j)
  58. col += [t_approx]
  59. targets +=[col]
  60. targets = np.array(targets).T
  61. plt.subplot(2,1,1)
  62. plt.plot(targets[0])
  63. START=1
  64. for i in range(START,k+1):
  65. plt.plot(targets[i])
  66. plt.legend(["target"] + ["{} terms".format(i) for i in range(START,k+1)], loc='upper right')
  67. Deltas = [0]
  68. for j in range(START,k+1):
  69. diff = np.array(targets[j])-np.array(targets[j-1])
  70. delta = np.sum(diff)
  71. Deltas += [delta]
  72. plt.subplot(2,1,2)
  73. Deltas_derivates = np.poly1d(Deltas)
  74. plt.plot(Deltas)
  75. plt.plot(Deltas_derivates.deriv())
  76. plt.legend(["delta", "derivative"], loc='upper right')
  77. plt.savefig("target.png")