shamir

package
v0.0.0-...-8cf541d Latest Latest
Warning

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

Go to latest
Published: Jun 18, 2020 License: GPL-3.0 Imports: 5 Imported by: 0

README

Shamir

Shamir Secret Sharing library.

shamir implements Shamir's Secret Sharing a form of secret sharing, where a secret is divided into parts, giving each participant its own unique part.

To reconstruct the original secret, a minimum number of parts is required. In the threshold scheme this number is less than the total number of parts. Otherwise all participants are needed to reconstruct the original secret

shamir uses Finite Field elements defined in ff

For more information, read https://en.wikipedia.org/wiki/Shamir%27s_Secret_Sharing

shamir implements SecretSharer interface.

Example

import (
	"fmt"
	"github.com/iden3/go-backup/ff"
)


minShares := 3
maxShares := 6
prime := ff.FF_BN256_FP

var cfg Shamir

// Initialize Shamir's configuration 
//   generate 6 shares, 3 required to retrieve secret
err := cfg.NewConfig(minShares, maxShares, ff.FF_BN256_FP)
if err != nil {
	fmt.Errorf("Incorrect Shamir's configuration")
}

// Generate New Secret
secret := cfg.NewSecret()


// Generate Shares from secret
shares, err := cfg.GenerateShares(secret)
if err != nil {
	fmt.Errorf("Ereror generating shares")
}

// Regenerate secret from 3 shares
revoveredSecret, err := cfg.GenerateSecret(shares[0:2])

if !secret.Equal(revoveredSecret) {
	fmt.Errorf("Secrets are not equal")
}

Documentation

Index

Constants

View Source
const (
	PX_OFFSET     = 0
	PY_OFFSET     = 8
	FFTYPE_OFFSET = 40
	SHARE_SIZE    = 41
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Shamir

type Shamir struct {
	MinShares   int
	MaxShares   int
	ElementType int
}

Define shamir configuration: MinShares -> minimum number of shares to generate secret MaxShares -> maximum number of shares distributed ElementType -> defines prime

func NewConfig

func NewConfig(minShares, maxShares, elementType int) (*Shamir, error)

Initialize Shamir's secret sharing configuration

func (Shamir) GenerateSecret

func (s Shamir) GenerateSecret(shares []Share) (ff.Element, error)

Generate secret from shares S[0],...,S[N-1], where S[i] = (sx[i], sy[i]) = (x, poly(x)) secret = Sum_fromj=0_to_N-1 sy[j] * Prod_from_m=0,m!=j_to_m=N-1 ( sx[m] / (sx[m] - sx[j]))

sx[i] is an integer, sy[i] is a FF in Montgomery

func (Shamir) GenerateShares

func (s Shamir) GenerateShares(secret ff.Element) ([]Share, error)

Generate shares in Montgomery for a given poly p(x), generate N shares (N=MaxShares) s[1], s[1],...,s[N] such that s[i] = p(i) for 0 < i < N and s[0] = secret (s[0] is not a share) is in Regular fmt

func (Shamir) GetElType

func (s Shamir) GetElType() int

func (Shamir) GetMaxShares

func (s Shamir) GetMaxShares() int

func (Shamir) GetMinShares

func (s Shamir) GetMinShares() int

func (Shamir) NewSecret

func (s Shamir) NewSecret() ff.Element

Generate new secret

type Share

type Share struct {
	Px int
	Py ff.Element
}

func (*Share) Hash

func (s *Share) Hash(primeF int) []byte

func (Share) Marshal

func (s Share) Marshal(p int) []byte

func (*Share) Unmarshal

func (s *Share) Unmarshal(b []byte) (*Share, error)

Jump to

Keyboard shortcuts

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