crypto_bigint/
array.rs

1//! Interop support for `hybrid-array`
2
3use crate::{Encoding, Integer};
4use core::ops::Add;
5use hybrid_array::{typenum::Unsigned, Array, ArraySize};
6
7/// Alias for a byte array whose size is defined by [`ArrayEncoding::ByteSize`].
8pub type ByteArray<T> = Array<u8, <T as ArrayEncoding>::ByteSize>;
9
10/// Support for encoding a big integer as a `Array`.
11pub trait ArrayEncoding: Encoding {
12    /// Size of a byte array which encodes a big integer.
13    type ByteSize: ArraySize + Add + Eq + Ord + Unsigned;
14
15    /// Deserialize from a big-endian byte array.
16    fn from_be_byte_array(bytes: ByteArray<Self>) -> Self;
17
18    /// Deserialize from a little-endian byte array.
19    fn from_le_byte_array(bytes: ByteArray<Self>) -> Self;
20
21    /// Serialize to a big-endian byte array.
22    fn to_be_byte_array(&self) -> ByteArray<Self>;
23
24    /// Serialize to a little-endian byte array.
25    fn to_le_byte_array(&self) -> ByteArray<Self>;
26}
27
28/// Support for decoding a `Array` as a big integer.
29pub trait ArrayDecoding {
30    /// Big integer which decodes a `Array`.
31    type Output: ArrayEncoding + Integer;
32
33    /// Deserialize from a big-endian `Array`.
34    fn into_uint_be(self) -> Self::Output;
35
36    /// Deserialize from a little-endian `Array`.
37    fn into_uint_le(self) -> Self::Output;
38}