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