Browse Source

add lagrange interpolation js to wasm wrapper and parser of the data struct

master
arnaucube 5 years ago
parent
commit
bf57b82e7d
4 changed files with 37 additions and 2 deletions
  1. +1
    -1
      shamirsecretsharing.go
  2. +1
    -1
      shamirsecretsharing_test.go
  3. +35
    -0
      wasm/shamirsecretsharing-wasm-wrapper.go
  4. BIN
      wasm/shamirsecretsharing.wasm

+ 1
- 1
shamirsecretsharing.go

@ -73,7 +73,7 @@ func unpackSharesAndI(sharesPacked [][]*big.Int) ([]*big.Int, []*big.Int) {
} }
// LagrangeInterpolation calculates the secret from given shares // LagrangeInterpolation calculates the secret from given shares
func LagrangeInterpolation(sharesGiven [][]*big.Int, p *big.Int) *big.Int {
func LagrangeInterpolation(p *big.Int, sharesGiven [][]*big.Int) *big.Int {
resultN := big.NewInt(int64(0)) resultN := big.NewInt(int64(0))
resultD := big.NewInt(int64(0)) resultD := big.NewInt(int64(0))

+ 1
- 1
shamirsecretsharing_test.go

@ -30,7 +30,7 @@ func TestCreate(t *testing.T) {
sharesToUse = append(sharesToUse, shares[2]) sharesToUse = append(sharesToUse, shares[2])
sharesToUse = append(sharesToUse, shares[1]) sharesToUse = append(sharesToUse, shares[1])
sharesToUse = append(sharesToUse, shares[0]) sharesToUse = append(sharesToUse, shares[0])
secr := LagrangeInterpolation(sharesToUse, p)
secr := LagrangeInterpolation(p, sharesToUse)
// fmt.Print("original secret: ") // fmt.Print("original secret: ")
// fmt.Println(k) // fmt.Println(k)

+ 35
- 0
wasm/shamirsecretsharing-wasm-wrapper.go

@ -43,7 +43,42 @@ func createShares(i []js.Value) {
println("error generating the shares") println("error generating the shares")
} }
println(shares) println(shares)
sharesStr := sharesToString(shares)
println(sharesStr)
}
func sharesToString(shares [][]*big.Int) []string {
var s []string
for i := 0; i < len(shares); i++ {
s = append(s, shares[i][0].String())
s = append(s, shares[i][1].String())
}
return s
} }
func lagrangeInterpolation(i []js.Value) { func lagrangeInterpolation(i []js.Value) {
p, ok := new(big.Int).SetString(i[0].String(), 10)
if !ok {
println("error parsing parameter in position 0 (p)")
}
// parse the shares array
var shares [][]*big.Int
for n := 1; n < len(i); n = n + 2 {
a, ok := new(big.Int).SetString(i[n].String(), 10)
if !ok {
println("error parsing parameter in position ", n)
}
b, ok := new(big.Int).SetString(i[n+1].String(), 10)
if !ok {
println("error parsing parameter in position ", n+1)
}
var share []*big.Int
share = append(share, a)
share = append(share, b)
shares = append(shares, share)
}
secr := shamirsecretsharing.LagrangeInterpolation(p, shares)
println(secr.String())
} }

BIN
wasm/shamirsecretsharing.wasm


Loading…
Cancel
Save