Skip to content

Commit

Permalink
Replace HexBytes with the hex_fmt crate.
Browse files Browse the repository at this point in the history
  • Loading branch information
afck committed Oct 11, 2018
1 parent d133bb6 commit 20de873
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 38 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ categories = ["cryptography"]
byteorder = "1.2.3"
errno = "0.2.4"
failure = "0.1"
hex_fmt = "0.1"
init_with = "1.1.0"
lazy_static = "1.1.0"
log = "0.4.1"
Expand Down
49 changes: 11 additions & 38 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ extern crate byteorder;
extern crate errno;
#[macro_use]
extern crate failure;
extern crate hex_fmt;
extern crate init_with;
#[macro_use]
extern crate lazy_static;
Expand Down Expand Up @@ -45,6 +46,7 @@ use std::hash::{Hash, Hasher};
use std::ptr::copy_nonoverlapping;

use byteorder::{BigEndian, ByteOrder};
use hex_fmt::HexFmt;
use init_with::InitWith;
use pairing::{CurveAffine, CurveProjective, Engine, Field};
use rand::{ChaChaRng, OsRng, Rand, Rng, SeedableRng};
Expand All @@ -67,28 +69,6 @@ pub use mock::{
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.
const CHACHA_RNG_SEED_SIZE: usize = 8;

Expand All @@ -107,8 +87,7 @@ impl Hash for PublicKey {
impl fmt::Debug for PublicKey {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let uncomp = self.0.into_affine().into_uncompressed();
let bytes = uncomp.as_ref();
f.debug_tuple("PublicKey").field(&HexBytes(bytes)).finish()
f.debug_tuple("PublicKey").field(&HexFmt(uncomp)).finish()
}
}

Expand Down Expand Up @@ -156,9 +135,8 @@ pub struct PublicKeyShare(PublicKey);
impl fmt::Debug for PublicKeyShare {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let uncomp = (self.0).0.into_affine().into_uncompressed();
let bytes = uncomp.as_ref();
f.debug_tuple("PublicKeyShare")
.field(&HexBytes(bytes))
.field(&HexFmt(uncomp))
.finish()
}
}
Expand Down Expand Up @@ -195,8 +173,7 @@ pub struct Signature(#[serde(with = "serde_impl::projective")] G2);
impl fmt::Debug for Signature {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let uncomp = self.0.into_affine().into_uncompressed();
let bytes = uncomp.as_ref();
f.debug_tuple("Signature").field(&HexBytes(bytes)).finish()
f.debug_tuple("Signature").field(&HexFmt(uncomp)).finish()
}
}

Expand All @@ -209,10 +186,9 @@ impl Hash for Signature {
impl Signature {
pub fn parity(&self) -> bool {
let uncomp = self.0.into_affine().into_uncompressed();
let bytes = uncomp.as_ref();
let xor_bytes: u8 = bytes.iter().fold(0, |result, byte| result ^ byte);
let xor_bytes: u8 = uncomp.as_ref().iter().fold(0, |result, byte| result ^ byte);
let parity = 0 != xor_bytes.count_ones() % 2;
debug!("Signature: {:?}, output: {}", HexBytes(bytes), parity);
debug!("Signature: {}, parity: {}", HexFmt(uncomp), parity);
parity
}
}
Expand All @@ -225,9 +201,8 @@ pub struct SignatureShare(pub Signature);
impl fmt::Debug for SignatureShare {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let uncomp = (self.0).0.into_affine().into_uncompressed();
let bytes = uncomp.as_ref();
f.debug_tuple("SignatureShare")
.field(&HexBytes(bytes))
.field(&HexFmt(uncomp))
.finish()
}
}
Expand Down Expand Up @@ -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 {
fn drop(&mut self) {
self.zero_secret();
Expand Down Expand Up @@ -342,8 +317,7 @@ impl SecretKey {
/// field element.
pub fn reveal(&self) -> String {
let uncomp = self.public_key().0.into_affine().into_uncompressed();
let bytes = uncomp.as_ref();
format!("SecretKey({:?})", HexBytes(bytes))
format!("SecretKey({})", HexFmt(uncomp))
}
}

Expand Down Expand Up @@ -402,8 +376,7 @@ impl SecretKeyShare {
/// field element.
pub fn reveal(&self) -> String {
let uncomp = self.0.public_key().0.into_affine().into_uncompressed();
let bytes = uncomp.as_ref();
format!("SecretKeyShare({:?})", HexBytes(bytes))
format!("SecretKeyShare({})", HexFmt(uncomp))
}
}

Expand Down

0 comments on commit 20de873

Please sign in to comment.