diff --git a/Cargo.lock b/Cargo.lock index 263d4bb1..c6345f0f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8,6 +8,12 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" +[[package]] +name = "base16ct" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "349a06037c7bf932dd7e7d1f653678b2038b9ad46a74102f1fc7bd7872678cce" + [[package]] name = "bincode" version = "1.3.3" @@ -70,8 +76,7 @@ dependencies = [ "rand_chacha", "rand_core", "rlp", - "serde", - "serde-big-array", + "serdect", "subtle", "zeroize", ] @@ -303,11 +308,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ce31e24b01e1e524df96f1c2fdd054405f8d7376249a5110886fb4b658484789" [[package]] -name = "serde-big-array" -version = "0.4.0" +name = "serdect" +version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "01890a6856e9a7fbb42f3bc847957b61aeaf024fe99f6abc76d137ed1799dc80" +checksum = "038fce1bf4d74b9b30ea7dcd59df75ba8ec669a5dcb3cc64fbfcef7334ced32c" dependencies = [ + "base16ct", "serde", ] diff --git a/Cargo.toml b/Cargo.toml index 813821be..c547d017 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,8 +24,7 @@ der = { version = "=0.6.0-pre.3", optional = true, default-features = false } generic-array = { version = "0.14", optional = true } rand_core = { version = "0.6", optional = true } rlp = { version = "0.5", optional = true, default-features = false } -serde_ = { version = "1", package = "serde", optional = true, default-features = false } -serde-big-array = { version = "0.4", optional = true } +serdect = { version = "0.1", optional = true, default-features = false } zeroize = { version = "1", optional = true, default-features = false } [dev-dependencies] @@ -41,7 +40,7 @@ rand_chacha = "0.3" default = ["rand"] alloc = [] rand = ["rand_core/std"] -serde = ["serde_", "serde-big-array"] +serde = ["serdect"] [package.metadata.docs.rs] all-features = true diff --git a/src/checked.rs b/src/checked.rs index 4d2a2df8..e02317e3 100644 --- a/src/checked.rs +++ b/src/checked.rs @@ -3,7 +3,7 @@ use subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption}; #[cfg(feature = "serde")] -use serde::{Deserialize, Deserializer, Serialize, Serializer}; +use serdect::serde::{Deserialize, Deserializer, Serialize, Serializer}; /// Provides intentionally-checked arithmetic on `T`. /// @@ -68,11 +68,8 @@ impl<'de, T: Default + Deserialize<'de>> Deserialize<'de> for Checked { D: Deserializer<'de>, { let value = Option::::deserialize(deserializer)?; - - Ok(Self(match value { - Some(value) => CtOption::new(value, Choice::from(1)), - None => CtOption::new(T::default(), Choice::from(0)), - })) + let choice = Choice::from(value.is_some() as u8); + Ok(Self(CtOption::new(value.unwrap_or_default(), choice))) } } diff --git a/src/lib.rs b/src/lib.rs index 7abbd95f..dfb129db 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -141,9 +141,6 @@ #[cfg(all(feature = "alloc", test))] extern crate alloc; -#[cfg(feature = "serde")] -extern crate serde_ as serde; - #[macro_use] mod nlimbs; diff --git a/src/limb.rs b/src/limb.rs index 396b3680..a827812f 100644 --- a/src/limb.rs +++ b/src/limb.rs @@ -23,7 +23,7 @@ use core::fmt; use subtle::{Choice, ConditionallySelectable}; #[cfg(feature = "serde")] -use serde::{Deserialize, Deserializer, Serialize, Serializer}; +use serdect::serde::{Deserialize, Deserializer, Serialize, Serializer}; #[cfg(not(any(target_pointer_width = "32", target_pointer_width = "64")))] compile_error!("this crate builds on 32-bit and 64-bit platforms only"); diff --git a/src/non_zero.rs b/src/non_zero.rs index b062ce17..fc3c9a84 100644 --- a/src/non_zero.rs +++ b/src/non_zero.rs @@ -18,7 +18,7 @@ use { }; #[cfg(feature = "serde")] -use serde::{ +use serdect::serde::{ de::{Error, Unexpected}, Deserialize, Deserializer, Serialize, Serializer, }; diff --git a/src/uint.rs b/src/uint.rs index f47595be..af526688 100644 --- a/src/uint.rs +++ b/src/uint.rs @@ -39,10 +39,7 @@ use core::fmt; use subtle::{Choice, ConditionallySelectable}; #[cfg(feature = "serde")] -use ::{ - serde::{Deserialize, Deserializer, Serialize, Serializer}, - serde_big_array::BigArray, -}; +use serdect::serde::{Deserialize, Deserializer, Serialize, Serializer}; #[cfg(feature = "zeroize")] use zeroize::DefaultIsZeroes; @@ -174,25 +171,32 @@ impl fmt::UpperHex for UInt { #[cfg(feature = "serde")] #[cfg_attr(docsrs, doc(cfg(feature = "serde")))] -impl<'de, const LIMBS: usize> Deserialize<'de> for UInt { +impl<'de, const LIMBS: usize> Deserialize<'de> for UInt +where + UInt: Encoding, +{ fn deserialize(deserializer: D) -> Result where D: Deserializer<'de>, { - Ok(Self { - limbs: BigArray::deserialize(deserializer)?, - }) + let mut buffer = Self::ZERO.to_le_bytes(); + serdect::array::deserialize_hex_or_bin(buffer.as_mut(), deserializer)?; + + Ok(Self::from_le_bytes(buffer)) } } #[cfg(feature = "serde")] #[cfg_attr(docsrs, doc(cfg(feature = "serde")))] -impl<'de, const LIMBS: usize> Serialize for UInt { +impl<'de, const LIMBS: usize> Serialize for UInt +where + UInt: Encoding, +{ fn serialize(&self, serializer: S) -> Result where S: Serializer, { - BigArray::serialize(&self.limbs, serializer) + serdect::array::serialize_hex_lower_or_bin(&Encoding::to_le_bytes(self), serializer) } } diff --git a/src/uint/macros.rs b/src/uint/macros.rs index 5ff76688..431866c2 100644 --- a/src/uint/macros.rs +++ b/src/uint/macros.rs @@ -20,7 +20,7 @@ macro_rules! impl_uint_aliases { } fn from_le_bytes(bytes: Self::Repr) -> Self { - Self::from_be_slice(&bytes) + Self::from_le_slice(&bytes) } #[inline] diff --git a/src/wrapping.rs b/src/wrapping.rs index 2a27bbe0..c2f6c2b3 100644 --- a/src/wrapping.rs +++ b/src/wrapping.rs @@ -5,7 +5,7 @@ use core::fmt; use subtle::{Choice, ConditionallySelectable, ConstantTimeEq}; #[cfg(feature = "serde")] -use serde::{Deserialize, Deserializer, Serialize, Serializer}; +use serdect::serde::{Deserialize, Deserializer, Serialize, Serializer}; /// Provides intentionally-wrapped arithmetic on `T`. ///