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.

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