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

This commit is contained in:
arnaucube
2019-04-12 21:22:13 +02:00
parent e988613b63
commit bf57b82e7d
4 changed files with 37 additions and 2 deletions

View File

@@ -73,7 +73,7 @@ func unpackSharesAndI(sharesPacked [][]*big.Int) ([]*big.Int, []*big.Int) {
}
// 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))
resultD := big.NewInt(int64(0))

View File

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

View File

@@ -43,7 +43,42 @@ func createShares(i []js.Value) {
println("error generating the 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) {
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())
}

Binary file not shown.