mirror of
https://github.com/saymrwulf/curve25519-dalek-source.git
synced 2026-09-10 21:21:13 +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 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 {
|
||||||
|
|
@ -56,8 +53,8 @@ impl AsRef<[u8]> for PublicKey {
|
||||||
impl<'a> From<&'a SecretKey> for PublicKey {
|
impl<'a> From<&'a SecretKey> for PublicKey {
|
||||||
/// Derive this public key from its corresponding `SecretKey`.
|
/// Derive this public key from its corresponding `SecretKey`.
|
||||||
fn from(secret_key: &SecretKey) -> PublicKey {
|
fn from(secret_key: &SecretKey) -> PublicKey {
|
||||||
let mut h: Sha512 = Sha512::new();
|
let mut h: Sha512 = Sha512::new();
|
||||||
let mut hash: [u8; 64] = [0u8; 64];
|
let mut hash: [u8; 64] = [0u8; 64];
|
||||||
let mut digest: [u8; 32] = [0u8; 32];
|
let mut digest: [u8; 32] = [0u8; 32];
|
||||||
|
|
||||||
h.input(secret_key.as_bytes());
|
h.input(secret_key.as_bytes());
|
||||||
|
|
@ -130,14 +127,18 @@ impl PublicKey {
|
||||||
#[inline]
|
#[inline]
|
||||||
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,8 +146,10 @@ 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[0] &= 248;
|
bits: &mut [u8; 32],
|
||||||
|
) -> PublicKey {
|
||||||
|
bits[0] &= 248;
|
||||||
bits[31] &= 127;
|
bits[31] &= 127;
|
||||||
bits[31] |= 64;
|
bits[31] |= 64;
|
||||||
|
|
||||||
|
|
@ -212,8 +215,8 @@ impl PublicKey {
|
||||||
context: Option<&[u8]>,
|
context: Option<&[u8]>,
|
||||||
signature: &Signature,
|
signature: &Signature,
|
||||||
) -> Result<(), SignatureError>
|
) -> Result<(), SignatureError>
|
||||||
where
|
where
|
||||||
D: Digest<OutputSize = U64>,
|
D: Digest<OutputSize = U64>,
|
||||||
{
|
{
|
||||||
let mut h: Sha512 = Sha512::default();
|
let mut h: Sha512 = Sha512::default();
|
||||||
let R: EdwardsPoint;
|
let R: EdwardsPoint;
|
||||||
|
|
@ -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)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue