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.

102 lines
2.7 KiB

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