-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathkey_test.go
More file actions
161 lines (145 loc) · 4.1 KB
/
Copy pathkey_test.go
File metadata and controls
161 lines (145 loc) · 4.1 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package bliss
import (
"fmt"
"github.com/LoCCS/bliss/sampler"
"io/ioutil"
"reflect"
"strconv"
"strings"
"testing"
)
func TestGeneratePrivateKey(t *testing.T) {
for i := 0; i <= 4; i++ {
testfile, err := ioutil.ReadFile(fmt.Sprintf("test_data/key_test_%d", i))
if err != nil {
t.Errorf("Failed to open file: %s", err.Error())
}
filecontent := strings.TrimSpace(string(testfile))
vs := strings.Split(filecontent, "\n")
if len(vs) != 3 {
t.Errorf("Error in data read from test_data: len(vs) = %d", len(vs))
}
v1 := strings.Split(strings.TrimSpace(vs[0]), " ")
v2 := strings.Split(strings.TrimSpace(vs[1]), " ")
v3 := strings.Split(strings.TrimSpace(vs[2]), " ")
seed := make([]uint8, sampler.SHA_512_DIGEST_LENGTH)
for i := 0; i < len(seed); i++ {
seed[i] = uint8(i % 8)
}
entropy, err := sampler.NewEntropy(seed)
if err != nil {
t.Errorf("Error in initializing entropy: %s", err.Error())
}
key, err := GeneratePrivateKey(i, entropy)
if err != nil {
t.Errorf("Error in generating private key: %s", err.Error())
}
for j := 0; j < int(key.s1.Size()); j++ {
tmp, err := strconv.Atoi(v1[j])
s1 := key.s1.GetData()
if err != nil {
t.Errorf("Invalid integer: %s", v1[j])
}
if int32(tmp) != s1[j] {
t.Errorf("Wrong s1 at %d: expect %d, got %d", j, tmp, s1[j])
}
tmp, err = strconv.Atoi(v2[j])
s2 := key.s2.GetData()
if err != nil {
t.Errorf("Invalid integer: %s", v2[j])
}
if int32(tmp) != s2[j] {
t.Errorf("Wrong s2 at %d: expect %d, got %d", j, tmp, s2[j])
}
tmp, err = strconv.Atoi(v3[j])
s3 := key.a.GetData()
if err != nil {
t.Errorf("Invalid integer: %s", v3[j])
}
if int32(tmp) != s3[j] {
t.Errorf("Wrong a at %d: expect %d, got %d", j, tmp, s3[j])
}
}
key.Destroy()
}
}
func TestKeyEncodeDecode(t *testing.T) {
for i := 0; i <= 4; i++ {
seed := make([]uint8, sampler.SHA_512_DIGEST_LENGTH)
for i := 0; i < len(seed); i++ {
seed[i] = uint8(i % 8)
}
entropy, err := sampler.NewEntropy(seed)
if err != nil {
t.Errorf("Error in initializing entropy: %s", err.Error())
}
key, err := GeneratePrivateKey(i, entropy)
if err != nil {
t.Errorf("Error in generating private key: %s", err.Error())
}
{
pub := key.PublicKey()
enc := pub.Encode()
tmp, err := DecodePublicKey(enc)
if err != nil {
t.Errorf("Error in decoding public key: %s", err.Error())
}
if !reflect.DeepEqual(pub, tmp) {
t.Errorf("Different public key decoded for version %d!", i)
}
}
{
enc := key.Encode()
tmp, err := DecodePrivateKey(enc)
if err != nil {
t.Errorf("Error in decoding private key: %s", err.Error())
}
if !reflect.DeepEqual(key, tmp) {
t.Errorf("Different private key decoded for version %d!\nOriginal:\n%s\nDecoded:\n%s\n",
i, key.String(), tmp.String())
}
}
key.Destroy()
}
}
func TestKeySerialization(t *testing.T) {
for i := 0; i <= 4; i++ {
seed := make([]uint8, sampler.SHA_512_DIGEST_LENGTH)
for i := 0; i < len(seed); i++ {
seed[i] = uint8(i % 8)
}
entropy, err := sampler.NewEntropy(seed)
if err != nil {
t.Errorf("Error in initializing entropy: %s", err.Error())
}
key, err := GeneratePrivateKey(i, entropy)
if err != nil {
t.Errorf("Error in generating private key: %s", err.Error())
}
{
pub := key.PublicKey()
enc := pub.Serialize()
fmt.Printf("Size of public key for BLISS-%d: %d bytes (%d bits)\n", i, len(enc), len(enc)*8)
tmp, err := DeserializePublicKey(enc)
if err != nil {
t.Errorf("Error in decoding public key: %s", err.Error())
}
if !reflect.DeepEqual(pub, tmp) {
t.Errorf("Different public key decoded for version %d!", i)
}
}
{
enc := key.Serialize()
fmt.Printf("Size of Private key for BLISS-%d: %d bytes (%d bits)\n", i, len(enc), len(enc)*8)
tmp, err := DeserializePrivateKey(enc)
if err != nil {
t.Errorf("Error in decoding private key: %s", err.Error())
}
if !reflect.DeepEqual(key, tmp) {
t.Errorf("Different private key decoded for version %d!\nOriginal:\n%s\nDeserialized:\n%s\n",
i, key.String(), tmp.String())
}
}
key.Destroy()
}
}