-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathecdsa_test.go
150 lines (114 loc) · 4.01 KB
/
ecdsa_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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package crypto_test
import (
"crypto/ecdsa"
"crypto/rand"
"encoding/json"
"runtime"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/republicprotocol/crypto-go"
"github.com/republicprotocol/co-go"
)
var _ = Describe("Ecdsa keys", func() {
Context("when generating", func() {
It("should be able to generate a random EcdsaKey without returning an error", func() {
_, err := RandomEcdsaPrivateKey()
Expect(err).ShouldNot(HaveOccurred())
})
It("should equal itself", func() {
key, err := RandomEcdsaPrivateKey()
Expect(err).ShouldNot(HaveOccurred())
Expect(key.Equal(&key)).Should(BeTrue())
})
It("should not equal another randomly generated EcdsaKey", func() {
key1, err := RandomEcdsaPrivateKey()
Expect(err).ShouldNot(HaveOccurred())
key2, err := RandomEcdsaPrivateKey()
Expect(err).ShouldNot(HaveOccurred())
Expect(key1.Equal(&key2)).Should(BeFalse())
})
})
Context("when signing and verifying", func() {
It("should be able to sign a hash", func() {
key, err := RandomEcdsaPrivateKey()
Expect(err).ShouldNot(HaveOccurred())
_, err = key.Sign(Keccak256([]byte("REN")))
Expect(err).ShouldNot(HaveOccurred())
})
It("should be able to verify a signature", func() {
co.ParForAll(runtime.NumCPU(), func(i int) {
key, err := RandomEcdsaPrivateKey()
Expect(err).ShouldNot(HaveOccurred())
data := Keccak256([]byte("REN"))
signature, err := key.Sign(data)
Expect(err).ShouldNot(HaveOccurred())
err = key.Verify(data, signature)
Expect(err).ShouldNot(HaveOccurred())
})
})
It("should be able to return an error when verifying random data", func() {
co.ParForAll(runtime.NumCPU(), func(i int) {
key, err := RandomEcdsaPrivateKey()
Expect(err).ShouldNot(HaveOccurred())
random := make([]byte, 32)
rand.Read(random)
sigRandom := make([]byte, 65)
rand.Read(sigRandom)
err = key.Verify(random, sigRandom)
Expect(err).Should(HaveOccurred())
})
})
It("should be able to return an error when verifying nil data", func() {
co.ParForAll(runtime.NumCPU(), func(i int) {
key, err := RandomEcdsaPrivateKey()
Expect(err).ShouldNot(HaveOccurred())
random := make([]byte, 32)
rand.Read(random)
sigRandom := make([]byte, 65)
rand.Read(sigRandom)
err = key.Verify([]byte{}, sigRandom)
Expect(err).Should(Equal(ErrNilData))
err = key.Verify(nil, sigRandom)
Expect(err).Should(Equal(ErrNilData))
})
})
It("should be able to return an error when verifying nil signatures", func() {
co.ParForAll(runtime.NumCPU(), func(i int) {
key, err := RandomEcdsaPrivateKey()
Expect(err).ShouldNot(HaveOccurred())
random := make([]byte, 32)
rand.Read(random)
sigRandom := make([]byte, 65)
rand.Read(sigRandom)
err = key.Verify(random, []byte{})
Expect(err).Should(Equal(ErrNilSignature))
err = key.Verify(random, nil)
Expect(err).Should(Equal(ErrNilSignature))
})
})
})
Context("when marshaling and unmarshaling", func() {
It("should be able to marshal and unmarshal as JSON", func() {
key, err := RandomEcdsaPrivateKey()
Expect(err).ShouldNot(HaveOccurred())
data, err := key.MarshalJSON()
Expect(err).ShouldNot(HaveOccurred())
keyDecoded := EcdsaPrivateKey{}
err = keyDecoded.UnmarshalJSON(data)
Expect(err).ShouldNot(HaveOccurred())
Expect(key.Equal(&keyDecoded)).Should(BeTrue())
Expect("s256").Should(Equal(keyDecoded.Curve.Params().Name)) // We explicitly name the curve here because the ethSecp256k1.S256() curve implementation does not include a name
})
It("should panic when marshalling a badly formatted JSON", func() {
key, err := RandomEcdsaPrivateKey()
Expect(err).ShouldNot(HaveOccurred())
key.PrivateKey.PublicKey = ecdsa.PublicKey{}
Expect(func() { json.Marshal(key) }).Should(Panic())
})
It("should return an error for invalid JSON", func() {
keyDecoded := EcdsaPrivateKey{}
err := keyDecoded.UnmarshalJSON([]byte{byte(1)})
Expect(err).Should(HaveOccurred())
})
})
})