Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Increased Decode Performance #102

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@ std = ["alloc"]
alloc = []
check = ["sha2"]
cb58 = ["sha2"]
bigint = ["num-bigint", "num-traits"]

[dependencies]
sha2 = { version = "0.10", optional = true, default-features = false }
num-bigint = { version = "0.4.3", optional = true, default-features = false }
num-traits = { version = "0.2.15", optional = true, default-features = false }

[dev_dependencies]
criterion = "0.3"
Expand Down
124 changes: 113 additions & 11 deletions src/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ use core::fmt;
#[cfg(feature = "alloc")]
use alloc::vec::Vec;

#[cfg(feature = "bigint")]
use num_bigint::BigUint;

use crate::Check;
#[cfg(any(feature = "check", feature = "cb58"))]
use crate::CHECKSUM_LEN;
Expand Down Expand Up @@ -287,22 +290,27 @@ impl<'a, I: AsRef<[u8]>> DecodeBuilder<'a, I> {
}
}

fn alpha_decode(index: usize, input_char: u8, alpha: &Alphabet) -> Result<u8> {
if input_char > 127 {
return Err(Error::NonAsciiCharacter { index });
};
let val = alpha.decode[input_char as usize];
if val >= 58 {
return Err(Error::InvalidCharacter {
character: input_char as char,
index,
});
}
return Ok(val);
}

#[cfg(not(feature = "bigint"))]
fn decode_into(input: &[u8], output: &mut [u8], alpha: &Alphabet) -> Result<usize> {
let mut index = 0;
let zero = alpha.encode[0];

for (i, c) in input.iter().enumerate() {
if *c > 127 {
return Err(Error::NonAsciiCharacter { index: i });
}

let mut val = alpha.decode[*c as usize] as usize;
if val == 0xFF {
return Err(Error::InvalidCharacter {
character: *c as char,
index: i,
});
}
let mut val = alpha_decode(i, *c, alpha)? as usize;

for byte in &mut output[..index] {
val += (*byte as usize) * 58;
Expand All @@ -328,6 +336,100 @@ fn decode_into(input: &[u8], output: &mut [u8], alpha: &Alphabet) -> Result<usiz
Ok(index)
}

#[cfg(feature = "bigint")]
fn decode_into(input: &[u8], output: &mut [u8], alpha: &Alphabet) -> Result<usize> {
let mut index = 0;
let zero = alpha.encode[0];

for (_, _) in input.iter().enumerate().take_while(|(_, c)| **c == zero) {
let byte = output.get_mut(index).ok_or(Error::BufferTooSmall)?;
*byte = 0;
index += 1;
}

let index_0 = index;
let input_len = input.len() - index_0;

if input_len > 0 && input_len <= 10 {
let mut output_uint = 0u64;
for (i, c) in input.iter().enumerate().skip(index_0) {
let val = alpha_decode(i, *c, alpha)? as u64;
output_uint = 58 * output_uint + val;
}
while output_uint > 0 {
let byte = output.get_mut(index).ok_or(Error::BufferTooSmall)?;
*byte = output_uint as u8;
index += 1;
output_uint >>= 8
}
output[index_0..index].reverse();
} else if input_len <= 21 {
let mut output_uint = 0u128;
for (i, c) in input.iter().enumerate().skip(index_0) {
let val = alpha_decode(i, *c, alpha)? as u128;
output_uint = 58 * output_uint + val;
}
while output_uint > 0 {
let byte = output.get_mut(index).ok_or(Error::BufferTooSmall)?;
*byte = output_uint as u8;
index += 1;
output_uint >>= 8
}
output[index_0..index].reverse();
} else if input_len <= 43 {
let mut output_uints = [0u64; 4];
let mut ll_index = 0;
for (i, c) in input.iter().enumerate().skip(index_0) {
let mut val = alpha_decode(i, *c, alpha)? as u128;

for ll in &mut output_uints[..ll_index] {
val += *ll as u128 * 58;
*ll = val as u64;
val >>= 64;
}
while val > 0 {
let ll = output_uints
.get_mut(ll_index)
.expect("Base58 input under 43 chars fit into [u64;4]");
*ll = val as u64;
ll_index += 1;
val >>= 64
}
}
output_uints.reverse();
let mut leading_0 = true;
for ll in output_uints {
for be_byte in ll.to_be_bytes() {
if leading_0 && be_byte == 0 {
continue;
} else {
leading_0 = false;
}
let byte = output.get_mut(index).ok_or(Error::BufferTooSmall)?;
*byte = be_byte;
index += 1;
}
}
} else {
let mut clean_input: Vec<u8> = Vec::with_capacity(input_len);
for (i, c) in input.iter().enumerate().skip(index_0) {
let val = alpha_decode(i, *c, alpha)?;
clean_input.push(val);
}
let b58_biguint = BigUint::from_radix_be(&clean_input, 58).unwrap();
let decoded_vec: Vec<u8> = b58_biguint.to_bytes_be();
for c in decoded_vec.iter() {
if index >= output.len() {
break;
}
let byte = output.get_mut(index).ok_or(Error::BufferTooSmall)?;
*byte = *c;
index += 1;
}
}
Ok(index)
}

#[cfg(feature = "check")]
fn decode_check_into(
input: &[u8],
Expand Down
39 changes: 39 additions & 0 deletions src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ use core::fmt;
#[cfg(feature = "alloc")]
use alloc::{string::String, vec::Vec};

#[cfg(feature = "bigint")]
use num_bigint::BigUint;
#[cfg(feature = "bigint")]
use num_traits::Zero;

use crate::Check;
#[cfg(any(feature = "check", feature = "cb58"))]
use crate::CHECKSUM_LEN;
Expand Down Expand Up @@ -361,6 +366,7 @@ fn max_encoded_len(len: usize) -> usize {
len + (len + 1) / 2
}

#[cfg(not(feature = "bigint"))]
fn encode_into<'a, I>(input: I, output: &mut [u8], alpha: &Alphabet) -> Result<usize>
where
I: Clone + IntoIterator<Item = &'a u8>,
Expand Down Expand Up @@ -399,6 +405,39 @@ where
Ok(index)
}

#[cfg(feature = "bigint")]
fn encode_into<'a, I>(input: I, output: &mut [u8], alpha: &Alphabet) -> Result<usize>
where
I: Clone + IntoIterator<Item = &'a u8>,
{
let mut index = 0;

let vector: Vec<u8> = input.into_iter().cloned().collect();
let big_uint = BigUint::from_radix_be(&vector, 256).unwrap();
let mut big_58 = Vec::with_capacity(vector.len() * 2);
for _ in vector.iter().take_while(|v| **v == 0) {
if index == output.len() {
return Err(Error::BufferTooSmall);
}
output[index] = 0;
index += 1;
}

if !big_uint.is_zero() {
big_58.append(&mut big_uint.to_radix_be(58))
}

for val in &big_58 {
if index == output.len() {
return Err(Error::BufferTooSmall);
}
output[index] = alpha.encode[*val as usize];
index += 1;
}

Ok(index)
}

#[cfg(feature = "check")]
fn encode_check_into(
input: &[u8],
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
//! `alloc` | implied by `std` | Support encoding/decoding to [`Vec`](alloc::vec::Vec) and [`String`](alloc::string::String) as appropriate
//! `check` | **off**-by-default | Integrated support for [Base58Check][]
//! `cb58` | **off**-by-default | Integrated support for [CB58][]
//! `bigint`| **off**-by-default | Integrated support for [BigUint][]
//!
//! [Base58Check]: https://en.bitcoin.it/wiki/Base58Check_encoding
//! [CB58]: https://support.avax.network/en/articles/4587395-what-is-cb58
Expand Down