-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolynomials.go
53 lines (41 loc) · 928 Bytes
/
polynomials.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
package sss
import (
"crypto/rand"
"math/big"
)
func generatePolynomial(secretByte byte, k int) ([]byte, error) {
poly := make([]byte, k)
poly[0] = secretByte
for i := 1; i < k; i++ {
coef, err := rand.Int(rand.Reader, big.NewInt(256))
if err != nil {
return nil, err
}
poly[i] = byte(coef.Int64())
}
return poly, nil
}
func evaluatePolynomial(poly []byte, x byte) byte {
result := byte(0)
for i := len(poly) - 1; i >= 0; i-- {
result = gfMul(result, x)
result = gfAdd(result, poly[i])
}
return result
}
func interpolatePolynomial(shares [][]byte, index int) byte {
secretByte := byte(0)
for i := 0; i < len(shares); i++ {
xi := shares[i][0]
yi := shares[i][index+1]
li := byte(1)
for m := 0; m < len(shares); m++ {
if i != m {
xm := shares[m][0]
li = gfMul(li, gfDiv(xm, gfAdd(xm, xi)))
}
}
secretByte = gfAdd(secretByte, gfMul(yi, li))
}
return secretByte
}