forked from solana-foundation/solana-go
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdecode.go
More file actions
222 lines (193 loc) · 5.5 KB
/
decode.go
File metadata and controls
222 lines (193 loc) · 5.5 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
package base58
import (
"encoding/binary"
"errors"
)
var (
ErrInvalidChar = errors.New("base58: invalid base58 character")
ErrInvalidLength = errors.New("base58: invalid encoded length")
ErrValueTooLarge = errors.New("base58: decoded value too large for output size")
ErrLeadingZeros = errors.New("base58: leading '1' count does not match leading zero bytes")
)
// Decode decodes a base58 string to bytes. Each leading '1' in encoded
// produces a leading zero byte in the output. Empty input produces an empty
// (non-nil) slice.
//
// Encoded lengths matching a 32 or 64-byte representation — the common Solana
// sizes — are dispatched to the matrix-multiply fast paths (Decode32 /
// Decode64), which are ~10x faster than the long-multiplication fallback. A
// 32-byte value always encodes to 32-44 base58 chars; 64-byte to 64-88. The
// fast paths reject inputs whose natural byte count differs (via leading-zero
// validation), so we fall back to long multiplication on error.
func Decode(encoded string) ([]byte, error) {
if len(encoded) == 0 {
return []byte{}, nil
}
encLen := len(encoded)
if encLen >= 32 && encLen <= EncodedMaxLen32 {
var dst [32]byte
if err := Decode32(encoded, &dst); err == nil {
out := make([]byte, 32)
copy(out, dst[:])
return out, nil
}
}
if encLen >= 64 && encLen <= EncodedMaxLen64 {
var dst [64]byte
if err := Decode64(encoded, &dst); err == nil {
out := make([]byte, 64)
copy(out, dst[:])
return out, nil
}
}
zeros := 0
for zeros < len(encoded) && encoded[zeros] == '1' {
zeros++
}
if zeros == len(encoded) {
return make([]byte, zeros), nil
}
// Upper bound on byte count of the non-leading-zero portion:
// ceil(n * log(58)/log(256)) ~ n * 0.7322. Use 733/1000 + 1 for safety.
size := ((len(encoded)-zeros)*733)/1000 + 1
work := make([]byte, size)
for i := zeros; i < len(encoded); i++ {
c := encoded[i]
if c < '1' || c > 'z' {
return nil, ErrInvalidChar
}
digit := base58Inverse[c-'1']
if digit == base58InvalidDigit {
return nil, ErrInvalidChar
}
// work = work * 58 + digit, treating work as a big-endian bigint.
carry := uint32(digit)
for j := len(work) - 1; j >= 0; j-- {
cur := uint32(work[j])*58 + carry
work[j] = byte(cur)
carry = cur >> 8
}
if carry != 0 {
return nil, ErrValueTooLarge
}
}
skip := 0
for skip < len(work) && work[skip] == 0 {
skip++
}
out := make([]byte, zeros+len(work)-skip)
copy(out[zeros:], work[skip:])
return out, nil
}
// Decode32 decodes a base58 string into a 32-byte array.
func Decode32(encoded string, dst *[32]byte) error {
encLen := len(encoded)
if encLen == 0 || encLen > raw58Sz32 {
return ErrInvalidLength
}
var raw [raw58Sz32]byte
offset := raw58Sz32 - encLen
for i := range encLen {
c := encoded[i]
if c < '1' || c > 'z' {
return ErrInvalidChar
}
digit := base58Inverse[c-'1']
if digit == base58InvalidDigit {
return ErrInvalidChar
}
raw[offset+i] = digit
}
var intermediate [intermediateSz32]uint64
for i := range intermediateSz32 {
intermediate[i] = uint64(raw[5*i+0])*11316496 +
uint64(raw[5*i+1])*195112 +
uint64(raw[5*i+2])*3364 +
uint64(raw[5*i+3])*58 +
uint64(raw[5*i+4])
}
// Matrix-vector multiply (assembly on arm64, Go on other archs).
var bin [binarySz32]uint64
decodeMatMul32(&intermediate, &bin)
for i := binarySz32 - 1; i >= 1; i-- {
bin[i-1] += bin[i] >> 32
bin[i] &= 0xFFFFFFFF
}
if bin[0] > 0xFFFFFFFF {
return ErrValueTooLarge
}
for i := range binarySz32 {
binary.BigEndian.PutUint32(dst[i*4:i*4+4], uint32(bin[i]))
}
return validateLeadingZeros(encoded, dst[:])
}
// Decode64 decodes a base58 string into a 64-byte array.
func Decode64(encoded string, dst *[64]byte) error {
encLen := len(encoded)
if encLen == 0 || encLen > raw58Sz64 {
return ErrInvalidLength
}
var raw [raw58Sz64]byte
offset := raw58Sz64 - encLen
for i := range encLen {
c := encoded[i]
if c < '1' || c > 'z' {
return ErrInvalidChar
}
digit := base58Inverse[c-'1']
if digit == base58InvalidDigit {
return ErrInvalidChar
}
raw[offset+i] = digit
}
var intermediate [intermediateSz64]uint64
for i := range intermediateSz64 {
intermediate[i] = uint64(raw[5*i+0])*11316496 +
uint64(raw[5*i+1])*195112 +
uint64(raw[5*i+2])*3364 +
uint64(raw[5*i+3])*58 +
uint64(raw[5*i+4])
}
// Plain uint64 accumulation — each product is ≤ 2^62 and the sum
// of 18 terms stays under 2^64 (verified by Firedancer analysis).
var bin [binarySz64]uint64
for k := range binarySz64 {
var acc uint64
for i := range intermediateSz64 {
acc += intermediate[i] * uint64(decTable64[i][k])
}
bin[k] = acc
}
for i := binarySz64 - 1; i >= 1; i-- {
bin[i-1] += bin[i] >> 32
bin[i] &= 0xFFFFFFFF
}
if bin[0] > 0xFFFFFFFF {
return ErrValueTooLarge
}
for i := range binarySz64 {
binary.BigEndian.PutUint32(dst[i*4:i*4+4], uint32(bin[i]))
}
return validateLeadingZeros(encoded, dst[:])
}
// validateLeadingZeros verifies that the number of leading '1' characters in
// the encoded input equals the number of leading zero bytes in the decoded
// output. This is a required invariant of base58: each leading zero byte in
// the raw value is represented by exactly one '1' in the encoding.
func validateLeadingZeros(encoded string, dst []byte) error {
inLeading1s := 0
for i := 0; i < len(encoded) && encoded[i] == '1'; i++ {
inLeading1s++
}
outLeading0s := 0
for _, b := range dst {
if b != 0 {
break
}
outLeading0s++
}
if inLeading1s != outLeading0s {
return ErrLeadingZeros
}
return nil
}