Make from_slice methods fallible; add TryFrom<&[u8]> (#495)

The `from_slice` methods on `CompressedEdwardsY` and
`CompressedRistretto` both previously panicked if the slice was the
wrong length.

This changes them to be fallible, returning `TryFromSliceError` in the
event the slice is the wrong length.

It also adds a `TryFrom<&[u8]>` impl for each of these types which calls
the corresponding `from_slice` method.
This commit is contained in:
Tony Arcieri 2023-01-19 12:08:18 -07:00 committed by GitHub
parent 8d1bc31805
commit bfacbe7ee4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 16 deletions

View file

@ -93,6 +93,7 @@
// affine and projective cakes and eat both of them too. // affine and projective cakes and eat both of them too.
#![allow(non_snake_case)] #![allow(non_snake_case)]
use core::array::TryFromSliceError;
use core::borrow::Borrow; use core::borrow::Borrow;
use core::fmt::Debug; use core::fmt::Debug;
use core::iter::Iterator; use core::iter::Iterator;
@ -213,6 +214,14 @@ impl CompressedEdwardsY {
} }
} }
impl TryFrom<&[u8]> for CompressedEdwardsY {
type Error = TryFromSliceError;
fn try_from(slice: &[u8]) -> Result<CompressedEdwardsY, TryFromSliceError> {
Self::from_slice(slice)
}
}
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
// Serde support // Serde support
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
@ -360,15 +369,12 @@ impl Default for CompressedEdwardsY {
impl CompressedEdwardsY { impl CompressedEdwardsY {
/// Construct a `CompressedEdwardsY` from a slice of bytes. /// Construct a `CompressedEdwardsY` from a slice of bytes.
/// ///
/// # Panics /// # Errors
/// ///
/// If the input `bytes` slice does not have a length of 32. /// Returns [`TryFromSliceError`] if the input `bytes` slice does not have
pub fn from_slice(bytes: &[u8]) -> CompressedEdwardsY { /// a length of 32.
let mut tmp = [0u8; 32]; pub fn from_slice(bytes: &[u8]) -> Result<CompressedEdwardsY, TryFromSliceError> {
bytes.try_into().map(CompressedEdwardsY)
tmp.copy_from_slice(bytes);
CompressedEdwardsY(tmp)
} }
} }

View file

@ -161,6 +161,7 @@
#[cfg(feature = "alloc")] #[cfg(feature = "alloc")]
use alloc::vec::Vec; use alloc::vec::Vec;
use core::array::TryFromSliceError;
use core::borrow::Borrow; use core::borrow::Borrow;
use core::fmt::Debug; use core::fmt::Debug;
use core::iter::Sum; use core::iter::Sum;
@ -244,15 +245,12 @@ impl CompressedRistretto {
/// Construct a `CompressedRistretto` from a slice of bytes. /// Construct a `CompressedRistretto` from a slice of bytes.
/// ///
/// # Panics /// # Errors
/// ///
/// If the input `bytes` slice does not have a length of 32. /// Returns [`TryFromSliceError`] if the input `bytes` slice does not have
pub fn from_slice(bytes: &[u8]) -> CompressedRistretto { /// a length of 32.
let mut tmp = [0u8; 32]; pub fn from_slice(bytes: &[u8]) -> Result<CompressedRistretto, TryFromSliceError> {
bytes.try_into().map(CompressedRistretto)
tmp.copy_from_slice(bytes);
CompressedRistretto(tmp)
} }
/// Attempt to decompress to an `RistrettoPoint`. /// Attempt to decompress to an `RistrettoPoint`.
@ -337,6 +335,14 @@ impl Default for CompressedRistretto {
} }
} }
impl TryFrom<&[u8]> for CompressedRistretto {
type Error = TryFromSliceError;
fn try_from(slice: &[u8]) -> Result<CompressedRistretto, TryFromSliceError> {
Self::from_slice(slice)
}
}
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
// Serde support // Serde support
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------