utils.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import random
  2. import math
  3. import numpy as np
  4. from 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 = [sigma*(stake)**(i+1) for i, sigma in enumerate(sigmas)]
  25. return -1*sum(T)
  26. def rnd(hp=False):
  27. return Num(random.random()) if hp else random.random()
  28. def lottery(T, hp=False, log=False):
  29. y = rnd(hp) * (L_HP if hp else L)
  30. if log:
  31. lottery_line = str(y)+","+str(T)+"\n"
  32. with open("/tmp/sim_lottery_history.log", "a+") as f:
  33. f.write(lottery_line)
  34. return y < T