Replace HexBytes with the hex_fmt crate.

master
Andreas Fackler 7 years ago committed by Andreas Fackler
parent d133bb6d79
commit 20de873356
  1. 1
      Cargo.toml
  2. 49
      src/lib.rs

@ -19,6 +19,7 @@ categories = ["cryptography"]
byteorder = "1.2.3" byteorder = "1.2.3"
errno = "0.2.4" errno = "0.2.4"
failure = "0.1" failure = "0.1"
hex_fmt = "0.1"
init_with = "1.1.0" init_with = "1.1.0"
lazy_static = "1.1.0" lazy_static = "1.1.0"
log = "0.4.1" log = "0.4.1"

@ -17,6 +17,7 @@ extern crate byteorder;
extern crate errno; extern crate errno;
#[macro_use] #[macro_use]
extern crate failure; extern crate failure;
extern crate hex_fmt;
extern crate init_with; extern crate init_with;
#[macro_use] #[macro_use]
extern crate lazy_static; extern crate lazy_static;
@ -45,6 +46,7 @@ use std::hash::{Hash, Hasher};
use std::ptr::copy_nonoverlapping; use std::ptr::copy_nonoverlapping;
use byteorder::{BigEndian, ByteOrder}; use byteorder::{BigEndian, ByteOrder};
use hex_fmt::HexFmt;
use init_with::InitWith; use init_with::InitWith;
use pairing::{CurveAffine, CurveProjective, Engine, Field}; use pairing::{CurveAffine, CurveProjective, Engine, Field};
use rand::{ChaChaRng, OsRng, Rand, Rng, SeedableRng}; use rand::{ChaChaRng, OsRng, Rand, Rng, SeedableRng};
@ -67,28 +69,6 @@ pub use mock::{
Ms8Projective as G1, Ms8Projective as G2, Ms8Projective as G1, Ms8Projective as G2,
}; };
/// Wrapper for a byte array, whose `Debug` implementation outputs shortened hexadecimal strings.
pub struct HexBytes<'a>(pub &'a [u8]);
impl<'a> fmt::Debug for HexBytes<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if self.0.len() > 6 {
for byte in &self.0[..3] {
write!(f, "{:02x}", byte)?;
}
write!(f, "..")?;
for byte in &self.0[(self.0.len() - 3)..] {
write!(f, "{:02x}", byte)?;
}
} else {
for byte in self.0 {
write!(f, "{:02x}", byte)?;
}
}
Ok(())
}
}
/// The number of words (`u32`) in a ChaCha RNG seed. /// The number of words (`u32`) in a ChaCha RNG seed.
const CHACHA_RNG_SEED_SIZE: usize = 8; const CHACHA_RNG_SEED_SIZE: usize = 8;
@ -107,8 +87,7 @@ impl Hash for PublicKey {
impl fmt::Debug for PublicKey { impl fmt::Debug for PublicKey {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let uncomp = self.0.into_affine().into_uncompressed(); let uncomp = self.0.into_affine().into_uncompressed();
let bytes = uncomp.as_ref(); f.debug_tuple("PublicKey").field(&HexFmt(uncomp)).finish()
f.debug_tuple("PublicKey").field(&HexBytes(bytes)).finish()
} }
} }
@ -156,9 +135,8 @@ pub struct PublicKeyShare(PublicKey);
impl fmt::Debug for PublicKeyShare { impl fmt::Debug for PublicKeyShare {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let uncomp = (self.0).0.into_affine().into_uncompressed(); let uncomp = (self.0).0.into_affine().into_uncompressed();
let bytes = uncomp.as_ref();
f.debug_tuple("PublicKeyShare") f.debug_tuple("PublicKeyShare")
.field(&HexBytes(bytes)) .field(&HexFmt(uncomp))
.finish() .finish()
} }
} }
@ -195,8 +173,7 @@ pub struct Signature(#[serde(with = "serde_impl::projective")] G2);
impl fmt::Debug for Signature { impl fmt::Debug for Signature {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let uncomp = self.0.into_affine().into_uncompressed(); let uncomp = self.0.into_affine().into_uncompressed();
let bytes = uncomp.as_ref(); f.debug_tuple("Signature").field(&HexFmt(uncomp)).finish()
f.debug_tuple("Signature").field(&HexBytes(bytes)).finish()
} }
} }
@ -209,10 +186,9 @@ impl Hash for Signature {
impl Signature { impl Signature {
pub fn parity(&self) -> bool { pub fn parity(&self) -> bool {
let uncomp = self.0.into_affine().into_uncompressed(); let uncomp = self.0.into_affine().into_uncompressed();
let bytes = uncomp.as_ref(); let xor_bytes: u8 = uncomp.as_ref().iter().fold(0, |result, byte| result ^ byte);
let xor_bytes: u8 = bytes.iter().fold(0, |result, byte| result ^ byte);
let parity = 0 != xor_bytes.count_ones() % 2; let parity = 0 != xor_bytes.count_ones() % 2;
debug!("Signature: {:?}, output: {}", HexBytes(bytes), parity); debug!("Signature: {}, parity: {}", HexFmt(uncomp), parity);
parity parity
} }
} }
@ -225,9 +201,8 @@ pub struct SignatureShare(pub Signature);
impl fmt::Debug for SignatureShare { impl fmt::Debug for SignatureShare {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let uncomp = (self.0).0.into_affine().into_uncompressed(); let uncomp = (self.0).0.into_affine().into_uncompressed();
let bytes = uncomp.as_ref();
f.debug_tuple("SignatureShare") f.debug_tuple("SignatureShare")
.field(&HexBytes(bytes)) .field(&HexFmt(uncomp))
.finish() .finish()
} }
} }
@ -263,7 +238,7 @@ impl Clone for SecretKey {
} }
} }
/// Zeroes out and unlocks the memory allocated from the `SecretKey`'s field element. /// Zeroes out the memory allocated from the `SecretKey`'s field element.
impl Drop for SecretKey { impl Drop for SecretKey {
fn drop(&mut self) { fn drop(&mut self) {
self.zero_secret(); self.zero_secret();
@ -342,8 +317,7 @@ impl SecretKey {
/// field element. /// field element.
pub fn reveal(&self) -> String { pub fn reveal(&self) -> String {
let uncomp = self.public_key().0.into_affine().into_uncompressed(); let uncomp = self.public_key().0.into_affine().into_uncompressed();
let bytes = uncomp.as_ref(); format!("SecretKey({})", HexFmt(uncomp))
format!("SecretKey({:?})", HexBytes(bytes))
} }
} }
@ -402,8 +376,7 @@ impl SecretKeyShare {
/// field element. /// field element.
pub fn reveal(&self) -> String { pub fn reveal(&self) -> String {
let uncomp = self.0.public_key().0.into_affine().into_uncompressed(); let uncomp = self.0.public_key().0.into_affine().into_uncompressed();
let bytes = uncomp.as_ref(); format!("SecretKeyShare({})", HexFmt(uncomp))
format!("SecretKeyShare({:?})", HexBytes(bytes))
} }
} }

Loading…
Cancel
Save