Nullus157 / bs58-rs

Another Rust Base58 codec implementation

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

encode() is O(n^2)

mikhailOK opened this issue · comments

    for &val in input.clone() {
        let mut carry = val as usize;
        for byte in &mut output[..] {
            carry += (*byte as usize) << 8;
            *byte = (carry % 58) as u8;
            carry /= 58;
        }
        while carry > 0 {
            output.push((carry % 58) as u8);
            carry /= 58;
        }
}

This is insanely slow on large inputs

I would highly recommend not using base58 for large inputs in any case, because the encoding maps each decoded byte to an irrational number of encoded bits it's impossible to stream the data in chunks, you must encode/decode the entire input before you can get the first byte. If you're transferring a large amount of data the upsides of base58 vs base64 don't really apply, and the latter has an exact mapping from 3 decoded bytes to 4 encoded bytes that allows streaming/chunked encoding/decoding.