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.

240 lines
5.3 KiB

  1. package main
  2. import (
  3. "fmt"
  4. "math/big"
  5. "syscall/js"
  6. "github.com/arnaucube/go-blindsecp256k1"
  7. blindsecp256k1v0 "github.com/arnaucube/go-blindsecp256k1/v0"
  8. )
  9. func main() {
  10. c := make(chan struct{}, 0)
  11. println("WASM blindsecp256k1 initialized")
  12. registerCallbacks()
  13. <-c
  14. }
  15. func registerCallbacks() {
  16. js.Global().Set("wasmReady", js.FuncOf(ready))
  17. // blindv0 & unblindv0 uses:
  18. // http://www.isecure-journal.com/article_39171_47f9ec605dd3918c2793565ec21fcd7a.pdf
  19. js.Global().Set("wasmBlindv0", js.FuncOf(blindv0))
  20. js.Global().Set("wasmUnblindv0", js.FuncOf(unblindv0))
  21. js.Global().Set("wasmVerifyv0", js.FuncOf(verifyv0))
  22. // blind & unblind uses: https://sci-hub.do/10.1109/ICCKE.2013.6682844
  23. js.Global().Set("wasmBlind", js.FuncOf(blind))
  24. js.Global().Set("wasmUnblind", js.FuncOf(unblind))
  25. js.Global().Set("wasmVerify", js.FuncOf(verify))
  26. }
  27. func stringToBigInt(s string) *big.Int {
  28. b, ok := new(big.Int).SetString(s, 10)
  29. if !ok {
  30. panic(fmt.Errorf("error parsing string *big.Int: %s", s))
  31. }
  32. return b
  33. }
  34. func ready(this js.Value, values []js.Value) interface{} {
  35. return "ready"
  36. }
  37. func blindv0(this js.Value, values []js.Value) interface{} {
  38. mStr := values[0].String()
  39. signerQxStr := values[1].String()
  40. signerQyStr := values[2].String()
  41. signerRxStr := values[3].String()
  42. signerRyStr := values[4].String()
  43. m := stringToBigInt(mStr)
  44. signerQx := stringToBigInt(signerQxStr)
  45. signerQy := stringToBigInt(signerQyStr)
  46. signerRx := stringToBigInt(signerRxStr)
  47. signerRy := stringToBigInt(signerRyStr)
  48. signerQ := &blindsecp256k1.PublicKey{
  49. X: signerQx,
  50. Y: signerQy,
  51. }
  52. signerR := &blindsecp256k1.Point{
  53. X: signerRx,
  54. Y: signerRy,
  55. }
  56. mBlinded, user := blindsecp256k1v0.Blind(m, signerQ, signerR)
  57. r := make(map[string]interface{})
  58. r["mBlinded"] = mBlinded.String()
  59. r["uA"] = user.A.String()
  60. r["uB"] = user.B.String()
  61. r["uC"] = user.C.String()
  62. r["uC"] = user.C.String()
  63. r["uFx"] = user.F.X.String()
  64. r["uFy"] = user.F.Y.String()
  65. return r
  66. }
  67. func unblindv0(this js.Value, values []js.Value) interface{} {
  68. sBlindStr := values[0].String()
  69. mStr := values[1].String()
  70. uBStr := values[2].String()
  71. uCStr := values[3].String()
  72. uFxStr := values[4].String()
  73. uFyStr := values[5].String()
  74. sBlind := stringToBigInt(sBlindStr)
  75. m := stringToBigInt(mStr)
  76. uB := stringToBigInt(uBStr)
  77. uC := stringToBigInt(uCStr)
  78. uFx := stringToBigInt(uFxStr)
  79. uFy := stringToBigInt(uFyStr)
  80. uF := &blindsecp256k1.Point{
  81. X: uFx,
  82. Y: uFy,
  83. }
  84. u := &blindsecp256k1v0.UserSecretData{
  85. // A not needed to Unblind
  86. B: uB,
  87. C: uC,
  88. F: uF,
  89. }
  90. sig := blindsecp256k1v0.Unblind(sBlind, m, u)
  91. r := make(map[string]interface{})
  92. r["s"] = sig.S.String()
  93. r["fx"] = sig.F.X.String()
  94. r["fy"] = sig.F.Y.String()
  95. return r
  96. }
  97. func verifyv0(this js.Value, values []js.Value) interface{} {
  98. mStr := values[0].String()
  99. sigSStr := values[1].String()
  100. sigFxStr := values[2].String()
  101. sigFyStr := values[3].String()
  102. qxStr := values[4].String()
  103. qyStr := values[5].String()
  104. m := stringToBigInt(mStr)
  105. sigS := stringToBigInt(sigSStr)
  106. sigFx := stringToBigInt(sigFxStr)
  107. sigFy := stringToBigInt(sigFyStr)
  108. qx := stringToBigInt(qxStr)
  109. qy := stringToBigInt(qyStr)
  110. q := &blindsecp256k1.PublicKey{
  111. X: qx,
  112. Y: qy,
  113. }
  114. sig := &blindsecp256k1.Signature{
  115. S: sigS,
  116. F: &blindsecp256k1.Point{
  117. X: sigFx,
  118. Y: sigFy,
  119. },
  120. }
  121. verified := blindsecp256k1.Verify(m, sig, q)
  122. r := verified
  123. return r
  124. }
  125. func blind(this js.Value, values []js.Value) interface{} {
  126. mStr := values[0].String()
  127. signerRxStr := values[1].String()
  128. signerRyStr := values[2].String()
  129. m := stringToBigInt(mStr)
  130. signerRx := stringToBigInt(signerRxStr)
  131. signerRy := stringToBigInt(signerRyStr)
  132. signerR := &blindsecp256k1.Point{
  133. X: signerRx,
  134. Y: signerRy,
  135. }
  136. mBlinded, user := blindsecp256k1.Blind(m, signerR)
  137. r := make(map[string]interface{})
  138. r["mBlinded"] = mBlinded.String()
  139. r["uA"] = user.A.String()
  140. r["uB"] = user.B.String()
  141. r["uFx"] = user.F.X.String()
  142. r["uFy"] = user.F.Y.String()
  143. return r
  144. }
  145. func unblind(this js.Value, values []js.Value) interface{} {
  146. sBlindStr := values[0].String()
  147. mStr := values[1].String()
  148. uAStr := values[2].String()
  149. uBStr := values[3].String()
  150. uFxStr := values[4].String()
  151. uFyStr := values[5].String()
  152. sBlind := stringToBigInt(sBlindStr)
  153. m := stringToBigInt(mStr)
  154. uA := stringToBigInt(uAStr)
  155. uB := stringToBigInt(uBStr)
  156. uFx := stringToBigInt(uFxStr)
  157. uFy := stringToBigInt(uFyStr)
  158. uF := &blindsecp256k1.Point{
  159. X: uFx,
  160. Y: uFy,
  161. }
  162. u := &blindsecp256k1.UserSecretData{
  163. A: uA,
  164. B: uB,
  165. F: uF,
  166. }
  167. sig := blindsecp256k1.Unblind(sBlind, m, u)
  168. r := make(map[string]interface{})
  169. r["s"] = sig.S.String()
  170. r["fx"] = sig.F.X.String()
  171. r["fy"] = sig.F.Y.String()
  172. return r
  173. }
  174. func verify(this js.Value, values []js.Value) interface{} {
  175. mStr := values[0].String()
  176. sigSStr := values[1].String()
  177. sigFxStr := values[2].String()
  178. sigFyStr := values[3].String()
  179. qxStr := values[4].String()
  180. qyStr := values[5].String()
  181. m := stringToBigInt(mStr)
  182. sigS := stringToBigInt(sigSStr)
  183. sigFx := stringToBigInt(sigFxStr)
  184. sigFy := stringToBigInt(sigFyStr)
  185. qx := stringToBigInt(qxStr)
  186. qy := stringToBigInt(qyStr)
  187. q := &blindsecp256k1.PublicKey{
  188. X: qx,
  189. Y: qy,
  190. }
  191. sig := &blindsecp256k1.Signature{
  192. S: sigS,
  193. F: &blindsecp256k1.Point{
  194. X: sigFx,
  195. Y: sigFy,
  196. },
  197. }
  198. verified := blindsecp256k1.Verify(m, sig, q)
  199. r := verified
  200. return r
  201. }