base58ck 0.5.0

Bitcoin base58 encoding with checksum.
Documentation
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
// SPDX-License-Identifier: CC0-1.0

//! # Bitcoin Base58 Encoding and Decoding
//!
//! This crate can be used in a no-std environment but requires an allocator for decoding.

#![no_std]
// Experimental features we need.
#![cfg_attr(bench, feature(test))]
// Coding conventions.
#![warn(missing_docs)]
#![warn(deprecated_in_future)]
#![doc(test(attr(warn(unused))))]
// Instead of littering the codebase for non-fuzzing and bench code just globally allow.
#![cfg_attr(fuzzing, allow(dead_code, unused_imports))]
#![cfg_attr(bench, allow(dead_code, unused_imports))]
// Exclude lints we don't think are valuable.
#![allow(clippy::incompatible_msrv)] // Has FPs and we're testing it which is more reliable anyway.

#[cfg(feature = "alloc")]
extern crate alloc;

#[cfg(bench)]
extern crate test;

#[cfg(feature = "std")]
extern crate std;

static BASE58_CHARS: &[u8] = b"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";

pub mod error;

#[cfg(feature = "alloc")]
#[cfg(not(feature = "std"))]
pub use alloc::{string::String, vec::Vec};
#[cfg(feature = "alloc")]
use core::convert::Infallible;
use core::fmt;
#[cfg(feature = "std")]
pub use std::{string::String, vec::Vec};

use hashes::sha256d;
use internals::array::ArrayExt;
use internals::array_vec::ArrayVec;
#[allow(unused)] // MSRV polyfill
use internals::slice::SliceExt;

use crate::error::{
    Base256Error, DecodeCheckArrayErrorInner, IncorrectChecksumError, TooShortError,
    UnexpectedLengthError,
};
#[cfg(not(feature = "alloc"))]
use crate::error::{DecodeCheckError, InputTooLongErrorInner, InvalidCharacterError};

#[rustfmt::skip]                // Keep public re-exports separate.
#[cfg(feature = "alloc")]
#[doc(no_inline)]
pub use self::error::{DecodeCheckError, InvalidCharacterError};
#[doc(no_inline)]
pub use self::error::{DecodeCheckArrayError, InputTooLongError};

#[rustfmt::skip]
static BASE58_DIGITS: [Option<u8>; 128] = [
    None,     None,     None,     None,     None,     None,     None,     None,     // 0-7
    None,     None,     None,     None,     None,     None,     None,     None,     // 8-15
    None,     None,     None,     None,     None,     None,     None,     None,     // 16-23
    None,     None,     None,     None,     None,     None,     None,     None,     // 24-31
    None,     None,     None,     None,     None,     None,     None,     None,     // 32-39
    None,     None,     None,     None,     None,     None,     None,     None,     // 40-47
    None,     Some(0),  Some(1),  Some(2),  Some(3),  Some(4),  Some(5),  Some(6),  // 48-55
    Some(7),  Some(8),  None,     None,     None,     None,     None,     None,     // 56-63
    None,     Some(9),  Some(10), Some(11), Some(12), Some(13), Some(14), Some(15), // 64-71
    Some(16), None,     Some(17), Some(18), Some(19), Some(20), Some(21), None,     // 72-79
    Some(22), Some(23), Some(24), Some(25), Some(26), Some(27), Some(28), Some(29), // 80-87
    Some(30), Some(31), Some(32), None,     None,     None,     None,     None,     // 88-95
    None,     Some(33), Some(34), Some(35), Some(36), Some(37), Some(38), Some(39), // 96-103
    Some(40), Some(41), Some(42), Some(43), None,     Some(44), Some(45), Some(46), // 104-111
    Some(47), Some(48), Some(49), Some(50), Some(51), Some(52), Some(53), Some(54), // 112-119
    Some(55), Some(56), Some(57), None,     None,     None,     None,     None,     // 120-127
];

/// Builds the little-endian base-256 representation of base58 `data` into `scratch`.
///
/// The padding zero bytes are not decoded, so the big-endian decoded value is directly read
/// back to front.
///
/// # Errors
///
/// Returns an error if the input contains an invalid base58 character (not in the base58 alphabet).
fn build_base256<T: Buffer>(data: &str, scratch: &mut T) -> Result<(), Base256Error<T::Err>> {
    // Build in base 256
    for d58 in data.bytes() {
        // Compute "X = X * 58 + next_digit" in base 256
        if usize::from(d58) >= BASE58_DIGITS.len() {
            return Err(Base256Error::InvalidChar(InvalidCharacterError::new(d58)));
        }
        let mut carry = match BASE58_DIGITS[usize::from(d58)] {
            Some(d58) => u32::from(d58),
            None => {
                return Err(Base256Error::InvalidChar(InvalidCharacterError::new(d58)));
            }
        };
        for d256 in scratch.slice_mut() {
            carry += u32::from(*d256) * 58;
            *d256 = carry as u8; // cast loses data intentionally
            carry /= 256;
        }
        while carry > 0 {
            // This function (build_base256) is only ever called with a Vec (infallible) or an ArrayVec with a pre-checked size.
            scratch.try_push(carry as u8).map_err(Base256Error::Buffer)?; // cast loses data intentionally
            carry /= 256;
        }
    }
    Ok(())
}

/// Decodes a base58-encoded string into a byte vector.
///
/// # Errors
///
/// Returns an error if the input contains an invalid base58 character (not in the base58 alphabet).
#[cfg(feature = "alloc")]
pub fn decode(data: &str) -> Result<Vec<u8>, InvalidCharacterError> {
    // 11/15 is just over log_256(58)
    let mut scratch = Vec::with_capacity(1 + data.len() * 11 / 15);
    build_base256(data, &mut scratch).map_err(|e| match e {
        Base256Error::Buffer(_) => unreachable!("Vec cannot fail try_push"),
        Base256Error::InvalidChar(err) => err,
    })?;

    // Copy leading zeroes directly
    let mut ret: Vec<u8> = data.bytes().take_while(|&x| x == BASE58_CHARS[0]).map(|_| 0).collect();
    // Copy rest of string
    ret.extend(scratch.into_iter().rev());
    Ok(ret)
}

/// Decodes a base58check-encoded string into a byte vector verifying the checksum.
///
/// # Errors
///
/// * The input contains an invalid base58 character.
/// * The decoded data is less than 4 bytes (too short for checksum verification).
/// * The checksum does not match the expected value.
#[cfg(feature = "alloc")]
pub fn decode_check(data: &str) -> Result<Vec<u8>, DecodeCheckError> {
    let mut ret: Vec<u8> = decode(data)?;
    let (remaining, &data_check) =
        ret.split_last_chunk::<4>().ok_or(TooShortError { length: ret.len() })?;

    let hash_check = *sha256d::Hash::hash(remaining).as_byte_array().sub_array::<0, 4>();

    let expected = u32::from_le_bytes(hash_check);
    let actual = u32::from_le_bytes(data_check);

    if actual != expected {
        return Err(IncorrectChecksumError { incorrect: actual, expected }.into());
    }

    ret.truncate(remaining.len());
    Ok(ret)
}

/// Decodes a base58check-encoded string into a fixed-size array, verifying the checksum.
///
/// This does not require `alloc`, but it only works for inputs up to 128 characters long. `N` is
/// the expected length of the decoded payload (excluding the 4 byte checksum). Decoding will fail if
/// the payload is any other length.
///
/// # Errors
///
/// * The input contains an invalid base58 character.
/// * The decoded data is less than 4 bytes (too short for checksum verification).
/// * The checksum does not match the expected value.
/// * The input is longer than 128 characters.
/// * The decoded payload length is not exactly `N` bytes.
#[allow(clippy::missing_panics_doc)] // payload length is checked before cast unwrap
pub fn decode_check_to_array<const N: usize>(data: &str) -> Result<[u8; N], DecodeCheckArrayError> {
    // 11/15 is just over log_256(58), so the decoded length never exceeds the input length.
    let mut scratch = ArrayVec::<u8, SHORT_OPT_BUFFER_LEN>::new();
    build_base256(data, &mut scratch)
        .map_err(|e| match e {
            // Too long to decode within the fixed buffer. Report an approximate decoded length.
            Base256Error::Buffer(_) =>
                DecodeCheckArrayErrorInner::UnexpectedLength(UnexpectedLengthError {
                    expected: N,
                    actual: data.len() * 11 / 15,
                }),
            Base256Error::InvalidChar(err) =>
                DecodeCheckArrayErrorInner::Decode(DecodeCheckError::from(err)),
        })
        .map_err(DecodeCheckArrayError)?;

    let leading_zeros = data.bytes().take_while(|&x| x == BASE58_CHARS[0]).count();
    let decoded_len = leading_zeros + scratch.len();

    let mut decoded = [0u8; SHORT_OPT_BUFFER_LEN];
    scratch.as_mut_slice().reverse();

    // Copy the scratch into a subslice, erroring if out of range.
    let write_slice = decoded
        .get_mut(leading_zeros..decoded_len)
        .ok_or(UnexpectedLengthError { expected: N, actual: data.len() * 11 / 15 })
        .map_err(DecodeCheckArrayErrorInner::UnexpectedLength)
        .map_err(DecodeCheckArrayError)?;
    write_slice.copy_from_slice(&scratch);
    let decoded = &decoded[..decoded_len];

    let (payload, &data_check) = decoded.split_last_chunk::<4>().ok_or_else(|| {
        DecodeCheckArrayError(DecodeCheckArrayErrorInner::Decode(DecodeCheckError::from(
            TooShortError { length: decoded_len },
        )))
    })?;

    if payload.len() != N {
        return Err(DecodeCheckArrayError(DecodeCheckArrayErrorInner::UnexpectedLength(
            UnexpectedLengthError { expected: N, actual: payload.len() },
        )));
    }

    let hash_check = *sha256d::Hash::hash(payload).as_byte_array().sub_array::<0, 4>();
    let expected = u32::from_le_bytes(hash_check);
    let actual = u32::from_le_bytes(data_check);

    if actual != expected {
        return Err(DecodeCheckArrayError(DecodeCheckArrayErrorInner::Decode(
            DecodeCheckError::from(IncorrectChecksumError { incorrect: actual, expected }),
        )));
    }

    Ok(payload.try_into().expect("payload length checked to equal N"))
}

const SHORT_OPT_BUFFER_LEN: usize = 128;

/// A base58check-encoded string (data followed by a 4 byte `SHA256d` checksum, base58-encoded).
///
/// Strings of at most 128 characters can be encoded without allocating. Longer strings can only
/// be produced when the `alloc` feature is enabled.
#[derive(Clone, Hash, PartialEq, Eq)]
pub struct Base58CkString(Base58CkInner);

#[derive(Clone, Hash, PartialEq, Eq)]
enum Base58CkInner {
    /// ASCII string of length at most 128 base58 characters (roughly 93 bytes)
    Small(ArrayVec<u8, SHORT_OPT_BUFFER_LEN>),
    /// Unbounded string (available with "alloc" only).
    #[cfg(feature = "alloc")]
    Large(Vec<u8>),
}

impl Base58CkString {
    /// Encodes `data` as a base58check string, including the checksum.
    ///
    /// The checksum is the first four bytes of the `SHA256d` of the data, concatenated onto the
    /// end before encoding.
    ///
    /// # Errors
    ///
    /// If the `alloc` feature is disabled and `data` encodes to more than 128 base58 characters.
    /// With `alloc` enabled this function is infallible. If you will only be using this with `alloc`,
    /// you can alternatively call [`Self::encode_unbounded`].
    pub fn encode(data: &[u8]) -> Result<Self, InputTooLongError> {
        #[cfg(feature = "alloc")]
        {
            Ok(Self::encode_unbounded(data))
        }
        #[cfg(not(feature = "alloc"))]
        {
            let mut buf = ArrayVec::<u8, SHORT_OPT_BUFFER_LEN>::new();
            let checksum = sha256d::Hash::hash(data);
            let iter = data.iter().copied().chain(checksum.as_byte_array()[0..4].iter().copied());

            encode_to_buffer(iter, &mut buf)
                .map(|()| Self(Base58CkInner::Small(buf)))
                .map_err(|_| InputTooLongError(InputTooLongErrorInner { input_len: data.len() }))
        }
    }

    /// Encodes `data` of any length as a base58check string.
    #[allow(clippy::missing_panics_doc)] // encode_to_buffer is infallible in both cases
    #[cfg(feature = "alloc")]
    pub fn encode_unbounded(data: &[u8]) -> Self {
        let checksum = sha256d::Hash::hash(data);
        let iter = data.iter().copied().chain(checksum.as_byte_array()[0..4].iter().copied());
        let reserve_len = encoded_check_reserve_len(data.len());
        if reserve_len <= SHORT_OPT_BUFFER_LEN {
            let mut buf = ArrayVec::<u8, SHORT_OPT_BUFFER_LEN>::new();
            encode_to_buffer(iter, &mut buf)
                .expect("encode_to_buffer is infallible with well-sized ArrayVec buf");
            Self(Base58CkInner::Small(buf))
        } else {
            let mut buf = Vec::with_capacity(reserve_len);
            encode_to_buffer(iter, &mut buf).expect("encode_to_buffer is infallible with Vec buf");
            Self(Base58CkInner::Large(buf))
        }
    }

    /// Returns the base58check-encoded string.
    #[allow(clippy::missing_panics_doc)] // Base58 characters are always valid ASCII.
    pub fn as_str(&self) -> &str {
        core::str::from_utf8(self.as_bytes()).expect("base58 characters are valid ASCII")
    }

    /// Returns the base58check-encoded string as ASCII bytes.
    pub fn as_bytes(&self) -> &[u8] {
        match self.0 {
            Base58CkInner::Small(ref data) => data.slice(),
            #[cfg(feature = "alloc")]
            Base58CkInner::Large(ref data) => data.slice(),
        }
    }

    /// Returns the number of bytes/ASCII chars in the base58check-encoded string.
    pub fn len(&self) -> usize { self.as_bytes().len() }

    /// Returns true if the base58check-encoded string is empty.
    pub fn is_empty(&self) -> bool { self.len() == 0 }
}

impl AsRef<str> for Base58CkString {
    fn as_ref(&self) -> &str { self.as_str() }
}

impl AsRef<[u8]> for Base58CkString {
    fn as_ref(&self) -> &[u8] { self.as_bytes() }
}

impl fmt::Display for Base58CkString {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { self.as_str().fmt(f) }
}

impl fmt::Debug for Base58CkString {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_tuple("Base58CkString").field(&self.as_str()).finish()
    }
}

/// Returns the length to reserve when encoding base58 without checksum
#[cfg(feature = "alloc")]
const fn encoded_reserve_len(unencoded_len: usize) -> usize {
    // log2(256) / log2(58) ~ 1.37 = 137 / 100
    unencoded_len * 137 / 100
}

/// Returns the length to reserve when encoding base58 with checksum
#[cfg(feature = "alloc")]
const fn encoded_check_reserve_len(unencoded_len: usize) -> usize {
    encoded_reserve_len(unencoded_len + 4)
}

trait Buffer: Sized {
    type Err: fmt::Debug;

    fn try_push(&mut self, val: u8) -> Result<(), Self::Err>;
    fn slice(&self) -> &[u8];
    fn slice_mut(&mut self) -> &mut [u8];
}

#[cfg(feature = "alloc")]
impl Buffer for Vec<u8> {
    type Err = Infallible;

    fn try_push(&mut self, val: u8) -> Result<(), Self::Err> {
        self.push(val);
        Ok(())
    }

    fn slice(&self) -> &[u8] { self }

    fn slice_mut(&mut self) -> &mut [u8] { self }
}

impl<const N: usize> Buffer for ArrayVec<u8, N> {
    type Err = internals::array_vec::error::Error;

    fn try_push(&mut self, val: u8) -> Result<(), Self::Err> { self.try_push(val) }

    fn slice(&self) -> &[u8] { self.as_slice() }

    fn slice_mut(&mut self) -> &mut [u8] { self.as_mut_slice() }
}

// Base58 encode the data in the iterator `data` to the buffer buf as ASCII bytes
fn encode_to_buffer<I: Iterator<Item = u8>, T: Buffer>(data: I, buf: &mut T) -> Result<(), T::Err> {
    let mut leading_zero_count = 0;
    let mut leading_zeroes = true;
    // Build string in little endian with 0-58 in place of characters...
    for d256 in data {
        let mut carry = u32::from(d256);
        if leading_zeroes && carry == 0 {
            leading_zero_count += 1;
        } else {
            leading_zeroes = false;
        }

        for ch in buf.slice_mut() {
            let new_ch = u32::from(*ch) * 256 + carry;
            *ch = (new_ch % 58) as u8; // cast loses data intentionally
            carry = new_ch / 58;
        }

        while carry > 0 {
            buf.try_push((carry % 58) as u8)?; // cast loses data intentionally
            carry /= 58;
        }
    }

    // ... then reverse it and convert to ASCII
    for _ in 0..leading_zero_count {
        buf.try_push(0)?;
    }

    buf.slice_mut().reverse();
    for ch in buf.slice_mut() {
        *ch = BASE58_CHARS[usize::from(*ch)];
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    #[cfg(feature = "alloc")]
    use alloc::vec;

    use hex::hex;

    use super::*;

    #[test]
    #[cfg(feature = "alloc")]
    fn base58_encode() {
        // Basics
        assert_eq!(Base58CkString::encode_unbounded(&[13, 36][..]).as_str(), "7YY3x3vS");

        // Leading zeroes
        assert_eq!(Base58CkString::encode_unbounded(&[0, 13, 36][..]).as_str(), "17YZPJu4L");
        assert_eq!(
            Base58CkString::encode_unbounded(&[0, 0, 0, 0, 13, 36][..]).as_str(),
            "11117YaXDHva"
        );

        // Long input (>128 bytes => has to use heap)
        let res = Base58CkString::encode_unbounded(
            "BitcoinBitcoinBitcoinBitcoinBitcoinBitcoinBitcoinBitcoinBitcoinBit\
        coinBitcoinBitcoinBitcoinBitcoinBitcoinBitcoinBitcoinBitcoinBitcoinBitcoin"
                .as_bytes(),
        );
        let exp =
            "4hqMa7U6Kxg4YstWo7KztyYAAkTuhuLWTvrHia8nrgx5eb2E8cf79wD9dBjd4c9STsTTXWZT5pp985vP\
        nL4MVTQrt4EW5jgAk5Fh81PoF6jjhCyUZY2kZ8iYaM5XpfPkZ6aki57S6oiuVv4cmJz2ou8ssxEKNRJMWjSFL5izLbe\
        s9rugAdBdrboyHMSAtSNY1Nrb4";
        assert_eq!(res.as_str(), exp);

        // Addresses
        let addr = hex!("00f8917303bfa8ef24f292e8fa1419b20460ba064d");
        assert_eq!(
            Base58CkString::encode_unbounded(&addr[..]).as_str(),
            "1PfJpZsjreyVrqeoAfabrRwwjQyoSQMmHH"
        );
    }

    #[test]
    #[cfg(feature = "alloc")]
    fn base58_decode() {
        // Basics
        assert_eq!(decode("1").ok(), Some(vec![0u8]));
        assert_eq!(decode("2").ok(), Some(vec![1u8]));
        assert_eq!(decode("21").ok(), Some(vec![58u8]));
        assert_eq!(decode("211").ok(), Some(vec![13u8, 36]));

        // Leading zeroes
        assert_eq!(decode("1211").ok(), Some(vec![0u8, 13, 36]));
        assert_eq!(decode("111211").ok(), Some(vec![0u8, 0, 0, 13, 36]));

        // Addresses
        assert_eq!(
            decode_check("1PfJpZsjreyVrqeoAfabrRwwjQyoSQMmHH").ok().unwrap().as_slice(),
            hex!("00f8917303bfa8ef24f292e8fa1419b20460ba064d")
        );
        // Non Base58 char.
        assert_eq!(decode("ยข").unwrap_err(), InvalidCharacterError::new(194));
    }

    #[test]
    fn decode_check_to_array_roundtrip() {
        let addr = hex!("00f8917303bfa8ef24f292e8fa1419b20460ba064d");
        let encoded = Base58CkString::encode(&addr).unwrap();

        let decoded = decode_check_to_array::<21>(encoded.as_str()).unwrap();
        assert_eq!(decoded, addr);
        #[cfg(feature = "alloc")]
        assert_eq!(decoded.as_slice(), decode_check(encoded.as_str()).unwrap().as_slice());
    }

    #[test]
    fn decode_check_to_array_errors() {
        use crate::error::DecodeCheckArrayErrorInner;

        const STRING_LEN: usize = SHORT_OPT_BUFFER_LEN + 1;
        const APPROX_LEN: usize = STRING_LEN * 11 / 15;

        let encoded = "1PfJpZsjreyVrqeoAfabrRwwjQyoSQMmHH"; // 21 byte payload

        let err = decode_check_to_array::<20>(encoded).unwrap_err();
        assert_eq!(
            err,
            DecodeCheckArrayError(DecodeCheckArrayErrorInner::UnexpectedLength(
                crate::error::UnexpectedLengthError { expected: 20, actual: 21 }
            ))
        );

        assert!(matches!(
            decode_check_to_array::<21>("ยข").unwrap_err(),
            DecodeCheckArrayError(DecodeCheckArrayErrorInner::Decode(_))
        ));

        assert!(matches!(
            decode_check_to_array::<21>("1PfJpZsjreyVrqeoAfabrRwwjQyoSQMmHG").unwrap_err(),
            DecodeCheckArrayError(DecodeCheckArrayErrorInner::Decode(_))
        ));

        let long = "1".repeat(STRING_LEN);
        assert!(matches!(
            decode_check_to_array::<21>(&long).unwrap_err(),
            DecodeCheckArrayError(DecodeCheckArrayErrorInner::UnexpectedLength(
                crate::UnexpectedLengthError { expected: 21, actual: APPROX_LEN }
            ))
        ));
    }

    #[test]
    fn decode_check_to_array_at_input_length_limit() {
        let encoded = "22UzJUbV3TnAhvzqfW411nkMuSfpgxfYfuuCyNPtrA9EQTViEdsmiBAqEyGP4EGFHb1c7XKWFmjWj9uzBdg8kpCVXAaWVGQmovSTnFjSjEEa9sAZqKUYrvnvgVtPVTuj";
        let want = [0xFFu8; 89];
        assert_eq!(encoded.len(), SHORT_OPT_BUFFER_LEN);
        assert_eq!(decode_check_to_array::<89>(encoded).unwrap(), want);

        let encoded = "11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111DH4Svg";
        let mut want = [0u8; 123];
        want[122] = 0x01;
        assert_eq!(encoded.len(), SHORT_OPT_BUFFER_LEN);
        assert_eq!(decode_check_to_array::<123>(encoded).unwrap(), want);
    }

    #[test]
    fn decode_check_to_array_leading_zeros() {
        let data = [0u8, 0, 1, 2, 3];
        let encoded = Base58CkString::encode(&data).unwrap();
        assert_eq!(decode_check_to_array::<5>(encoded.as_str()).unwrap(), data);
    }

    #[test]
    #[cfg(feature = "alloc")]
    fn base58_roundtrip() {
        let s = "xprv9wTYmMFdV23N2TdNG573QoEsfRrWKQgWeibmLntzniatZvR9BmLnvSxqu53Kw1UmYPxLgboyZQaXwTCg8MSY3H2EU4pWcQDnRnrVA1xe8fs";
        let v: Vec<u8> = decode_check(s).unwrap();
        assert_eq!(Base58CkString::encode_unbounded(&v[..]).as_str(), s);
        assert_eq!(decode_check(Base58CkString::encode_unbounded(&v[..]).as_str()).ok(), Some(v));

        // Check that empty slice passes roundtrip.
        assert_eq!(decode_check(Base58CkString::encode_unbounded(&[]).as_str()), Ok(vec![]));
        // Check that `len > 4` is enforced.
        assert_eq!(decode_check("Ldp"), Err(TooShortError { length: 3 }.into()));
    }
}

#[cfg(bench)]
mod benches {
    use test::{black_box, Bencher};

    #[bench]
    pub fn bench_encode_check_50(bh: &mut Bencher) {
        let data: alloc::vec::Vec<_> = (0u8..50).collect();

        bh.iter(|| {
            let r = super::Base58CkString::encode_unbounded(&data);
            black_box(r.as_str());
        });
    }

    #[bench]
    pub fn bench_encode_check_xpub(bh: &mut Bencher) {
        let data: alloc::vec::Vec<_> = (0u8..78).collect(); // length of xpub

        bh.iter(|| {
            let r = super::Base58CkString::encode_unbounded(&data);
            black_box(r.as_str());
        });
    }
}