From ff5f1fdcde6845ed632782bc5d673b01149c4e93 Mon Sep 17 00:00:00 2001 From: flodesi Date: Wed, 15 Jul 2020 17:57:36 -0400 Subject: [PATCH 1/8] add message signing test --- Cargo.lock | 1 + tests/serialization_tests/Cargo.toml | 1 + .../serialization_tests/tests/signing_ser.rs | 42 +++++++++++++++++++ 3 files changed, 44 insertions(+) create mode 100644 tests/serialization_tests/tests/signing_ser.rs diff --git a/Cargo.lock b/Cargo.lock index 464194c69f34..f53005b5bb43 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5647,6 +5647,7 @@ name = "serialization_tests" version = "0.1.0" dependencies = [ "base64 0.12.3", + "bls-signatures", "fil_types", "forest_address", "forest_blocks", diff --git a/tests/serialization_tests/Cargo.toml b/tests/serialization_tests/Cargo.toml index 09ce79f41c1b..50abaedc6101 100644 --- a/tests/serialization_tests/Cargo.toml +++ b/tests/serialization_tests/Cargo.toml @@ -23,3 +23,4 @@ forest_message = { path = "../../vm/message", features = ["json"] } encoding = { package = "forest_encoding", path = "../../encoding" } forest_blocks = { path = "../../blockchain/blocks", features = ["json"] } num-traits = "0.2" +bls-signatures = "0.6.0" \ No newline at end of file diff --git a/tests/serialization_tests/tests/signing_ser.rs b/tests/serialization_tests/tests/signing_ser.rs new file mode 100644 index 000000000000..cdef4293ad3d --- /dev/null +++ b/tests/serialization_tests/tests/signing_ser.rs @@ -0,0 +1,42 @@ +// Copyright 2020 ChainSafe Systems +// SPDX-License-Identifier: Apache-2.0, MIT + +// Doesn't run these unless feature specified +#![cfg(feature = "submodule_tests")] +#![allow(dead_code)] + +use bls_signatures::{PrivateKey, Serialize}; +use crypto::{signature, Signature}; +use encoding::Cbor; +use forest_message::{unsigned_message, UnsignedMessage}; +use serde::Deserialize; +use std::fs::File; +use std::io::prelude::*; + +#[derive(Deserialize)] +struct TestVec { + #[serde(with = "unsigned_message::json")] + unsigned: UnsignedMessage, + cid: String, + cid_hex_bytes: String, + private_key: String, + #[serde(with = "signature::json")] + signature: Signature, +} + +#[test] +fn signing_test() { + let mut file = File::open("../serialization-vectors/message_signing.json").unwrap(); + let mut string = String::new(); + file.read_to_string(&mut string).unwrap(); + + let vectors: Vec = + serde_json::from_str(&string).expect("Test vector deserialization failed"); + for test_vec in vectors { + let test = base64::decode(test_vec.private_key).unwrap(); + let priv_key = PrivateKey::from_bytes(&test).unwrap(); + let cid = test_vec.unsigned.cid().unwrap(); + let sig = priv_key.sign(cid.to_bytes().as_slice()); + assert_eq!(sig.as_bytes().as_slice(), test_vec.signature.bytes()); + } +} From 14b3dcfc2624420fb0cdb217028f3905fed1508b Mon Sep 17 00:00:00 2001 From: flodesi Date: Thu, 16 Jul 2020 14:07:12 -0400 Subject: [PATCH 2/8] fixed serialization of unsigned messages --- .../serialization_tests/tests/signing_ser.rs | 18 ++++++------- vm/message/src/unsigned_message.rs | 26 +++++++++++++++++++ 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/tests/serialization_tests/tests/signing_ser.rs b/tests/serialization_tests/tests/signing_ser.rs index cdef4293ad3d..f49b575583b0 100644 --- a/tests/serialization_tests/tests/signing_ser.rs +++ b/tests/serialization_tests/tests/signing_ser.rs @@ -3,7 +3,7 @@ // Doesn't run these unless feature specified #![cfg(feature = "submodule_tests")] -#![allow(dead_code)] +#![allow(dead_code, non_snake_case)] use bls_signatures::{PrivateKey, Serialize}; use crypto::{signature, Signature}; @@ -16,12 +16,12 @@ use std::io::prelude::*; #[derive(Deserialize)] struct TestVec { #[serde(with = "unsigned_message::json")] - unsigned: UnsignedMessage, - cid: String, - cid_hex_bytes: String, - private_key: String, + Unsigned: UnsignedMessage, + Cid: String, + CidHexBytes: String, + PrivateKey: String, #[serde(with = "signature::json")] - signature: Signature, + Signature: Signature, } #[test] @@ -33,10 +33,10 @@ fn signing_test() { let vectors: Vec = serde_json::from_str(&string).expect("Test vector deserialization failed"); for test_vec in vectors { - let test = base64::decode(test_vec.private_key).unwrap(); + let test = base64::decode(test_vec.PrivateKey).unwrap(); let priv_key = PrivateKey::from_bytes(&test).unwrap(); - let cid = test_vec.unsigned.cid().unwrap(); + let cid = test_vec.Unsigned.cid().unwrap(); let sig = priv_key.sign(cid.to_bytes().as_slice()); - assert_eq!(sig.as_bytes().as_slice(), test_vec.signature.bytes()); + assert_eq!(sig.as_bytes().as_slice(), test_vec.Signature.bytes()); } } diff --git a/vm/message/src/unsigned_message.rs b/vm/message/src/unsigned_message.rs index 031987c5c1a7..06af4bf5e072 100644 --- a/vm/message/src/unsigned_message.rs +++ b/vm/message/src/unsigned_message.rs @@ -149,6 +149,31 @@ impl Message for UnsignedMessage { impl Cbor for UnsignedMessage {} +mod params_serde { + use super::*; + use serde::Serializer; + + pub fn serialize(param: &str, s: S) -> Result + where + S: Serializer + { + s.serialize_str(param) + } + + pub fn deserialize<'de, D>(deserializer: D) -> Result + where + D: Deserializer<'de>, + { + let s: Option = Option::deserialize(deserializer)?; + if let Some(s) = s { + return Ok( + s + ); + } + Ok("".to_string()) + } +} + #[cfg(feature = "json")] pub mod json { use super::*; @@ -183,6 +208,7 @@ pub mod json { gas_limit: u64, #[serde(rename = "Method")] method_num: u64, + #[serde(with = "params_serde")] params: String, } From 68c0201d877b6d56b3dadc2d0c52d1385ba18e62 Mon Sep 17 00:00:00 2001 From: flodesi Date: Thu, 16 Jul 2020 14:17:01 -0400 Subject: [PATCH 3/8] fmt --- vm/message/src/unsigned_message.rs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/vm/message/src/unsigned_message.rs b/vm/message/src/unsigned_message.rs index 06af4bf5e072..b09797a0f42c 100644 --- a/vm/message/src/unsigned_message.rs +++ b/vm/message/src/unsigned_message.rs @@ -155,20 +155,18 @@ mod params_serde { pub fn serialize(param: &str, s: S) -> Result where - S: Serializer + S: Serializer, { s.serialize_str(param) } pub fn deserialize<'de, D>(deserializer: D) -> Result - where - D: Deserializer<'de>, + where + D: Deserializer<'de>, { let s: Option = Option::deserialize(deserializer)?; if let Some(s) = s { - return Ok( - s - ); + return Ok(s); } Ok("".to_string()) } From f52d84d62b6d18cb99c395b1af8162397c046fe9 Mon Sep 17 00:00:00 2001 From: flodesi Date: Thu, 16 Jul 2020 16:02:58 -0400 Subject: [PATCH 4/8] applied fixes --- .../serialization_tests/tests/signing_ser.rs | 1 + vm/message/src/unsigned_message.rs | 33 ++++--------------- 2 files changed, 7 insertions(+), 27 deletions(-) diff --git a/tests/serialization_tests/tests/signing_ser.rs b/tests/serialization_tests/tests/signing_ser.rs index f49b575583b0..9f7d72b48683 100644 --- a/tests/serialization_tests/tests/signing_ser.rs +++ b/tests/serialization_tests/tests/signing_ser.rs @@ -34,6 +34,7 @@ fn signing_test() { serde_json::from_str(&string).expect("Test vector deserialization failed"); for test_vec in vectors { let test = base64::decode(test_vec.PrivateKey).unwrap(); + // TODO set up a private key based on sig type let priv_key = PrivateKey::from_bytes(&test).unwrap(); let cid = test_vec.Unsigned.cid().unwrap(); let sig = priv_key.sign(cid.to_bytes().as_slice()); diff --git a/vm/message/src/unsigned_message.rs b/vm/message/src/unsigned_message.rs index b09797a0f42c..68209f451b15 100644 --- a/vm/message/src/unsigned_message.rs +++ b/vm/message/src/unsigned_message.rs @@ -149,29 +149,6 @@ impl Message for UnsignedMessage { impl Cbor for UnsignedMessage {} -mod params_serde { - use super::*; - use serde::Serializer; - - pub fn serialize(param: &str, s: S) -> Result - where - S: Serializer, - { - s.serialize_str(param) - } - - pub fn deserialize<'de, D>(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - let s: Option = Option::deserialize(deserializer)?; - if let Some(s) = s { - return Ok(s); - } - Ok("".to_string()) - } -} - #[cfg(feature = "json")] pub mod json { use super::*; @@ -206,8 +183,7 @@ pub mod json { gas_limit: u64, #[serde(rename = "Method")] method_num: u64, - #[serde(with = "params_serde")] - params: String, + params: Option, } pub fn serialize(m: &UnsignedMessage, serializer: S) -> Result @@ -223,7 +199,7 @@ pub mod json { gas_price: m.gas_price.to_string(), gas_limit: m.gas_limit, method_num: m.method_num, - params: base64::encode(m.params.bytes()), + params: Some(base64::encode(m.params.bytes())), } .serialize(serializer) } @@ -242,7 +218,10 @@ pub mod json { gas_price: m.gas_price.parse().map_err(de::Error::custom)?, gas_limit: m.gas_limit, method_num: m.method_num, - params: Serialized::new(base64::decode(&m.params).map_err(de::Error::custom)?), + params: Serialized::new( + base64::decode(&m.params.unwrap_or_else(|| "".to_string())) + .map_err(de::Error::custom)?, + ), }) } From f20b9b659f97f2aa1e9ce2001f82c496cd88e455 Mon Sep 17 00:00:00 2001 From: flodesi Date: Thu, 16 Jul 2020 22:43:36 -0400 Subject: [PATCH 5/8] add cid check --- .../serialization_tests/tests/signing_ser.rs | 28 ++++++++++++------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/tests/serialization_tests/tests/signing_ser.rs b/tests/serialization_tests/tests/signing_ser.rs index 9f7d72b48683..eedebb7f7c78 100644 --- a/tests/serialization_tests/tests/signing_ser.rs +++ b/tests/serialization_tests/tests/signing_ser.rs @@ -3,25 +3,27 @@ // Doesn't run these unless feature specified #![cfg(feature = "submodule_tests")] -#![allow(dead_code, non_snake_case)] +#![allow(dead_code)] use bls_signatures::{PrivateKey, Serialize}; +use cid::Cid; use crypto::{signature, Signature}; use encoding::Cbor; -use forest_message::{unsigned_message, UnsignedMessage}; +use forest_message::{unsigned_message, SignedMessage, UnsignedMessage}; use serde::Deserialize; use std::fs::File; use std::io::prelude::*; +use std::str::FromStr; #[derive(Deserialize)] +#[serde(rename_all = "PascalCase")] struct TestVec { #[serde(with = "unsigned_message::json")] - Unsigned: UnsignedMessage, - Cid: String, - CidHexBytes: String, - PrivateKey: String, + unsigned: UnsignedMessage, + cid: String, + private_key: String, #[serde(with = "signature::json")] - Signature: Signature, + signature: Signature, } #[test] @@ -33,11 +35,17 @@ fn signing_test() { let vectors: Vec = serde_json::from_str(&string).expect("Test vector deserialization failed"); for test_vec in vectors { - let test = base64::decode(test_vec.PrivateKey).unwrap(); + let test = base64::decode(test_vec.private_key).unwrap(); // TODO set up a private key based on sig type let priv_key = PrivateKey::from_bytes(&test).unwrap(); - let cid = test_vec.Unsigned.cid().unwrap(); + let cid = test_vec.unsigned.cid().unwrap(); let sig = priv_key.sign(cid.to_bytes().as_slice()); - assert_eq!(sig.as_bytes().as_slice(), test_vec.Signature.bytes()); + let msg_sig = Signature::new_bls(sig.as_bytes()); + let signed_message = SignedMessage::new_from_parts(test_vec.unsigned, msg_sig); + let cid = signed_message.cid().unwrap(); + let cid_test = Cid::from_str(&test_vec.cid).unwrap(); + + assert_eq!(sig.as_bytes().as_slice(), test_vec.signature.bytes()); + assert_eq!(cid, cid_test); } } From 6203a867d73a60a639515effdd5fdfff8213a805 Mon Sep 17 00:00:00 2001 From: flodesi Date: Thu, 16 Jul 2020 22:44:34 -0400 Subject: [PATCH 6/8] remove unneeded allow --- tests/serialization_tests/tests/signing_ser.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/serialization_tests/tests/signing_ser.rs b/tests/serialization_tests/tests/signing_ser.rs index eedebb7f7c78..2d376a431bb9 100644 --- a/tests/serialization_tests/tests/signing_ser.rs +++ b/tests/serialization_tests/tests/signing_ser.rs @@ -3,7 +3,6 @@ // Doesn't run these unless feature specified #![cfg(feature = "submodule_tests")] -#![allow(dead_code)] use bls_signatures::{PrivateKey, Serialize}; use cid::Cid; From 21963ed8e1734ee597ecf6fd9e9e24f5faf82f5b Mon Sep 17 00:00:00 2001 From: flodesi Date: Fri, 17 Jul 2020 12:56:38 -0400 Subject: [PATCH 7/8] fixed signed message creation --- tests/serialization_tests/tests/signing_ser.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/serialization_tests/tests/signing_ser.rs b/tests/serialization_tests/tests/signing_ser.rs index 2d376a431bb9..d71fce499fc0 100644 --- a/tests/serialization_tests/tests/signing_ser.rs +++ b/tests/serialization_tests/tests/signing_ser.rs @@ -40,7 +40,7 @@ fn signing_test() { let cid = test_vec.unsigned.cid().unwrap(); let sig = priv_key.sign(cid.to_bytes().as_slice()); let msg_sig = Signature::new_bls(sig.as_bytes()); - let signed_message = SignedMessage::new_from_parts(test_vec.unsigned, msg_sig); + let signed_message = SignedMessage::new_from_parts(test_vec.unsigned, msg_sig).unwrap(); let cid = signed_message.cid().unwrap(); let cid_test = Cid::from_str(&test_vec.cid).unwrap(); From c52a479e46828ec7afbe65b8159e97d3182ad75b Mon Sep 17 00:00:00 2001 From: flodesi Date: Sat, 18 Jul 2020 18:38:11 -0400 Subject: [PATCH 8/8] fixed merge conflict --- tests/serialization_tests/tests/signing_ser.rs | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/tests/serialization_tests/tests/signing_ser.rs b/tests/serialization_tests/tests/signing_ser.rs index d71fce499fc0..c5cb11cfbbc4 100644 --- a/tests/serialization_tests/tests/signing_ser.rs +++ b/tests/serialization_tests/tests/signing_ser.rs @@ -37,14 +37,19 @@ fn signing_test() { let test = base64::decode(test_vec.private_key).unwrap(); // TODO set up a private key based on sig type let priv_key = PrivateKey::from_bytes(&test).unwrap(); - let cid = test_vec.unsigned.cid().unwrap(); - let sig = priv_key.sign(cid.to_bytes().as_slice()); - let msg_sig = Signature::new_bls(sig.as_bytes()); - let signed_message = SignedMessage::new_from_parts(test_vec.unsigned, msg_sig).unwrap(); - let cid = signed_message.cid().unwrap(); + let msg_cid = test_vec.unsigned.cid().unwrap(); + let cid_sig = priv_key.sign(msg_cid.to_bytes().as_slice()); + + let msg_cbor = Cbor::marshal_cbor(&test_vec.unsigned).unwrap(); + let priv_key = PrivateKey::from_bytes(&test).unwrap(); + let sig = priv_key.sign(msg_cbor); + let crypto_sig = Signature::new_bls(sig.as_bytes()); + let smsg = SignedMessage::new_from_parts(test_vec.unsigned, crypto_sig).unwrap(); + let cid = smsg.cid().unwrap(); + let cid_test = Cid::from_str(&test_vec.cid).unwrap(); - assert_eq!(sig.as_bytes().as_slice(), test_vec.signature.bytes()); + assert_eq!(cid_sig.as_bytes().as_slice(), test_vec.signature.bytes()); assert_eq!(cid, cid_test); } }