Skip to content

Commit b429bfc

Browse files
committed
Migrate from generic-array to hybrid-array
This commit replaces the optional `generic-array` feature with `hybrid-array`, which adds support for (de)serializing `Uint`s as `hybrid_array::Array<u8, _>`. The `hybrid_array` crate notably has significantly less unsafe code than `generic-array`, which it accomplishes by being built on const generic core arrays, which are the public inner type of `Array`.
1 parent b3a9b88 commit b429bfc

10 files changed

Lines changed: 50 additions & 57 deletions

File tree

.github/workflows/crypto-bigint.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ jobs:
3434
- run: cargo build --target ${{ matrix.target }} --release --no-default-features
3535
- run: cargo build --target ${{ matrix.target }} --release --no-default-features --features alloc
3636
- run: cargo build --target ${{ matrix.target }} --release --no-default-features --features der
37-
- run: cargo build --target ${{ matrix.target }} --release --no-default-features --features generic-array
37+
- run: cargo build --target ${{ matrix.target }} --release --no-default-features --features hybrid-array
3838
- run: cargo build --target ${{ matrix.target }} --release --no-default-features --features rand_core
3939
- run: cargo build --target ${{ matrix.target }} --release --no-default-features --features rlp
4040
- run: cargo build --target ${{ matrix.target }} --release --no-default-features --features serde
4141
- run: cargo build --target ${{ matrix.target }} --release --no-default-features --features zeroize
42-
- run: cargo build --target ${{ matrix.target }} --release --no-default-features --features alloc,der,generic-array,rand_core,rlp,serde,zeroize
42+
- run: cargo build --target ${{ matrix.target }} --release --no-default-features --features alloc,der,hybrid-array,rand_core,rlp,serde,zeroize
4343

4444
test:
4545
runs-on: ubuntu-latest

Cargo.lock

Lines changed: 14 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ rust-version = "1.73"
2020
subtle = { version = "2.5", default-features = false }
2121

2222
# optional dependencies
23-
der = { version = "=0.8.0-pre.0", optional = true, default-features = false }
24-
generic-array = { version = "0.14", optional = true }
23+
der = { version = "0.7", optional = true, default-features = false }
24+
hybrid-array = { version = "=0.2.0-pre.8", optional = true }
2525
num-traits = { version = "0.2", default-features = false }
2626
rand_core = { version = "0.6.4", optional = true }
2727
rlp = { version = "0.5", optional = true, default-features = false }
28-
serdect = { version = "=0.3.0-pre.0", optional = true, default-features = false }
28+
serdect = { version = "0.2", optional = true, default-features = false }
2929
zeroize = { version = "1", optional = true, default-features = false }
3030

3131
[dev-dependencies]

src/array.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
//! Interop support for `generic-array`
1+
//! Interop support for `hybrid-array`
22
33
use crate::{Encoding, Integer};
44
use core::ops::Add;
5-
use generic_array::{typenum::Unsigned, ArrayLength, GenericArray};
5+
use hybrid_array::{typenum::Unsigned, Array, ArraySize};
66

77
/// Alias for a byte array whose size is defined by [`ArrayEncoding::ByteSize`].
8-
pub type ByteArray<T> = GenericArray<u8, <T as ArrayEncoding>::ByteSize>;
8+
pub type ByteArray<T> = Array<u8, <T as ArrayEncoding>::ByteSize>;
99

10-
/// Support for encoding a big integer as a `GenericArray`.
10+
/// Support for encoding a big integer as a `Array`.
1111
pub trait ArrayEncoding: Encoding {
1212
/// Size of a byte array which encodes a big integer.
13-
type ByteSize: ArrayLength<u8> + Add + Eq + Ord + Unsigned;
13+
type ByteSize: ArraySize + Add + Eq + Ord + Unsigned;
1414

1515
/// Deserialize from a big-endian byte array.
1616
fn from_be_byte_array(bytes: ByteArray<Self>) -> Self;
@@ -25,14 +25,14 @@ pub trait ArrayEncoding: Encoding {
2525
fn to_le_byte_array(&self) -> ByteArray<Self>;
2626
}
2727

28-
/// Support for decoding a `GenericArray` as a big integer.
28+
/// Support for decoding a `Array` as a big integer.
2929
pub trait ArrayDecoding {
30-
/// Big integer which decodes a `GenericArray`.
30+
/// Big integer which decodes a `Array`.
3131
type Output: ArrayEncoding + Integer;
3232

33-
/// Deserialize from a big-endian `GenericArray`.
33+
/// Deserialize from a big-endian `Array`.
3434
fn into_uint_be(self) -> Self::Output;
3535

36-
/// Deserialize from a little-endian `GenericArray`.
36+
/// Deserialize from a little-endian `Array`.
3737
fn into_uint_le(self) -> Self::Output;
3838
}

src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ mod macros;
163163

164164
pub mod modular;
165165

166-
#[cfg(feature = "generic-array")]
166+
#[cfg(feature = "hybrid-array")]
167167
mod array;
168168
mod checked;
169169
mod const_choice;
@@ -191,10 +191,10 @@ pub use subtle;
191191
#[cfg(feature = "alloc")]
192192
pub use crate::uint::boxed::{encoding::DecodeError, BoxedUint};
193193

194-
#[cfg(feature = "generic-array")]
194+
#[cfg(feature = "hybrid-array")]
195195
pub use {
196196
crate::array::{ArrayDecoding, ArrayEncoding, ByteArray},
197-
generic_array::{self, typenum::consts},
197+
hybrid_array::{self, typenum::consts},
198198
};
199199

200200
#[cfg(feature = "rand_core")]
@@ -210,7 +210,7 @@ pub use zeroize;
210210
pub mod prelude {
211211
pub use crate::traits::*;
212212

213-
#[cfg(feature = "generic-array")]
213+
#[cfg(feature = "hybrid-array")]
214214
pub use crate::array::{ArrayDecoding, ArrayEncoding};
215215
}
216216

src/non_zero.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use core::{
88
};
99
use subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption};
1010

11-
#[cfg(feature = "generic-array")]
11+
#[cfg(feature = "hybrid-array")]
1212
use crate::{ArrayEncoding, ByteArray};
1313

1414
#[cfg(feature = "rand_core")]
@@ -142,7 +142,7 @@ impl<const LIMBS: usize> NonZero<Uint<LIMBS>> {
142142
}
143143
}
144144

145-
#[cfg(feature = "generic-array")]
145+
#[cfg(feature = "hybrid-array")]
146146
impl<T> NonZero<T>
147147
where
148148
T: ArrayEncoding + Zero,

src/uint.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ mod sqrt;
3232
mod sub;
3333
mod sub_mod;
3434

35-
#[cfg(feature = "generic-array")]
35+
#[cfg(feature = "hybrid-array")]
3636
mod array;
3737
#[cfg(feature = "alloc")]
3838
pub(crate) mod boxed;
@@ -63,9 +63,9 @@ use zeroize::DefaultIsZeroes;
6363
/// encoding functions that can be used with [`Uint`] constants.
6464
///
6565
/// Optional crate features for encoding (off-by-default):
66-
/// - `generic-array`: enables [`ArrayEncoding`][`crate::ArrayEncoding`] trait which can be used to
67-
/// [`Uint`] as `GenericArray<u8, N>` and a [`ArrayDecoding`][`crate::ArrayDecoding`] trait which
68-
/// can be used to `GenericArray<u8, N>` as [`Uint`].
66+
/// - `hybrid-array`: enables [`ArrayEncoding`][`crate::ArrayEncoding`] trait which can be used to
67+
/// [`Uint`] as `Array<u8, N>` and a [`ArrayDecoding`][`crate::ArrayDecoding`] trait which
68+
/// can be used to `Array<u8, N>` as [`Uint`].
6969
/// - `rlp`: support for [Recursive Length Prefix (RLP)][RLP] encoding.
7070
///
7171
/// [RLP]: https://eth.wiki/fundamentals/rlp

src/uint/array.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
//! `generic-array` integration with `Uint`.
2-
// TODO(tarcieri): completely phase out `generic-array` when const generics are powerful enough
1+
//! `hybrid-array` integration with `Uint`.
2+
// TODO(tarcieri): completely phase out `hybrid-array` when const generics are powerful enough
33

44
use crate::{ArrayDecoding, ArrayEncoding, ByteArray};
5-
use generic_array::{typenum, GenericArray};
5+
use hybrid_array::{typenum, Array};
66

77
macro_rules! impl_uint_array_encoding {
88
($(($uint:ident, $bytes:path)),+) => {
@@ -22,20 +22,20 @@ macro_rules! impl_uint_array_encoding {
2222

2323
#[inline]
2424
fn to_be_byte_array(&self) -> ByteArray<Self> {
25-
let mut result = GenericArray::default();
25+
let mut result = Array::default();
2626
self.write_be_bytes(&mut result);
2727
result
2828
}
2929

3030
#[inline]
3131
fn to_le_byte_array(&self) -> ByteArray<Self> {
32-
let mut result = GenericArray::default();
32+
let mut result = Array::default();
3333
self.write_le_bytes(&mut result);
3434
result
3535
}
3636
}
3737

38-
impl ArrayDecoding for GenericArray<u8, $bytes> {
38+
impl ArrayDecoding for Array<u8, $bytes> {
3939
type Output = super::$uint;
4040

4141
fn into_uint_be(self) -> Self::Output {

src/uint/encoding.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Const-friendly decoding/encoding operations for [`Uint`].
22
3-
#[cfg(all(feature = "der", feature = "generic-array"))]
3+
#[cfg(all(feature = "der", feature = "hybrid-array"))]
44
mod der;
55

66
#[cfg(feature = "rlp")]
@@ -9,7 +9,7 @@ mod rlp;
99
use super::Uint;
1010
use crate::{Limb, Word};
1111

12-
#[cfg(feature = "generic-array")]
12+
#[cfg(feature = "hybrid-array")]
1313
use crate::Encoding;
1414

1515
impl<const LIMBS: usize> Uint<LIMBS> {
@@ -131,7 +131,7 @@ impl<const LIMBS: usize> Uint<LIMBS> {
131131

132132
/// Serialize this [`Uint`] as big-endian, writing it into the provided
133133
/// byte slice.
134-
#[cfg(feature = "generic-array")]
134+
#[cfg(feature = "hybrid-array")]
135135
#[inline]
136136
pub(crate) fn write_be_bytes(&self, out: &mut [u8]) {
137137
debug_assert_eq!(out.len(), Limb::BYTES * LIMBS);
@@ -149,7 +149,7 @@ impl<const LIMBS: usize> Uint<LIMBS> {
149149

150150
/// Serialize this [`Uint`] as little-endian, writing it into the provided
151151
/// byte slice.
152-
#[cfg(feature = "generic-array")]
152+
#[cfg(feature = "hybrid-array")]
153153
#[inline]
154154
pub(crate) fn write_le_bytes(&self, out: &mut [u8]) {
155155
debug_assert_eq!(out.len(), Limb::BYTES * LIMBS);

src/uint/encoding/der.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Support for decoding/encoding [`Uint`] as an ASN.1 DER `INTEGER`.
22
3-
use crate::{generic_array::GenericArray, ArrayEncoding, Uint};
3+
use crate::{hybrid_array::Array, ArrayEncoding, Uint};
44
use ::der::{
55
asn1::{AnyRef, UintRef},
66
DecodeValue, EncodeValue, FixedTag, Length, Tag,
@@ -24,7 +24,7 @@ where
2424
type Error = der::Error;
2525

2626
fn try_from(bytes: UintRef<'a>) -> der::Result<Uint<LIMBS>> {
27-
let mut array = GenericArray::default();
27+
let mut array = Array::default();
2828
let offset = array.len().saturating_sub(bytes.len().try_into()?);
2929
array[offset..].copy_from_slice(bytes.as_bytes());
3030
Ok(Uint::from_be_byte_array(array))

0 commit comments

Comments
 (0)