aboutsummaryrefslogtreecommitdiffstats
path: root/crypto/secp256k1/secp256_test.go
blob: 5e657cd7241c9c8d3c97c8c458338b4c74c4f514 (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
package secp256k1

import (
    "bytes"
    "fmt"
    "github.com/ethereum/go-ethereum/crypto/randentropy"
    "log"
    "testing"
)

const TESTS = 10000 // how many tests
const SigSize = 65  //64+1

func Test_Secp256_00(t *testing.T) {

    var nonce []byte = randentropy.GetEntropyMixed(32) //going to get bitcoins stolen!

    if len(nonce) != 32 {
        t.Fatal()
    }

}

//tests for Malleability
//highest bit of S must be 0; 32nd byte
func CompactSigTest(sig []byte) {

    var b int = int(sig[32])
    if b < 0 {
        log.Panic()
    }
    if ((b >> 7) == 1) != ((b & 0x80) == 0x80) {
        log.Panic("b= %v b2= %v \n", b, b>>7)
    }
    if (b & 0x80) == 0x80 {
        log.Panic("b= %v b2= %v \n", b, b&0x80)
    }
}

//test pubkey/private generation
func Test_Secp256_01(t *testing.T) {
    pubkey, seckey := GenerateKeyPair()
    if err := VerifySeckeyValidity(seckey); err != nil {
        t.Fatal()
    }
    if err := VerifyPubkeyValidity(pubkey); err != nil {
        t.Fatal()
    }
}

//test size of messages
func Test_Secp256_02s(t *testing.T) {
    pubkey, seckey := GenerateKeyPair()
    msg := randentropy.GetEntropyMixed(32)
    sig, _ := Sign(msg, seckey)
    CompactSigTest(sig)
    if sig == nil {
        t.Fatal("Signature nil")
    }
    if len(pubkey) != 65 {
        t.Fail()
    }
    if len(seckey) != 32 {
        t.Fail()
    }
    if len(sig) != 64+1 {
        t.Fail()
    }
    if int(sig[64]) > 4 {
        t.Fail()
    } //should be 0 to 4
}

//test signing message
func Test_Secp256_02(t *testing.T) {
    pubkey1, seckey := GenerateKeyPair()
    msg := randentropy.GetEntropyMixed(32)
    sig, _ := Sign(msg, seckey)
    if sig == nil {
        t.Fatal("Signature nil")
    }

    pubkey2, _ := RecoverPubkey(msg, sig)
    if pubkey2 == nil {
        t.Fatal("Recovered pubkey invalid")
    }
    if bytes.Equal(pubkey1, pubkey2) == false {
        t.Fatal("Recovered pubkey does not match")
    }

    err := VerifySignature(msg, sig, pubkey1)
    if err != nil {
        t.Fatal("Signature invalid")
    }
}

//test pubkey recovery
func Test_Secp256_02a(t *testing.T) {
    pubkey1, seckey1 := GenerateKeyPair()
    msg := randentropy.GetEntropyMixed(32)
    sig, _ := Sign(msg, seckey1)

    if sig == nil {
        t.Fatal("Signature nil")
    }
    err := VerifySignature(msg, sig, pubkey1)
    if err != nil {
        t.Fatal("Signature invalid")
    }

    pubkey2, _ := RecoverPubkey(msg, sig)
    if len(pubkey1) != len(pubkey2) {
        t.Fatal()
    }
    for i, _ := range pubkey1 {
        if pubkey1[i] != pubkey2[i] {
            t.Fatal()
        }
    }
    if bytes.Equal(pubkey1, pubkey2) == false {
        t.Fatal()
    }
}

//test random messages for the same pub/private key
func Test_Secp256_03(t *testing.T) {
    _, seckey := GenerateKeyPair()
    for i := 0; i < TESTS; i++ {
        msg := randentropy.GetEntropyMixed(32)
        sig, _ := Sign(msg, seckey)
        CompactSigTest(sig)

        sig[len(sig)-1] %= 4
        pubkey2, _ := RecoverPubkey(msg, sig)
        if pubkey2 == nil {
            t.Fail()
        }
    }
}

//test random messages for different pub/private keys
func Test_Secp256_04(t *testing.T) {
    for i := 0; i < TESTS; i++ {
        pubkey1, seckey := GenerateKeyPair()
        msg := randentropy.GetEntropyMixed(32)
        sig, _ := Sign(msg, seckey)
        CompactSigTest(sig)

        if sig[len(sig)-1] >= 4 {
            t.Fail()
        }
        pubkey2, _ := RecoverPubkey(msg, sig)
        if pubkey2 == nil {
            t.Fail()
        }
        if bytes.Equal(pubkey1, pubkey2) == false {
            t.Fail()
        }
    }
}

//test random signatures against fixed messages; should fail

//crashes:
//  -SIPA look at this

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

func Test_Secp256_06a_alt0(t *testing.T) {
    pubkey1, seckey := GenerateKeyPair()
    msg := randentropy.GetEntropyMixed(32)
    sig, _ := Sign(msg, seckey)

    if sig == nil {
        t.Fail()
    }
    if len(sig) != 65 {
        t.Fail()
    }
    for i := 0; i < TESTS; i++ {
        sig = randSig()
        pubkey2, _ := RecoverPubkey(msg, sig)

        if bytes.Equal(pubkey1, pubkey2) == true {
            t.Fail()
        }

        if pubkey2 != nil && VerifySignature(msg, sig, pubkey2) != nil {
            t.Fail()
        }

        if VerifySignature(msg, sig, pubkey1) == nil {
            t.Fail()
        }
    }
}

//test random messages against valid signature: should fail

func Test_Secp256_06b(t *testing.T) {
    pubkey1, seckey := GenerateKeyPair()
    msg := randentropy.GetEntropyMixed(32)
    sig, _ := Sign(msg, seckey)

    fail_count := 0
    for i := 0; i < TESTS; i++ {
        msg = randentropy.GetEntropyMixed(32)
        pubkey2, _ := RecoverPubkey(msg, sig)
        if bytes.Equal(pubkey1, pubkey2) == true {
            t.Fail()
        }

        if pubkey2 != nil && VerifySignature(msg, sig, pubkey2) != nil {
            t.Fail()
        }

        if VerifySignature(msg, sig, pubkey1) == nil {
            t.Fail()
        }
    }
    if fail_count != 0 {
        fmt.Printf("ERROR: Accepted signature for %v of %v random messages\n", fail_count, TESTS)
    }
}