forked from solana-foundation/solana-go
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbase58_test.go
More file actions
228 lines (198 loc) · 5.59 KB
/
base58_test.go
File metadata and controls
228 lines (198 loc) · 5.59 KB
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
package base58
import (
"crypto/rand"
"encoding/hex"
"testing"
mrtronbase58 "github.com/mr-tron/base58"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// Known test vectors cross-validated against multiple base58 implementations
// (Bitcoin Core, bs58, mr-tron, five8). Any implementation that encodes these
// bytes to the given strings — and decodes them back — is bit-compatible.
var knownVectors32 = []struct {
hex string
b58 string
}{
{
"0000000000000000000000000000000000000000000000000000000000000000",
"11111111111111111111111111111111",
},
{
"0000000000000000000000000000000000000000000000000000000000000001",
"11111111111111111111111111111112",
},
{
// Solana pubkey: 4cHoJNmLed5PBgFBezHmJkMJLEZrcTvr3aopjnYBRxUb
"359d6209a1296a422463405b82829cf2f0a86b2e87077c80a74372841e185efc",
"4cHoJNmLed5PBgFBezHmJkMJLEZrcTvr3aopjnYBRxUb",
},
}
var knownVectors64 = []struct {
hex string
b58 string
}{
{
// Solana signature: 5YBLhMBLjhAHnEPnHKLLnVwHSfXGPJMCvKAfNsiaEw2T63edrYxVFHKUxRXfP6KA1HVo7c9JZ3LAJQR72giX7Cb
// Hex cross-checked against Python's `base58` package.
"03e9bb70b0ae091b4a3233dc952a2da569afaa0ae1c06aa7d3c2a4da2f2854ec76dfae30d9474b4593726761345bec7ce1a95812c1fa8ddc740314cb29fef458",
"5YBLhMBLjhAHnEPnHKLLnVwHSfXGPJMCvKAfNsiaEw2T63edrYxVFHKUxRXfP6KA1HVo7c9JZ3LAJQR72giX7Cb",
},
}
func TestEncode32_KnownVectors(t *testing.T) {
for _, tv := range knownVectors32 {
raw, err := hex.DecodeString(tv.hex)
require.NoError(t, err)
var src [32]byte
copy(src[:], raw)
assert.Equal(t, tv.b58, Encode32(&src), "hex=%s", tv.hex)
}
}
func TestDecode32_KnownVectors(t *testing.T) {
for _, tv := range knownVectors32 {
expected, err := hex.DecodeString(tv.hex)
require.NoError(t, err)
var dst [32]byte
err = Decode32(tv.b58, &dst)
require.NoError(t, err)
assert.Equal(t, expected, dst[:], "b58=%s", tv.b58)
}
}
func TestEncode64_KnownVectors(t *testing.T) {
for _, tv := range knownVectors64 {
raw, err := hex.DecodeString(tv.hex)
require.NoError(t, err)
var src [64]byte
copy(src[:], raw)
assert.Equal(t, tv.b58, Encode64(&src), "hex=%s", tv.hex)
}
}
func TestDecode64_KnownVectors(t *testing.T) {
for _, tv := range knownVectors64 {
expected, err := hex.DecodeString(tv.hex)
require.NoError(t, err)
var dst [64]byte
err = Decode64(tv.b58, &dst)
require.NoError(t, err)
assert.Equal(t, expected, dst[:], "b58=%s", tv.b58)
}
}
func TestEncode32_Zeros(t *testing.T) {
var src [32]byte
assert.Equal(t, "11111111111111111111111111111111", Encode32(&src))
}
func TestDecode32_Zeros(t *testing.T) {
var dst [32]byte
require.NoError(t, Decode32("11111111111111111111111111111111", &dst))
assert.Equal(t, [32]byte{}, dst)
}
func TestRoundtrip32_Random(t *testing.T) {
// Cross-check the specialized fixed-size path against mr-tron's
// well-tested general-purpose implementation.
for range 1000 {
var src [32]byte
rand.Read(src[:])
encoded := Encode32(&src)
assert.Equal(t, mrtronbase58.Encode(src[:]), encoded, "encode mismatch for %x", src)
var decoded [32]byte
require.NoError(t, Decode32(encoded, &decoded))
assert.Equal(t, src, decoded, "decode mismatch for %s", encoded)
}
}
func TestRoundtrip64_Random(t *testing.T) {
for range 1000 {
var src [64]byte
rand.Read(src[:])
encoded := Encode64(&src)
assert.Equal(t, mrtronbase58.Encode(src[:]), encoded, "encode mismatch for %x", src)
var decoded [64]byte
require.NoError(t, Decode64(encoded, &decoded))
assert.Equal(t, src, decoded, "decode mismatch for %s", encoded)
}
}
func TestAppendEncode32_ZeroAlloc(t *testing.T) {
var src [32]byte
rand.Read(src[:])
expected := Encode32(&src)
// Pre-sized buffer: should not allocate.
buf := make([]byte, 0, EncodedMaxLen32)
buf = AppendEncode32(buf, &src)
assert.Equal(t, expected, string(buf))
// Append to an existing buffer.
prefix := []byte("pubkey=")
buf2 := make([]byte, 0, len(prefix)+EncodedMaxLen32)
buf2 = append(buf2, prefix...)
buf2 = AppendEncode32(buf2, &src)
assert.Equal(t, "pubkey="+expected, string(buf2))
}
func TestAppendEncode64_ZeroAlloc(t *testing.T) {
var src [64]byte
rand.Read(src[:])
expected := Encode64(&src)
buf := make([]byte, 0, EncodedMaxLen64)
buf = AppendEncode64(buf, &src)
assert.Equal(t, expected, string(buf))
}
func TestDecode_InvalidChars(t *testing.T) {
var dst [32]byte
assert.Error(t, Decode32("0invalid", &dst)) // '0' is not in base58
assert.Error(t, Decode32("I\x00nvalid", &dst))
assert.Error(t, Decode32("Oinvalid", &dst)) // 'O' is not in base58
}
// Benchmarks
var (
benchSrc32 [32]byte
benchSrc64 [64]byte
benchStr32 string
benchStr64 string
)
func init() {
rand.Read(benchSrc32[:])
rand.Read(benchSrc64[:])
benchStr32 = Encode32(&benchSrc32)
benchStr64 = Encode64(&benchSrc64)
}
func BenchmarkBase58_Encode32(b *testing.B) {
src := &benchSrc32
b.SetBytes(32)
for b.Loop() {
Encode32(src)
}
}
func BenchmarkBase58_AppendEncode32(b *testing.B) {
src := &benchSrc32
buf := make([]byte, 0, EncodedMaxLen32)
b.SetBytes(32)
for b.Loop() {
buf = AppendEncode32(buf[:0], src)
}
}
func BenchmarkBase58_AppendEncode64(b *testing.B) {
src := &benchSrc64
buf := make([]byte, 0, EncodedMaxLen64)
b.SetBytes(64)
for b.Loop() {
buf = AppendEncode64(buf[:0], src)
}
}
func BenchmarkBase58_Decode32(b *testing.B) {
var dst [32]byte
b.SetBytes(32)
for b.Loop() {
Decode32(benchStr32, &dst)
}
}
func BenchmarkBase58_Encode64(b *testing.B) {
src := &benchSrc64
b.SetBytes(64)
for b.Loop() {
Encode64(src)
}
}
func BenchmarkBase58_Decode64(b *testing.B) {
var dst [64]byte
b.SetBytes(64)
for b.Loop() {
Decode64(benchStr64, &dst)
}
}