diff --git a/src/edwards.rs b/src/edwards.rs index 5c90195..d72ec28 100644 --- a/src/edwards.rs +++ b/src/edwards.rs @@ -93,7 +93,6 @@ #![allow(non_snake_case)] use core::borrow::Borrow; -use core::convert::TryFrom; use core::fmt::Debug; use core::iter::Iterator; use core::iter::Sum; @@ -108,8 +107,6 @@ use subtle::ConstantTimeEq; use constants; -use errors::{CurveError, InternalError}; - use field::FieldElement; use scalar::Scalar; @@ -338,33 +335,23 @@ impl Default for CompressedEdwardsY { } } -impl TryFrom<&[u8]> for CompressedEdwardsY { - type Error = CurveError; - - fn try_from(bytes: &[u8]) -> Result { - if bytes.len() != 32 { - return Err(CurveError( - InternalError::BytesLengthError{name: "CompressedEdwardsY", length: 32})); - } - - Ok(CompressedEdwardsY::from_slice(bytes)) - } -} - impl CompressedEdwardsY { /// Construct a `CompressedEdwardsY` from a slice of bytes. /// - /// # Panics + /// # Returns /// - /// If the input `bytes` slice does not have a length of 32. For - /// a panic-safe version of this API, see the implementation of - /// `TryFrom<&[u8]>`. - pub fn from_slice(bytes: &[u8]) -> CompressedEdwardsY { + /// An `Option` which is `None` if the input `bytes` + /// slice does not have a length of 32. + pub fn from_slice(bytes: &[u8]) -> Option { + if bytes.len() != 32 { + return None; + } + let mut tmp = [0u8; 32]; tmp.copy_from_slice(bytes); - CompressedEdwardsY(tmp) + Some(CompressedEdwardsY(tmp)) } } diff --git a/src/errors.rs b/src/errors.rs deleted file mode 100644 index 13a7638..0000000 --- a/src/errors.rs +++ /dev/null @@ -1,69 +0,0 @@ -// -*- mode: rust; -*- -// -// This file is part of curve25519-dalek. -// Copyright (c) 2019 Isis Lovecruft -// See LICENSE for licensing information. -// -// Authors: -// - Isis Agora Lovecruft - -//! Errors which may occur. -//! -//! Currently, these are only used in the implementations of `TryFrom`. -//! -//! If used with `std` support, this public types in this module implement the -//! `std::error::Error` trait. - -use core::fmt; -use core::fmt::Display; - -#[cfg(feature = "std")] -use std::error::Error; - -/// Internal errors. Most application-level developers will likely not -/// need to pay any attention to these. -#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)] -pub(crate) enum InternalError { - /// An error in the length of bytes handed to a constructor. - /// - /// To use this, pass a string specifying the `name` of the type which is - /// returning the error, and the `length` in bytes which its constructor - /// expects. - BytesLengthError { - name: &'static str, - length: usize, - }, -} - -impl Display for InternalError { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match *self { - InternalError::BytesLengthError{ name: n, length: l} - => write!(f, "{} must be {} bytes in length", n, l), - } - } -} - -#[cfg(feature = "std")] -impl Error for InternalError { } - -/// Errors which may occur. -/// -/// This error may arise due to: -/// -/// * Being given bytes with a length different to what was expected. -#[derive(Clone, Copy, Eq, PartialEq, Hash, Debug)] -pub struct CurveError(pub(crate) InternalError); - -impl Display for CurveError { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "{}", self.0) - } -} - -#[cfg(feature = "std")] -impl Error for CurveError { - fn source(&self) -> Option<&(dyn Error + 'static)> { - Some(&self.0) - } -} diff --git a/src/lib.rs b/src/lib.rs index f9bb55d..0216628 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -82,9 +82,6 @@ pub mod constants; // External (and internal) traits. pub mod traits; -// Errors which may occur. -pub mod errors; - //------------------------------------------------------------------------ // curve25519-dalek internal modules //------------------------------------------------------------------------ diff --git a/src/montgomery.rs b/src/montgomery.rs index 7a4f841..66b4670 100644 --- a/src/montgomery.rs +++ b/src/montgomery.rs @@ -48,12 +48,10 @@ // affine and projective cakes and eat both of them too. #![allow(non_snake_case)] -use core::convert::TryFrom; use core::ops::{Mul, MulAssign}; use constants::APLUS2_OVER_FOUR; use edwards::{CompressedEdwardsY, EdwardsPoint}; -use errors::{CurveError, InternalError}; use field::FieldElement; use scalar::Scalar; @@ -112,29 +110,6 @@ impl ValidityCheck for MontgomeryPoint { } } -impl TryFrom<&[u8]> for MontgomeryPoint { - type Error = CurveError; - - fn try_from(bytes: &[u8]) -> Result { - if bytes.len() != 32 { - return Err(CurveError( - InternalError::BytesLengthError{name: "MontgomeryPoint", length: 32})); - } - - let mut array = [0u8; 32]; - array.copy_from_slice(&bytes[..32]); - - let P = MontgomeryPoint(array); - - if P.is_valid() { - return Ok(P); - } - - Err(CurveError( - InternalError::BytesLengthError{name: "MontgomeryPoint", length: 32})) - } -} - impl MontgomeryPoint { /// View this `MontgomeryPoint` as an array of bytes. pub fn as_bytes<'a>(&'a self) -> &'a [u8; 32] { @@ -146,6 +121,29 @@ impl MontgomeryPoint { self.0 } + /// Attempt to create a `MontgomeryPoint` from a slice of bytes. + /// + /// # Returns + /// + /// An `Option` which is `None` if the length of the slice + /// of bytes is not 32, or if the bytes did not represent a canonical + /// `FieldElement`. + pub fn from_slice(bytes: &[u8]) -> Option { + if bytes.len() != 32 { + return None; + } + + let mut array = [0u8; 32]; + array.copy_from_slice(&bytes[..32]); + + let P = MontgomeryPoint(array); + + if P.is_valid() { + return Some(P); + } + None + } + /// Attempt to convert to an `EdwardsPoint`, using the supplied /// choice of sign for the `EdwardsPoint`. /// diff --git a/src/ristretto.rs b/src/ristretto.rs index 46c1c2d..b730938 100644 --- a/src/ristretto.rs +++ b/src/ristretto.rs @@ -158,7 +158,6 @@ //! https://ristretto.group/ use core::borrow::Borrow; -use core::convert::TryFrom; use core::fmt::Debug; use core::iter::Sum; use core::ops::{Add, Neg, Sub}; @@ -181,8 +180,6 @@ use subtle::ConstantTimeEq; use edwards::EdwardsBasepointTable; use edwards::EdwardsPoint; -use errors::{CurveError, InternalError}; - #[allow(unused_imports)] use prelude::*; @@ -220,19 +217,6 @@ impl ConstantTimeEq for CompressedRistretto { } } -impl TryFrom<&[u8]> for CompressedRistretto { - type Error = CurveError; - - fn try_from(bytes: &[u8]) -> Result { - if bytes.len() != 32 { - return Err(CurveError( - InternalError::BytesLengthError{name: "CompressedRistretto", length: 32})); - } - - Ok(CompressedRistretto::from_slice(bytes)) - } -} - impl CompressedRistretto { /// Copy the bytes of this `CompressedRistretto`. pub fn to_bytes(&self) -> [u8; 32] { @@ -246,17 +230,20 @@ impl CompressedRistretto { /// Construct a `CompressedRistretto` from a slice of bytes. /// - /// # Panics + /// # Returns /// - /// If the input `bytes` slice does not have a length of 32. For a - /// panic-safe version of this API, see the implementation of - /// `TryFrom<&[u8]>`. - pub fn from_slice(bytes: &[u8]) -> CompressedRistretto { + /// An `Option` which is `None` if the input `bytes` + /// slice does not have a length of 32. + pub fn from_slice(bytes: &[u8]) -> Option { + if bytes.len() != 32 { + return None; + } + let mut tmp = [0u8; 32]; tmp.copy_from_slice(bytes); - CompressedRistretto(tmp) + Some(CompressedRistretto(tmp)) } /// Attempt to decompress to an `RistrettoPoint`.