macro-test.lisp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. (def! inc (fn* [a] (i+ a 1)))
  2. (def! gensym
  3. (let* [counter (atom 0)]
  4. (fn* []
  5. (symbol (str "G__" (swap! counter inc))))))
  6. (defmacro! zk-square (fn* [var] (
  7. (let* [v1 (gensym)
  8. v2 (gensym)] (
  9. `(alloc ~v1 ~var)
  10. `(def! output (alloc ~v2 (square ~var)))
  11. `(enforce
  12. (scalar::one ~v1)
  13. (scalar::one ~v1)
  14. (scalar::one ~v2)
  15. )
  16. `{ "v2" output }
  17. )
  18. ))
  19. ))
  20. (defmacro! zk-mul (fn* [val1 val2] (
  21. (let* [v1 (gensym)
  22. v2 (gensym)
  23. var (gensym)] (
  24. `(alloc ~v1 ~val1)
  25. `(alloc ~v2 ~val2)
  26. `(def! result (alloc ~var (* ~val1 ~val2)))
  27. `(enforce
  28. (scalar::one ~v1)
  29. (scalar::one ~v2)
  30. (scalar::one ~var)
  31. )
  32. `{ "result" result }
  33. )
  34. ))
  35. ))
  36. ;; -u^2 + v^2 = 1 + du^2v^2
  37. (defmacro! zk-witness (fn* [val1 val2] (
  38. (let* [u2v2 (gensym)] (
  39. ;; i know this is ugly, we need to work better on how to
  40. ;; fetch the result of the function that is by design
  41. ;; a list/vector so we use nth to get the element that
  42. ;; is the hashmap defined inside zk-square with key v2
  43. `(def! u2 (alloc ~u2 (get (nth (nth (zk-square ~val1) 0) 3) "v2")))
  44. `(def! v2 (alloc ~v2 (get (nth (nth (zk-square ~val2) 0) 3) "v2")))
  45. `(alloc ~u2v2 (get (nth (nth (zk-mul u2 v2) 0) 3) "XXXX"))
  46. `(enforce
  47. ((scalar::one::neg ~u2) (scalar::one ~v2))
  48. (scalar::one ~v1)
  49. (scalar::one ~v2)
  50. )
  51. )
  52. ))
  53. ))
  54. (def! param1 (scalar 3))
  55. (def! param2 (scalar 1))
  56. (prove
  57. (
  58. ;; (def! result1 (zk-square param1))
  59. ;; (println 'result1_map (get (nth (nth result1 0) 3) "v2"))
  60. ;; (def! result2 (zk-square param2))
  61. ;; (println 'result_mul (get (last (last (zk-mul param1 param1))) "result"))
  62. )
  63. )