aboutsummaryrefslogtreecommitdiffstats
path: root/crypto/secp256k1/secp256_test.go
blob: b608bcfcf107c0fe73e97fc362b04163a9236f35 (plain) (blame)
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// Copyright 2015 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

package secp256k1

import (
    "bytes"
    "crypto/ecdsa"
    "crypto/elliptic"
    "crypto/rand"
    "encoding/hex"
    "testing"

    "github.com/ethereum/go-ethereum/common/math"
    "github.com/ethereum/go-ethereum/crypto/randentropy"
)

const TestCount = 1000

func generateKeyPair() (pubkey, privkey []byte) {
    key, err := ecdsa.GenerateKey(S256(), rand.Reader)
    if err != nil {
        panic(err)
    }
    pubkey = elliptic.Marshal(S256(), key.X, key.Y)
    return pubkey, math.PaddedBigBytes(key.D, 32)
}

func randSig() []byte {
    sig := randentropy.GetEntropyCSPRNG(65)
    sig[32] &= 0x70
    sig[64] %= 4
    return sig
}

// tests for malleability
// highest bit of signature ECDSA s value must be 0, in the 33th byte
func compactSigCheck(t *testing.T, sig []byte) {
    var b = int(sig[32])
    if b < 0 {
        t.Errorf("highest bit is negative: %d", b)
    }
    if ((b >> 7) == 1) != ((b & 0x80) == 0x80) {
        t.Errorf("highest bit: %d bit >> 7: %d", b, b>>7)
    }
    if (b & 0x80) == 0x80 {
        t.Errorf("highest bit: %d bit & 0x80: %d", b, b&0x80)
    }
}

func TestSignatureValidity(t *testing.T) {
    pubkey, seckey := generateKeyPair()
    msg := randentropy.GetEntropyCSPRNG(32)
    sig, err := Sign(msg, seckey)
    if err != nil {
        t.Errorf("signature error: %s", err)
    }
    compactSigCheck(t, sig)
    if len(pubkey) != 65 {
        t.Errorf("pubkey length mismatch: want: 65 have: %d", len(pubkey))
    }
    if len(seckey) != 32 {
        t.Errorf("seckey length mismatch: want: 32 have: %d", len(seckey))
    }
    if len(sig) != 65 {
        t.Errorf("sig length mismatch: want: 65 have: %d", len(sig))
    }
    recid := int(sig[64])
    if recid > 4 || recid < 0 {
        t.Errorf("sig recid mismatch: want: within 0 to 4 have: %d", int(sig[64]))
    }
}

func TestInvalidRecoveryID(t *testing.T) {
    _, seckey := generateKeyPair()
    msg := randentropy.GetEntropyCSPRNG(32)
    sig, _ := Sign(msg, seckey)
    sig[64] = 99
    _, err := RecoverPubkey(msg, sig)
    if err != ErrInvalidRecoveryID {
        t.Fatalf("got %q, want %q", err, ErrInvalidRecoveryID)
    }
}

func TestSignAndRecover(t *testing.T) {
    pubkey1, seckey := generateKeyPair()
    msg := randentropy.GetEntropyCSPRNG(32)
    sig, err := Sign(msg, seckey)
    if err != nil {
        t.Errorf("signature error: %s", err)
    }
    pubkey2, err := RecoverPubkey(msg, sig)
    if err != nil {
        t.Errorf("recover error: %s", err)
    }
    if !bytes.Equal(pubkey1, pubkey2) {
        t.Errorf("pubkey mismatch: want: %x have: %x", pubkey1, pubkey2)
    }
}

func TestSignDeterministic(t *testing.T) {
    _, seckey := generateKeyPair()
    msg := make([]byte, 32)
    copy(msg, "hi there")

    sig1, err := Sign(msg, seckey)
    if err != nil {
        t.Fatal(err)
    }
    sig2, err := Sign(msg, seckey)
    if err != nil {
        t.Fatal(err)
    }
    if !bytes.Equal(sig1, sig2) {
        t.Fatal("signatures not equal")
    }
}

func TestRandomMessagesWithSameKey(t *testing.T) {
    pubkey, seckey := generateKeyPair()
    keys := func() ([]byte, []byte) {
        return pubkey, seckey
    }
    signAndRecoverWithRandomMessages(t, keys)
}

func TestRandomMessagesWithRandomKeys(t *testing.T) {
    keys := func() ([]byte, []byte) {
        pubkey, seckey := generateKeyPair()
        return pubkey, seckey
    }
    signAndRecoverWithRandomMessages(t, keys)
}

func signAndRecoverWithRandomMessages(t *testing.T, keys func() ([]byte, []byte)) {
    for i := 0; i < TestCount; i++ {
        pubkey1, seckey := keys()
        msg := randentropy.GetEntropyCSPRNG(32)
        sig, err := Sign(msg, seckey)
        if err != nil {
            t.Fatalf("signature error: %s", err)
        }
        if sig == nil {
            t.Fatal("signature is nil")
        }
        compactSigCheck(t, sig)

        // TODO: why do we flip around the recovery id?
        sig[len(sig)-1] %= 4

        pubkey2, err := RecoverPubkey(msg, sig)
        if err != nil {
            t.Fatalf("recover error: %s", err)
        }
        if pubkey2 == nil {
            t.Error("pubkey is nil")
        }
        if !bytes.Equal(pubkey1, pubkey2) {
            t.Fatalf("pubkey mismatch: want: %x have: %x", pubkey1, pubkey2)
        }
    }
}

func TestRecoveryOfRandomSignature(t *testing.T) {
    pubkey1, _ := generateKeyPair()
    msg := randentropy.GetEntropyCSPRNG(32)

    for i := 0; i < TestCount; i++ {
        // recovery can sometimes work, but if so should always give wrong pubkey
        pubkey2, _ := RecoverPubkey(msg, randSig())
        if bytes.Equal(pubkey1, pubkey2) {
            t.Fatalf("iteration: %d: pubkey mismatch: do NOT want %x: ", i, pubkey2)
        }
    }
}

func TestRandomMessagesAgainstValidSig(t *testing.T) {
    pubkey1, seckey := generateKeyPair()
    msg := randentropy.GetEntropyCSPRNG(32)
    sig, _ := Sign(msg, seckey)

    for i := 0; i < TestCount; i++ {
        msg = randentropy.GetEntropyCSPRNG(32)
        pubkey2, _ := RecoverPubkey(msg, sig)
        // recovery can sometimes work, but if so should always give wrong pubkey
        if bytes.Equal(pubkey1, pubkey2) {
            t.Fatalf("iteration: %d: pubkey mismatch: do NOT want %x: ", i, pubkey2)
        }
    }
}

// Useful when the underlying libsecp256k1 API changes to quickly
// check only recover function without use of signature function
func TestRecoverSanity(t *testing.T) {
    msg, _ := hex.DecodeString("ce0677bb30baa8cf067c88db9811f4333d131bf8bcf12fe7065d211dce971008")
    sig, _ := hex.DecodeString("90f27b8b488db00b00606796d2987f6a5f59ae62ea05effe84fef5b8b0e549984a691139ad57a3f0b906637673aa2f63d1f55cb1a69199d4009eea23ceaddc9301")
    pubkey1, _ := hex.DecodeString("04e32df42865e97135acfb65f3bae71bdc86f4d49150ad6a440b6f15878109880a0a2b2667f7e725ceea70c673093bf67663e0312623c8e091b13cf2c0f11ef652")
    pubkey2, err := RecoverPubkey(msg, sig)
    if err != nil {
        t.Fatalf("recover error: %s", err)
    }
    if !bytes.Equal(pubkey1, pubkey2) {
        t.Errorf("pubkey mismatch: want: %x have: %x", pubkey1, pubkey2)
    }
}

func BenchmarkSign(b *testing.B) {
    _, seckey := generateKeyPair()
    msg := randentropy.GetEntropyCSPRNG(32)
    b.ResetTimer()

    for i := 0; i < b.N; i++ {
        Sign(msg, seckey)
    }
}

func BenchmarkRecover(b *testing.B) {
    msg := randentropy.GetEntropyCSPRNG(32)
    _, seckey := generateKeyPair()
    sig, _ := Sign(msg, seckey)
    b.ResetTimer()

    for i := 0; i < b.N; i++ {
        RecoverPubkey(msg, sig)
    }
}