-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathethereum_test.go
73 lines (67 loc) · 1.93 KB
/
ethereum_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
// Copyright (c) 2021 dustinxie. All rights reserved.
//
// Use of this source code is governed by MIT license
// that can be found in the LICENSE file.
package ecc
import (
"bytes"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/sha256"
"testing"
)
func TestEther(t *testing.T) {
curve := P256k1()
param := curve.Params()
for i := 0; i < 16; i++ {
privKey, err := ecdsa.GenerateKey(curve, rand.Reader)
if err != nil {
panic(err)
}
msg := sha256.Sum256(privKey.PublicKey.X.Bytes())
sig, err := SignEthereum(msg[:], privKey)
if err != nil {
t.Errorf("SignEthereum failed for %T", curve)
}
// verify both uncompressed and compressed public key
pubkey := make([][]byte, 2)
pubkey[0] = elliptic.Marshal(curve, privKey.X, privKey.Y)
pubkey[1] = elliptic.MarshalCompressed(curve, privKey.X, privKey.Y)
for _, pk := range pubkey {
if VerifyEthereum(pk[:len(pk)-1], msg[:], sig[:64], false) {
t.Error("VerifyEthereum passed with wrong public key length")
}
if VerifyEthereum(pk, msg[:31], sig[:64], false) {
t.Error("VerifyEthereum passed with wrong msg length")
}
if VerifyEthereum(pk, msg[:], sig, false) {
t.Error("VerifyEthereum passed with wrong sig length")
}
if !VerifyEthereum(pk, msg[:], sig[:64], false) {
t.Error("VerifyEthereum failed")
}
if !VerifyEthereum(pk, msg[:], sig[:64], true) {
t.Error("VerifyEthereum failed")
}
r, s, _ := decodeSigBytes(param, sig)
s.Sub(param.N, s)
rs := make([]byte, 64)
r.FillBytes(rs[:32])
s.FillBytes(rs[32:])
if !VerifyEthereum(pk, msg[:], rs, false) {
t.Error("VerifyEthereum failed")
}
if VerifyEthereum(pk, msg[:], rs, true) {
t.Error("VerifyEthereum passed with higher s value")
}
}
pk, err := RecoverEthereum(msg[:], sig)
if err != nil {
t.Error("RecoverEthereum failed")
}
if bytes.Compare(pk, pubkey[0]) != 0 {
t.Errorf("Recovered key %x not equal to %x", pk, pubkey[0])
}
}
}