|
| 1 | +// Package secretsharing provides methods to split a secret in shares. |
| 2 | +// |
| 3 | +// Shamir's secret sharing: https://dl.acm.org/doi/10.1145/359168.359176 |
| 4 | +// |
| 5 | +// Feldman's verifiable secret sharing: https://ieeexplore.ieee.org/document/4568297 |
| 6 | +package secretsharing |
| 7 | + |
| 8 | +import ( |
| 9 | + "errors" |
| 10 | + "fmt" |
| 11 | + "io" |
| 12 | + |
| 13 | + "github.com/cloudflare/circl/group" |
| 14 | +) |
| 15 | + |
| 16 | +type SecretShare struct { |
| 17 | + ID uint64 |
| 18 | + Share group.Scalar |
| 19 | +} |
| 20 | + |
| 21 | +// SecretSharing implements Shamir's secret sharing. |
| 22 | +type SecretSharing interface { |
| 23 | + Shard(rnd io.Reader, secret group.Scalar) []SecretShare |
| 24 | + Recover(shares []SecretShare) (secret group.Scalar, err error) |
| 25 | +} |
| 26 | + |
| 27 | +type secretSharing struct { |
| 28 | + G group.Group |
| 29 | + T, N uint64 |
| 30 | +} |
| 31 | + |
| 32 | +// New returns a struct implementing SecretSharing interface. |
| 33 | +func New(g group.Group, t, n uint64) (secretSharing, error) { |
| 34 | + if !(0 < t && t <= n) || g == nil { |
| 35 | + return secretSharing{}, errors.New("secretsharing: bad parameters") |
| 36 | + } |
| 37 | + ss := secretSharing{G: g, T: t, N: n} |
| 38 | + var _ SecretSharing = ss // checking at compile-time |
| 39 | + return ss, nil |
| 40 | +} |
| 41 | + |
| 42 | +func (s secretSharing) polyFromSecret(rnd io.Reader, secret group.Scalar) (p polynomial) { |
| 43 | + p = randomPolynomial(rnd, s.G, s.T) |
| 44 | + p.coeff[0] = secret.Copy() |
| 45 | + return |
| 46 | +} |
| 47 | + |
| 48 | +func (s secretSharing) generateShares(poly polynomial) []SecretShare { |
| 49 | + shares := make([]SecretShare, s.N) |
| 50 | + x := s.G.NewScalar() |
| 51 | + for i := range shares { |
| 52 | + id := uint64(i + 1) |
| 53 | + x.SetUint64(id) |
| 54 | + shares[i].ID = id |
| 55 | + shares[i].Share = poly.evaluate(x) |
| 56 | + } |
| 57 | + |
| 58 | + return shares |
| 59 | +} |
| 60 | + |
| 61 | +func (s secretSharing) Shard(rnd io.Reader, secret group.Scalar) []SecretShare { |
| 62 | + return s.generateShares(s.polyFromSecret(rnd, secret)) |
| 63 | +} |
| 64 | + |
| 65 | +func (s secretSharing) Recover(shares []SecretShare) (group.Scalar, error) { |
| 66 | + if l := len(shares); l <= int(s.T) { |
| 67 | + return nil, fmt.Errorf("secretsharing: do not met threshold %v with %v shares", s.T, l) |
| 68 | + } else if l > int(s.N) { |
| 69 | + return nil, fmt.Errorf("secretsharing: %v shares above max number of shares %v", l, s.N) |
| 70 | + } |
| 71 | + |
| 72 | + x := make([]group.Scalar, len(shares)) |
| 73 | + px := make([]group.Scalar, len(shares)) |
| 74 | + for i := range shares { |
| 75 | + x[i] = s.G.NewScalar() |
| 76 | + x[i].SetUint64(shares[i].ID) |
| 77 | + px[i] = shares[i].Share |
| 78 | + } |
| 79 | + |
| 80 | + return LagrangeInterpolate(s.G, x, px) |
| 81 | +} |
| 82 | + |
| 83 | +type SharesCommitment = []group.Element |
| 84 | + |
| 85 | +type verifiableSecretSharing struct { |
| 86 | + s secretSharing |
| 87 | +} |
| 88 | + |
| 89 | +// SecretSharing implements Feldman's secret sharing. |
| 90 | +type VerifiableSecretSharing interface { |
| 91 | + Shard(rnd io.Reader, secret group.Scalar) ([]SecretShare, SharesCommitment) |
| 92 | + Recover(shares []SecretShare) (secret group.Scalar, err error) |
| 93 | + Verify(share SecretShare, coms SharesCommitment) bool |
| 94 | +} |
| 95 | + |
| 96 | +// New returns a struct implementing VerifiableSecretSharing interface. |
| 97 | +func NewVerifiable(g group.Group, t, n uint64) (verifiableSecretSharing, error) { |
| 98 | + s, err := New(g, t, n) |
| 99 | + vs := verifiableSecretSharing{s} |
| 100 | + var _ verifiableSecretSharing = vs // checking at compile-time |
| 101 | + return vs, err |
| 102 | +} |
| 103 | + |
| 104 | +func (v verifiableSecretSharing) Shard(rnd io.Reader, secret group.Scalar) ([]SecretShare, SharesCommitment) { |
| 105 | + poly := v.s.polyFromSecret(rnd, secret) |
| 106 | + shares := v.s.generateShares(poly) |
| 107 | + |
| 108 | + vecComm := make(SharesCommitment, v.s.T+1) |
| 109 | + for i, ki := range poly.coeff { |
| 110 | + vecComm[i] = v.s.G.NewElement() |
| 111 | + vecComm[i].MulGen(ki) |
| 112 | + } |
| 113 | + |
| 114 | + return shares, vecComm |
| 115 | +} |
| 116 | + |
| 117 | +func (v verifiableSecretSharing) Verify(s SecretShare, c SharesCommitment) bool { |
| 118 | + if len(c) != int(v.s.T+1) { |
| 119 | + return false |
| 120 | + } |
| 121 | + |
| 122 | + polI := v.s.G.NewElement().MulGen(s.Share) |
| 123 | + |
| 124 | + lc := len(c) - 1 |
| 125 | + sum := c[lc].Copy() |
| 126 | + x := v.s.G.NewScalar() |
| 127 | + for i := lc - 1; i >= 0; i-- { |
| 128 | + x.SetUint64(s.ID) |
| 129 | + sum.Mul(sum, x) |
| 130 | + sum.Add(sum, c[i]) |
| 131 | + } |
| 132 | + |
| 133 | + return polI.IsEqual(sum) |
| 134 | +} |
| 135 | + |
| 136 | +func (v verifiableSecretSharing) Recover(shares []SecretShare) (group.Scalar, error) { |
| 137 | + return v.s.Recover(shares) |
| 138 | +} |
0 commit comments