mirror of
https://github.com/saymrwulf/curve25519-dalek-source.git
synced 2026-09-05 20:30:57 +00:00
Run rustfmt on src/public.rs.
This commit is contained in:
parent
a141863542
commit
d853856c36
1 changed files with 36 additions and 23 deletions
|
|
@ -12,22 +12,22 @@
|
|||
use core::fmt::Debug;
|
||||
|
||||
use curve25519_dalek::constants;
|
||||
use curve25519_dalek::digest::Digest;
|
||||
use curve25519_dalek::digest::generic_array::typenum::U64;
|
||||
use curve25519_dalek::digest::Digest;
|
||||
use curve25519_dalek::edwards::CompressedEdwardsY;
|
||||
use curve25519_dalek::edwards::EdwardsPoint;
|
||||
use curve25519_dalek::scalar::Scalar;
|
||||
|
||||
pub use sha2::Sha512;
|
||||
|
||||
#[cfg(feature = "serde")]
|
||||
use serde::{Serialize, Deserialize};
|
||||
#[cfg(feature = "serde")]
|
||||
use serde::{Serializer, Deserializer};
|
||||
#[cfg(feature = "serde")]
|
||||
use serde::de::Error as SerdeError;
|
||||
#[cfg(feature = "serde")]
|
||||
use serde::de::Visitor;
|
||||
#[cfg(feature = "serde")]
|
||||
use serde::{Deserialize, Serialize};
|
||||
#[cfg(feature = "serde")]
|
||||
use serde::{Deserializer, Serializer};
|
||||
|
||||
use crate::constants::*;
|
||||
use crate::errors::*;
|
||||
|
|
@ -36,10 +36,7 @@ use crate::signature::*;
|
|||
|
||||
/// An ed25519 public key.
|
||||
#[derive(Copy, Clone, Default, Eq, PartialEq)]
|
||||
pub struct PublicKey(
|
||||
pub (crate) CompressedEdwardsY,
|
||||
pub (crate) EdwardsPoint,
|
||||
);
|
||||
pub struct PublicKey(pub(crate) CompressedEdwardsY, pub(crate) EdwardsPoint);
|
||||
|
||||
impl Debug for PublicKey {
|
||||
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
|
||||
|
|
@ -56,8 +53,8 @@ impl AsRef<[u8]> for PublicKey {
|
|||
impl<'a> From<&'a SecretKey> for PublicKey {
|
||||
/// Derive this public key from its corresponding `SecretKey`.
|
||||
fn from(secret_key: &SecretKey) -> PublicKey {
|
||||
let mut h: Sha512 = Sha512::new();
|
||||
let mut hash: [u8; 64] = [0u8; 64];
|
||||
let mut h: Sha512 = Sha512::new();
|
||||
let mut hash: [u8; 64] = [0u8; 64];
|
||||
let mut digest: [u8; 32] = [0u8; 32];
|
||||
|
||||
h.input(secret_key.as_bytes());
|
||||
|
|
@ -130,14 +127,18 @@ impl PublicKey {
|
|||
#[inline]
|
||||
pub fn from_bytes(bytes: &[u8]) -> Result<PublicKey, SignatureError> {
|
||||
if bytes.len() != PUBLIC_KEY_LENGTH {
|
||||
return Err(SignatureError(InternalError::BytesLengthError{
|
||||
name: "PublicKey", length: PUBLIC_KEY_LENGTH }));
|
||||
return Err(SignatureError(InternalError::BytesLengthError {
|
||||
name: "PublicKey",
|
||||
length: PUBLIC_KEY_LENGTH,
|
||||
}));
|
||||
}
|
||||
let mut bits: [u8; 32] = [0u8; 32];
|
||||
bits.copy_from_slice(&bytes[..32]);
|
||||
|
||||
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))
|
||||
}
|
||||
|
|
@ -145,8 +146,10 @@ impl PublicKey {
|
|||
/// Internal utility function for mangling the bits of a (formerly
|
||||
/// mathematically well-defined) "scalar" and multiplying it to produce a
|
||||
/// public key.
|
||||
fn mangle_scalar_bits_and_multiply_by_basepoint_to_produce_public_key(bits: &mut [u8; 32]) -> PublicKey {
|
||||
bits[0] &= 248;
|
||||
fn mangle_scalar_bits_and_multiply_by_basepoint_to_produce_public_key(
|
||||
bits: &mut [u8; 32],
|
||||
) -> PublicKey {
|
||||
bits[0] &= 248;
|
||||
bits[31] &= 127;
|
||||
bits[31] |= 64;
|
||||
|
||||
|
|
@ -212,8 +215,8 @@ impl PublicKey {
|
|||
context: Option<&[u8]>,
|
||||
signature: &Signature,
|
||||
) -> Result<(), SignatureError>
|
||||
where
|
||||
D: Digest<OutputSize = U64>,
|
||||
where
|
||||
D: Digest<OutputSize = U64>,
|
||||
{
|
||||
let mut h: Sha512 = Sha512::default();
|
||||
let R: EdwardsPoint;
|
||||
|
|
@ -245,25 +248,35 @@ impl PublicKey {
|
|||
|
||||
#[cfg(feature = "serde")]
|
||||
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())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "serde")]
|
||||
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;
|
||||
|
||||
impl<'d> Visitor<'d> for PublicKeyVisitor {
|
||||
type Value = PublicKey;
|
||||
|
||||
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)))
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue