-
Notifications
You must be signed in to change notification settings - Fork 0
/
secp256k1_test.go
61 lines (55 loc) · 1.65 KB
/
secp256k1_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
package secp256k1
import (
"crypto/ecdsa"
"fmt"
"strings"
"testing"
"github.com/ethereum/go-ethereum/crypto"
secp_eth "github.com/ethereum/go-ethereum/crypto/secp256k1"
"github.com/stretchr/testify/require"
)
const (
sk string = "0x5d7da1c2a925ea9c454c120806360428e91756b98d4ade2bef09fd924c02e803" // test private key
message string = "hello" // test message
)
func TestSign(t *testing.T) {
skECDSA, err := crypto.HexToECDSA(strings.TrimPrefix(sk, "0x"))
require.Nil(t, err)
hash := crypto.Keccak256Hash([]byte(message))
r, s, err := SignRFC6979(skECDSA, hash)
require.Nil(t, err)
// verify result
pub := skECDSA.Public()
pubECDSA, ok := pub.(*ecdsa.PublicKey)
require.True(t, ok)
require.True(t, ecdsa.Verify(pubECDSA, hash.Bytes(), r, s))
// print result
fmt.Println("r", r.Text(16))
fmt.Println("s", s.Text(16))
}
func TestGetPublicKey(t *testing.T) {
skECDSA, err := crypto.HexToECDSA(strings.TrimPrefix(sk, "0x"))
require.Nil(t, err)
r, err := GetPublicKey(skECDSA)
require.Nil(t, err)
// verify result
pub := skECDSA.Public()
pubECDSA, ok := pub.(*ecdsa.PublicKey)
require.True(t, ok)
require.True(t, pubECDSA.Equal(r))
// print result
fmt.Println("pub.x", r.X.Text(16))
fmt.Println("pub.y", r.Y.Text(16))
}
func TestScalarMult(t *testing.T) {
skECDSA, err := crypto.HexToECDSA(strings.TrimPrefix(sk, "0x"))
require.Nil(t, err)
curve := secp_eth.S256()
x, y, err := ScalarMult(skECDSA, curve.Gx, curve.Gy)
require.Nil(t, err)
// verify result
require.True(t, curve.IsOnCurve(x, y))
// print result
fmt.Println("curve.x", x.Text(16))
fmt.Println("curve.y", y.Text(16))
}