main.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. def approx_target_with_div(c, stake, Sigma, k):
  36. sigmas = [(c/Sigma)**i * (L/fact(i)) for i in range(1, k+1)]
  37. return -1*approx_target_in_zk(sigmas, stake)
  38. f = 0.5
  39. x = (1-f)
  40. c = math.log(x)
  41. # let's assume stakeholde having 1% of the stake, 1/100.
  42. # each iteration increases stake by value 1.
  43. TOTAL = 10000
  44. S = []
  45. stake = 0
  46. targets = []
  47. T = []
  48. T_approx_2term = []
  49. T_approx_3term = []
  50. T_approx_5term = []
  51. k=7
  52. START=1
  53. for i in range(TOTAL):
  54. if random.random() >= 0.9:
  55. stake+=1
  56. S+=[(stake, i+1.0)]
  57. col = []
  58. t = target(f, stake/(i+1.0))
  59. col += [t]
  60. for j in range(1,k+1):
  61. t_approx = approx_target_with_div(c, stake, (i+1.0), j)
  62. col += [t_approx]
  63. for j in range(1,k+1):
  64. t_approx = approx_target(c, stake, (i+1.0), j)
  65. col += [t_approx]
  66. targets +=[col]
  67. targets = np.array(targets).T
  68. plt.subplot(4,1,1)
  69. plt.plot(targets[0])
  70. for i in range(START,k+1):
  71. plt.plot(targets[i])
  72. plt.legend(["target"] + ["{} terms".format(i) for i in range(START,k+1)], loc='upper right')
  73. Deltas = []
  74. for j in range(START+1,k+1):
  75. diff = np.array(targets[j])-np.array(targets[j-1])
  76. delta = np.sum(diff)
  77. Deltas += [delta]
  78. print(len(Deltas))
  79. plt.subplot(4,1,2)
  80. Deltas_derivates = np.poly1d(Deltas)
  81. plt.plot(Deltas)
  82. plt.plot(Deltas_derivates.deriv())
  83. plt.legend(["delta", "derivative"], loc='upper right')
  84. plt.subplot(4,1,3)
  85. plt.plot(targets[0])
  86. for i in range(k,2*(k)+1):
  87. plt.plot(targets[i])
  88. plt.legend(["target"] + ["{} terms(with div)".format(i) for i in range(START,k+1)] , loc='upper right')
  89. Deltas = []
  90. for j in range(k+2,2*(k)+1):
  91. diff = np.array(targets[j])-np.array(targets[j-1])
  92. delta = np.sum(diff)
  93. Deltas += [delta]
  94. plt.subplot(4,1,4)
  95. Deltas_derivates = np.poly1d(Deltas)
  96. plt.plot(Deltas)
  97. plt.plot(Deltas_derivates.deriv())
  98. plt.legend(["delta(with div)", "derivative(with div)"], loc='upper right')
  99. plt.savefig("target.png")