mimc.psm 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. {% include 'mimc_constants.psm' %}
  2. contract mimc
  3. param left_0
  4. param right
  5. {# Jinja cannot set variables inside loops so use this hack #}
  6. {% set ns = namespace(right="right") %}
  7. {% for i in range(322) %}
  8. # Each round perform these steps:
  9. # xL, xR := xR + (xL + Ci)^3, xL
  10. local mimc_const
  11. load mimc_const mimc_constant_{{i}}
  12. private tmp_{{i}}
  13. set tmp_{{i}} left_{{i}}
  14. add tmp_{{i}} mimc_const
  15. square tmp_{{i}}
  16. lc0_add left_{{i}}
  17. lc0_add_constant mimc_constant_{{i}}
  18. lc1_add left_{{i}}
  19. lc1_add_constant mimc_constant_{{i}}
  20. lc2_add tmp_{{i}}
  21. enforce
  22. # new_xL = xR + (xL + Ci)^3
  23. # new_xL = xR + tmp * (xL + Ci)
  24. # new_xL - xR = tmp * (xL + Ci)
  25. private left_{{i+1}}
  26. set left_{{i+1}} left_{{i}}
  27. add left_{{i+1}} mimc_const
  28. mul left_{{i+1}} tmp_{{i}}
  29. add left_{{i+1}} {{ns.right}}
  30. lc0_add tmp_{{i}}
  31. lc1_add left_{{i}}
  32. lc1_add_constant mimc_constant_{{i}}
  33. lc2_add left_{{i+1}}
  34. lc2_sub {{ns.right}}
  35. enforce
  36. # xR = xL
  37. # right_{{i+1}} = left_{{i}}
  38. {% set ns.right = "left_" + i|string %}
  39. # xL = new_xL
  40. {% endfor %}
  41. public hash_result
  42. set hash_result left_322
  43. lc0_add left_322
  44. lc1_add_one
  45. lc2_add hash_result
  46. enforce
  47. end