pub const fn right_encode_bytes(x: usize) -> RightEncodeBytes
Expand description
Encodes x*8
as a byte string in a way that can be
unambiguously parsed from the beginning.
§Rationale
right_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::{right_encode, right_encode_bytes};
assert_eq!(
right_encode(8192 * 8).as_bytes(),
right_encode_bytes(8192).as_bytes(),
);
// usize::MAX*8 overflows, causing an incorrect result.
assert_ne!(
right_encode(usize::MAX.wrapping_mul(8)).as_bytes(),
right_encode_bytes(usize::MAX).as_bytes(),
);