1
2
3
4
5
6 package secp256k1
7
8 import (
9 "bytes"
10 cryptorand "crypto/rand"
11 "errors"
12 "math/big"
13 "testing"
14 )
15
16
17 func TestGeneratePrivateKey(t *testing.T) {
18 priv, err := GeneratePrivateKey()
19 if err != nil {
20 t.Errorf("failed to generate private key: %s", err)
21 return
22 }
23 pub := priv.PubKey()
24 if !isOnCurve(&pub.x, &pub.y) {
25 t.Error("public key is not on the curve")
26 }
27 }
28
29
30
31 func TestGeneratePrivateKeyFromRand(t *testing.T) {
32 priv, err := GeneratePrivateKeyFromRand(cryptorand.Reader)
33 if err != nil {
34 t.Errorf("failed to generate private key: %s", err)
35 return
36 }
37 pub := priv.PubKey()
38 if !isOnCurve(&pub.x, &pub.y) {
39 t.Error("public key is not on the curve")
40 }
41 }
42
43
44
45 type mockPrivateKeyReaderFunc func([]byte) (int, error)
46
47
48 func (f mockPrivateKeyReaderFunc) Read(p []byte) (int, error) {
49 return f(p)
50 }
51
52
53
54
55
56 func TestGeneratePrivateKeyCorners(t *testing.T) {
57
58
59
60
61
62 oneModN := hexToModNScalar("01")
63 var numReads int
64 mockReader := mockPrivateKeyReaderFunc(func(p []byte) (int, error) {
65 numReads++
66 switch numReads {
67 case 1:
68 return copy(p, bytes.Repeat([]byte{0x00}, len(p))), nil
69 case 2:
70 return copy(p, curveParams.N.Bytes()), nil
71 case 3:
72 nPlusOne := new(big.Int).Add(curveParams.N, big.NewInt(1))
73 return copy(p, nPlusOne.Bytes()), nil
74 }
75 oneModNBytes := oneModN.Bytes()
76 return copy(p, oneModNBytes[:]), nil
77 })
78
79
80
81
82 priv, err := GeneratePrivateKeyFromRand(mockReader)
83 if err != nil {
84 t.Errorf("failed to generate private key: %s", err)
85 return
86 }
87 if !priv.Key.Equals(oneModN) {
88 t.Fatalf("unexpected private key -- got: %x, want %x", priv.Serialize(),
89 oneModN.Bytes())
90 }
91 }
92
93
94
95 func TestGeneratePrivateKeyError(t *testing.T) {
96
97 errDisabled := errors.New("disabled")
98 mockReader := mockPrivateKeyReaderFunc(func(p []byte) (int, error) {
99 return 0, errDisabled
100 })
101
102
103
104 _, err := GeneratePrivateKeyFromRand(mockReader)
105 if !errors.Is(err, errDisabled) {
106 t.Fatalf("mismatched err -- got %v, want %v", err, errDisabled)
107 return
108 }
109 }
110
111
112
113 func TestPrivKeys(t *testing.T) {
114 tests := []struct {
115 name string
116 priv string
117 pub string
118 }{{
119 name: "random private key 1",
120 priv: "eaf02ca348c524e6392655ba4d29603cd1a7347d9d65cfe93ce1ebffdca22694",
121 pub: "025ceeba2ab4a635df2c0301a3d773da06ac5a18a7c3e0d09a795d7e57d233edf1",
122 }, {
123 name: "random private key 2",
124 priv: "24b860d0651db83feba821e7a94ba8b87162665509cefef0cbde6a8fbbedfe7c",
125 pub: "032a6e51bf218085647d330eac2fafaeee07617a777ad9e8e7141b4cdae92cb637",
126 }}
127
128 for _, test := range tests {
129
130 privKeyBytes := hexToBytes(test.priv)
131 wantPubKeyBytes := hexToBytes(test.pub)
132
133 priv := PrivKeyFromBytes(privKeyBytes)
134 pub := priv.PubKey()
135
136 serializedPubKey := pub.SerializeCompressed()
137 if !bytes.Equal(serializedPubKey, wantPubKeyBytes) {
138 t.Errorf("%s unexpected serialized public key - got: %x, want: %x",
139 test.name, serializedPubKey, wantPubKeyBytes)
140 }
141
142 serializedPrivKey := priv.Serialize()
143 if !bytes.Equal(serializedPrivKey, privKeyBytes) {
144 t.Errorf("%s unexpected serialized private key - got: %x, want: %x",
145 test.name, serializedPrivKey, privKeyBytes)
146 }
147 }
148 }
149
150
151
152 func TestPrivateKeyZero(t *testing.T) {
153
154
155 key := new(ModNScalar).SetHex("eaf02ca348c524e6392655ba4d29603cd1a7347d9d65cfe93ce1ebffdca22694")
156 privKey := NewPrivateKey(key)
157 key.Zero()
158
159
160 if privKey.Key.IsZero() {
161 t.Fatal("private key is zero when it should be non zero")
162 }
163
164
165 privKey.Zero()
166 if !privKey.Key.IsZero() {
167 t.Fatal("private key is non zero when it should be zero")
168 }
169 }
170
View as plain text