-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshamir_test.go
129 lines (125 loc) · 3.13 KB
/
shamir_test.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
package shamir_test
import (
"strconv"
"testing"
shamir "github.com/jmastr/trezor-shamir-go"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestInterpolate(t *testing.T) {
t.Parallel()
// test vectors can be found in `./trezor-firmware/crypto/tests/test_check.c` line 6185
testCases := []struct {
result []uint8
resultIndex uint8
shareIndices []uint8
shareValues [][]uint8
shareCount uint8
length uint8
ret bool
}{
{
result: []uint8{7, 151, 168, 57, 186, 104, 218, 21, 209, 96, 106, 152, 252, 35, 210, 208, 43, 47, 13, 21, 142, 122, 24, 42, 149, 192, 95, 24, 240, 24, 148, 110},
resultIndex: 0,
shareIndices: []uint8{2},
shareValues: [][]uint8{
{7, 151, 168, 57, 186, 104, 218, 21, 209, 96, 106, 152, 252, 35, 210, 208, 43, 47, 13, 21, 142, 122, 24, 42, 149, 192, 95, 24, 240, 24, 148, 110},
},
shareCount: 1,
length: 32,
ret: true,
},
{
result: []uint8{53},
resultIndex: 255,
shareIndices: []uint8{14, 10, 1, 13, 8, 7, 3, 11, 9, 4, 6, 0, 5, 12, 15, 2},
shareValues: [][]uint8{
{114},
{41},
{116},
{67},
{198},
{109},
{232},
{39},
{90},
{241},
{156},
{75},
{46},
{181},
{144},
{175},
},
shareCount: 16,
length: 1,
ret: true,
},
{
result: []uint8{91, 188, 226, 91, 254, 197, 225},
resultIndex: 1,
shareIndices: []uint8{5, 1, 10},
shareValues: [][]uint8{
{129, 18, 104, 86, 236, 73, 176},
{91, 188, 226, 91, 254, 197, 225},
{69, 53, 151, 204, 224, 37, 19},
},
shareCount: 3,
length: 7,
ret: true,
},
{
result: []uint8{0},
resultIndex: 1,
shareIndices: []uint8{5, 1, 1},
shareValues: [][]uint8{
{129, 18, 104, 86, 236, 73, 176},
{91, 188, 226, 91, 254, 197, 225},
{69, 53, 151, 204, 224, 37, 19},
},
shareCount: 3,
length: 7,
ret: false,
},
{
result: []uint8{0},
resultIndex: 255,
shareIndices: []uint8{3, 12, 3},
shareValues: [][]uint8{
{100, 176, 99, 142, 115, 192, 138},
{54, 139, 99, 172, 29, 137, 58},
{216, 119, 222, 40, 87, 25, 147},
},
shareCount: 3,
length: 7,
ret: false,
},
{
result: []uint8{163, 120, 30, 243, 179, 172, 196, 137, 119, 17},
resultIndex: 3,
shareIndices: []uint8{1, 0, 12},
shareValues: [][]uint8{
{80, 180, 198, 131, 111, 251, 45, 181, 2, 242},
{121, 9, 79, 98, 132, 164, 9, 165, 19, 230},
{86, 52, 173, 138, 189, 223, 122, 102, 248, 157},
},
shareCount: 3,
length: 10,
ret: true,
},
}
for index, testCase := range testCases {
index, testCase := index, testCase
t.Run(strconv.Itoa(index), func(t *testing.T) {
t.Parallel()
result, err := shamir.Interpolate(testCase.resultIndex, testCase.shareIndices, testCase.shareValues, testCase.shareCount, testCase.length)
if testCase.ret {
require.NoError(t, err)
} else {
require.Error(t, err)
assert.Equal(t, "interpolate failed", shamir.ErrInterpolateFailed.Error())
}
assert.Equal(t, testCase.result, result)
})
}
}