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.

101 lines
2.7 KiB

  1. package main
  2. import (
  3. "math/big"
  4. "syscall/js"
  5. "github.com/arnaucube/shamirsecretsharing"
  6. )
  7. func main() {
  8. c := make(chan struct{}, 0)
  9. println("WASM Go Initialized")
  10. // register functions
  11. registerCallbacks()
  12. <-c
  13. }
  14. func registerCallbacks() {
  15. js.Global().Set("createShares", js.FuncOf(createShares))
  16. js.Global().Set("lagrangeInterpolation", js.FuncOf(lagrangeInterpolation))
  17. }
  18. func createShares(this js.Value, i []js.Value) interface{} {
  19. nNeededShares, ok := new(big.Int).SetString(i[0].String(), 10)
  20. if !ok {
  21. println("error parsing parameter in position 0")
  22. }
  23. nShares, ok := new(big.Int).SetString(i[1].String(), 10)
  24. if !ok {
  25. println("error parsing parameter in position 1")
  26. }
  27. p, ok := new(big.Int).SetString(i[2].String(), 10)
  28. if !ok {
  29. println("error parsing parameter in position 2")
  30. }
  31. // bits := 2048
  32. // p, err := rand.Prime(rand.Reader, bits/2) // move this out from wasm, it tooks too much time
  33. // if err != nil {
  34. // println(err.Error())
  35. // }
  36. k, ok := new(big.Int).SetString(i[3].String(), 10)
  37. if !ok {
  38. println("error parsing parameter in position 3")
  39. }
  40. println("nNeededShares", nNeededShares.String())
  41. println("nShares", nShares.String())
  42. println("p", p.String())
  43. println("k (secret)", k.String())
  44. shares, err := shamirsecretsharing.Create(nNeededShares, nShares, p, k)
  45. if err != nil {
  46. println("error generating the shares")
  47. println(err.Error())
  48. }
  49. println("shares", shares)
  50. sharesStr := sharesToString(shares)
  51. println("sharesStr", sharesStr)
  52. return js.ValueOf(sharesStr)
  53. }
  54. func sharesToString(shares [][]*big.Int) []string {
  55. var printString string
  56. var s []string
  57. for i := 0; i < len(shares); i++ {
  58. s = append(s, shares[i][0].String())
  59. s = append(s, shares[i][1].String())
  60. printString = printString + "[" + shares[i][0].String() + ", " + shares[i][1].String() + "]\n"
  61. println(shares[i][0].String())
  62. println(shares[i][1].String())
  63. }
  64. js.Global().Get("document").Call("getElementById", "sharesResult").Set("innerHTML", printString)
  65. return s
  66. }
  67. func lagrangeInterpolation(this js.Value, i []js.Value) interface{} {
  68. p, ok := new(big.Int).SetString(i[0].String(), 10)
  69. if !ok {
  70. println("error parsing parameter in position 0 (p)")
  71. }
  72. // parse the shares array
  73. var shares [][]*big.Int
  74. for n := 1; n < len(i); n = n + 2 {
  75. a, ok := new(big.Int).SetString(i[n].String(), 10)
  76. if !ok {
  77. println("error parsing parameter in position ", n)
  78. }
  79. b, ok := new(big.Int).SetString(i[n+1].String(), 10)
  80. if !ok {
  81. println("error parsing parameter in position ", n+1)
  82. }
  83. var share []*big.Int
  84. share = append(share, a)
  85. share = append(share, b)
  86. shares = append(shares, share)
  87. }
  88. secr := shamirsecretsharing.LagrangeInterpolation(p, shares)
  89. println(secr.String())
  90. return js.ValueOf(secr)
  91. }