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