arnaucube
arnaucube/shamirsecretsharingPublic Self-hosted repository
shamirsecretsharing/shamirsecretsharing_test.go
shamirsecretsharing_test.goRaw
 1package shamirsecretsharing
 2
 3import (
 4	"bytes"
 5	"crypto/rand"
 6	"math/big"
 7	"testing"
 8
 9	"github.com/stretchr/testify/assert"
10)
11
12func TestCreate(t *testing.T) {
13	k, ok := new(big.Int).SetString("123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890", 10)
14	assert.True(t, ok)
15
16	p, err := rand.Prime(rand.Reader, bits/2)
17	assert.Nil(t, err)
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(sharesToUse, p)
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}