Implement new uncompressed bytes for PublicKey

This commit is contained in:
Paul Hauner 2019-03-12 17:15:45 +11:00
parent c92f867cd8
commit 1b252c3f82
No known key found for this signature in database
GPG Key ID: D362883A9218FCC6
2 changed files with 19 additions and 6 deletions

View File

@ -4,7 +4,7 @@ use std::fs::File;
use std::io::{Error, ErrorKind, Read, Write};
use std::path::Path;
pub const PUBLIC_KEY_BYTES_LEN: usize = 48;
pub const PUBLIC_KEY_BYTES_LEN: usize = 96;
pub const SECRET_KEY_BYTES_LEN: usize = 48;
pub const BATCH_SIZE: usize = 1_000; // ~15MB
@ -26,7 +26,7 @@ impl KeypairsFile for Vec<Keypair> {
for keypair in keypair_batch {
buf.append(&mut keypair.sk.as_raw().as_bytes());
buf.append(&mut keypair.pk.as_raw().as_bytes());
buf.append(&mut keypair.pk.clone().as_uncompressed_bytes());
}
keypairs_file.write_all(&buf)?;

View File

@ -27,14 +27,21 @@ impl PublicKey {
&self.0
}
/// Converts compressed bytes to PublicKey
pub fn from_bytes(bytes: &[u8]) -> Result<Self, DecodeError> {
let pubkey = RawPublicKey::from_bytes(&bytes).map_err(|_| DecodeError::Invalid)?;
Ok(PublicKey(pubkey))
}
/// Returns the PublicKey as (x, y) bytes
pub fn as_uncompressed_bytes(&mut self) -> Vec<u8> {
RawPublicKey::as_uncompressed_bytes(&mut self.0)
pub fn as_uncompressed_bytes(&self) -> Vec<u8> {
RawPublicKey::as_uncompressed_bytes(&mut self.0.clone())
}
/// Converts (x, y) bytes to PublicKey
pub fn from_uncompressed_bytes(bytes: &[u8]) -> Result<Self, DecodeError> {
let pubkey = RawPublicKey::from_uncompressed_bytes(&bytes).map_err(|_| DecodeError::Invalid)?;
let pubkey =
RawPublicKey::from_uncompressed_bytes(&bytes).map_err(|_| DecodeError::Invalid)?;
Ok(PublicKey(pubkey))
}
@ -103,8 +110,14 @@ impl PartialEq for PublicKey {
}
impl Hash for PublicKey {
/// Note: this is distinct from consensus serialization, it will produce a different hash.
///
/// This method uses the uncompressed bytes, which are much faster to obtain than the
/// compressed bytes required for consensus serialization.
///
/// Use `ssz::Encode` to obtain the bytes required for consensus hashing.
fn hash<H: Hasher>(&self, state: &mut H) {
ssz_encode(self).hash(state)
self.as_uncompressed_bytes().hash(state)
}
}