Skip to main content

crypto_bigint/
array.rs

1//! Interop support for `generic-array`
2
3use crate::Encoding;
4use core::ops::Add;
5use generic_array::{typenum::Unsigned, ArrayLength, GenericArray};
6
7/// Alias for a byte array whose size is defined by [`ArrayEncoding::ByteSize`].
8#[cfg_attr(docsrs, doc(cfg(feature = "generic-array")))]
9pub type ByteArray<T> = GenericArray<u8, <T as ArrayEncoding>::ByteSize>;
10
11/// Support for encoding a big integer as a `GenericArray`.
12#[cfg_attr(docsrs, doc(cfg(feature = "generic-array")))]
13pub trait ArrayEncoding: Encoding {
14    /// Size of a byte array which encodes a big integer.
15    type ByteSize: ArrayLength<u8> + Add + Eq + Ord + Unsigned;
16
17    /// Deserialize from a big-endian byte array.
18    fn from_be_byte_array(bytes: ByteArray<Self>) -> Self;
19
20    /// Deserialize from a little-endian byte array.
21    fn from_le_byte_array(bytes: ByteArray<Self>) -> Self;
22
23    /// Serialize to a big-endian byte array.
24    fn to_be_byte_array(&self) -> ByteArray<Self>;
25
26    /// Serialize to a little-endian byte array.
27    fn to_le_byte_array(&self) -> ByteArray<Self>;
28}