github.com/cloudflare/circl@v1.5.0/secretsharing/example_test.go (about)

     1  package secretsharing_test
     2  
     3  import (
     4  	"crypto/rand"
     5  	"fmt"
     6  
     7  	"github.com/cloudflare/circl/group"
     8  	"github.com/cloudflare/circl/secretsharing"
     9  )
    10  
    11  func ExampleSecretSharing() {
    12  	g := group.P256
    13  	t := uint(2)
    14  	n := uint(5)
    15  
    16  	secret := g.RandomScalar(rand.Reader)
    17  	ss := secretsharing.New(rand.Reader, t, secret)
    18  	shares := ss.Share(n)
    19  
    20  	got, err := secretsharing.Recover(t, shares[:t])
    21  	fmt.Printf("Recover secret: %v\nError: %v\n", secret.IsEqual(got), err)
    22  
    23  	got, err = secretsharing.Recover(t, shares[:t+1])
    24  	fmt.Printf("Recover secret: %v\nError: %v\n", secret.IsEqual(got), err)
    25  	// Output:
    26  	// Recover secret: false
    27  	// Error: secretsharing: number of shares (n=2) must be above the threshold (t=2)
    28  	// Recover secret: true
    29  	// Error: <nil>
    30  }
    31  
    32  func ExampleVerify() {
    33  	g := group.P256
    34  	t := uint(2)
    35  	n := uint(5)
    36  
    37  	secret := g.RandomScalar(rand.Reader)
    38  	ss := secretsharing.New(rand.Reader, t, secret)
    39  	shares := ss.Share(n)
    40  	coms := ss.CommitSecret()
    41  
    42  	for i := range shares {
    43  		ok := secretsharing.Verify(t, shares[i], coms)
    44  		fmt.Printf("Share %v is valid: %v\n", i, ok)
    45  	}
    46  
    47  	got, err := secretsharing.Recover(t, shares)
    48  	fmt.Printf("Recover secret: %v\nError: %v\n", secret.IsEqual(got), err)
    49  	// Output:
    50  	// Share 0 is valid: true
    51  	// Share 1 is valid: true
    52  	// Share 2 is valid: true
    53  	// Share 3 is valid: true
    54  	// Share 4 is valid: true
    55  	// Recover secret: true
    56  	// Error: <nil>
    57  }