-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsecret_share.go
136 lines (115 loc) · 2.63 KB
/
secret_share.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package mpc
import (
"math/rand"
"time"
)
type SecretPart struct {
index int
Threshold int
Value []byte
}
type Poly func(uint8) uint8
func SplitSecret(secret []byte, n, t int) []SecretPart {
rand.Seed(time.Now().UnixNano())
result := make([]SecretPart, n)
for i := 0; i < n; i++ {
result[i] = SecretPart{
index: i + 1,
Threshold: t,
Value: make([]byte, len(secret)),
}
}
for i := 0; i < len(secret); i++ {
parts := split(secret[i], uint8(n), uint8(t))
for j := 0; j < n; j++ {
result[j].Value[i] = parts[j]
}
}
return result
}
func CombainSecret(parts []SecretPart) []byte {
t := parts[0].Threshold
secretLen := len(parts[0].Value)
coeffs := make([][]int, t)
values := make([][]int, t)
for i := 0; i < t; i++ {
// make coeffs
coeffsRow := make([]int, t)
coeffsRow[0] = 1
index := parts[i].index
mult := index
for j := 1; j < t; j++ {
coeffsRow[j] = mult
mult *= index
}
coeffs[i] = coeffsRow
// make values
values[i] = make([]int, secretLen)
for j := 0; j < secretLen; j++ {
values[i][j] = int(parts[i].Value[j])
}
}
coeffs, values = recursion(coeffs, values)
secrets := make([]byte, secretLen)
for i := 0; i < secretLen; i++ {
v := values[0][i] / coeffs[0][0]
v = v % 256
if v < 0 {
v += 256
}
secrets[i] = uint8(v)
}
return secrets
}
func split(M byte, n, t uint8) []byte {
result := make([]byte, n)
poly := makePoly(M, t)
for i := uint8(0); i < n; i++ {
result[i] = poly(i + 1)
}
return result
}
func makePoly(M, t byte) Poly {
coeffs := make([]uint8, t)
coeffs[0] = M
for i := uint8(1); i < t; i++ {
coeffs[i] = uint8(rand.Intn(256))
}
return func(index byte) byte {
result := int(M)
mult := int(index)
for i := uint8(1); i < t; i++ {
result += mult * int(coeffs[i])
mult *= int(index)
}
return uint8(result % 256)
}
}
func recursion(coeffs [][]int, values [][]int) ([][]int, [][]int) {
t := len(coeffs)
if t == 1 {
return coeffs, values
}
coeffs, values = elimination(coeffs, values, t)
return recursion(coeffs, values)
}
func elimination(coeffs [][]int, values [][]int, t int) ([][]int, [][]int) {
newCoeffs := make([][]int, t-1)
newValues := make([][]int, t-1)
base := coeffs[0][t-1]
for i := 0; i < t-1; i++ {
coeffsRow := make([]int, t-1)
mult := coeffs[i+1][t-1]
for j := 0; j < t-1; j++ {
coeffsRow[j] = coeffs[i+1][j]*base - coeffs[0][j]*mult
}
newCoeffs[i] = coeffsRow
secretLen := len(values[0])
valuesRow := make([]int, secretLen)
for j := 0; j < secretLen; j++ {
valuesRow[j] = values[i+1][j]*base - values[0][j]*mult
}
newValues[i] = valuesRow
}
return newCoeffs, newValues
}