shamirsecretsharing/go-shamirsecretsharing/shamirsecretsharing_test.go
go-shamirsecretsharing/shamirsecretsharing_test.goRaw
1package shamirsecretsharing
2
3import (
4 "bytes"
5 "math/big"
6 "testing"
7
8 "github.com/stretchr/testify/assert"
9)
10
11func TestCreate(t *testing.T) {
12 k, ok := new(big.Int).SetString("12345678901234567890123456789012345678", 10)
13 assert.True(t, ok)
14
15 // 2 ** 127 - 1
16 p, ok := new(big.Int).SetString("170141183460469231731687303715884105727", 10)
17 assert.True(t, ok)
18
19 nShares := big.NewInt(int64(6))
20 nNeededShares := big.NewInt(int64(3))
21 shares, err := Create(
22 nNeededShares,
23 nShares,
24 p,
25 k)
26 assert.Nil(t, err)
27
28 //generate sharesToUse
29 var sharesToUse [][]*big.Int
30 sharesToUse = append(sharesToUse, shares[2])
31 sharesToUse = append(sharesToUse, shares[1])
32 sharesToUse = append(sharesToUse, shares[0])
33 secr := LagrangeInterpolation(p, sharesToUse)
34
35 // fmt.Print("original secret: ")
36 // fmt.Println(k)
37 // fmt.Print("p: ")
38 // fmt.Println(p)
39 // fmt.Print("shares: ")
40 // fmt.Println(shares)
41 // fmt.Print("recovered secret result: ")
42 // fmt.Println(secr)
43 if !bytes.Equal(k.Bytes(), secr.Bytes()) {
44 t.Errorf("reconstructed secret not correspond to original secret")
45 }
46 assert.Equal(t, k, secr)
47}