sss

package
v0.3.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 12, 2017 License: GPL-3.0 Imports: 5 Imported by: 2

Documentation

Overview

Package sss implements Distribute and Reconstruct functions, which execute distribute and reconstruct procedures defined in the Shamir's Secret Sharing scheme.

This package also provides useful structures, Field and Polynomial.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Reconstruct

func Reconstruct(shares []Share) (bytes []byte, err error)

Reconstruct computes the secret value from a set of shares.

Example

Example code for Reconstruct function.

// filenames is a slice of file names of shares.
filenames := []string{"share1.dat", "share2.dat", "share3.dat"}

shares := make([]Share, len(filenames))
for i, f := range filenames {

	data, err := ioutil.ReadFile(f)
	if err != nil {
		log.Fatal(err)
	}

	if err = json.Unmarshal(data, &shares[i]); err != nil {
		log.Fatal(err)
	}

}

secret, err := Reconstruct(shares)
if err != nil {
	log.Fatal(err)
}
err = ioutil.WriteFile("secret-file", secret, 0644)
if err != nil {
	log.Fatal(err)
}
Output:

Types

type Field

type Field struct {
	// Prime number.
	Prime *big.Int
	// Max is Prime - 1.
	Max *big.Int
}

Field represents a finite field Z/pZ.

func NewField

func NewField(prime *big.Int) *Field

NewField creates a new finite field.

func (*Field) MarshalJSON

func (f *Field) MarshalJSON() ([]byte, error)

MarshalJSON implements Marshaler interface.

func (*Field) UnmarshalJSON

func (f *Field) UnmarshalJSON(data []byte) (err error)

UnmarshalJSON implements Unmarshaler interface.

type Polynomial

type Polynomial struct {
	Field        *Field
	Dimension    int
	Coefficients []*big.Int
	Const        *big.Int
}

Polynomial represents a polynomial defined on a finite field.

func NewPolynomial

func NewPolynomial(field *Field, c *big.Int, dim int) (*Polynomial, error)

NewPolynomial creates a new random polynomial on the given finite field. The dimension of the polynomial is the given dim, and it has a given constant.

func (*Polynomial) Call

func (p *Polynomial) Call(x *big.Int) *big.Int

Call computes a value F(x) where x is the given number.

type Share

type Share struct {
	Key   *big.Int
	Value []*big.Int
	Field *Field
}

Share defines a share of Shamir's Secret Sharing scheme.

func Distribute

func Distribute(secret []byte, chunkByte, size, threshold int) (shares []Share, err error)

Distribute computes shares having a given secret.

Example

Example code for Distribute function.

chunksize := 256
totalShares := 10
threshold := 5

secret, err := ioutil.ReadFile("secret-file")
if err != nil {
	log.Fatal(err)
}

shares, err := Distribute(secret, chunksize, totalShares, threshold)
if err != nil {
	log.Fatal(err)
}

for i, s := range shares {
	data, err := json.Marshal(s)
	if err != nil {
		log.Fatal(err)
	}
	filename := fmt.Sprintf("%s.%d.json", "share-", i)
	if err = ioutil.WriteFile(filename, data, 0644); err != nil {
		log.Fatal(err)
	}
}
Output:

func (Share) MarshalJSON

func (s Share) MarshalJSON() ([]byte, error)

MarshalJSON implements Marshaler interface.

func (*Share) UnmarshalJSON

func (s *Share) UnmarshalJSON(data []byte) (err error)

UnmarshalJSON implements Unmarshaler interface.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL