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
//! An optimized implementation of [Base58][] encoding/decoding for 32 and 64 byte numbers.
//! This library is based off of the original C implementation from Jump Crypto's [Firedancer]
//! repo which can be found [here]. These algorithms are significantly faster than the commonly used
//! [`bs58`] library for 32 and 64 bytes.
//!
//! # Performance vs. [`bs58`]
//!
//! Algorithm | Speedup
//! -------------|-----------
//! `encode_32` | ~9x
//! `encode_64` | ~13x
//! `decode_32` | ~3x
//! `decode_64` | ~5x
//!
//! [Base58]: https://en.wikipedia.org/wiki/Base58
//! [`bs58`]: https://github.com/Nullus157/bs58-rs
//! [Firedancer]: https://github.com/firedancer-io/firedancer
//! [here]: https://github.com/firedancer-io/firedancer/pull/75
//!
use ;
/// Encodes the given 32 bytes using an optimized base58 encoding algorithm.
///
/// # Examples
///
/// ## Basic example
///
/// ```rust
/// assert_eq!(
/// [7, 224, 70, 147, 60, 112, 144, 250, 46, 62, 133, 57, 252, 149, 220, 143, 237, 77, 21, 208, 191, 61, 58, 206, 152, 136, 129, 103, 129, 48, 141, 139],
/// fd_bs58::decode_32("XkCriyrNwS3G4rzAXtG5B1nnvb5Ka1JtCku93VqeKAr")?);
/// # Ok::<(), fd_bs58::Error>(())
/// ```
/// Encodes the given 64 bytes using an optimized base58 encoding algorithm.
///
/// # Examples
///
/// ## Basic example
///
/// ```rust
/// assert_eq!(
/// [7, 224, 70, 147, 60, 112, 144, 250, 46, 62, 133, 57, 252, 149, 220, 143, 237, 77, 21, 208, 191, 61, 58, 206, 152, 136, 129, 103, 129, 48, 141, 139],
/// fd_bs58::decode_32("XkCriyrNwS3G4rzAXtG5B1nnvb5Ka1JtCku93VqeKAr")?);
/// # Ok::<(), fd_bs58::Error>(())
/// ```
/// Decodes the given base58 string into 32 bytes using an optimized decoding algorithm.
/// This function will return an error if the string is not base58 encoded or the result is not 32 bytes.
///
/// # Examples
///
/// ## Basic example
///
/// ```rust
/// assert_eq!(
/// [7, 224, 70, 147, 60, 112, 144, 250, 46, 62, 133, 57, 252, 149, 220, 143, 237, 77, 21, 208, 191, 61, 58, 206, 152, 136, 129, 103, 129, 48, 141, 139],
/// fd_bs58::decode_32("XkCriyrNwS3G4rzAXtG5B1nnvb5Ka1JtCku93VqeKAr")?);
/// # Ok::<(), fd_bs58::Error>(())
/// ```
///
/// ## Errors
///
/// ### Invalid Character
///
/// ```rust
/// assert_eq!(
/// fd_bs58::Error::InvalidCharacter,
/// fd_bs58::decode_32("l").unwrap_err());
/// ```
///
/// ### Input Too Long
///
/// ```rust
/// assert_eq!(
/// fd_bs58::Error::InputTooLong,
/// fd_bs58::decode_32("4uQeVj5tqViQh7yWWGStvkEG1Zmhx6uasJtWCJziofLRda4").unwrap_err());
/// ```
///
/// ### Input Too Short
///
/// ```rust
/// assert_eq!(
/// fd_bs58::Error::InputTooShort,
/// fd_bs58::decode_32("4uQeVj5tqViQh7yWWGStvkEG1Zmhx6uasJtWCJz").unwrap_err());
/// ```
/// ### Input Byte Amount
///
/// ```rust
/// assert_eq!(
/// fd_bs58::Error::InvalidByteAmount,
/// fd_bs58::decode_32("JEKNVnkbo3jma5nREBBJCDoXFVeKkD56V3xKrvRmWxFJ").unwrap_err());
/// ```
///
/// Decodes the given base58 string into 64 bytes using an optimized decoding algorithm.
/// This function will return an error if the string is not base58 encoded or the result is not 64 bytes.
///
/// # Examples
///
/// ## Basic example
///
/// ```rust
/// assert_eq!(
/// [0, 0, 10, 85, 198, 191, 71, 18, 5, 54, 6, 255, 181, 32, 227, 150, 208, 3, 157, 135, 222, 67, 50, 23, 237, 51, 240, 123, 34, 148, 111, 84, 98, 162, 236, 133, 31, 93, 185, 142, 108, 41, 191, 1, 138, 6, 192, 0, 46, 93, 25, 65, 243, 223, 225, 225, 85, 55, 82, 251, 109, 132, 165, 2],
/// fd_bs58::decode_64("11cgTH4D5e8S3snD444WbbGrkepjTvWMj2jkmCGJtgn3H7qrPb1BnwapxpbGdRtHQh9t9Wbn9t6ZDGHzWpL4df")?);
/// # Ok::<(), fd_bs58::Error>(())
/// ```
///
/// ## Errors
///
/// ### Invalid Character
///
/// ```rust
/// assert_eq!(
/// fd_bs58::Error::InvalidCharacter,
/// fd_bs58::decode_64("l").unwrap_err());
/// ```
///
/// ### Input Too Long
///
/// ```rust
/// assert_eq!(
/// fd_bs58::Error::InputTooLong,
/// fd_bs58::decode_64("2AFv15MNPuA84RmU66xw2uMzGipcVxNpzAffoacGVvjFue3CBmf633fAWuiP9cwL9C3z3CJiGgRSFjJfeEcA6QWabc").unwrap_err());
/// ```
///
/// ### Input Too Short
///
/// ```rust
/// assert_eq!(
/// fd_bs58::Error::InputTooShort,
/// fd_bs58::decode_64("2AFv15MNPuA84RmU66xw2uMzGipcVxNpzAffoacGVvjFue3CBmf633fAWuiP9cwL9C3z3CJiGgRSFjJfeEcA").unwrap_err());
/// ```
/// ### Input Byte Amount
///
/// ```rust
/// assert_eq!(
/// fd_bs58::Error::InvalidByteAmount,
/// fd_bs58::decode_64("67rpwLCuS5DGA8KGZXKsVQ7dnPb9goRLoKfgGbLfQg9WoLUgNY77E2jT11fem3coV9nAkguBACzrU1iyZM4B8roS").unwrap_err());
/// ```
///