cgss: Index | Examples | Files

package cgss

import ""



Package Files

allocation.go cgss.go share.go

func Distribute Uses

func Distribute(ctx context.Context, secret []byte, opt *DistributeOpt, status io.Writer) (shares []Share, err error)

Distribute computes cross-groupt secret sharing shares of the given secret according to the given configuration, opt. If status isn't nil, logging information will be written to the Writer.

The following example assumes three groups and assigns two shares to each of them, and the group and data thredholds are set to 2 and 3, respectively. It means, to reconstruct the secret, at least three shares must be collected from at least 2 groups.

Since the chunk size is set to 8bytes, the secret will be divided every 8bytes and each chunk will be converted to a set of shares.

From the share assignment, the dictribute function makes totally 6 shares, and it thus returns a slice of shares of which the length is 6. Note that the returned shares do not have any information about groups but the order of them are associated with the given allocation. More precisely, shares[0] and shares[1] are for the group 1, shares[2] and shares[3] are for the groupt 2, and shares[4] and shares[5] are for the group 3 in this example.


secret := []byte(`abcdefgaerogih:weori:ih:opih:oeijhg@roeinv;dlkjh:
chunksize := 8

allocation := Allocation{2, 2, 2}
gthreshold := 2
dthreshold := 3
ctx := context.Background()

shares, err := Distribute(ctx, secret, &DistributeOpt{
    ChunkSize:      chunksize,
    Allocation:     allocation,
    GroupThreshold: gthreshold,
    DataThreshold:  dthreshold,
}, nil)
if err != nil {




func Reconstruct Uses

func Reconstruct(ctx context.Context, shares []Share, status io.Writer) (res []byte, err error)

Reconstruct computes the secret from a set of shares. The given shares must satisfy the group and data constraints set to distribute the secret. If the number of groups and/or the number of shares are not enough, the result is undefined (maybe some random value).

If status isn't nil, logging information will be written to the Writer.

The following example reconstructs the secret from a subset of distributed shares. As same as the example of Distribute function, the secret is distributed into 6 shares over 3 groups so that each group has 2 shares.

Since the group threshold and data threshold are 2 and 3, respectively, we use two shares from group 1 and one share from group 2, i.e. three shares from two groups, to reconstruct the secret.

Reconstruct function returns a byte slice; you might need to cast it to string if the secret is a string.


ctx := context.Background()
// Distribute step is as same as the example of Dictribute function.
secret := []byte("This is secret information")
shares, err := Distribute(ctx, secret, &DistributeOpt{
    ChunkSize:      8,
    Allocation:     Allocation{2, 2, 2},
    GroupThreshold: 2,
    DataThreshold:  3,
}, nil)
if err != nil {

// Pick up two shares from group 1 and one share from group 2.
subset := []Share{shares[0], shares[1], shares[2]}
res, err := Reconstruct(ctx, subset, nil)
if err != nil {


This is secret information

type Allocation Uses

type Allocation []int

Allocation defines a share allocation.

func (Allocation) Iterator Uses

func (a Allocation) Iterator() *Iterator

Iterator returns an iterator of the assignment.

func (Allocation) Size Uses

func (a Allocation) Size() int

Size returns the size of allocated regions.

func (Allocation) Sum Uses

func (a Allocation) Sum() (res int)

Sum returns the summation of this allocate values.

type DistributeOpt Uses

type DistributeOpt struct {
    // Secrets will be divided into chunks based on the ChunkSize.
    ChunkSize int
    // Share assignment information.
    Allocation Allocation
    // Minimum number of groups from which shares must be collected to reconstruct secrets.
    GroupThreshold int
    // Minimum number of shares required to reconstruct secrets.
    DataThreshold int

DistributeOpt defines arguments of Distribute function.

type Iterator Uses

type Iterator struct {
    // contains filtered or unexported fields

Iterator defines a set of private members for an iterator of an allocation.

func (*Iterator) Next Uses

func (i *Iterator) Next() (int, bool)

Next returns a next group number.

type Share Uses

type Share struct {
    Field       *sss.Field
    GroupKey    *big.Int
    GroupShares []*big.Int
    DataKey     *big.Int
    DataShares  []*big.Int

Share defines a share of the Cross-Group Secret Sharing scheme.

Package cgss imports 11 packages (graph) and is imported by 2 packages. Updated 2017-03-28. Refresh now. Tools for package owners.