Crate five8

Source
Expand description

§five8

five8 provides fast base58 encoding and decoding for 32-byte and 64-byte arrays. It is a Rust port of fd_base58. There are four functions in the public api:

  • encode_32
  • encode_64
  • decode_32
  • decode_64

§Examples

§Encoding

let mut buf = [0u8; 44];
let bytes = &[
    24, 243, 6, 223, 230, 153, 210, 8, 92, 137, 123, 67, 164, 197, 79, 196, 125, 43, 183, 85,
    103, 91, 232, 167, 73, 131, 104, 131, 0, 101, 214, 231,
];
let len = five8::encode_32(bytes, &mut buf);
assert_eq!(
    &buf[..len as usize],
    [
        50, 103, 80, 105, 104, 85, 84, 106, 116, 51, 70, 74, 113, 102, 49, 86, 112, 105,
        100, 103, 114, 89, 53, 99, 90, 54, 80, 117, 121, 77, 99, 99, 71, 86, 119, 81, 72,
        82, 102, 106, 77, 80, 90, 71
    ]
);
assert_eq!(len, 44);

§Decoding

fn example_decode_32() {
    let bytes = b"2gPihUTjt3FJqf1VpidgrY5cZ6PuyMccGVwQHRfjMPZG";
    let mut out = [0u8; 32];
    five8::decode_32(bytes, &mut out).unwrap();
    assert_eq!(
        out,
        [
            24, 243, 6, 223, 230, 153, 210, 8, 92, 137, 123, 67, 164, 197, 79, 196, 125, 43,
            183, 85, 103, 91, 232, 167, 73, 131, 104, 131, 0, 101, 214, 231
        ]
    );
}

§Benchmarks

These benchmarks were run on a laptop with AVX2 support. If your machine does not support AVX2 instructions it will be slower but should still be faster than the alternatives - see the second set of benchmarks where AVX2 is disabled.

§AVX2 enabled (RUSTFLAGS='-C target-cpu-native)

Benchmarkfive8Lou-Kamades/fd_bs58bs58-rs
decode_3236 ns88 ns291 ns
decode_64124 ns203 ns1092 ns
encode_3255 ns96 ns682 ns
encode_64102 ns209 ns2781 ns

§AVX2 disabled (default RUSTFLAGS)

Benchmarkfive8Lou-Kamades/fd_bs58bs58-rs
decode_3249 ns107 ns320 ns
decode_64162 ns246 ns1176 ns
encode_3286 ns98 ns824 ns
encode_64179 ns219 ns3370 ns

§See Also

five8_const: compile-time base58 decoding.

Enums§

DecodeError

Constants§

BASE58_ENCODED_32_MAX_LEN
BASE58_ENCODED_64_MAX_LEN

Functions§

decode_32
Decode base58 data onto a 32-byte array.
decode_64
Decode base58 data onto a 64-byte array.
encode_32
Encode a 32-byte array.
encode_64
Encode a 64-byte array.