|
| 1 | +// Package secretsharing provides methods to split secrets in shares. |
| 2 | +// |
| 3 | +// Let n be the number of parties, and t the number of corrupted parties such |
| 4 | +// that 0 <= t < n. A (t,n) secret sharing allows to split a secret into n |
| 5 | +// shares, such that the secret can be recovered from any subset of t+1 shares. |
| 6 | +// |
| 7 | +// The NewShamirSecretSharing function creates a Shamir secret sharing [1], |
| 8 | +// which relies on Lagrange polynomial interpolation. |
| 9 | +// |
| 10 | +// The NewFeldmanSecretSharing function creates a Feldman secret sharing [2], |
| 11 | +// which extends Shamir's by allowing to verify that a share was honestly |
| 12 | +// generated. |
| 13 | +// |
| 14 | +// References |
| 15 | +// |
| 16 | +// [1] https://dl.acm.org/doi/10.1145/359168.359176 |
| 17 | +// [2] https://ieeexplore.ieee.org/document/4568297 |
| 18 | +package secretsharing |
| 19 | + |
| 20 | +import ( |
| 21 | + "errors" |
| 22 | + "fmt" |
| 23 | + "io" |
| 24 | + |
| 25 | + "github.com/cloudflare/circl/group" |
| 26 | + "github.com/cloudflare/circl/math/polynomial" |
| 27 | +) |
| 28 | + |
| 29 | +// Share represents a share of a secret. |
| 30 | +type Share struct { |
| 31 | + ID uint // ID uniquely identifies a share in a secret sharing instance. |
| 32 | + Value group.Scalar // Value stores the share generated from a secret sharing instance. |
| 33 | +} |
| 34 | + |
| 35 | +type ss struct { |
| 36 | + g group.Group |
| 37 | + t, n uint |
| 38 | +} |
| 39 | + |
| 40 | +// NewShamirSecretSharing implements a (t,n) Shamir's secret sharing. |
| 41 | +// A (t,n) secret sharing allows to split a secret into n shares, such that the |
| 42 | +// secret can be only recovered from any subset of t+1 shares. Returns an error |
| 43 | +// if 0 <= t < n does not hold. |
| 44 | +func NewShamirSecretSharing(g group.Group, t, n uint) (ss, error) { |
| 45 | + if t >= n { |
| 46 | + return ss{}, errors.New("secretsharing: bad parameters") |
| 47 | + } |
| 48 | + return ss{g: g, t: t, n: n}, nil |
| 49 | +} |
| 50 | + |
| 51 | +// Params returns the t and n parameters of the secret sharing. |
| 52 | +func (s ss) Params() (t, n uint) { return s.t, s.n } |
| 53 | + |
| 54 | +func (s ss) polyFromSecret(rnd io.Reader, secret group.Scalar) (p polynomial.Polynomial) { |
| 55 | + c := make([]group.Scalar, s.t+1) |
| 56 | + for i := range c { |
| 57 | + c[i] = s.g.RandomScalar(rnd) |
| 58 | + } |
| 59 | + c[0].Set(secret) |
| 60 | + return polynomial.New(c) |
| 61 | +} |
| 62 | + |
| 63 | +func (s ss) generateShares(poly polynomial.Polynomial) []Share { |
| 64 | + shares := make([]Share, s.n) |
| 65 | + x := s.g.NewScalar() |
| 66 | + for i := range shares { |
| 67 | + id := i + 1 |
| 68 | + x.SetUint64(uint64(id)) |
| 69 | + shares[i].ID = uint(id) |
| 70 | + shares[i].Value = poly.Evaluate(x) |
| 71 | + } |
| 72 | + |
| 73 | + return shares |
| 74 | +} |
| 75 | + |
| 76 | +// Shard splits the secret into n shares. |
| 77 | +func (s ss) Shard(rnd io.Reader, secret group.Scalar) []Share { |
| 78 | + return s.generateShares(s.polyFromSecret(rnd, secret)) |
| 79 | +} |
| 80 | + |
| 81 | +// Recover returns the secret provided more than t shares are given. Returns an |
| 82 | +// error if the number of shares is not above the threshold or goes beyond the |
| 83 | +// maximum number of shares. |
| 84 | +func (s ss) Recover(shares []Share) (group.Scalar, error) { |
| 85 | + if l := len(shares); l <= int(s.t) { |
| 86 | + return nil, fmt.Errorf("secretsharing: does not reach the threshold %v with %v shares", s.t, l) |
| 87 | + } else if l > int(s.n) { |
| 88 | + return nil, fmt.Errorf("secretsharing: %v shares above max number of shares %v", l, s.n) |
| 89 | + } |
| 90 | + |
| 91 | + x := make([]group.Scalar, s.t+1) |
| 92 | + px := make([]group.Scalar, s.t+1) |
| 93 | + for i := range shares[:s.t+1] { |
| 94 | + x[i] = s.g.NewScalar().SetUint64(uint64(shares[i].ID)) |
| 95 | + px[i] = shares[i].Value |
| 96 | + } |
| 97 | + |
| 98 | + l := polynomial.NewLagrangePolynomial(x, px) |
| 99 | + zero := s.g.NewScalar() |
| 100 | + |
| 101 | + return l.Evaluate(zero), nil |
| 102 | +} |
| 103 | + |
| 104 | +type SharesCommitment = []group.Element |
| 105 | + |
| 106 | +type vss struct{ s ss } |
| 107 | + |
| 108 | +// NewFeldmanSecretSharing implements a (t,n) Feldman's verifiable secret |
| 109 | +// sharing. A (t,n) secret sharing allows to split a secret into n shares, such |
| 110 | +// that the secret can be only recovered from any subset of t+1 shares. This |
| 111 | +// method is verifiable because once the shares and the secret are committed |
| 112 | +// during sharding, one can later verify whether the share was generated |
| 113 | +// honestly. Returns an error if 0 < t <= n does not hold. |
| 114 | +func NewFeldmanSecretSharing(g group.Group, t, n uint) (vss, error) { |
| 115 | + s, err := NewShamirSecretSharing(g, t, n) |
| 116 | + return vss{s}, err |
| 117 | +} |
| 118 | + |
| 119 | +// Params returns the t and n parameters of the secret sharing. |
| 120 | +func (v vss) Params() (t, n uint) { return v.s.Params() } |
| 121 | + |
| 122 | +// Shard splits the secret into n shares, and also returns a commitment to both |
| 123 | +// the secret and the shares. |
| 124 | +func (v vss) Shard(rnd io.Reader, secret group.Scalar) ([]Share, SharesCommitment) { |
| 125 | + poly := v.s.polyFromSecret(rnd, secret) |
| 126 | + shares := v.s.generateShares(poly) |
| 127 | + coeffs := poly.Coefficients() |
| 128 | + shareComs := make(SharesCommitment, len(coeffs)) |
| 129 | + for i := range coeffs { |
| 130 | + shareComs[i] = v.s.g.NewElement().MulGen(coeffs[i]) |
| 131 | + } |
| 132 | + |
| 133 | + return shares, shareComs |
| 134 | +} |
| 135 | + |
| 136 | +// Verify returns true if a share was produced by sharding a secret. It uses |
| 137 | +// the share commitments generated by the Shard function to verify this |
| 138 | +// property. |
| 139 | +func (v vss) Verify(s Share, c SharesCommitment) bool { |
| 140 | + if len(c) != int(v.s.t+1) { |
| 141 | + return false |
| 142 | + } |
| 143 | + |
| 144 | + lc := len(c) - 1 |
| 145 | + sum := v.s.g.NewElement().Set(c[lc]) |
| 146 | + x := v.s.g.NewScalar() |
| 147 | + for i := lc - 1; i >= 0; i-- { |
| 148 | + x.SetUint64(uint64(s.ID)) |
| 149 | + sum.Mul(sum, x) |
| 150 | + sum.Add(sum, c[i]) |
| 151 | + } |
| 152 | + polI := v.s.g.NewElement().MulGen(s.Value) |
| 153 | + return polI.IsEqual(sum) |
| 154 | +} |
| 155 | + |
| 156 | +// Recover returns the secret provided more than t shares are given. Returns an |
| 157 | +// error if the number of shares is not above the threshold (t) or is larger |
| 158 | +// than the maximum number of shares (n). |
| 159 | +func (v vss) Recover(shares []Share) (group.Scalar, error) { return v.s.Recover(shares) } |
0 commit comments