Skip to content

Commit 98efd72

Browse files
committed
Initial import and refactoring
0 parents  commit 98efd72

File tree

7 files changed

+454
-0
lines changed

7 files changed

+454
-0
lines changed

LICENSE

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2014 Coda Hale
4+
Copyright (c) 2021 Lauris BH
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in
14+
all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
THE SOFTWARE.

README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Shamir's Secret Sharing
2+
3+
Based on [github.com/codahale/sss](https://github.com/codahale/sss)
4+
5+
A pure Go implementation of [Shamir's Secret Sharing algorithm](http://en.wikipedia.org/wiki/Shamir's_Secret_Sharing)
6+
7+
## Usage
8+
9+
```sh
10+
go get -u github.com/lafriks/go-shamir
11+
```
12+
13+
## Example
14+
15+
```go
16+
package main
17+
18+
import (
19+
"fmt"
20+
21+
"github.com/lafriks/go-shamir"
22+
)
23+
24+
func main() {
25+
secret := []byte("example")
26+
27+
// Split secret to 5 shares and require 3 shares to reconstruct secret
28+
shares, err := Split(5, 3, secret)
29+
if err != nil {
30+
panic(err)
31+
}
32+
33+
// Reconstruct secret from shares
34+
reconstructed, err := Combine([][]byte{shares[0], shares[2], shares[4]})
35+
if err != nil {
36+
panic(err)
37+
}
38+
39+
// secret == reconstructed
40+
}
41+
```

gf256.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package shamir
2+
3+
import "crypto/subtle"
4+
5+
func mul(e, a byte) byte {
6+
ret := exp[(int(log[e])+int(log[a]))%255]
7+
8+
ret = byte(subtle.ConstantTimeSelect(subtle.ConstantTimeByteEq(e, 0), 0, int(ret)))
9+
return byte(subtle.ConstantTimeSelect(subtle.ConstantTimeByteEq(a, 0), 0, int(ret)))
10+
}
11+
12+
func div(e, a byte) byte {
13+
if a == 0 {
14+
panic("div by zero")
15+
}
16+
17+
p := ((int(log[e]) - int(log[a])) + 255) % 255
18+
19+
ret := exp[p]
20+
21+
return byte(subtle.ConstantTimeSelect(subtle.ConstantTimeByteEq(e, 0), 0, int(ret)))
22+
}
23+
24+
var (
25+
// 0x11b prime polynomial and 0x03 as generator
26+
exp = [256]byte{
27+
0x01, 0xe5, 0x4c, 0xb5, 0xfb, 0x9f, 0xfc, 0x12,
28+
0x03, 0x34, 0xd4, 0xc4, 0x16, 0xba, 0x1f, 0x36,
29+
0x05, 0x5c, 0x67, 0x57, 0x3a, 0xd5, 0x21, 0x5a,
30+
0x0f, 0xe4, 0xa9, 0xf9, 0x4e, 0x64, 0x63, 0xee,
31+
0x11, 0x37, 0xe0, 0x10, 0xd2, 0xac, 0xa5, 0x29,
32+
0x33, 0x59, 0x3b, 0x30, 0x6d, 0xef, 0xf4, 0x7b,
33+
0x55, 0xeb, 0x4d, 0x50, 0xb7, 0x2a, 0x07, 0x8d,
34+
0xff, 0x26, 0xd7, 0xf0, 0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22, 0x6e, 0xdb, 0x20, 0xbf, 0x43, 0x51, 0x52,
57+
0x66, 0xb2, 0x76, 0x60, 0xda, 0xc5, 0xf3, 0xf6,
58+
0xaa, 0xcd, 0x9a, 0xa0, 0x75, 0x54, 0x0e, 0x01,
59+
}
60+
log = [256]byte{
61+
0x00, 0xff, 0xc8, 0x08, 0x91, 0x10, 0xd0, 0x36,
62+
0x5a, 0x3e, 0xd8, 0x43, 0x99, 0x77, 0xfe, 0x18,
63+
0x23, 0x20, 0x07, 0x70, 0xa1, 0x6c, 0x0c, 0x7f,
64+
0x62, 0x8b, 0x40, 0x46, 0xc7, 0x4b, 0xe0, 0x0e,
65+
0xeb, 0x16, 0xe8, 0xad, 0xcf, 0xcd, 0x39, 0x53,
66+
0x6a, 0x27, 0x35, 0x93, 0xd4, 0x4e, 0x48, 0xc3,
67+
0x2b, 0x79, 0x54, 0x28, 0x09, 0x78, 0x0f, 0x21,
68+
0x90, 0x87, 0x14, 0x2a, 0xa9, 0x9c, 0xd6, 0x74,
69+
0xb4, 0x7c, 0xde, 0xed, 0xb1, 0x86, 0x76, 0xa4,
70+
0x98, 0xe2, 0x96, 0x8f, 0x02, 0x32, 0x1c, 0xc1,
71+
0x33, 0xee, 0xef, 0x81, 0xfd, 0x30, 0x5c, 0x13,
72+
0x9d, 0x29, 0x17, 0xc4, 0x11, 0x44, 0x8c, 0x80,
73+
0xf3, 0x73, 0x42, 0x1e, 0x1d, 0xb5, 0xf0, 0x12,
74+
0xd1, 0x5b, 0x41, 0xa2, 0xd7, 0x2c, 0xe9, 0xd5,
75+
0x59, 0xcb, 0x50, 0xa8, 0xdc, 0xfc, 0xf2, 0x56,
76+
0x72, 0xa6, 0x65, 0x2f, 0x9f, 0x9b, 0x3d, 0xba,
77+
0x7d, 0xc2, 0x45, 0x82, 0xa7, 0x57, 0xb6, 0xa3,
78+
0x7a, 0x75, 0x4f, 0xae, 0x3f, 0x37, 0x6d, 0x47,
79+
0x61, 0xbe, 0xab, 0xd3, 0x5f, 0xb0, 0x58, 0xaf,
80+
0xca, 0x5e, 0xfa, 0x85, 0xe4, 0x4d, 0x8a, 0x05,
81+
0xfb, 0x60, 0xb7, 0x7b, 0xb8, 0x26, 0x4a, 0x67,
82+
0xc6, 0x1a, 0xf8, 0x69, 0x25, 0xb3, 0xdb, 0xbd,
83+
0x66, 0xdd, 0xf1, 0xd2, 0xdf, 0x03, 0x8d, 0x34,
84+
0xd9, 0x92, 0x0d, 0x63, 0x55, 0xaa, 0x49, 0xec,
85+
0xbc, 0x95, 0x3c, 0x84, 0x0b, 0xf5, 0xe6, 0xe7,
86+
0xe5, 0xac, 0x7e, 0x6e, 0xb9, 0xf9, 0xda, 0x8e,
87+
0x9a, 0xc9, 0x24, 0xe1, 0x0a, 0x15, 0x6b, 0x3a,
88+
0xa0, 0x51, 0xf4, 0xea, 0xb2, 0x97, 0x9e, 0x5d,
89+
0x22, 0x88, 0x94, 0xce, 0x19, 0x01, 0x71, 0x4c,
90+
0xa5, 0xe3, 0xc5, 0x31, 0xbb, 0xcc, 0x1f, 0x2d,
91+
0x3b, 0x52, 0x6f, 0xf6, 0x2e, 0x89, 0xf7, 0xc0,
92+
0x68, 0x1b, 0x64, 0x04, 0x06, 0xbf, 0x83, 0x38,
93+
}
94+
)

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/lafriks/go-shamir
2+
3+
go 1.14

polynomial.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package shamir
2+
3+
import (
4+
"crypto/rand"
5+
"io"
6+
)
7+
8+
// evaluate the polynomial at the given point
9+
func eval(p []byte, x byte) (result byte) {
10+
if x == 0 {
11+
return p[0]
12+
}
13+
14+
// Horner's scheme
15+
for i := 1; i <= len(p); i++ {
16+
result = mul(result, x) ^ p[len(p)-i]
17+
}
18+
return
19+
}
20+
21+
// generates a random n-degree polynomial w/ a given x-intercept
22+
func generate(degree byte, x byte) ([]byte, error) {
23+
result := make([]byte, degree+1)
24+
result[0] = x
25+
26+
buf := make([]byte, degree-1)
27+
if _, err := io.ReadFull(rand.Reader, buf); err != nil {
28+
return nil, err
29+
}
30+
31+
for i := byte(1); i < degree; i++ {
32+
result[i] = buf[i-1]
33+
}
34+
35+
// the Nth term can't be zero, or else it's a (N-1) degree polynomial
36+
for {
37+
buf = make([]byte, 1)
38+
if _, err := io.ReadFull(rand.Reader, buf); err != nil {
39+
return nil, err
40+
}
41+
42+
if buf[0] != 0 {
43+
result[degree] = buf[0]
44+
return result, nil
45+
}
46+
}
47+
}
48+
49+
// an input/output pair
50+
type pair struct {
51+
x, y byte
52+
}
53+
54+
// Lagrange interpolation
55+
func interpolate(points []pair, x byte) (value byte) {
56+
for i, a := range points {
57+
weight := byte(1)
58+
for j, b := range points {
59+
if i != j {
60+
top := x ^ b.x
61+
bottom := a.x ^ b.x
62+
factor := div(top, bottom)
63+
weight = mul(weight, factor)
64+
}
65+
}
66+
value = value ^ mul(weight, a.y)
67+
}
68+
return
69+
}

shamir.go

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
// Package shamir implements Shamir's Secret Sharing algorithm over GF(2^8).
2+
//
3+
// Shamir's Secret Sharing algorithm allows you to securely share a secret with
4+
// N people, allowing the recovery of that secret if K of those people combine
5+
// their shares.
6+
//
7+
// It begins by encoding a secret as a number (e.g., 42), and generating N
8+
// random polynomial equations of degree K-1 which have an X-intercept equal to
9+
// the secret. Given K=3, the following equations might be generated:
10+
//
11+
// f1(x) = 78x^2 + 19x + 42
12+
// f2(x) = 128x^2 + 171x + 42
13+
// f3(x) = 121x^2 + 3x + 42
14+
// f4(x) = 91x^2 + 95x + 42
15+
// etc.
16+
//
17+
// These polynomials are then evaluated for values of X > 0:
18+
//
19+
// f1(1) = 139
20+
// f2(2) = 896
21+
// f3(3) = 1140
22+
// f4(4) = 1783
23+
// etc.
24+
//
25+
// These (x, y) pairs are the shares given to the parties. In order to combine
26+
// shares to recover the secret, these (x, y) pairs are used as the input points
27+
// for Lagrange interpolation, which produces a polynomial which matches the
28+
// given points. This polynomial can be evaluated for f(0), producing the secret
29+
// value--the common x-intercept for all the generated polynomials.
30+
//
31+
// If fewer than K shares are combined, the interpolated polynomial will be
32+
// wrong, and the result of f(0) will not be the secret.
33+
//
34+
// This package constructs polynomials over the field GF(2^8) for each byte of
35+
// the secret, allowing for fast splitting and combining of anything which can
36+
// be encoded as bytes.
37+
//
38+
// This package has not been audited by cryptography or security professionals.
39+
package shamir
40+
41+
import (
42+
"errors"
43+
mrand "math/rand"
44+
"time"
45+
)
46+
47+
var (
48+
// ErrInvalidCount is returned when the count parameter is invalid.
49+
ErrInvalidCount = errors.New("Shares must be more or equal to treshold but not more than 255")
50+
// ErrInvalidThreshold is returned when the threshold parameter is invalid.
51+
ErrInvalidThreshold = errors.New("Treshold must be at least 2 but not more than 255")
52+
// ErrEmptySecret is returned when provided secret is empty.
53+
ErrEmptySecret = errors.New("Secret can not be empty")
54+
// ErrInvalidShares is returned when not required minimum shares are provided or shares does not have same length.
55+
ErrInvalidShares = errors.New("At least 2 shares are required and must have same length")
56+
)
57+
58+
// Split the given secret into N shares of which K are required to recover the
59+
// secret. Returns an array of shares.
60+
func Split(n, k int, secret []byte) ([][]byte, error) {
61+
if k <= 1 || k > 255 {
62+
return nil, ErrInvalidThreshold
63+
}
64+
65+
if n < k || n > 255 {
66+
return nil, ErrInvalidCount
67+
}
68+
69+
if len(secret) == 0 {
70+
return nil, ErrEmptySecret
71+
}
72+
73+
mrand.Seed(time.Now().UnixNano())
74+
cords := mrand.Perm(255)
75+
76+
shares := make([][]byte, n)
77+
for i := range shares {
78+
shares[i] = make([]byte, len(secret)+1)
79+
shares[i][len(secret)] = byte(cords[i]) + 1
80+
}
81+
82+
for i, b := range secret {
83+
p, err := generate(byte(k)-1, b)
84+
if err != nil {
85+
return nil, err
86+
}
87+
88+
for j := 0; j < n; j++ {
89+
x := byte(cords[j]) + 1
90+
shares[j][i] = eval(p, x)
91+
}
92+
}
93+
94+
return shares, nil
95+
}
96+
97+
// Combine the given shares into the original secret.
98+
func Combine(shares [][]byte) ([]byte, error) {
99+
if len(shares) < 2 || len(shares[0]) < 2 {
100+
return nil, ErrInvalidShares
101+
}
102+
103+
l := len(shares[0])
104+
c := make(map[byte]bool, len(shares))
105+
c[shares[0][l-1]] = true
106+
for i := 1; i < len(shares); i++ {
107+
if len(shares[i]) != l {
108+
return nil, ErrInvalidShares
109+
}
110+
if ok := c[shares[i][l-1]]; ok {
111+
return nil, ErrInvalidShares
112+
}
113+
c[shares[i][l-1]] = true
114+
}
115+
116+
secret := make([]byte, l-1)
117+
118+
points := make([]pair, len(shares))
119+
for i := range secret {
120+
for p, v := range shares {
121+
points[p] = pair{x: v[l-1], y: v[i]}
122+
}
123+
secret[i] = interpolate(points, 0)
124+
}
125+
126+
return secret, nil
127+
}

0 commit comments

Comments
 (0)