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.

239 lines
5.2 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. uBStr := values[1].String()
  70. uCStr := values[2].String()
  71. uFxStr := values[3].String()
  72. uFyStr := values[4].String()
  73. sBlind := stringToBigInt(sBlindStr)
  74. uB := stringToBigInt(uBStr)
  75. uC := stringToBigInt(uCStr)
  76. uFx := stringToBigInt(uFxStr)
  77. uFy := stringToBigInt(uFyStr)
  78. uF := &blindsecp256k1.Point{
  79. X: uFx,
  80. Y: uFy,
  81. }
  82. u := &blindsecp256k1v0.UserSecretData{
  83. // A not needed to Unblind
  84. B: uB,
  85. C: uC,
  86. F: uF,
  87. }
  88. sig := blindsecp256k1v0.Unblind(sBlind, u)
  89. r := make(map[string]interface{})
  90. r["s"] = sig.S.String()
  91. r["fx"] = sig.F.X.String()
  92. r["fy"] = sig.F.Y.String()
  93. return r
  94. }
  95. func verifyv0(this js.Value, values []js.Value) interface{} {
  96. mStr := values[0].String()
  97. sigSStr := values[1].String()
  98. sigFxStr := values[2].String()
  99. sigFyStr := values[3].String()
  100. qxStr := values[4].String()
  101. qyStr := values[5].String()
  102. m := stringToBigInt(mStr)
  103. sigS := stringToBigInt(sigSStr)
  104. sigFx := stringToBigInt(sigFxStr)
  105. sigFy := stringToBigInt(sigFyStr)
  106. qx := stringToBigInt(qxStr)
  107. qy := stringToBigInt(qyStr)
  108. q := &blindsecp256k1.PublicKey{
  109. X: qx,
  110. Y: qy,
  111. }
  112. sig := &blindsecp256k1.Signature{
  113. S: sigS,
  114. F: &blindsecp256k1.Point{
  115. X: sigFx,
  116. Y: sigFy,
  117. },
  118. }
  119. verified := blindsecp256k1.Verify(m, sig, q)
  120. r := verified
  121. return r
  122. }
  123. func blind(this js.Value, values []js.Value) interface{} {
  124. mStr := values[0].String()
  125. signerRxStr := values[1].String()
  126. signerRyStr := values[2].String()
  127. m := stringToBigInt(mStr)
  128. signerRx := stringToBigInt(signerRxStr)
  129. signerRy := stringToBigInt(signerRyStr)
  130. signerR := &blindsecp256k1.Point{
  131. X: signerRx,
  132. Y: signerRy,
  133. }
  134. mBlinded, user, err := blindsecp256k1.Blind(m, signerR)
  135. if err != nil {
  136. panic(err)
  137. }
  138. r := make(map[string]interface{})
  139. r["mBlinded"] = mBlinded.String()
  140. r["uA"] = user.A.String()
  141. r["uB"] = user.B.String()
  142. r["uFx"] = user.F.X.String()
  143. r["uFy"] = user.F.Y.String()
  144. return r
  145. }
  146. func unblind(this js.Value, values []js.Value) interface{} {
  147. sBlindStr := values[0].String()
  148. uAStr := values[1].String()
  149. uBStr := values[2].String()
  150. uFxStr := values[3].String()
  151. uFyStr := values[4].String()
  152. sBlind := stringToBigInt(sBlindStr)
  153. uA := stringToBigInt(uAStr)
  154. uB := stringToBigInt(uBStr)
  155. uFx := stringToBigInt(uFxStr)
  156. uFy := stringToBigInt(uFyStr)
  157. uF := &blindsecp256k1.Point{
  158. X: uFx,
  159. Y: uFy,
  160. }
  161. u := &blindsecp256k1.UserSecretData{
  162. A: uA,
  163. B: uB,
  164. F: uF,
  165. }
  166. sig := blindsecp256k1.Unblind(sBlind, u)
  167. r := make(map[string]interface{})
  168. r["s"] = sig.S.String()
  169. r["fx"] = sig.F.X.String()
  170. r["fy"] = sig.F.Y.String()
  171. return r
  172. }
  173. func verify(this js.Value, values []js.Value) interface{} {
  174. mStr := values[0].String()
  175. sigSStr := values[1].String()
  176. sigFxStr := values[2].String()
  177. sigFyStr := values[3].String()
  178. qxStr := values[4].String()
  179. qyStr := values[5].String()
  180. m := stringToBigInt(mStr)
  181. sigS := stringToBigInt(sigSStr)
  182. sigFx := stringToBigInt(sigFxStr)
  183. sigFy := stringToBigInt(sigFyStr)
  184. qx := stringToBigInt(qxStr)
  185. qy := stringToBigInt(qyStr)
  186. q := &blindsecp256k1.PublicKey{
  187. X: qx,
  188. Y: qy,
  189. }
  190. sig := &blindsecp256k1.Signature{
  191. S: sigS,
  192. F: &blindsecp256k1.Point{
  193. X: sigFx,
  194. Y: sigFy,
  195. },
  196. }
  197. verified := blindsecp256k1.Verify(m, sig, q)
  198. r := verified
  199. return r
  200. }