//! Checked arithmetic. use subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption}; #[cfg(feature = "serde")] use serdect::serde::{Deserialize, Deserializer, Serialize, Serializer}; /// Provides intentionally-checked arithmetic on `T`. /// /// Internally this leverages the [`CtOption`] type from the [`subtle`] crate /// in order to handle overflows in constant time. #[derive(Copy, Clone, Debug)] pub struct Checked(pub CtOption); impl Checked { /// Create a new checked arithmetic wrapper for the given value. pub fn new(val: T) -> Self { Self(CtOption::new(val, Choice::from(1))) } } impl Default for Checked where T: Default, { fn default() -> Self { Self::new(T::default()) } } impl ConditionallySelectable for Checked { #[inline] fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self { Self(CtOption::conditional_select(&a.0, &b.0, choice)) } } impl ConstantTimeEq for Checked { #[inline] fn ct_eq(&self, rhs: &Self) -> Choice { self.0.ct_eq(&rhs.0) } } impl From> for CtOption { fn from(checked: Checked) -> CtOption { checked.0 } } impl From> for Checked { fn from(ct_option: CtOption) -> Checked { Checked(ct_option) } } impl From> for Option { fn from(checked: Checked) -> Option { checked.0.into() } } #[cfg(feature = "serde")] #[cfg_attr(docsrs, doc(cfg(feature = "serde")))] impl<'de, T: Default + Deserialize<'de>> Deserialize<'de> for Checked { fn deserialize(deserializer: D) -> Result where D: Deserializer<'de>, { let value = Option::::deserialize(deserializer)?; let choice = Choice::from(value.is_some() as u8); Ok(Self(CtOption::new(value.unwrap_or_default(), choice))) } } #[cfg(feature = "serde")] #[cfg_attr(docsrs, doc(cfg(feature = "serde")))] impl<'de, T: Copy + Serialize> Serialize for Checked { fn serialize(&self, serializer: S) -> Result where S: Serializer, { Option::::from(self.0).serialize(serializer) } } #[cfg(all(test, feature = "serde"))] mod tests { use crate::{Checked, U64}; use subtle::{Choice, ConstantTimeEq, CtOption}; #[test] fn serde() { let test = Checked::new(U64::from_u64(0x0011223344556677)); let serialized = bincode::serialize(&test).unwrap(); let deserialized: Checked = bincode::deserialize(&serialized).unwrap(); assert!(bool::from(test.ct_eq(&deserialized))); let test = Checked::new(U64::ZERO) - Checked::new(U64::ONE); assert!(bool::from( test.ct_eq(&Checked(CtOption::new(U64::ZERO, Choice::from(0)))) )); let serialized = bincode::serialize(&test).unwrap(); let deserialized: Checked = bincode::deserialize(&serialized).unwrap(); assert!(bool::from(test.ct_eq(&deserialized))); } #[test] fn serde_owned() { let test = Checked::new(U64::from_u64(0x0011223344556677)); let serialized = bincode::serialize(&test).unwrap(); let deserialized: Checked = bincode::deserialize_from(serialized.as_slice()).unwrap(); assert!(bool::from(test.ct_eq(&deserialized))); let test = Checked::new(U64::ZERO) - Checked::new(U64::ONE); assert!(bool::from( test.ct_eq(&Checked(CtOption::new(U64::ZERO, Choice::from(0)))) )); let serialized = bincode::serialize(&test).unwrap(); let deserialized: Checked = bincode::deserialize_from(serialized.as_slice()).unwrap(); assert!(bool::from(test.ct_eq(&deserialized))); } }