From 894c8c1f76f2ac8631ae367f80ee2be70d38aba0 Mon Sep 17 00:00:00 2001 From: flodesi Date: Mon, 15 Jun 2020 15:28:40 -0400 Subject: [PATCH 1/5] changed BLS verification test to use wallet for signing --- crypto/src/signature.rs | 46 +++++++++++++--------------- key_management/src/wallet.rs | 18 +++++++++++ key_management/src/wallet_helpers.rs | 8 +++-- 3 files changed, 46 insertions(+), 26 deletions(-) diff --git a/crypto/src/signature.rs b/crypto/src/signature.rs index 18f7bb3471c8..c1312d028472 100644 --- a/crypto/src/signature.rs +++ b/crypto/src/signature.rs @@ -48,8 +48,8 @@ pub struct Signature { impl ser::Serialize for Signature { fn serialize(&self, serializer: S) -> Result - where - S: ser::Serializer, + where + S: ser::Serializer, { let mut bytes = self.bytes.clone(); // Insert signature type byte @@ -61,8 +61,8 @@ impl ser::Serialize for Signature { impl<'de> de::Deserialize<'de> for Signature { fn deserialize(deserializer: D) -> Result - where - D: de::Deserializer<'de>, + where + D: de::Deserializer<'de>, { let mut bytes: Vec = serde_bytes::Deserialize::deserialize(deserializer)?; if bytes.is_empty() { @@ -148,6 +148,8 @@ impl Signature { if &rec_addr == addr { Ok(()) } else { + println!("{:?}", rec_addr); + println!("{:?}", addr); Err("Secp signature verification failed".to_owned()) } } @@ -179,20 +181,16 @@ pub fn verify_bls_aggregate(data: &[&[u8]], pub_keys: &[&[u8]], aggregate_sig: & } // TODO: verify signature data format after signing implemented -fn ecrecover(hash: &[u8; 32], signature: &[u8; 65]) -> Result { - /* Recovery id is the last big-endian byte. */ - let v = (signature[64] as i8 - 27) as u8; - if v != 0 && v != 1 { - return Err(Error::InvalidRecovery("invalid recovery byte".to_owned())); - } +pub fn ecrecover(hash: &[u8; 32], signature: &[u8; 65]) -> Result { + // generate types to recover key from + let rec_id = RecoveryId::parse(signature[64])?; + // let rec_id = RecoveryId::parse(2)?; + let message = Message::parse(&hash); // Signature value without recovery byte let mut s = [0u8; 64]; - s[..64].clone_from_slice(signature.as_ref()); - - // generate types to recover key from - let message = Message::parse(&hash); - let rec_id = RecoveryId::parse(signature[64])?; + s.clone_from_slice(signature[..64].as_ref()); + // generate Signature let sig = EcsdaSignature::parse(&s); let key = recover(&message, &sig, &rec_id)?; @@ -294,19 +292,19 @@ pub mod json { } pub fn serialize(m: &Signature, serializer: S) -> Result - where - S: Serializer, + where + S: Serializer, { JsonHelper { sig_type: m.sig_type, bytes: base64::encode(&m.bytes), } - .serialize(serializer) + .serialize(serializer) } pub fn deserialize<'de, D>(deserializer: D) -> Result - where - D: Deserializer<'de>, + where + D: Deserializer<'de>, { let JsonHelper { sig_type, bytes } = Deserialize::deserialize(deserializer)?; Ok(Signature { @@ -320,8 +318,8 @@ pub mod json { use serde::{self, Deserialize, Deserializer, Serialize, Serializer}; pub fn serialize(v: &Option, serializer: S) -> Result - where - S: Serializer, + where + S: Serializer, { v.as_ref() .map(|s| SignatureJsonRef(s)) @@ -329,8 +327,8 @@ pub mod json { } pub fn deserialize<'de, D>(deserializer: D) -> Result, D::Error> - where - D: Deserializer<'de>, + where + D: Deserializer<'de>, { let s: Option = Deserialize::deserialize(deserializer)?; Ok(s.map(|v| v.0)) diff --git a/key_management/src/wallet.rs b/key_management/src/wallet.rs index d20c40a526d7..acd389cc6256 100644 --- a/key_management/src/wallet.rs +++ b/key_management/src/wallet.rs @@ -336,4 +336,22 @@ mod tests { // check to make sure that the test_addr is actually the default addr for the wallet assert_eq!(wallet.get_default().unwrap(), test_addr); } + + + #[test] + fn bls_verify_test() { + let key_vec = construct_priv_keys(); + let priv_key = key_vec[6].key_info.private_key().clone(); + let addr = key_vec[6].address.clone(); + let pub_key = key_vec[6].public_key.clone(); + let mut wallet = Wallet::new_from_keys(MemKeyStore::new(), key_vec); + + let msg: [u8; 32] = [ + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, + ]; + + let sig = wallet.sign(&addr, &msg).unwrap(); + let test = sig.verify(&msg, &addr).unwrap(); + } } diff --git a/key_management/src/wallet_helpers.rs b/key_management/src/wallet_helpers.rs index 73b3e2ac930a..5ff30cd58a5d 100644 --- a/key_management/src/wallet_helpers.rs +++ b/key_management/src/wallet_helpers.rs @@ -56,8 +56,12 @@ pub fn sign(sig_type: SignatureType, private_key: &[u8], msg: &[u8]) -> Result Date: Mon, 15 Jun 2020 16:09:07 -0400 Subject: [PATCH 2/5] first attempt at sig verification for secp --- crypto/src/signature.rs | 1 + key_management/src/wallet.rs | 15 +++++++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/crypto/src/signature.rs b/crypto/src/signature.rs index c1312d028472..8e5cce2705a2 100644 --- a/crypto/src/signature.rs +++ b/crypto/src/signature.rs @@ -194,6 +194,7 @@ pub fn ecrecover(hash: &[u8; 32], signature: &[u8; 65]) -> Result Date: Mon, 15 Jun 2020 18:37:02 -0400 Subject: [PATCH 3/5] fixed secp verification and ecrecover --- crypto/src/signature.rs | 58 +++++++--------------------- key_management/Cargo.toml | 3 +- key_management/src/wallet.rs | 31 +++++++-------- key_management/src/wallet_helpers.rs | 7 ++-- 4 files changed, 33 insertions(+), 66 deletions(-) diff --git a/crypto/src/signature.rs b/crypto/src/signature.rs index 8e5cce2705a2..7507a01b6622 100644 --- a/crypto/src/signature.rs +++ b/crypto/src/signature.rs @@ -48,8 +48,8 @@ pub struct Signature { impl ser::Serialize for Signature { fn serialize(&self, serializer: S) -> Result - where - S: ser::Serializer, + where + S: ser::Serializer, { let mut bytes = self.bytes.clone(); // Insert signature type byte @@ -61,8 +61,8 @@ impl ser::Serialize for Signature { impl<'de> de::Deserialize<'de> for Signature { fn deserialize(deserializer: D) -> Result - where - D: de::Deserializer<'de>, + where + D: de::Deserializer<'de>, { let mut bytes: Vec = serde_bytes::Deserialize::deserialize(deserializer)?; if bytes.is_empty() { @@ -148,8 +148,6 @@ impl Signature { if &rec_addr == addr { Ok(()) } else { - println!("{:?}", rec_addr); - println!("{:?}", addr); Err("Secp signature verification failed".to_owned()) } } @@ -180,11 +178,10 @@ pub fn verify_bls_aggregate(data: &[&[u8]], pub_keys: &[&[u8]], aggregate_sig: & verify(&sig, &hashed_data[..], &pks[..]) } -// TODO: verify signature data format after signing implemented +/// Return Address for a message given it's hash and signature pub fn ecrecover(hash: &[u8; 32], signature: &[u8; 65]) -> Result { // generate types to recover key from let rec_id = RecoveryId::parse(signature[64])?; - // let rec_id = RecoveryId::parse(2)?; let message = Message::parse(&hash); // Signature value without recovery byte @@ -194,7 +191,6 @@ pub fn ecrecover(hash: &[u8; 32], signature: &[u8; 65]) -> Result Result>(); - let signature = sk.sign(&msg); - - let signature_bytes = signature.as_bytes(); - assert_eq!(signature_bytes.len(), 96); - assert_eq!( - BlsSignature::from_bytes(&signature_bytes).unwrap(), - signature - ); - - let pk = sk.public_key(); - let addr = Address::new_bls(&pk.as_bytes()).unwrap(); - - Signature::new_bls(signature_bytes.clone()) - .verify(&msg, &addr) - .unwrap(); - Signature::new_bls(signature_bytes.clone()) - .verify_bls_sig(&msg, &addr) - .unwrap(); - } #[test] fn bls_agg_verify() { // The number of signatures in aggregate @@ -293,19 +263,19 @@ pub mod json { } pub fn serialize(m: &Signature, serializer: S) -> Result - where - S: Serializer, + where + S: Serializer, { JsonHelper { sig_type: m.sig_type, bytes: base64::encode(&m.bytes), } - .serialize(serializer) + .serialize(serializer) } pub fn deserialize<'de, D>(deserializer: D) -> Result - where - D: Deserializer<'de>, + where + D: Deserializer<'de>, { let JsonHelper { sig_type, bytes } = Deserialize::deserialize(deserializer)?; Ok(Signature { @@ -319,8 +289,8 @@ pub mod json { use serde::{self, Deserialize, Deserializer, Serialize, Serializer}; pub fn serialize(v: &Option, serializer: S) -> Result - where - S: Serializer, + where + S: Serializer, { v.as_ref() .map(|s| SignatureJsonRef(s)) @@ -328,8 +298,8 @@ pub mod json { } pub fn deserialize<'de, D>(deserializer: D) -> Result, D::Error> - where - D: Deserializer<'de>, + where + D: Deserializer<'de>, { let s: Option = Deserialize::deserialize(deserializer)?; Ok(s.map(|v| v.0)) diff --git a/key_management/Cargo.toml b/key_management/Cargo.toml index 14fd74f1890e..945386b917b8 100644 --- a/key_management/Cargo.toml +++ b/key_management/Cargo.toml @@ -10,4 +10,5 @@ address = { package = "forest_address", path = "../vm/address", version = "0.2" crypto = { package = "forest_crypto", path = "../crypto" } bls-signatures = "0.6.0" libsecp256k1 = "0.3.4" -rand = "0.7.3" \ No newline at end of file +rand = "0.7.3" +encoding = { package = "forest_encoding", path = "../encoding", version = "0.1" } \ No newline at end of file diff --git a/key_management/src/wallet.rs b/key_management/src/wallet.rs index 258fa8c7cd03..8ba11f51fa89 100644 --- a/key_management/src/wallet.rs +++ b/key_management/src/wallet.rs @@ -164,6 +164,7 @@ fn generate_key(typ: SignatureType) -> Result { mod tests { use super::*; use crate::{generate, MemKeyStore}; + use encoding::blake2b_256; use secp256k1::{Message as SecpMessage, SecretKey as SecpPrivate}; fn construct_priv_keys() -> Vec { @@ -224,17 +225,18 @@ mod tests { let priv_key_bytes = key_vec[2].key_info.private_key().clone(); let addr = key_vec[2].address.clone(); let mut wallet = Wallet::new_from_keys(MemKeyStore::new(), key_vec); - let msg: [u8; 32] = [ - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, - ]; + let msg = [0u8; 64]; let msg_sig = wallet.sign(&addr, &msg).unwrap(); + let msg_complete = blake2b_256(&msg); + let message = SecpMessage::parse(&msg_complete); let priv_key = SecpPrivate::parse_slice(&priv_key_bytes).unwrap(); - let message = SecpMessage::parse_slice(&msg).unwrap(); - let (sig, _) = secp256k1::sign(&message, &priv_key); - let actual = Signature::new_secp256k1(sig.serialize().to_vec()); + let (sig, recovery_id) = secp256k1::sign(&message, &priv_key); + let mut new_bytes = [0; 65]; + new_bytes[..64].copy_from_slice(&sig.serialize()); + new_bytes[64] = recovery_id.serialize(); + let actual = Signature::new_secp256k1(new_bytes.to_vec()); assert_eq!(msg_sig, actual) } @@ -343,12 +345,10 @@ mod tests { let addr = key_vec[0].address.clone(); let mut wallet = Wallet::new_from_keys(MemKeyStore::new(), key_vec); - let msg: [u8; 32] = [ - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, - ]; + let msg = [0u8; 64]; + let sig = wallet.sign(&addr, &msg).unwrap(); - let test = sig.verify(&msg, &addr).unwrap(); + sig.verify(&msg, &addr).unwrap(); } #[test] @@ -357,12 +357,9 @@ mod tests { let addr = key_vec[6].address.clone(); let mut wallet = Wallet::new_from_keys(MemKeyStore::new(), key_vec); - let msg: [u8; 32] = [ - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, - ]; + let msg = [0u8; 64]; let sig = wallet.sign(&addr, &msg).unwrap(); - let test = sig.verify(&msg, &addr).unwrap(); + sig.verify(&msg, &addr).unwrap(); } } diff --git a/key_management/src/wallet_helpers.rs b/key_management/src/wallet_helpers.rs index 5ff30cd58a5d..95de0f9f973a 100644 --- a/key_management/src/wallet_helpers.rs +++ b/key_management/src/wallet_helpers.rs @@ -5,6 +5,7 @@ use super::errors::Error; use address::Address; use bls_signatures::{PrivateKey as BlsPrivate, Serialize}; use crypto::{Signature, SignatureType}; +use encoding::blake2b_256; use rand::rngs::OsRng; use secp256k1::{Message as SecpMessage, PublicKey as SecpPublic, SecretKey as SecpPrivate}; @@ -53,14 +54,12 @@ pub fn sign(sig_type: SignatureType, private_key: &[u8], msg: &[u8]) -> Result { let priv_key = SecpPrivate::parse_slice(private_key) .map_err(|err| Error::Other(err.to_string()))?; - let message = - SecpMessage::parse_slice(msg).map_err(|err| Error::Other(err.to_string()))?; - // this returns a signature of secp256k1 type, next lines convert this sig to crypto signature type + let msg_complete = blake2b_256(msg); + let message = SecpMessage::parse(&msg_complete); let (sig, recovery_id) = secp256k1::sign(&message, &priv_key); let mut new_bytes = [0; 65]; new_bytes[..64].copy_from_slice(&sig.serialize()); new_bytes[64] = recovery_id.serialize(); - println!("{}", recovery_id.serialize()); let crypto_sig = Signature::new_secp256k1(new_bytes.to_vec()); Ok(crypto_sig) } From 4a3d9ab03ae04905ffcd4f923aad3a3cadb1b1c8 Mon Sep 17 00:00:00 2001 From: flodesi Date: Tue, 16 Jun 2020 10:31:36 -0400 Subject: [PATCH 4/5] updated verify tests and refactor --- crypto/src/signature.rs | 4 +- key_management/src/wallet.rs | 69 ++++++++++++++++---- key_management/src/wallet_helpers.rs | 8 +-- vm/interpreter/src/gas_tracker/price_list.rs | 2 +- 4 files changed, 62 insertions(+), 21 deletions(-) diff --git a/crypto/src/signature.rs b/crypto/src/signature.rs index 7507a01b6622..62090d3afaa7 100644 --- a/crypto/src/signature.rs +++ b/crypto/src/signature.rs @@ -21,7 +21,7 @@ pub const BLS_PUB_LEN: usize = 48; #[derive(Clone, Debug, PartialEq, FromPrimitive, Copy, Eq, Serialize_repr, Deserialize_repr)] #[repr(u8)] pub enum SignatureType { - Secp256 = 1, + Secp256k1 = 1, BLS = 2, } @@ -81,7 +81,7 @@ impl Signature { /// Creates a SECP Signature given the raw bytes pub fn new_secp256k1(bytes: Vec) -> Self { Self { - sig_type: SignatureType::Secp256, + sig_type: SignatureType::Secp256k1, bytes, } } diff --git a/key_management/src/wallet.rs b/key_management/src/wallet.rs index 8ba11f51fa89..13c648ebcade 100644 --- a/key_management/src/wallet.rs +++ b/key_management/src/wallet.rs @@ -171,8 +171,8 @@ mod tests { let mut secp_keys = Vec::new(); let mut bls_keys = Vec::new(); for _ in 1..5 { - let secp_priv_key = generate(SignatureType::Secp256).unwrap(); - let secp_key_info = KeyInfo::new(SignatureType::Secp256, secp_priv_key); + let secp_priv_key = generate(SignatureType::Secp256k1).unwrap(); + let secp_key_info = KeyInfo::new(SignatureType::Secp256k1, secp_priv_key); let secp_key = Key::try_from(secp_key_info).unwrap(); secp_keys.push(secp_key); @@ -250,15 +250,15 @@ mod tests { // test to see if export returns the correct key_info assert_eq!(key_info, key.key_info.clone()); - let new_priv_key = generate(SignatureType::Secp256).unwrap(); + let new_priv_key = generate(SignatureType::Secp256k1).unwrap(); let pub_key = - wallet_helpers::to_public(SignatureType::Secp256, new_priv_key.as_slice()).unwrap(); + wallet_helpers::to_public(SignatureType::Secp256k1, new_priv_key.as_slice()).unwrap(); let test_addr = Address::new_secp256k1(pub_key.as_slice()).unwrap(); let key_info_err = wallet.export(&test_addr).unwrap_err(); // test to make sure that an error is raised when an incorrect address is added assert_eq!(key_info_err, Error::KeyInfo); - let test_key_info = KeyInfo::new(SignatureType::Secp256, new_priv_key); + let test_key_info = KeyInfo::new(SignatureType::Secp256k1, new_priv_key); // make sure that key_info has been imported to wallet assert!(wallet.import(test_key_info.clone()).is_ok()); @@ -322,12 +322,12 @@ mod tests { // check to make sure that there is no default assert_eq!(wallet.get_default().unwrap_err(), Error::KeyInfo); - let new_priv_key = generate(SignatureType::Secp256).unwrap(); + let new_priv_key = generate(SignatureType::Secp256k1).unwrap(); let pub_key = - wallet_helpers::to_public(SignatureType::Secp256, new_priv_key.as_slice()).unwrap(); + wallet_helpers::to_public(SignatureType::Secp256k1, new_priv_key.as_slice()).unwrap(); let test_addr = Address::new_secp256k1(pub_key.as_slice()).unwrap(); - let key_info = KeyInfo::new(SignatureType::Secp256, new_priv_key); + let key_info = KeyInfo::new(SignatureType::Secp256k1, new_priv_key); let test_addr_string = format!("wallet-{}", test_addr.to_string()); wallet.keystore.put(test_addr_string, key_info).unwrap(); @@ -341,9 +341,11 @@ mod tests { #[test] fn secp_verify() { - let key_vec = construct_priv_keys(); - let addr = key_vec[0].address.clone(); - let mut wallet = Wallet::new_from_keys(MemKeyStore::new(), key_vec); + let secp_priv_key = generate(SignatureType::Secp256k1).unwrap(); + let secp_key_info = KeyInfo::new(SignatureType::Secp256k1, secp_priv_key); + let secp_key = Key::try_from(secp_key_info).unwrap(); + let addr = secp_key.address.clone(); + let mut wallet = Wallet::new_from_keys(MemKeyStore::new(), vec![secp_key]); let msg = [0u8; 64]; @@ -353,13 +355,52 @@ mod tests { #[test] fn bls_verify_test() { - let key_vec = construct_priv_keys(); - let addr = key_vec[6].address.clone(); - let mut wallet = Wallet::new_from_keys(MemKeyStore::new(), key_vec); + let bls_priv_key = generate(SignatureType::BLS).unwrap(); + let bls_key_info = KeyInfo::new(SignatureType::BLS, bls_priv_key); + let bls_key = Key::try_from(bls_key_info).unwrap(); + let addr = bls_key.address.clone(); + let mut wallet = Wallet::new_from_keys(MemKeyStore::new(), vec![bls_key]); let msg = [0u8; 64]; let sig = wallet.sign(&addr, &msg).unwrap(); sig.verify(&msg, &addr).unwrap(); } + + #[test] + fn invalid_verify() { + // construct secp address to sign with + let secp_priv_key = generate(SignatureType::Secp256k1).unwrap(); + let secp_key_info = KeyInfo::new(SignatureType::Secp256k1, secp_priv_key); + let secp_key = Key::try_from(secp_key_info).unwrap(); + let secp_addr = secp_key.address.clone(); + + // construct random secp address to use in invalid verification + let invalid_secp_priv = generate(SignatureType::Secp256k1).unwrap(); + let invalid_secp_key_info = KeyInfo::new(SignatureType::Secp256k1, invalid_secp_priv); + let invalid_secp_key = Key::try_from(invalid_secp_key_info).unwrap(); + let invalid_secp_addr = invalid_secp_key.address; + + // construct bls address to sign message with + let bls_priv_key = generate(SignatureType::BLS).unwrap(); + let bls_key_info = KeyInfo::new(SignatureType::BLS, bls_priv_key); + let bls_key = Key::try_from(bls_key_info).unwrap(); + let bls_addr = bls_key.address.clone(); + + // construct random bls address to use in invalid verification + let invalid_bls_priv = generate(SignatureType::BLS).unwrap(); + let invalid_bls_key_info = KeyInfo::new(SignatureType::BLS, invalid_bls_priv); + let invalid_bls_key = Key::try_from(invalid_bls_key_info).unwrap(); + let invalid_bls_addr = invalid_bls_key.address; + + // construct wallet and sign 2 messages one with secp addr and one iwth bls addr + let mut wallet = Wallet::new_from_keys(MemKeyStore::new(), vec![secp_key, bls_key]); + let msg = [0u8; 64]; + let secp_sig = wallet.sign(&secp_addr, &msg).unwrap(); + let bls_sig = wallet.sign(&bls_addr, &msg).unwrap(); + + // assert verification failure with invalid address + assert!(secp_sig.verify(&msg, &invalid_secp_addr).is_err()); + assert!(bls_sig.verify(&msg, &invalid_bls_addr).is_err()); + } } diff --git a/key_management/src/wallet_helpers.rs b/key_management/src/wallet_helpers.rs index 95de0f9f973a..1d9c86fd4c2b 100644 --- a/key_management/src/wallet_helpers.rs +++ b/key_management/src/wallet_helpers.rs @@ -16,7 +16,7 @@ pub fn to_public(sig_type: SignatureType, private_key: &[u8]) -> Result, .map_err(|err| Error::Other(err.to_string()))? .public_key() .as_bytes()), - SignatureType::Secp256 => { + SignatureType::Secp256k1 => { let private_key = SecpPrivate::parse_slice(private_key) .map_err(|err| Error::Other(err.to_string()))?; let public_key = SecpPublic::from_secret_key(&private_key); @@ -32,7 +32,7 @@ pub fn new_address(sig_type: SignatureType, public_key: &[u8]) -> Result
{ + SignatureType::Secp256k1 => { let addr = Address::new_secp256k1(public_key).map_err(|err| Error::Other(err.to_string()))?; Ok(addr) @@ -51,7 +51,7 @@ pub fn sign(sig_type: SignatureType, private_key: &[u8], msg: &[u8]) -> Result { + SignatureType::Secp256k1 => { let priv_key = SecpPrivate::parse_slice(private_key) .map_err(|err| Error::Other(err.to_string()))?; let msg_complete = blake2b_256(msg); @@ -74,7 +74,7 @@ pub fn generate(sig_type: SignatureType) -> Result, Error> { let key = BlsPrivate::generate(rng); Ok(key.as_bytes()) } - SignatureType::Secp256 => { + SignatureType::Secp256k1 => { let key = SecpPrivate::random(rng); Ok(key.serialize().to_vec()) } diff --git a/vm/interpreter/src/gas_tracker/price_list.rs b/vm/interpreter/src/gas_tracker/price_list.rs index 44d8ec260883..8c7340552f8b 100644 --- a/vm/interpreter/src/gas_tracker/price_list.rs +++ b/vm/interpreter/src/gas_tracker/price_list.rs @@ -122,7 +122,7 @@ impl PriceList { pub fn on_verify_signature(&self, sig_type: SignatureType, plain_text_size: usize) -> i64 { match sig_type { SignatureType::BLS => (3 * plain_text_size + 2) as i64, - SignatureType::Secp256 => (3 * plain_text_size + 2) as i64, + SignatureType::Secp256k1 => (3 * plain_text_size + 2) as i64, } } /// Returns gas required for hashing data From a32543a0e06ad331da77220dee9a13a9b295dc0d Mon Sep 17 00:00:00 2001 From: flodesi Date: Tue, 16 Jun 2020 11:02:09 -0400 Subject: [PATCH 5/5] fixed test --- key_management/src/wallet.rs | 49 ++++++++---------------------------- 1 file changed, 10 insertions(+), 39 deletions(-) diff --git a/key_management/src/wallet.rs b/key_management/src/wallet.rs index 13c648ebcade..be3559fc8597 100644 --- a/key_management/src/wallet.rs +++ b/key_management/src/wallet.rs @@ -131,8 +131,8 @@ where Ok(()) } - /// Generate a new Key that fits the requirement of the given SignatureType - pub fn generate_key(&mut self, typ: SignatureType) -> Result { + /// Generate a new Address that fits the requirement of the given SignatureType + pub fn generate_addr(&mut self, typ: SignatureType) -> Result { let key = generate_key(typ)?; let addr = format!("wallet-{}", key.address.to_string()); self.keystore.put(addr, key.key_info.clone())?; @@ -301,7 +301,7 @@ mod tests { #[test] fn generate_new_key() { let mut wallet = generate_wallet(); - let addr = wallet.generate_key(SignatureType::BLS).unwrap(); + let addr = wallet.generate_addr(SignatureType::BLS).unwrap(); let key = wallet.keystore.get("default").unwrap(); // make sure that the newly generated key is the default key - checking by key type assert_eq!(&SignatureType::BLS, key.key_type()); @@ -351,6 +351,10 @@ mod tests { let sig = wallet.sign(&addr, &msg).unwrap(); sig.verify(&msg, &addr).unwrap(); + + // invalid verify check + let invalid_addr = wallet.generate_addr(SignatureType::Secp256k1).unwrap(); + assert!(sig.verify(&msg, &invalid_addr).is_err()) } #[test] @@ -365,42 +369,9 @@ mod tests { let sig = wallet.sign(&addr, &msg).unwrap(); sig.verify(&msg, &addr).unwrap(); - } - - #[test] - fn invalid_verify() { - // construct secp address to sign with - let secp_priv_key = generate(SignatureType::Secp256k1).unwrap(); - let secp_key_info = KeyInfo::new(SignatureType::Secp256k1, secp_priv_key); - let secp_key = Key::try_from(secp_key_info).unwrap(); - let secp_addr = secp_key.address.clone(); - - // construct random secp address to use in invalid verification - let invalid_secp_priv = generate(SignatureType::Secp256k1).unwrap(); - let invalid_secp_key_info = KeyInfo::new(SignatureType::Secp256k1, invalid_secp_priv); - let invalid_secp_key = Key::try_from(invalid_secp_key_info).unwrap(); - let invalid_secp_addr = invalid_secp_key.address; - - // construct bls address to sign message with - let bls_priv_key = generate(SignatureType::BLS).unwrap(); - let bls_key_info = KeyInfo::new(SignatureType::BLS, bls_priv_key); - let bls_key = Key::try_from(bls_key_info).unwrap(); - let bls_addr = bls_key.address.clone(); - - // construct random bls address to use in invalid verification - let invalid_bls_priv = generate(SignatureType::BLS).unwrap(); - let invalid_bls_key_info = KeyInfo::new(SignatureType::BLS, invalid_bls_priv); - let invalid_bls_key = Key::try_from(invalid_bls_key_info).unwrap(); - let invalid_bls_addr = invalid_bls_key.address; - - // construct wallet and sign 2 messages one with secp addr and one iwth bls addr - let mut wallet = Wallet::new_from_keys(MemKeyStore::new(), vec![secp_key, bls_key]); - let msg = [0u8; 64]; - let secp_sig = wallet.sign(&secp_addr, &msg).unwrap(); - let bls_sig = wallet.sign(&bls_addr, &msg).unwrap(); - // assert verification failure with invalid address - assert!(secp_sig.verify(&msg, &invalid_secp_addr).is_err()); - assert!(bls_sig.verify(&msg, &invalid_bls_addr).is_err()); + // invalid verify check + let invalid_addr = wallet.generate_addr(SignatureType::BLS).unwrap(); + assert!(sig.verify(&msg, &invalid_addr).is_err()) } }