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

Implement ArrayDecoding #12

Merged
merged 4 commits into from
Sep 18, 2021
Merged
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
15 changes: 14 additions & 1 deletion src/array.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Interop support for `generic-array`

use crate::Encoding;
use crate::{Encoding, Integer};
use core::ops::Add;
use generic_array::{typenum::Unsigned, ArrayLength, GenericArray};

Expand All @@ -26,3 +26,16 @@ pub trait ArrayEncoding: Encoding {
/// Serialize to a little-endian byte array.
fn to_le_byte_array(&self) -> ByteArray<Self>;
}

/// Support for decoding a `GenericArray` as a big integer.
#[cfg_attr(docsrs, doc(cfg(feature = "generic-array")))]
pub trait ArrayDecoding {
/// Big integer which decodes a `GenericArray`.
type Output: Integer;

/// Deserialize from a big-endian `GenericArray`.
fn into_bigint_be(self) -> Self::Output;

/// Deserialize from a little-endian `GenericArray`.
fn into_bigint_le(self) -> Self::Output;
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub use subtle;

#[cfg(feature = "generic-array")]
pub use {
self::array::{ArrayEncoding, ByteArray},
self::array::{ArrayDecoding, ArrayEncoding, ByteArray},
generic_array::{self, typenum::consts},
};

Expand Down
3 changes: 2 additions & 1 deletion src/uint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ use zeroize::DefaultIsZeroes;
///
/// Optional crate features for encoding (off-by-default):
/// - `generic-array`: enables [`ArrayEncoding`][`crate::ArrayEncoding`] trait which can be used to
/// [`UInt`] as `GenericArray<u8, N>`.
/// [`UInt`] as `GenericArray<u8, N>` and a [`ArrayDecoding`][`crate::ArrayDecoding`] trait which
/// can be used to `GenericArray<u8, N>` as [`UInt`].
/// - `rlp`: support for [Recursive Length Prefix (RLP)][RLP] encoding.
///
/// [RLP]: https://eth.wiki/fundamentals/rlp
Expand Down
49 changes: 47 additions & 2 deletions src/uint/array.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! `generic-array` integration with `UInt`.
// TODO(tarcieri): completely phase out `generic-array` when const generics are powerful enough

use crate::{ArrayEncoding, ByteArray};
use crate::{ArrayDecoding, ArrayEncoding, ByteArray};
use generic_array::{typenum, GenericArray};

macro_rules! impl_uint_array_encoding {
Expand Down Expand Up @@ -35,6 +35,19 @@ macro_rules! impl_uint_array_encoding {
result
}
}

#[cfg_attr(docsrs, doc(cfg(feature = "generic-array")))]
impl ArrayDecoding for GenericArray<u8, $bytes> {
type Output = super::$uint;

fn into_bigint_be(self) -> Self::Output {
Self::Output::from_be_byte_array(self)
}

fn into_bigint_le(self) -> Self::Output {
Self::Output::from_le_byte_array(self)
}
}
)+
};
}
Expand Down Expand Up @@ -63,7 +76,7 @@ impl_uint_array_encoding! {

#[cfg(test)]
mod tests {
use crate::{ArrayEncoding, Limb};
use crate::{ArrayDecoding, ArrayEncoding, Limb};
use hex_literal::hex;

#[cfg(target_pointer_width = "32")]
Expand Down Expand Up @@ -140,4 +153,36 @@ mod tests {
let actual_bytes = UIntEx::from_le_byte_array(expected_bytes).to_le_byte_array();
assert_eq!(expected_bytes, actual_bytes);
}

#[test]
#[cfg(target_pointer_width = "32")]
fn into_bigint_be() {
let expected_bytes = ByteArray::from(hex!("0011223344556677"));
let actual_bytes = expected_bytes.into_bigint_be().to_be_byte_array();
assert_eq!(expected_bytes, actual_bytes);
}

#[test]
#[cfg(target_pointer_width = "64")]
fn into_bigint_be() {
let expected_bytes = ByteArray::from(hex!("00112233445566778899aabbccddeeff"));
let actual_bytes = expected_bytes.into_bigint_be().to_be_byte_array();
assert_eq!(expected_bytes, actual_bytes);
}

#[test]
#[cfg(target_pointer_width = "32")]
fn into_bigint_le() {
let expected_bytes = ByteArray::from(hex!("7766554433221100"));
let actual_bytes = expected_bytes.into_bigint_le().to_le_byte_array();
assert_eq!(expected_bytes, actual_bytes);
}

#[test]
#[cfg(target_pointer_width = "64")]
fn into_bigint_le() {
let expected_bytes = ByteArray::from(hex!("ffeeddccbbaa99887766554433221100"));
let actual_bytes = expected_bytes.into_bigint_le().to_le_byte_array();
assert_eq!(expected_bytes, actual_bytes);
}
}