utils.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import random
  2. import math
  3. import numpy as np
  4. from core.constants import *
  5. # naive factorial
  6. def fact(n, hp=False):
  7. assert (n>0)
  8. n = int(n)
  9. if n==1:
  10. return Num(1) if hp else 1
  11. elif n==2:
  12. return Num(2) if hp else 2
  13. else:
  14. return (Num(n) if hp else n)* fact(n-1, hp)
  15. # all inputs to this function are integers
  16. # sigmas are public
  17. # stake is private
  18. def approx_target_in_zk(sigmas, stake):
  19. # both sigma_1, sigma_2 are constants, if f is a constant.
  20. # if f is constant then sigma_12, sigma_2
  21. # this dictates that tuning need to be hardcoded,
  22. # secondly the reward, or at least the total stake in the network,
  23. # can't be anonymous, should be public.
  24. T = 0
  25. for i, sigma in enumerate(sigmas):
  26. try:
  27. T += Num(sigma)*Num(stake)**(i+1)
  28. except Exception as e:
  29. T +=0
  30. return-1*T
  31. def rnd(hp=False):
  32. return Num(random.random()) if hp else random.random()
  33. def lottery(T, hp=False, log=False):
  34. y = rnd(hp) * (L_HP if hp else L)
  35. if log:
  36. lottery_line = str(y)+","+str(T)+"\n"
  37. with open("/tmp/sim_lottery_history.log", "a+") as f:
  38. f.write(lottery_line)
  39. return y < T if y is not None and T is not None else False