From 54c338c06a13c5c5764926625df09ff82c892da9 Mon Sep 17 00:00:00 2001 From: austinabell Date: Wed, 27 Nov 2019 14:03:20 -0500 Subject: [PATCH 01/15] Created cbor and json traits --- Cargo.toml | 3 ++- encoding/Cargo.toml | 9 +++++++++ encoding/src/cbor.rs | 4 ++++ encoding/src/errors.rs | 31 +++++++++++++++++++++++++++++++ encoding/src/json.rs | 4 ++++ encoding/src/lib.rs | 7 +++++++ vm/Cargo.toml | 2 ++ vm/address/Cargo.toml | 2 ++ vm/address/src/lib.rs | 13 +++++++++---- vm/src/message/mod.rs | 13 ++++++++----- vm/src/message/signed_message.rs | 12 +++++++----- 11 files changed, 85 insertions(+), 15 deletions(-) create mode 100644 encoding/Cargo.toml create mode 100644 encoding/src/cbor.rs create mode 100644 encoding/src/errors.rs create mode 100644 encoding/src/json.rs create mode 100644 encoding/src/lib.rs diff --git a/Cargo.toml b/Cargo.toml index 360a1dcd769a..49c554d35c7f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,5 +4,6 @@ members = [ "vm", "vm/address", "node", - "crypto" + "crypto", + "encoding" ] diff --git a/encoding/Cargo.toml b/encoding/Cargo.toml new file mode 100644 index 000000000000..0c763ae5ee4f --- /dev/null +++ b/encoding/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "encoding" +version = "0.1.0" +authors = ["ChainSafe Systems "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/encoding/src/cbor.rs b/encoding/src/cbor.rs new file mode 100644 index 000000000000..75d8726c675d --- /dev/null +++ b/encoding/src/cbor.rs @@ -0,0 +1,4 @@ +pub trait Cbor { + fn unmarshall_cbor(&mut self, bz: &mut [u8]) -> Result<(), String>; + fn marshall_cbor(&self) -> Result, String>; +} diff --git a/encoding/src/errors.rs b/encoding/src/errors.rs new file mode 100644 index 000000000000..fae2bf431e5d --- /dev/null +++ b/encoding/src/errors.rs @@ -0,0 +1,31 @@ +use std::fmt; + +#[derive(Debug, PartialEq)] +pub enum Error { + DecodingError(CodecProtocol), + EncodingError(CodecProtocol), +} + +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match *self { + Error::DecodingError(ref cdc) => write!(f, "Could not decode {}.", cdc), + Error::EncodingError(ref cdc) => write!(f, "Could not encode {}.", cdc), + } + } +} + +#[derive(Debug, PartialEq)] +pub enum CodecProtocol { + Cbor, + JSON, +} + +impl fmt::Display for CodecProtocol { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match *self { + CodecProtocol::Cbor => write!(f, "Cbor"), + CodecProtocol::JSON => write!(f, "JSON"), + } + } +} diff --git a/encoding/src/json.rs b/encoding/src/json.rs new file mode 100644 index 000000000000..648a2c0853d7 --- /dev/null +++ b/encoding/src/json.rs @@ -0,0 +1,4 @@ +pub trait JSON { + fn unmarshall_json(&mut self, bz: &mut [u8]) -> Result<(), String>; + fn marshall_json(&self) -> Result, String>; +} diff --git a/encoding/src/lib.rs b/encoding/src/lib.rs new file mode 100644 index 000000000000..741316513566 --- /dev/null +++ b/encoding/src/lib.rs @@ -0,0 +1,7 @@ +mod cbor; +mod errors; +mod json; + +pub use cbor::*; +pub use errors::*; +pub use json::*; diff --git a/vm/Cargo.toml b/vm/Cargo.toml index 15e442a7d9ea..bc93283a0827 100644 --- a/vm/Cargo.toml +++ b/vm/Cargo.toml @@ -11,4 +11,6 @@ cid = "0.3.1" num-bigint = "0.2.3" crypto = {path = "../crypto"} address = {path = "./address"} +encoding = {path = "../encoding"} + diff --git a/vm/address/Cargo.toml b/vm/address/Cargo.toml index b30a69c4c5f4..12204e102155 100644 --- a/vm/address/Cargo.toml +++ b/vm/address/Cargo.toml @@ -13,3 +13,5 @@ data-encoding = "2.1.2" data-encoding-macro = "0.1.7" blake2 = "0.8.1" leb128 = "0.2.1" +encoding = {path = "../../encoding"} + diff --git a/vm/address/src/lib.rs b/vm/address/src/lib.rs index 8da90fb6d332..b92ed43a1a20 100644 --- a/vm/address/src/lib.rs +++ b/vm/address/src/lib.rs @@ -9,6 +9,7 @@ use blake2::digest::{Input, VariableOutput}; use blake2::VarBlake2b; use data_encoding::Encoding; use data_encoding_macro::{internal_new_encoding, new_encoding}; +use encoding::{Cbor, JSON}; use leb128; /// defines the encoder for base32 encoding with the provided string with no padding @@ -168,23 +169,27 @@ impl Address { None => encode(self, Network::Testnet), } } +} +impl Cbor for Address { // Marshalling and unmarshalling - pub fn unmarshall_cbor(&mut self, _bz: &mut [u8]) -> Result<(), String> { + fn unmarshall_cbor(&mut self, _bz: &mut [u8]) -> Result<(), String> { // TODO Err("Unmarshall is unimplemented".to_owned()) } - pub fn marshall_cbor(&self) -> Result, String> { + fn marshall_cbor(&self) -> Result, String> { // TODO Err("Marshall is unimplemented".to_owned()) } +} +impl JSON for Address { // JSON Marshalling and unmarshalling - pub fn unmarshall_json(&mut self, _bz: &mut [u8]) -> Result<(), String> { + fn unmarshall_json(&mut self, _bz: &mut [u8]) -> Result<(), String> { // TODO Err("JSON unmarshall is unimplemented".to_owned()) } - pub fn marshall_json(&self) -> Result, String> { + fn marshall_json(&self) -> Result, String> { // TODO Err("JSON marshall is unimplemented".to_owned()) } diff --git a/vm/src/message/mod.rs b/vm/src/message/mod.rs index c2fc62e3a2f0..c0219cf7c548 100644 --- a/vm/src/message/mod.rs +++ b/vm/src/message/mod.rs @@ -5,6 +5,7 @@ pub use message_receipt::*; pub use signed_message::*; use address::Address; +use encoding::Cbor; use num_bigint::BigUint; /// VM message type which includes all data needed for a state transition @@ -33,13 +34,15 @@ impl Message { pub fn to(&self) -> Address { self.to.clone() } - // Marshalling and unmarshalling - pub fn unmarshall_cbor(&mut self, _bz: &mut [u8]) -> Result<(), String> { +} + +impl Cbor for Message { + fn unmarshall_cbor(&mut self, _bz: &mut [u8]) -> Result<(), String> { // TODO - Err("Unmarshall cbor not implemented".to_owned()) + Err("Unmarshall is unimplemented".to_owned()) } - pub fn marshall_cbor(&self) -> Result, String> { + fn marshall_cbor(&self) -> Result, String> { // TODO - Err("Marshall cbor not implemented".to_owned()) + Err("Marshall is unimplemented".to_owned()) } } diff --git a/vm/src/message/signed_message.rs b/vm/src/message/signed_message.rs index 7daf36349bee..ced3cacfb042 100644 --- a/vm/src/message/signed_message.rs +++ b/vm/src/message/signed_message.rs @@ -1,5 +1,6 @@ use super::Message; use crypto::{Error as CryptoError, Signature, Signer}; +use encoding::Cbor; /// SignedMessage represents a wrapped message with signature bytes #[derive(PartialEq, Clone)] @@ -19,14 +20,15 @@ impl SignedMessage { signature: sig, }) } +} - // Marshalling and unmarshalling - pub fn unmarshall_cbor(&mut self, _bz: &mut [u8]) -> Result<(), String> { +impl Cbor for SignedMessage { + fn unmarshall_cbor(&mut self, _bz: &mut [u8]) -> Result<(), String> { // TODO - Err("Unmarshall cbor not implemented".to_owned()) + Err("Unmarshall is unimplemented".to_owned()) } - pub fn marshall_cbor(&self) -> Result, String> { + fn marshall_cbor(&self) -> Result, String> { // TODO - Err("Marshall cbor not implemented".to_owned()) + Err("Marshall is unimplemented".to_owned()) } } From 92adf5ca4cb2af17e21136b3cf77c1d017db5378 Mon Sep 17 00:00:00 2001 From: austinabell Date: Wed, 27 Nov 2019 14:15:04 -0500 Subject: [PATCH 02/15] abstract variable hashing --- encoding/Cargo.toml | 1 + encoding/src/hash.rs | 18 ++++++++++++++++++ encoding/src/lib.rs | 2 ++ vm/address/Cargo.toml | 1 - vm/address/src/lib.rs | 24 +++--------------------- 5 files changed, 24 insertions(+), 22 deletions(-) create mode 100644 encoding/src/hash.rs diff --git a/encoding/Cargo.toml b/encoding/Cargo.toml index 0c763ae5ee4f..b9ed16795623 100644 --- a/encoding/Cargo.toml +++ b/encoding/Cargo.toml @@ -7,3 +7,4 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +blake2 = "0.8.1" diff --git a/encoding/src/hash.rs b/encoding/src/hash.rs new file mode 100644 index 000000000000..d5afcacc9b68 --- /dev/null +++ b/encoding/src/hash.rs @@ -0,0 +1,18 @@ +use blake2::digest::{Input, VariableOutput}; +use blake2::VarBlake2b; + +/// generates blake2b hash with provided size +pub fn variable_hash(ingest: Vec, size: usize) -> Vec { + let mut hasher = VarBlake2b::new(size).unwrap(); + hasher.input(ingest); + + // allocate hash result vector + let mut result: Vec = vec![0; size]; + + hasher.variable_result(|res| { + // Copy result slice to vector return + result[..size].clone_from_slice(res); + }); + + result +} diff --git a/encoding/src/lib.rs b/encoding/src/lib.rs index 741316513566..98278ff3211c 100644 --- a/encoding/src/lib.rs +++ b/encoding/src/lib.rs @@ -1,7 +1,9 @@ mod cbor; mod errors; +mod hash; mod json; pub use cbor::*; pub use errors::*; +pub use hash::*; pub use json::*; diff --git a/vm/address/Cargo.toml b/vm/address/Cargo.toml index 12204e102155..d9c44e2d073a 100644 --- a/vm/address/Cargo.toml +++ b/vm/address/Cargo.toml @@ -11,7 +11,6 @@ num-traits = "0.2" num-derive = "0.2" data-encoding = "2.1.2" data-encoding-macro = "0.1.7" -blake2 = "0.8.1" leb128 = "0.2.1" encoding = {path = "../../encoding"} diff --git a/vm/address/src/lib.rs b/vm/address/src/lib.rs index b92ed43a1a20..966e8fad61c5 100644 --- a/vm/address/src/lib.rs +++ b/vm/address/src/lib.rs @@ -5,11 +5,9 @@ pub use self::errors::Error; pub use self::network::Network; pub use self::protocol::Protocol; -use blake2::digest::{Input, VariableOutput}; -use blake2::VarBlake2b; use data_encoding::Encoding; use data_encoding_macro::{internal_new_encoding, new_encoding}; -use encoding::{Cbor, JSON}; +use encoding::{variable_hash, Cbor, JSON}; use leb128; /// defines the encoder for base32 encoding with the provided string with no padding @@ -229,7 +227,7 @@ fn encode(addr: &Address, network: Network) -> String { /// Checksum calculates the 4 byte checksum hash pub fn checksum(ingest: Vec) -> Vec { - hash(ingest, CHECKSUM_HASH_LEN) + variable_hash(ingest, CHECKSUM_HASH_LEN) } /// Validates the checksum against the ingest data @@ -240,21 +238,5 @@ pub fn validate_checksum(ingest: Vec, expect: Vec) -> bool { /// Returns an address hash for given data fn address_hash(ingest: Vec) -> Vec { - hash(ingest, PAYLOAD_HASH_LEN) -} - -/// generates blake2b hash with provided size -fn hash(ingest: Vec, size: usize) -> Vec { - let mut hasher = VarBlake2b::new(size).unwrap(); - hasher.input(ingest); - - // allocate hash result vector - let mut result: Vec = vec![0; size]; - - hasher.variable_result(|res| { - // Copy result slice to vector return - result[..size].clone_from_slice(res); - }); - - result + variable_hash(ingest, PAYLOAD_HASH_LEN) } From fd6be6638fe4b9aadb2dd5e24259bea2ffd29b5d Mon Sep 17 00:00:00 2001 From: austinabell Date: Wed, 27 Nov 2019 14:20:26 -0500 Subject: [PATCH 03/15] abstract blake 256 hashing --- crypto/Cargo.toml | 1 + crypto/src/signature.rs | 14 +------------- encoding/src/hash.rs | 11 +++++++++++ 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/crypto/Cargo.toml b/crypto/Cargo.toml index c3785d65c4e1..95e3f4416e46 100644 --- a/crypto/Cargo.toml +++ b/crypto/Cargo.toml @@ -9,6 +9,7 @@ edition = "2018" [dependencies] blake2 = "0.8.1" address = {path = "../vm/address"} +encoding = {path = "../encoding"} libsecp256k1 = "0.2.1" bls-signatures = "0.2.0" diff --git a/crypto/src/signature.rs b/crypto/src/signature.rs index 098449c0529e..d7d2e08855f3 100644 --- a/crypto/src/signature.rs +++ b/crypto/src/signature.rs @@ -1,10 +1,9 @@ use super::errors::Error; use address::{Address, Protocol}; -use blake2::digest::{Input, VariableOutput}; -use blake2::VarBlake2b; use bls_signatures::{ hash as bls_hash, verify, PublicKey as BlsPubKey, Serialize, Signature as BlsSignature, }; +use encoding::blake2b_256; use secp256k1::{recover, Message, RecoveryId, Signature as EcsdaSignature}; @@ -86,17 +85,6 @@ fn ecrecover(hash: &[u8; 32], signature: &[u8; 65]) -> Result { Ok(addr) } -/// generates blake2b hash of 32 bytes -fn blake2b_256(ingest: Vec, hash: &mut [u8; 32]) { - let mut hasher = VarBlake2b::new(32).unwrap(); - hasher.input(ingest); - - hasher.variable_result(|res| { - // Copy result slice to vector return - hash[..32].clone_from_slice(res); - }); -} - #[cfg(test)] mod tests { use super::*; diff --git a/encoding/src/hash.rs b/encoding/src/hash.rs index d5afcacc9b68..ef2d4d5a9b52 100644 --- a/encoding/src/hash.rs +++ b/encoding/src/hash.rs @@ -16,3 +16,14 @@ pub fn variable_hash(ingest: Vec, size: usize) -> Vec { result } + +/// generates blake2b hash of 32 bytes +pub fn blake2b_256(ingest: Vec, hash: &mut [u8; 32]) { + let mut hasher = VarBlake2b::new(32).unwrap(); + hasher.input(ingest); + + hasher.variable_result(|res| { + // Copy result slice to vector return + hash[..32].clone_from_slice(res); + }); +} From aafd59e1a8bac2711c63a3c9ec30558584c91a6b Mon Sep 17 00:00:00 2001 From: austinabell Date: Wed, 27 Nov 2019 14:44:37 -0500 Subject: [PATCH 04/15] test variable lengths --- encoding/src/hash.rs | 20 ++++++++++++++++++-- vm/address/src/lib.rs | 6 +++--- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/encoding/src/hash.rs b/encoding/src/hash.rs index ef2d4d5a9b52..b9a437fc739c 100644 --- a/encoding/src/hash.rs +++ b/encoding/src/hash.rs @@ -2,7 +2,7 @@ use blake2::digest::{Input, VariableOutput}; use blake2::VarBlake2b; /// generates blake2b hash with provided size -pub fn variable_hash(ingest: Vec, size: usize) -> Vec { +pub fn blake2b_variable(ingest: Vec, size: usize) -> Vec { let mut hasher = VarBlake2b::new(size).unwrap(); hasher.input(ingest); @@ -23,7 +23,23 @@ pub fn blake2b_256(ingest: Vec, hash: &mut [u8; 32]) { hasher.input(ingest); hasher.variable_result(|res| { - // Copy result slice to vector return + // Copy result slice to hash array reference hash[..32].clone_from_slice(res); }); } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn hash_length() { + let ingest = vec![1, 4, 2, 3]; + let hash = blake2b_variable(ingest.clone(), 8); + assert_eq!(hash.len(), 8); + let hash = blake2b_variable(ingest.clone(), 20); + assert_eq!(hash.len(), 20); + let hash = blake2b_variable(ingest.clone(), 32); + assert_eq!(hash.len(), 32); + } +} diff --git a/vm/address/src/lib.rs b/vm/address/src/lib.rs index 966e8fad61c5..e273dcb83da5 100644 --- a/vm/address/src/lib.rs +++ b/vm/address/src/lib.rs @@ -7,7 +7,7 @@ pub use self::protocol::Protocol; use data_encoding::Encoding; use data_encoding_macro::{internal_new_encoding, new_encoding}; -use encoding::{variable_hash, Cbor, JSON}; +use encoding::{blake2b_variable, Cbor, JSON}; use leb128; /// defines the encoder for base32 encoding with the provided string with no padding @@ -227,7 +227,7 @@ fn encode(addr: &Address, network: Network) -> String { /// Checksum calculates the 4 byte checksum hash pub fn checksum(ingest: Vec) -> Vec { - variable_hash(ingest, CHECKSUM_HASH_LEN) + blake2b_variable(ingest, CHECKSUM_HASH_LEN) } /// Validates the checksum against the ingest data @@ -238,5 +238,5 @@ pub fn validate_checksum(ingest: Vec, expect: Vec) -> bool { /// Returns an address hash for given data fn address_hash(ingest: Vec) -> Vec { - variable_hash(ingest, PAYLOAD_HASH_LEN) + blake2b_variable(ingest, PAYLOAD_HASH_LEN) } From 48e0e11cdfed808a0d146fe83f5296ddffbbdf52 Mon Sep 17 00:00:00 2001 From: austinabell Date: Wed, 27 Nov 2019 14:47:16 -0500 Subject: [PATCH 05/15] remove irrelevant comments --- vm/address/src/lib.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/vm/address/src/lib.rs b/vm/address/src/lib.rs index e273dcb83da5..710348a076bf 100644 --- a/vm/address/src/lib.rs +++ b/vm/address/src/lib.rs @@ -170,7 +170,6 @@ impl Address { } impl Cbor for Address { - // Marshalling and unmarshalling fn unmarshall_cbor(&mut self, _bz: &mut [u8]) -> Result<(), String> { // TODO Err("Unmarshall is unimplemented".to_owned()) @@ -182,7 +181,6 @@ impl Cbor for Address { } impl JSON for Address { - // JSON Marshalling and unmarshalling fn unmarshall_json(&mut self, _bz: &mut [u8]) -> Result<(), String> { // TODO Err("JSON unmarshall is unimplemented".to_owned()) From 91985bcfcbdb5d313e8ab73191da955462ce771f Mon Sep 17 00:00:00 2001 From: austinabell Date: Wed, 27 Nov 2019 16:11:07 -0500 Subject: [PATCH 06/15] fix error usage --- crypto/src/errors.rs | 7 ++++--- encoding/src/cbor.rs | 6 ++++-- encoding/src/errors.rs | 8 ++++---- encoding/src/json.rs | 6 ++++-- vm/address/src/lib.rs | 18 +++++++++--------- vm/src/message/mod.rs | 10 +++++----- vm/src/message/signed_message.rs | 10 +++++----- 7 files changed, 35 insertions(+), 30 deletions(-) diff --git a/crypto/src/errors.rs b/crypto/src/errors.rs index 0d7ac9e980b1..92a9f3df4318 100644 --- a/crypto/src/errors.rs +++ b/crypto/src/errors.rs @@ -1,4 +1,5 @@ use address::Error as AddressError; +use encoding::Error as EncodingError; use secp256k1::Error as SecpError; use std::error; use std::fmt; @@ -48,9 +49,9 @@ impl From for Error { } // TODO: Remove once cbor marshalling and unmarshalling implemented -impl From for Error { - fn from(err: String) -> Error { +impl From for Error { + fn from(err: EncodingError) -> Error { // Pass error encountered in signer trait as module error type - Error::SigningError(err) + Error::SigningError(err.to_string()) } } diff --git a/encoding/src/cbor.rs b/encoding/src/cbor.rs index 75d8726c675d..e9252e16a8af 100644 --- a/encoding/src/cbor.rs +++ b/encoding/src/cbor.rs @@ -1,4 +1,6 @@ +use super::errors::Error; + pub trait Cbor { - fn unmarshall_cbor(&mut self, bz: &mut [u8]) -> Result<(), String>; - fn marshall_cbor(&self) -> Result, String>; + fn unmarshall_cbor(&mut self, bz: &mut [u8]) -> Result<(), Error>; + fn marshall_cbor(&self) -> Result, Error>; } diff --git a/encoding/src/errors.rs b/encoding/src/errors.rs index fae2bf431e5d..8d7355fb1013 100644 --- a/encoding/src/errors.rs +++ b/encoding/src/errors.rs @@ -2,15 +2,15 @@ use std::fmt; #[derive(Debug, PartialEq)] pub enum Error { - DecodingError(CodecProtocol), - EncodingError(CodecProtocol), + Unmarshalling(CodecProtocol), + Marshalling(CodecProtocol), } impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match *self { - Error::DecodingError(ref cdc) => write!(f, "Could not decode {}.", cdc), - Error::EncodingError(ref cdc) => write!(f, "Could not encode {}.", cdc), + Error::Unmarshalling(ref cdc) => write!(f, "Could not decode {}.", cdc), + Error::Marshalling(ref cdc) => write!(f, "Could not encode {}.", cdc), } } } diff --git a/encoding/src/json.rs b/encoding/src/json.rs index 648a2c0853d7..4a6c99c92962 100644 --- a/encoding/src/json.rs +++ b/encoding/src/json.rs @@ -1,4 +1,6 @@ +use super::errors::Error; + pub trait JSON { - fn unmarshall_json(&mut self, bz: &mut [u8]) -> Result<(), String>; - fn marshall_json(&self) -> Result, String>; + fn unmarshall_json(&mut self, bz: &mut [u8]) -> Result<(), Error>; + fn marshall_json(&self) -> Result, Error>; } diff --git a/vm/address/src/lib.rs b/vm/address/src/lib.rs index 710348a076bf..4bef85f4d7ce 100644 --- a/vm/address/src/lib.rs +++ b/vm/address/src/lib.rs @@ -7,7 +7,7 @@ pub use self::protocol::Protocol; use data_encoding::Encoding; use data_encoding_macro::{internal_new_encoding, new_encoding}; -use encoding::{blake2b_variable, Cbor, JSON}; +use encoding::{blake2b_variable, Cbor, CodecProtocol, Error as EncodingError, JSON}; use leb128; /// defines the encoder for base32 encoding with the provided string with no padding @@ -170,24 +170,24 @@ impl Address { } impl Cbor for Address { - fn unmarshall_cbor(&mut self, _bz: &mut [u8]) -> Result<(), String> { + fn unmarshall_cbor(&mut self, _bz: &mut [u8]) -> Result<(), EncodingError> { // TODO - Err("Unmarshall is unimplemented".to_owned()) + Err(EncodingError::Unmarshalling(CodecProtocol::Cbor)) } - fn marshall_cbor(&self) -> Result, String> { + fn marshall_cbor(&self) -> Result, EncodingError> { // TODO - Err("Marshall is unimplemented".to_owned()) + Err(EncodingError::Marshalling(CodecProtocol::Cbor)) } } impl JSON for Address { - fn unmarshall_json(&mut self, _bz: &mut [u8]) -> Result<(), String> { + fn unmarshall_json(&mut self, _bz: &mut [u8]) -> Result<(), EncodingError> { // TODO - Err("JSON unmarshall is unimplemented".to_owned()) + Err(EncodingError::Unmarshalling(CodecProtocol::JSON)) } - fn marshall_json(&self) -> Result, String> { + fn marshall_json(&self) -> Result, EncodingError> { // TODO - Err("JSON marshall is unimplemented".to_owned()) + Err(EncodingError::Marshalling(CodecProtocol::JSON)) } } diff --git a/vm/src/message/mod.rs b/vm/src/message/mod.rs index c0219cf7c548..52e9b17a08a3 100644 --- a/vm/src/message/mod.rs +++ b/vm/src/message/mod.rs @@ -5,7 +5,7 @@ pub use message_receipt::*; pub use signed_message::*; use address::Address; -use encoding::Cbor; +use encoding::{Cbor, CodecProtocol, Error as EncodingError}; use num_bigint::BigUint; /// VM message type which includes all data needed for a state transition @@ -37,12 +37,12 @@ impl Message { } impl Cbor for Message { - fn unmarshall_cbor(&mut self, _bz: &mut [u8]) -> Result<(), String> { + fn unmarshall_cbor(&mut self, _bz: &mut [u8]) -> Result<(), EncodingError> { // TODO - Err("Unmarshall is unimplemented".to_owned()) + Err(EncodingError::Unmarshalling(CodecProtocol::Cbor)) } - fn marshall_cbor(&self) -> Result, String> { + fn marshall_cbor(&self) -> Result, EncodingError> { // TODO - Err("Marshall is unimplemented".to_owned()) + Err(EncodingError::Marshalling(CodecProtocol::Cbor)) } } diff --git a/vm/src/message/signed_message.rs b/vm/src/message/signed_message.rs index ced3cacfb042..4d6ffbc70d80 100644 --- a/vm/src/message/signed_message.rs +++ b/vm/src/message/signed_message.rs @@ -1,6 +1,6 @@ use super::Message; use crypto::{Error as CryptoError, Signature, Signer}; -use encoding::Cbor; +use encoding::{Cbor, CodecProtocol, Error as EncodingError}; /// SignedMessage represents a wrapped message with signature bytes #[derive(PartialEq, Clone)] @@ -23,12 +23,12 @@ impl SignedMessage { } impl Cbor for SignedMessage { - fn unmarshall_cbor(&mut self, _bz: &mut [u8]) -> Result<(), String> { + fn unmarshall_cbor(&mut self, _bz: &mut [u8]) -> Result<(), EncodingError> { // TODO - Err("Unmarshall is unimplemented".to_owned()) + Err(EncodingError::Unmarshalling(CodecProtocol::Cbor)) } - fn marshall_cbor(&self) -> Result, String> { + fn marshall_cbor(&self) -> Result, EncodingError> { // TODO - Err("Marshall is unimplemented".to_owned()) + Err(EncodingError::Marshalling(CodecProtocol::Cbor)) } } From 029b105e5fe72175109625ae1c6ec0f2e731ce26 Mon Sep 17 00:00:00 2001 From: austinabell Date: Wed, 27 Nov 2019 16:12:10 -0500 Subject: [PATCH 07/15] remove todo --- crypto/src/errors.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/crypto/src/errors.rs b/crypto/src/errors.rs index 92a9f3df4318..c0910cfb9d36 100644 --- a/crypto/src/errors.rs +++ b/crypto/src/errors.rs @@ -48,7 +48,6 @@ impl From for Error { } } -// TODO: Remove once cbor marshalling and unmarshalling implemented impl From for Error { fn from(err: EncodingError) -> Error { // Pass error encountered in signer trait as module error type From 4d55e032008859da25cfef4a40cf02d758f383fd Mon Sep 17 00:00:00 2001 From: austinabell Date: Thu, 28 Nov 2019 11:20:40 -0500 Subject: [PATCH 08/15] Clean error usage and remove JSON trait until needed --- encoding/src/errors.rs | 52 +++++++++++++++++++++++++++++--- encoding/src/hash.rs | 21 ++++++++++++- encoding/src/json.rs | 6 ---- encoding/src/lib.rs | 2 -- vm/address/src/lib.rs | 25 ++++++--------- vm/address/src/protocol.rs | 2 +- vm/address/tests/address_test.rs | 2 +- vm/src/message/mod.rs | 12 ++++++-- vm/src/message/signed_message.rs | 12 ++++++-- 9 files changed, 97 insertions(+), 37 deletions(-) delete mode 100644 encoding/src/json.rs diff --git a/encoding/src/errors.rs b/encoding/src/errors.rs index 8d7355fb1013..696f247ec552 100644 --- a/encoding/src/errors.rs +++ b/encoding/src/errors.rs @@ -1,20 +1,62 @@ use std::fmt; +/// Error type for encoding and decoding data through any Ferret supported protocol +/// +/// This error will provide any details about the data which was attempted to be +/// encoded or decoded. The +/// +/// Usage: +/// ```no_run +/// use encoding::{Error, CodecProtocol}; +/// +/// Error::Marshalling { +/// formatted_data: format!("{:?}", vec![0]), +/// protocol: CodecProtocol::Cbor, +/// }; +/// Error::Unmarshalling { +/// formatted_data: format!("{:?}", vec![0]), +/// protocol: CodecProtocol::JSON, +/// }; +/// ``` #[derive(Debug, PartialEq)] pub enum Error { - Unmarshalling(CodecProtocol), - Marshalling(CodecProtocol), + Unmarshalling { + formatted_data: String, + protocol: CodecProtocol, + }, + Marshalling { + formatted_data: String, + protocol: CodecProtocol, + }, } impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match *self { - Error::Unmarshalling(ref cdc) => write!(f, "Could not decode {}.", cdc), - Error::Marshalling(ref cdc) => write!(f, "Could not encode {}.", cdc), + match self { + Error::Unmarshalling { + formatted_data, + protocol, + } => write!( + f, + "Could not decode: {} in format: {}.", + formatted_data, protocol + ), + Error::Marshalling { + formatted_data, + protocol, + } => write!( + f, + "Could not encode: {} in format: {}.", + formatted_data, protocol + ), } } } +/// CodecProtocol defines the protocol in which the data is encoded or decoded +/// +/// This is used with the encoding errors, to detail the encoding protocol or any other +/// information about how the data was encoded or decoded #[derive(Debug, PartialEq)] pub enum CodecProtocol { Cbor, diff --git a/encoding/src/hash.rs b/encoding/src/hash.rs index b9a437fc739c..349224312e82 100644 --- a/encoding/src/hash.rs +++ b/encoding/src/hash.rs @@ -2,6 +2,15 @@ use blake2::digest::{Input, VariableOutput}; use blake2::VarBlake2b; /// generates blake2b hash with provided size +/// +/// # Example +/// ``` +/// use encoding::blake2b_variable; +/// +/// let ingest: Vec = vec![]; +/// let hash = blake2b_variable(ingest, 20); +/// assert_eq!(hash.len(), 20); +/// ``` pub fn blake2b_variable(ingest: Vec, size: usize) -> Vec { let mut hasher = VarBlake2b::new(size).unwrap(); hasher.input(ingest); @@ -17,7 +26,17 @@ pub fn blake2b_variable(ingest: Vec, size: usize) -> Vec { result } -/// generates blake2b hash of 32 bytes +/// generates blake2b hash of fixed 32 bytes size +/// +/// # Example +/// ``` +/// use encoding::blake2b_256; +/// +/// let ingest: Vec = vec![]; +/// +/// let mut hash = [0u8; 32]; +/// blake2b_256(ingest, &mut hash); +/// ``` pub fn blake2b_256(ingest: Vec, hash: &mut [u8; 32]) { let mut hasher = VarBlake2b::new(32).unwrap(); hasher.input(ingest); diff --git a/encoding/src/json.rs b/encoding/src/json.rs deleted file mode 100644 index 4a6c99c92962..000000000000 --- a/encoding/src/json.rs +++ /dev/null @@ -1,6 +0,0 @@ -use super::errors::Error; - -pub trait JSON { - fn unmarshall_json(&mut self, bz: &mut [u8]) -> Result<(), Error>; - fn marshall_json(&self) -> Result, Error>; -} diff --git a/encoding/src/lib.rs b/encoding/src/lib.rs index 98278ff3211c..cc359b69cccd 100644 --- a/encoding/src/lib.rs +++ b/encoding/src/lib.rs @@ -1,9 +1,7 @@ mod cbor; mod errors; mod hash; -mod json; pub use cbor::*; pub use errors::*; pub use hash::*; -pub use json::*; diff --git a/vm/address/src/lib.rs b/vm/address/src/lib.rs index 4bef85f4d7ce..43d42ea4a391 100644 --- a/vm/address/src/lib.rs +++ b/vm/address/src/lib.rs @@ -7,7 +7,7 @@ pub use self::protocol::Protocol; use data_encoding::Encoding; use data_encoding_macro::{internal_new_encoding, new_encoding}; -use encoding::{blake2b_variable, Cbor, CodecProtocol, Error as EncodingError, JSON}; +use encoding::{blake2b_variable, Cbor, CodecProtocol, Error as EncodingError}; use leb128; /// defines the encoder for base32 encoding with the provided string with no padding @@ -25,7 +25,7 @@ const TESTNET_PREFIX: &str = "t"; /// Address is the struct that defines the protocol and data payload conversion from either /// a public key or value -#[derive(PartialEq, Clone)] +#[derive(PartialEq, Clone, Debug)] pub struct Address { protocol: Protocol, payload: Vec, @@ -172,22 +172,17 @@ impl Address { impl Cbor for Address { fn unmarshall_cbor(&mut self, _bz: &mut [u8]) -> Result<(), EncodingError> { // TODO - Err(EncodingError::Unmarshalling(CodecProtocol::Cbor)) + Err(EncodingError::Unmarshalling { + formatted_data: format!("{:?}", self), + protocol: CodecProtocol::Cbor, + }) } fn marshall_cbor(&self) -> Result, EncodingError> { // TODO - Err(EncodingError::Marshalling(CodecProtocol::Cbor)) - } -} - -impl JSON for Address { - fn unmarshall_json(&mut self, _bz: &mut [u8]) -> Result<(), EncodingError> { - // TODO - Err(EncodingError::Unmarshalling(CodecProtocol::JSON)) - } - fn marshall_json(&self) -> Result, EncodingError> { - // TODO - Err(EncodingError::Marshalling(CodecProtocol::JSON)) + Err(EncodingError::Marshalling { + formatted_data: format!("{:?}", self), + protocol: CodecProtocol::Cbor, + }) } } diff --git a/vm/address/src/protocol.rs b/vm/address/src/protocol.rs index fb323b74bf9c..160ea74e9336 100644 --- a/vm/address/src/protocol.rs +++ b/vm/address/src/protocol.rs @@ -3,7 +3,7 @@ use num_traits::FromPrimitive; use std::fmt; /// Protocol defines the addressing protocol used to derive data to an address -#[derive(PartialEq, Copy, Clone, FromPrimitive)] +#[derive(PartialEq, Copy, Clone, FromPrimitive, Debug)] pub enum Protocol { // ID protocol addressing ID = 0, diff --git a/vm/address/tests/address_test.rs b/vm/address/tests/address_test.rs index 702b57c20cd1..5d64377a8213 100644 --- a/vm/address/tests/address_test.rs +++ b/vm/address/tests/address_test.rs @@ -47,7 +47,7 @@ fn test_address(addr: Address, protocol: Protocol, expected: &'static str) { let from_bytes = Address::from_bytes(decoded.to_bytes()).unwrap(); assert!(decoded == from_bytes); - // TODO: test JSON/cbor encoding and decoding + // TODO: test cbor encoding and decoding } #[test] diff --git a/vm/src/message/mod.rs b/vm/src/message/mod.rs index 52e9b17a08a3..00f4a398d2b2 100644 --- a/vm/src/message/mod.rs +++ b/vm/src/message/mod.rs @@ -9,7 +9,7 @@ use encoding::{Cbor, CodecProtocol, Error as EncodingError}; use num_bigint::BigUint; /// VM message type which includes all data needed for a state transition -#[derive(PartialEq, Clone)] +#[derive(PartialEq, Clone, Debug)] pub struct Message { from: Address, to: Address, @@ -39,10 +39,16 @@ impl Message { impl Cbor for Message { fn unmarshall_cbor(&mut self, _bz: &mut [u8]) -> Result<(), EncodingError> { // TODO - Err(EncodingError::Unmarshalling(CodecProtocol::Cbor)) + Err(EncodingError::Unmarshalling { + formatted_data: format!("{:?}", self), + protocol: CodecProtocol::Cbor, + }) } fn marshall_cbor(&self) -> Result, EncodingError> { // TODO - Err(EncodingError::Marshalling(CodecProtocol::Cbor)) + Err(EncodingError::Marshalling { + formatted_data: format!("{:?}", self), + protocol: CodecProtocol::Cbor, + }) } } diff --git a/vm/src/message/signed_message.rs b/vm/src/message/signed_message.rs index 4d6ffbc70d80..a3e94f44223b 100644 --- a/vm/src/message/signed_message.rs +++ b/vm/src/message/signed_message.rs @@ -3,7 +3,7 @@ use crypto::{Error as CryptoError, Signature, Signer}; use encoding::{Cbor, CodecProtocol, Error as EncodingError}; /// SignedMessage represents a wrapped message with signature bytes -#[derive(PartialEq, Clone)] +#[derive(PartialEq, Clone, Debug)] pub struct SignedMessage { pub(crate) message: Message, pub(crate) signature: Signature, @@ -25,10 +25,16 @@ impl SignedMessage { impl Cbor for SignedMessage { fn unmarshall_cbor(&mut self, _bz: &mut [u8]) -> Result<(), EncodingError> { // TODO - Err(EncodingError::Unmarshalling(CodecProtocol::Cbor)) + Err(EncodingError::Unmarshalling { + formatted_data: format!("{:?}", self), + protocol: CodecProtocol::Cbor, + }) } fn marshall_cbor(&self) -> Result, EncodingError> { // TODO - Err(EncodingError::Marshalling(CodecProtocol::Cbor)) + Err(EncodingError::Marshalling { + formatted_data: format!("{:?}", self), + protocol: CodecProtocol::Cbor, + }) } } From 0979885ca1e2eb33cf6cd3fdb8435723955e7596 Mon Sep 17 00:00:00 2001 From: austinabell Date: Fri, 29 Nov 2019 10:47:02 -0500 Subject: [PATCH 09/15] switch blake2 hashing to more efficient lib --- encoding/Cargo.toml | 2 +- encoding/src/hash.rs | 32 +++++++++++++------------------- 2 files changed, 14 insertions(+), 20 deletions(-) diff --git a/encoding/Cargo.toml b/encoding/Cargo.toml index b9ed16795623..180ea2c6c3a7 100644 --- a/encoding/Cargo.toml +++ b/encoding/Cargo.toml @@ -7,4 +7,4 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -blake2 = "0.8.1" +blake2b_simd = "0.5.9" diff --git a/encoding/src/hash.rs b/encoding/src/hash.rs index 349224312e82..de69c986f54a 100644 --- a/encoding/src/hash.rs +++ b/encoding/src/hash.rs @@ -1,5 +1,4 @@ -use blake2::digest::{Input, VariableOutput}; -use blake2::VarBlake2b; +use blake2b_simd::Params; /// generates blake2b hash with provided size /// @@ -12,18 +11,13 @@ use blake2::VarBlake2b; /// assert_eq!(hash.len(), 20); /// ``` pub fn blake2b_variable(ingest: Vec, size: usize) -> Vec { - let mut hasher = VarBlake2b::new(size).unwrap(); - hasher.input(ingest); + let hash = Params::new() + .hash_length(size) + .to_state() + .update(&ingest) + .finalize(); - // allocate hash result vector - let mut result: Vec = vec![0; size]; - - hasher.variable_result(|res| { - // Copy result slice to vector return - result[..size].clone_from_slice(res); - }); - - result + hash.as_bytes().to_vec() } /// generates blake2b hash of fixed 32 bytes size @@ -38,13 +32,13 @@ pub fn blake2b_variable(ingest: Vec, size: usize) -> Vec { /// blake2b_256(ingest, &mut hash); /// ``` pub fn blake2b_256(ingest: Vec, hash: &mut [u8; 32]) { - let mut hasher = VarBlake2b::new(32).unwrap(); - hasher.input(ingest); + let hsh = Params::new() + .hash_length(32) + .key(&ingest) + .to_state() + .finalize(); - hasher.variable_result(|res| { - // Copy result slice to hash array reference - hash[..32].clone_from_slice(res); - }); + hash[..32].clone_from_slice(hsh.as_bytes()); } #[cfg(test)] From cb93889d0cb8d2ff87f0b11ea2800f312dd120b0 Mon Sep 17 00:00:00 2001 From: austinabell Date: Fri, 29 Nov 2019 13:13:06 -0500 Subject: [PATCH 10/15] Change trait function signature and changed format of error --- encoding/Cargo.toml | 1 + encoding/src/cbor.rs | 4 +++- encoding/src/errors.rs | 29 +++++++++++++++++++---------- vm/address/Cargo.toml | 2 +- vm/address/src/lib.rs | 6 +++--- vm/src/message/mod.rs | 6 +++--- vm/src/message/signed_message.rs | 6 +++--- 7 files changed, 33 insertions(+), 21 deletions(-) diff --git a/encoding/Cargo.toml b/encoding/Cargo.toml index 180ea2c6c3a7..8fd50e72ee7e 100644 --- a/encoding/Cargo.toml +++ b/encoding/Cargo.toml @@ -8,3 +8,4 @@ edition = "2018" [dependencies] blake2b_simd = "0.5.9" +serde_cbor = "0.10.2" diff --git a/encoding/src/cbor.rs b/encoding/src/cbor.rs index e9252e16a8af..b0a5024764a2 100644 --- a/encoding/src/cbor.rs +++ b/encoding/src/cbor.rs @@ -1,6 +1,8 @@ use super::errors::Error; pub trait Cbor { - fn unmarshall_cbor(&mut self, bz: &mut [u8]) -> Result<(), Error>; + fn unmarshall_cbor(bz: &mut [u8]) -> Result + where + Self: Sized; fn marshall_cbor(&self) -> Result, Error>; } diff --git a/encoding/src/errors.rs b/encoding/src/errors.rs index 696f247ec552..b62a07363983 100644 --- a/encoding/src/errors.rs +++ b/encoding/src/errors.rs @@ -10,22 +10,22 @@ use std::fmt; /// use encoding::{Error, CodecProtocol}; /// /// Error::Marshalling { -/// formatted_data: format!("{:?}", vec![0]), +/// description: format!("{:?}", vec![0]), /// protocol: CodecProtocol::Cbor, /// }; /// Error::Unmarshalling { -/// formatted_data: format!("{:?}", vec![0]), +/// description: format!("{:?}", vec![0]), /// protocol: CodecProtocol::JSON, /// }; /// ``` #[derive(Debug, PartialEq)] pub enum Error { Unmarshalling { - formatted_data: String, + description: String, protocol: CodecProtocol, }, Marshalling { - formatted_data: String, + description: String, protocol: CodecProtocol, }, } @@ -34,25 +34,34 @@ impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { Error::Unmarshalling { - formatted_data, + description, protocol, } => write!( f, - "Could not decode: {} in format: {}.", - formatted_data, protocol + "Could not decode in format {}: {}", + protocol, description ), Error::Marshalling { - formatted_data, + description, protocol, } => write!( f, - "Could not encode: {} in format: {}.", - formatted_data, protocol + "Could not encode in format {}: {}", + protocol, description ), } } } +impl From for Error { + fn from(err: serde_cbor::error::Error) -> Error { + Error::Marshalling { + description: err.to_string(), + protocol: CodecProtocol::Cbor, + } + } +} + /// CodecProtocol defines the protocol in which the data is encoded or decoded /// /// This is used with the encoding errors, to detail the encoding protocol or any other diff --git a/vm/address/Cargo.toml b/vm/address/Cargo.toml index d9c44e2d073a..da1fb5008201 100644 --- a/vm/address/Cargo.toml +++ b/vm/address/Cargo.toml @@ -13,4 +13,4 @@ data-encoding = "2.1.2" data-encoding-macro = "0.1.7" leb128 = "0.2.1" encoding = {path = "../../encoding"} - +serde_cbor = "0.10.2" diff --git a/vm/address/src/lib.rs b/vm/address/src/lib.rs index 43d42ea4a391..cf7cfb6a6c39 100644 --- a/vm/address/src/lib.rs +++ b/vm/address/src/lib.rs @@ -170,17 +170,17 @@ impl Address { } impl Cbor for Address { - fn unmarshall_cbor(&mut self, _bz: &mut [u8]) -> Result<(), EncodingError> { + fn unmarshall_cbor(_bz: &mut [u8]) -> Result { // TODO Err(EncodingError::Unmarshalling { - formatted_data: format!("{:?}", self), + description: "Not Implemented".to_string(), protocol: CodecProtocol::Cbor, }) } fn marshall_cbor(&self) -> Result, EncodingError> { // TODO Err(EncodingError::Marshalling { - formatted_data: format!("{:?}", self), + description: format!("Not implemented, data: {:?}", self), protocol: CodecProtocol::Cbor, }) } diff --git a/vm/src/message/mod.rs b/vm/src/message/mod.rs index 00f4a398d2b2..bed17b7d6325 100644 --- a/vm/src/message/mod.rs +++ b/vm/src/message/mod.rs @@ -37,17 +37,17 @@ impl Message { } impl Cbor for Message { - fn unmarshall_cbor(&mut self, _bz: &mut [u8]) -> Result<(), EncodingError> { + fn unmarshall_cbor(_bz: &mut [u8]) -> Result { // TODO Err(EncodingError::Unmarshalling { - formatted_data: format!("{:?}", self), + description: "Not Implemented".to_string(), protocol: CodecProtocol::Cbor, }) } fn marshall_cbor(&self) -> Result, EncodingError> { // TODO Err(EncodingError::Marshalling { - formatted_data: format!("{:?}", self), + description: format!("Not implemented, data: {:?}", self), protocol: CodecProtocol::Cbor, }) } diff --git a/vm/src/message/signed_message.rs b/vm/src/message/signed_message.rs index a3e94f44223b..72513a9c427a 100644 --- a/vm/src/message/signed_message.rs +++ b/vm/src/message/signed_message.rs @@ -23,17 +23,17 @@ impl SignedMessage { } impl Cbor for SignedMessage { - fn unmarshall_cbor(&mut self, _bz: &mut [u8]) -> Result<(), EncodingError> { + fn unmarshall_cbor(_bz: &mut [u8]) -> Result { // TODO Err(EncodingError::Unmarshalling { - formatted_data: format!("{:?}", self), + description: "Not Implemented".to_string(), protocol: CodecProtocol::Cbor, }) } fn marshall_cbor(&self) -> Result, EncodingError> { // TODO Err(EncodingError::Marshalling { - formatted_data: format!("{:?}", self), + description: format!("Not implemented, data: {:?}", self), protocol: CodecProtocol::Cbor, }) } From 482a074c47e7cc08bff1bdc835234531e2d8c724 Mon Sep 17 00:00:00 2001 From: austinabell Date: Fri, 29 Nov 2019 13:13:45 -0500 Subject: [PATCH 11/15] remove JSON encoding type --- encoding/src/errors.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/encoding/src/errors.rs b/encoding/src/errors.rs index b62a07363983..fbe8fc154367 100644 --- a/encoding/src/errors.rs +++ b/encoding/src/errors.rs @@ -15,7 +15,7 @@ use std::fmt; /// }; /// Error::Unmarshalling { /// description: format!("{:?}", vec![0]), -/// protocol: CodecProtocol::JSON, +/// protocol: CodecProtocol::Cbor, /// }; /// ``` #[derive(Debug, PartialEq)] @@ -69,14 +69,12 @@ impl From for Error { #[derive(Debug, PartialEq)] pub enum CodecProtocol { Cbor, - JSON, } impl fmt::Display for CodecProtocol { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match *self { CodecProtocol::Cbor => write!(f, "Cbor"), - CodecProtocol::JSON => write!(f, "JSON"), } } } From 1ccb4f1b0a82dc22727fa59caf3e19294829cfad Mon Sep 17 00:00:00 2001 From: austinabell Date: Fri, 29 Nov 2019 13:52:37 -0500 Subject: [PATCH 12/15] change unmarshalling from requiring mutable ref --- encoding/src/cbor.rs | 2 +- vm/address/src/lib.rs | 2 +- vm/src/message/mod.rs | 2 +- vm/src/message/signed_message.rs | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/encoding/src/cbor.rs b/encoding/src/cbor.rs index b0a5024764a2..611457766d20 100644 --- a/encoding/src/cbor.rs +++ b/encoding/src/cbor.rs @@ -1,7 +1,7 @@ use super::errors::Error; pub trait Cbor { - fn unmarshall_cbor(bz: &mut [u8]) -> Result + fn unmarshall_cbor(bz: &[u8]) -> Result where Self: Sized; fn marshall_cbor(&self) -> Result, Error>; diff --git a/vm/address/src/lib.rs b/vm/address/src/lib.rs index cf7cfb6a6c39..fe314356fd8d 100644 --- a/vm/address/src/lib.rs +++ b/vm/address/src/lib.rs @@ -170,7 +170,7 @@ impl Address { } impl Cbor for Address { - fn unmarshall_cbor(_bz: &mut [u8]) -> Result { + fn unmarshall_cbor(_bz: &[u8]) -> Result { // TODO Err(EncodingError::Unmarshalling { description: "Not Implemented".to_string(), diff --git a/vm/src/message/mod.rs b/vm/src/message/mod.rs index bed17b7d6325..293cc8724233 100644 --- a/vm/src/message/mod.rs +++ b/vm/src/message/mod.rs @@ -37,7 +37,7 @@ impl Message { } impl Cbor for Message { - fn unmarshall_cbor(_bz: &mut [u8]) -> Result { + fn unmarshall_cbor(_bz: &[u8]) -> Result { // TODO Err(EncodingError::Unmarshalling { description: "Not Implemented".to_string(), diff --git a/vm/src/message/signed_message.rs b/vm/src/message/signed_message.rs index 72513a9c427a..12c3f7c0145b 100644 --- a/vm/src/message/signed_message.rs +++ b/vm/src/message/signed_message.rs @@ -23,7 +23,7 @@ impl SignedMessage { } impl Cbor for SignedMessage { - fn unmarshall_cbor(_bz: &mut [u8]) -> Result { + fn unmarshall_cbor(_bz: &[u8]) -> Result { // TODO Err(EncodingError::Unmarshalling { description: "Not Implemented".to_string(), From b239c4d53302f404e18620bf2b0aad7daf922115 Mon Sep 17 00:00:00 2001 From: austinabell Date: Fri, 29 Nov 2019 13:56:24 -0500 Subject: [PATCH 13/15] fix typo --- encoding/src/cbor.rs | 4 ++-- vm/address/src/lib.rs | 4 ++-- vm/src/message/mod.rs | 4 ++-- vm/src/message/signed_message.rs | 6 +++--- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/encoding/src/cbor.rs b/encoding/src/cbor.rs index 611457766d20..a365333ded31 100644 --- a/encoding/src/cbor.rs +++ b/encoding/src/cbor.rs @@ -1,8 +1,8 @@ use super::errors::Error; pub trait Cbor { - fn unmarshall_cbor(bz: &[u8]) -> Result + fn unmarshal_cbor(bz: &[u8]) -> Result where Self: Sized; - fn marshall_cbor(&self) -> Result, Error>; + fn marshal_cbor(&self) -> Result, Error>; } diff --git a/vm/address/src/lib.rs b/vm/address/src/lib.rs index fe314356fd8d..41055e0b78ba 100644 --- a/vm/address/src/lib.rs +++ b/vm/address/src/lib.rs @@ -170,14 +170,14 @@ impl Address { } impl Cbor for Address { - fn unmarshall_cbor(_bz: &[u8]) -> Result { + fn unmarshal_cbor(_bz: &[u8]) -> Result { // TODO Err(EncodingError::Unmarshalling { description: "Not Implemented".to_string(), protocol: CodecProtocol::Cbor, }) } - fn marshall_cbor(&self) -> Result, EncodingError> { + fn marshal_cbor(&self) -> Result, EncodingError> { // TODO Err(EncodingError::Marshalling { description: format!("Not implemented, data: {:?}", self), diff --git a/vm/src/message/mod.rs b/vm/src/message/mod.rs index 293cc8724233..d7cae9699d3c 100644 --- a/vm/src/message/mod.rs +++ b/vm/src/message/mod.rs @@ -37,14 +37,14 @@ impl Message { } impl Cbor for Message { - fn unmarshall_cbor(_bz: &[u8]) -> Result { + fn unmarshal_cbor(_bz: &[u8]) -> Result { // TODO Err(EncodingError::Unmarshalling { description: "Not Implemented".to_string(), protocol: CodecProtocol::Cbor, }) } - fn marshall_cbor(&self) -> Result, EncodingError> { + fn marshal_cbor(&self) -> Result, EncodingError> { // TODO Err(EncodingError::Marshalling { description: format!("Not implemented, data: {:?}", self), diff --git a/vm/src/message/signed_message.rs b/vm/src/message/signed_message.rs index 12c3f7c0145b..d06771d3eaaa 100644 --- a/vm/src/message/signed_message.rs +++ b/vm/src/message/signed_message.rs @@ -11,7 +11,7 @@ pub struct SignedMessage { impl SignedMessage { pub fn new(msg: &Message, s: impl Signer) -> Result { - let bz = msg.marshall_cbor()?; + let bz = msg.marshal_cbor()?; let sig = s.sign_bytes(bz, msg.from())?; @@ -23,14 +23,14 @@ impl SignedMessage { } impl Cbor for SignedMessage { - fn unmarshall_cbor(_bz: &[u8]) -> Result { + fn unmarshal_cbor(_bz: &[u8]) -> Result { // TODO Err(EncodingError::Unmarshalling { description: "Not Implemented".to_string(), protocol: CodecProtocol::Cbor, }) } - fn marshall_cbor(&self) -> Result, EncodingError> { + fn marshal_cbor(&self) -> Result, EncodingError> { // TODO Err(EncodingError::Marshalling { description: format!("Not implemented, data: {:?}", self), From c8a71149d910e40ca39f69503c894eb45599931f Mon Sep 17 00:00:00 2001 From: austinabell Date: Fri, 29 Nov 2019 16:45:28 -0500 Subject: [PATCH 14/15] change fixed hash to return a vector --- crypto/src/signature.rs | 7 ++++--- encoding/src/hash.rs | 13 ++++++------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/crypto/src/signature.rs b/crypto/src/signature.rs index d7d2e08855f3..6a85c36a4fbc 100644 --- a/crypto/src/signature.rs +++ b/crypto/src/signature.rs @@ -47,13 +47,14 @@ fn verify_bls_sig(data: Vec, pub_k: Vec, sig: Signature) -> bool { /// returns true if a secp256k1 signature is valid fn verify_secp256k1_sig(data: Vec, addr: Address, sig: Signature) -> bool { // blake2b 256 hash - let mut hash = [0u8; 32]; - blake2b_256(data, &mut hash); + let hash = blake2b_256(data); + let mut arr = [0u8; 32]; + arr.clone_from_slice(&hash[..32]); // Ecrecover with hash and signature let mut signature = [0u8; 65]; signature[..].clone_from_slice(sig.as_ref()); - let rec_addr = ecrecover(&hash, &signature); + let rec_addr = ecrecover(&arr, &signature); // check address against recovered address match rec_addr { diff --git a/encoding/src/hash.rs b/encoding/src/hash.rs index de69c986f54a..05b69df8ae81 100644 --- a/encoding/src/hash.rs +++ b/encoding/src/hash.rs @@ -27,18 +27,17 @@ pub fn blake2b_variable(ingest: Vec, size: usize) -> Vec { /// use encoding::blake2b_256; /// /// let ingest: Vec = vec![]; -/// -/// let mut hash = [0u8; 32]; -/// blake2b_256(ingest, &mut hash); +/// let hash = blake2b_256(ingest); +/// assert_eq!(hash.len(), 32); /// ``` -pub fn blake2b_256(ingest: Vec, hash: &mut [u8; 32]) { - let hsh = Params::new() +pub fn blake2b_256(ingest: Vec) -> Vec { + let hash = Params::new() .hash_length(32) - .key(&ingest) .to_state() + .update(&ingest) .finalize(); - hash[..32].clone_from_slice(hsh.as_bytes()); + hash.as_bytes().to_vec() } #[cfg(test)] From 525242ddcbb94d9f21d0a19d80688afcfd347ddd Mon Sep 17 00:00:00 2001 From: austinabell Date: Fri, 29 Nov 2019 16:54:31 -0500 Subject: [PATCH 15/15] changed back to fixed array --- crypto/src/signature.rs | 4 +--- encoding/src/hash.rs | 8 +++++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/crypto/src/signature.rs b/crypto/src/signature.rs index 6a85c36a4fbc..775ee6d8f14d 100644 --- a/crypto/src/signature.rs +++ b/crypto/src/signature.rs @@ -48,13 +48,11 @@ fn verify_bls_sig(data: Vec, pub_k: Vec, sig: Signature) -> bool { fn verify_secp256k1_sig(data: Vec, addr: Address, sig: Signature) -> bool { // blake2b 256 hash let hash = blake2b_256(data); - let mut arr = [0u8; 32]; - arr.clone_from_slice(&hash[..32]); // Ecrecover with hash and signature let mut signature = [0u8; 65]; signature[..].clone_from_slice(sig.as_ref()); - let rec_addr = ecrecover(&arr, &signature); + let rec_addr = ecrecover(&hash, &signature); // check address against recovered address match rec_addr { diff --git a/encoding/src/hash.rs b/encoding/src/hash.rs index 05b69df8ae81..92c8cf3affe3 100644 --- a/encoding/src/hash.rs +++ b/encoding/src/hash.rs @@ -30,14 +30,16 @@ pub fn blake2b_variable(ingest: Vec, size: usize) -> Vec { /// let hash = blake2b_256(ingest); /// assert_eq!(hash.len(), 32); /// ``` -pub fn blake2b_256(ingest: Vec) -> Vec { - let hash = Params::new() +pub fn blake2b_256(ingest: Vec) -> [u8; 32] { + let digest = Params::new() .hash_length(32) .to_state() .update(&ingest) .finalize(); - hash.as_bytes().to_vec() + let mut ret = [0u8; 32]; + ret.clone_from_slice(digest.as_bytes()); + ret } #[cfg(test)]