Implement Uint::from_be_slice()

Needed because Rust 1.29 does not easily allow converting from a slice
into an array.
This commit is contained in:
Nadav Ivgi 2020-12-30 05:26:50 +02:00
parent 3761b0d808
commit 67ae602e2a
No known key found for this signature in database
GPG Key ID: 81F6104CD0F150FC
1 changed files with 22 additions and 1 deletions

View File

@ -90,9 +90,23 @@ macro_rules! construct_uint {
}
}
/// Creates big integer value from a byte slice array using
/// Creates big integer value from a byte array using
/// big-endian encoding
pub fn from_be_bytes(bytes: [u8; $n_words * 8]) -> $name {
Self::_from_be_slice(&bytes)
}
/// Creates big integer value from a byte slice using
/// big-endian encoding
pub fn from_be_slice(bytes: &[u8]) -> Result<$name, Error> {
if bytes.len() != $n_words * 8 {
Err(Error::InvalidLength(bytes.len(), $n_words*8))
} else {
Ok(Self::_from_be_slice(bytes))
}
}
fn _from_be_slice(bytes: &[u8]) -> $name {
use super::endian::slice_to_u64_be;
let mut slice = [0u64; $n_words];
slice.iter_mut()
@ -420,6 +434,13 @@ macro_rules! construct_uint {
construct_uint!(Uint256, 4);
construct_uint!(Uint128, 2);
/// Uint error
#[derive(Debug)]
pub enum Error {
/// Invalid slice length (actual, expected)
InvalidLength(usize, usize),
}
impl Uint256 {
/// Increment by 1
#[inline]