Run rustfmt on src/public.rs.

This commit is contained in:
Isis Lovecruft 2018-12-30 04:07:50 +00:00
parent a141863542
commit d853856c36
No known key found for this signature in database
GPG key ID: AB41313533E8E812

View file

@ -12,22 +12,22 @@
use core::fmt::Debug; use core::fmt::Debug;
use curve25519_dalek::constants; use curve25519_dalek::constants;
use curve25519_dalek::digest::Digest;
use curve25519_dalek::digest::generic_array::typenum::U64; use curve25519_dalek::digest::generic_array::typenum::U64;
use curve25519_dalek::digest::Digest;
use curve25519_dalek::edwards::CompressedEdwardsY; use curve25519_dalek::edwards::CompressedEdwardsY;
use curve25519_dalek::edwards::EdwardsPoint; use curve25519_dalek::edwards::EdwardsPoint;
use curve25519_dalek::scalar::Scalar; use curve25519_dalek::scalar::Scalar;
pub use sha2::Sha512; pub use sha2::Sha512;
#[cfg(feature = "serde")]
use serde::{Serialize, Deserialize};
#[cfg(feature = "serde")]
use serde::{Serializer, Deserializer};
#[cfg(feature = "serde")] #[cfg(feature = "serde")]
use serde::de::Error as SerdeError; use serde::de::Error as SerdeError;
#[cfg(feature = "serde")] #[cfg(feature = "serde")]
use serde::de::Visitor; use serde::de::Visitor;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
#[cfg(feature = "serde")]
use serde::{Deserializer, Serializer};
use crate::constants::*; use crate::constants::*;
use crate::errors::*; use crate::errors::*;
@ -36,10 +36,7 @@ use crate::signature::*;
/// An ed25519 public key. /// An ed25519 public key.
#[derive(Copy, Clone, Default, Eq, PartialEq)] #[derive(Copy, Clone, Default, Eq, PartialEq)]
pub struct PublicKey( pub struct PublicKey(pub(crate) CompressedEdwardsY, pub(crate) EdwardsPoint);
pub (crate) CompressedEdwardsY,
pub (crate) EdwardsPoint,
);
impl Debug for PublicKey { impl Debug for PublicKey {
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result { fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
@ -131,13 +128,17 @@ impl PublicKey {
pub fn from_bytes(bytes: &[u8]) -> Result<PublicKey, SignatureError> { pub fn from_bytes(bytes: &[u8]) -> Result<PublicKey, SignatureError> {
if bytes.len() != PUBLIC_KEY_LENGTH { if bytes.len() != PUBLIC_KEY_LENGTH {
return Err(SignatureError(InternalError::BytesLengthError { return Err(SignatureError(InternalError::BytesLengthError {
name: "PublicKey", length: PUBLIC_KEY_LENGTH })); name: "PublicKey",
length: PUBLIC_KEY_LENGTH,
}));
} }
let mut bits: [u8; 32] = [0u8; 32]; let mut bits: [u8; 32] = [0u8; 32];
bits.copy_from_slice(&bytes[..32]); bits.copy_from_slice(&bytes[..32]);
let compressed = CompressedEdwardsY(bits); let compressed = CompressedEdwardsY(bits);
let point = compressed.decompress().ok_or(SignatureError(InternalError::PointDecompressionError))?; let point = compressed
.decompress()
.ok_or(SignatureError(InternalError::PointDecompressionError))?;
Ok(PublicKey(compressed, point)) Ok(PublicKey(compressed, point))
} }
@ -145,7 +146,9 @@ impl PublicKey {
/// Internal utility function for mangling the bits of a (formerly /// Internal utility function for mangling the bits of a (formerly
/// mathematically well-defined) "scalar" and multiplying it to produce a /// mathematically well-defined) "scalar" and multiplying it to produce a
/// public key. /// public key.
fn mangle_scalar_bits_and_multiply_by_basepoint_to_produce_public_key(bits: &mut [u8; 32]) -> PublicKey { fn mangle_scalar_bits_and_multiply_by_basepoint_to_produce_public_key(
bits: &mut [u8; 32],
) -> PublicKey {
bits[0] &= 248; bits[0] &= 248;
bits[31] &= 127; bits[31] &= 127;
bits[31] |= 64; bits[31] |= 64;
@ -245,25 +248,35 @@ impl PublicKey {
#[cfg(feature = "serde")] #[cfg(feature = "serde")]
impl Serialize for PublicKey { impl Serialize for PublicKey {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer { fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_bytes(self.as_bytes()) serializer.serialize_bytes(self.as_bytes())
} }
} }
#[cfg(feature = "serde")] #[cfg(feature = "serde")]
impl<'d> Deserialize<'d> for PublicKey { impl<'d> Deserialize<'d> for PublicKey {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: Deserializer<'d> { fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'d>,
{
struct PublicKeyVisitor; struct PublicKeyVisitor;
impl<'d> Visitor<'d> for PublicKeyVisitor { impl<'d> Visitor<'d> for PublicKeyVisitor {
type Value = PublicKey; type Value = PublicKey;
fn expecting(&self, formatter: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result { fn expecting(&self, formatter: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
formatter.write_str("An ed25519 public key as a 32-byte compressed point, as specified in RFC8032") formatter.write_str(
"An ed25519 public key as a 32-byte compressed point, as specified in RFC8032",
)
} }
fn visit_bytes<E>(self, bytes: &[u8]) -> Result<PublicKey, E> where E: SerdeError { fn visit_bytes<E>(self, bytes: &[u8]) -> Result<PublicKey, E>
where
E: SerdeError,
{
PublicKey::from_bytes(bytes).or(Err(SerdeError::invalid_length(bytes.len(), &self))) PublicKey::from_bytes(bytes).or(Err(SerdeError::invalid_length(bytes.len(), &self)))
} }
} }