Function left_encode_bytes

Source
pub const fn left_encode_bytes(x: usize) -> LeftEncodeBytes
Expand description

Encodes x*8 as a byte string in a way that can be unambiguously parsed from the beginning.

§Rationale

left_encode is typically used to encode a length in bits. In practice, we usually have a length in bytes. The conversion from bytes to bits might overflow if the number of bytes is large. This method avoids overflowing.

§Example

use sha3_utils::{left_encode, left_encode_bytes};

assert_eq!(
    left_encode(8192 * 8).as_bytes(),
    left_encode_bytes(8192).as_bytes(),
);

// usize::MAX*8 overflows, causing an incorrect result.
assert_ne!(
    left_encode(usize::MAX.wrapping_mul(8)).as_bytes(),
    left_encode_bytes(usize::MAX).as_bytes(),
);