You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

246 lines
6.5 KiB

  1. package main
  2. import (
  3. "encoding/json"
  4. "math/big"
  5. "syscall/js"
  6. "github.com/arnaucube/go-snark-study"
  7. "github.com/arnaucube/go-snark-study/circuitcompiler"
  8. "github.com/arnaucube/go-snark-study/groth16"
  9. "github.com/arnaucube/go-snark-study/utils"
  10. )
  11. func main() {
  12. c := make(chan struct{}, 0)
  13. println("WASM Go Initialized")
  14. registerCallbacks()
  15. <-c
  16. }
  17. func registerCallbacks() {
  18. js.Global().Set("generateProofs", js.FuncOf(generateProofs))
  19. js.Global().Set("verifyProofs", js.FuncOf(verifyProofs))
  20. js.Global().Set("grothGenerateProofs", js.FuncOf(grothGenerateProofs))
  21. js.Global().Set("grothVerifyProofs", js.FuncOf(grothVerifyProofs))
  22. }
  23. func generateProofs(this js.Value, i []js.Value) interface{} {
  24. var circuitStr utils.CircuitString
  25. err := json.Unmarshal([]byte(i[0].String()), &circuitStr)
  26. if err != nil {
  27. println(i[0].String())
  28. println("error parsing circuit from stringified json")
  29. }
  30. circuit, err := utils.CircuitFromString(circuitStr)
  31. if err != nil {
  32. println("error " + err.Error())
  33. }
  34. sj, err := json.Marshal(circuit)
  35. if err != nil {
  36. println("error " + err.Error())
  37. }
  38. println("circuit", string(sj))
  39. var setupStr utils.SetupString
  40. println(i[1].String())
  41. err = json.Unmarshal([]byte(i[1].String()), &setupStr)
  42. if err != nil {
  43. println("error parsing setup from stringified json")
  44. }
  45. setup, err := utils.SetupFromString(setupStr)
  46. if err != nil {
  47. println("error " + err.Error())
  48. }
  49. sj, err = json.Marshal(setup)
  50. if err != nil {
  51. println("error " + err.Error())
  52. }
  53. println("set", string(sj))
  54. var pxStr []string
  55. err = json.Unmarshal([]byte(i[2].String()), &pxStr)
  56. if err != nil {
  57. println("error parsing pxStr from stringified json")
  58. }
  59. px, err := utils.ArrayStringToBigInt(pxStr)
  60. if err != nil {
  61. println(err.Error())
  62. }
  63. sj, err = json.Marshal(px)
  64. if err != nil {
  65. println("error " + err.Error())
  66. }
  67. println("px", string(sj))
  68. var inputs circuitcompiler.Inputs
  69. err = json.Unmarshal([]byte(i[3].String()), &inputs)
  70. if err != nil {
  71. println("error parsing inputs from stringified json")
  72. }
  73. w, err := circuit.CalculateWitness(inputs.Private, inputs.Public)
  74. proof, err := snark.GenerateProofs(circuit, setup.Pk, w, px)
  75. if err != nil {
  76. println("error generating proof", err)
  77. }
  78. proofString := utils.ProofToString(proof)
  79. proofJson, err := json.Marshal(proofString)
  80. if err != nil {
  81. println("error marshal proof to json", err)
  82. }
  83. println("proofJson", string(proofJson))
  84. return js.ValueOf(string(proofJson))
  85. }
  86. func verifyProofs(this js.Value, i []js.Value) interface{} {
  87. var setupStr utils.SetupString
  88. println(i[0].String())
  89. err := json.Unmarshal([]byte(i[0].String()), &setupStr)
  90. if err != nil {
  91. println("error parsing setup from stringified json")
  92. }
  93. setup, err := utils.SetupFromString(setupStr)
  94. if err != nil {
  95. println("error " + err.Error())
  96. }
  97. var proofStr utils.ProofString
  98. err = json.Unmarshal([]byte(i[1].String()), &proofStr)
  99. if err != nil {
  100. println(i[1].String())
  101. println("error parsing proof from stringified json")
  102. }
  103. proof, err := utils.ProofFromString(proofStr)
  104. if err != nil {
  105. println("error " + err.Error())
  106. }
  107. var publicInputs []*big.Int
  108. err = json.Unmarshal([]byte(i[2].String()), &publicInputs)
  109. if err != nil {
  110. println(i[2].String())
  111. println("error parsing publicInputs from stringified json")
  112. }
  113. verified := snark.VerifyProof(setup.Vk, proof, publicInputs, false)
  114. if err != nil {
  115. println("error verifiyng proof", err)
  116. }
  117. verifiedJson, err := json.Marshal(verified)
  118. if err != nil {
  119. println("error marshal verified to json", err)
  120. }
  121. println("verifiedJson", string(verifiedJson))
  122. return js.ValueOf(string(verifiedJson))
  123. }
  124. func grothGenerateProofs(this js.Value, i []js.Value) interface{} {
  125. var circuitStr utils.CircuitString
  126. err := json.Unmarshal([]byte(i[0].String()), &circuitStr)
  127. if err != nil {
  128. println(i[0].String())
  129. println("error parsing circuit from stringified json")
  130. }
  131. circuit, err := utils.CircuitFromString(circuitStr)
  132. if err != nil {
  133. println("error " + err.Error())
  134. }
  135. sj, err := json.Marshal(circuit)
  136. if err != nil {
  137. println("error " + err.Error())
  138. }
  139. println("circuit", string(sj))
  140. var setupStr utils.GrothSetupString
  141. println(i[1].String())
  142. err = json.Unmarshal([]byte(i[1].String()), &setupStr)
  143. if err != nil {
  144. println("error parsing setup from stringified json")
  145. }
  146. setup, err := utils.GrothSetupFromString(setupStr)
  147. if err != nil {
  148. println("error " + err.Error())
  149. }
  150. sj, err = json.Marshal(setup)
  151. if err != nil {
  152. println("error " + err.Error())
  153. }
  154. println("set", string(sj))
  155. var pxStr []string
  156. err = json.Unmarshal([]byte(i[2].String()), &pxStr)
  157. if err != nil {
  158. println("error parsing pxStr from stringified json")
  159. }
  160. px, err := utils.ArrayStringToBigInt(pxStr)
  161. if err != nil {
  162. println(err.Error())
  163. }
  164. sj, err = json.Marshal(px)
  165. if err != nil {
  166. println("error " + err.Error())
  167. }
  168. println("px", string(sj))
  169. var inputs circuitcompiler.Inputs
  170. err = json.Unmarshal([]byte(i[3].String()), &inputs)
  171. if err != nil {
  172. println("error parsing inputs from stringified json")
  173. }
  174. w, err := circuit.CalculateWitness(inputs.Private, inputs.Public)
  175. proof, err := groth16.GenerateProofs(circuit, setup.Pk, w, px)
  176. if err != nil {
  177. println("error generating proof", err)
  178. }
  179. proofString := utils.GrothProofToString(proof)
  180. proofJson, err := json.Marshal(proofString)
  181. if err != nil {
  182. println("error marshal proof to json", err)
  183. }
  184. println("proofJson", string(proofJson))
  185. return js.ValueOf(string(proofJson))
  186. }
  187. func grothVerifyProofs(this js.Value, i []js.Value) interface{} {
  188. var setupStr utils.GrothSetupString
  189. println(i[0].String())
  190. err := json.Unmarshal([]byte(i[0].String()), &setupStr)
  191. if err != nil {
  192. println("error parsing setup from stringified json")
  193. }
  194. setup, err := utils.GrothSetupFromString(setupStr)
  195. if err != nil {
  196. println("error " + err.Error())
  197. }
  198. var proofStr utils.GrothProofString
  199. err = json.Unmarshal([]byte(i[1].String()), &proofStr)
  200. if err != nil {
  201. println(i[1].String())
  202. println("error parsing proof from stringified json")
  203. }
  204. proof, err := utils.GrothProofFromString(proofStr)
  205. if err != nil {
  206. println("error " + err.Error())
  207. }
  208. var publicInputs []*big.Int
  209. err = json.Unmarshal([]byte(i[2].String()), &publicInputs)
  210. if err != nil {
  211. println(i[2].String())
  212. println("error parsing publicInputs from stringified json")
  213. }
  214. verified := groth16.VerifyProof(setup.Vk, proof, publicInputs, false)
  215. if err != nil {
  216. println("error verifiyng proof", err)
  217. }
  218. verifiedJson, err := json.Marshal(verified)
  219. if err != nil {
  220. println("error marshal verified to json", err)
  221. }
  222. println("verifiedJson", string(verifiedJson))
  223. return js.ValueOf(string(verifiedJson))
  224. }