Skip to content

Commit

Permalink
Signature cleanup (openethereum#1921)
Browse files Browse the repository at this point in the history
* Address renamed to H160 at bigint library level

* moved uint specific test from util to bigint library

* naming

* unifing hashes in progress

* unifing hashes

* cleanup redundant unwraps in tests

* Removing util/crypto in progress.

* fixed compiling

* signature cleanup in progress

* new module - ethcrypto used by ethstore and ethcore-network

* fixed compiling

* fixed compiling

* fixed merge
  • Loading branch information
debris authored and gavofyork committed Aug 24, 2016
1 parent f07a1e6 commit b0d462c
Show file tree
Hide file tree
Showing 39 changed files with 444 additions and 808 deletions.
16 changes: 16 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions ethcore/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ ethcore-devtools = { path = "../devtools" }
ethjson = { path = "../json" }
ethcore-ipc = { path = "../ipc/rpc" }
ethstore = { path = "../ethstore" }
ethkey = { path = "../ethkey" }
ethcore-ipc-nano = { path = "../ipc/nano" }
rand = "0.3"

Expand Down
13 changes: 7 additions & 6 deletions ethcore/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.

use util::*;
use crypto::sha2::Sha256;
use crypto::ripemd160::Ripemd160;
use crypto::digest::Digest;
use util::*;
use ethkey::{Signature, recover};
use ethjson;

/// Definition of a contract whose implementation is built-in.
Expand Down Expand Up @@ -92,19 +93,19 @@ pub fn new_builtin_exec(name: &str) -> Box<Fn(&[u8], &mut [u8])> {
}),
"ecrecover" => Box::new(move|input: &[u8], output: &mut[u8]| {
#[repr(packed)]
#[derive(Debug)]
#[derive(Debug, Default)]
struct InType {
hash: H256,
v: H256,
r: H256,
s: H256,
}
let mut it: InType = InType { hash: H256::new(), v: H256::new(), r: H256::new(), s: H256::new() };
let mut it = InType::default();
it.copy_raw(input);
if it.v == H256::from(&U256::from(27)) || it.v == H256::from(&U256::from(28)) {
let s = signature_from_rsv(&it.r, &it.s, it.v[31] - 27);
if ec::is_valid(&s) {
if let Ok(p) = ec::recover(&s, &it.hash) {
let s = Signature::from_rsv(&it.r, &it.s, it.v[31] - 27);
if s.is_valid() {
if let Ok(p) = recover(&s, &it.hash) {
let r = p.as_slice().sha3();
// NICE: optimise and separate out into populate-like function
for i in 0..min(32, output.len()) {
Expand Down
3 changes: 2 additions & 1 deletion ethcore/src/client/test_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use std::sync::atomic::{AtomicUsize, Ordering as AtomicOrder};
use util::*;
use ethkey::{Generator, Random};
use devtools::*;
use transaction::{Transaction, LocalizedTransaction, SignedTransaction, Action};
use blockchain::TreeRoute;
Expand Down Expand Up @@ -188,7 +189,7 @@ impl TestBlockChainClient {
let txs = match with {
EachBlockWith::Transaction | EachBlockWith::UncleAndTransaction => {
let mut txs = RlpStream::new_list(1);
let keypair = KeyPair::create().unwrap();
let keypair = Random.generate().unwrap();
// Update nonces value
self.nonces.write().insert(keypair.address(), U256::one());
let tx = Transaction {
Expand Down
12 changes: 4 additions & 8 deletions ethcore/src/engines/basic_authority.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
//! A blockchain engine that supports a basic, non-BFT proof-of-authority.
use common::*;
use ethkey::{recover, public_to_address};
use account_provider::AccountProvider;
use block::*;
use spec::CommonParams;
Expand Down Expand Up @@ -133,7 +134,7 @@ impl Engine for BasicAuthority {
fn verify_block_unordered(&self, header: &Header, _block: Option<&[u8]>) -> result::Result<(), Error> {
// check the signature is legit.
let sig = try!(UntrustedRlp::new(&header.seal[0]).as_val::<H520>());
let signer = Address::from(try!(ec::recover(&sig, &header.bare_hash())).sha3());
let signer = public_to_address(&try!(recover(&sig.into(), &header.bare_hash())));
if !self.our_params.authorities.contains(&signer) {
return try!(Err(BlockError::InvalidSeal));
}
Expand Down Expand Up @@ -228,15 +229,10 @@ mod tests {
fn can_do_signature_verification_fail() {
let engine = new_test_authority().engine;
let mut header: Header = Header::default();
header.set_seal(vec![rlp::encode(&Signature::zero()).to_vec()]);
header.set_seal(vec![rlp::encode(&H520::default()).to_vec()]);

let verify_result = engine.verify_block_unordered(&header, None);

match verify_result {
Err(Error::Util(UtilError::Crypto(CryptoError::InvalidSignature))) => {},
Err(_) => { panic!("should be block difficulty error (got {:?})", verify_result); },
_ => { panic!("Should be error, got Ok"); },
}
assert!(verify_result.is_err());
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion ethcore/src/engines/instant_seal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ mod tests {

assert!(engine.verify_block_basic(&header, None).is_ok());

header.set_seal(vec![rlp::encode(&Signature::zero()).to_vec()]);
header.set_seal(vec![rlp::encode(&H520::default()).to_vec()]);

assert!(engine.verify_block_unordered(&header, None).is_ok());
}
Expand Down
16 changes: 10 additions & 6 deletions ethcore/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use client::Error as ClientError;
use ipc::binary::{BinaryConvertError, BinaryConvertable};
use types::block_import_error::BlockImportError;
use snapshot::Error as SnapshotError;
use ethkey::Error as EthkeyError;

pub use types::executed::{ExecutionError, CallError};

Expand Down Expand Up @@ -238,6 +239,8 @@ pub enum Error {
Snappy(::util::snappy::InvalidInput),
/// Snapshot error.
Snapshot(SnapshotError),
/// Ethkey error.
Ethkey(EthkeyError),
}

impl fmt::Display for Error {
Expand All @@ -258,6 +261,7 @@ impl fmt::Display for Error {
Error::StdIo(ref err) => err.fmt(f),
Error::Snappy(ref err) => err.fmt(f),
Error::Snapshot(ref err) => err.fmt(f),
Error::Ethkey(ref err) => err.fmt(f),
}
}
}
Expand Down Expand Up @@ -298,12 +302,6 @@ impl From<ExecutionError> for Error {
}
}

impl From<CryptoError> for Error {
fn from(err: CryptoError) -> Error {
Error::Util(UtilError::Crypto(err))
}
}

impl From<DecoderError> for Error {
fn from(err: DecoderError) -> Error {
Error::Util(UtilError::Decoder(err))
Expand Down Expand Up @@ -361,6 +359,12 @@ impl From<SnapshotError> for Error {
}
}

impl From<EthkeyError> for Error {
fn from(err: EthkeyError) -> Error {
Error::Ethkey(err)
}
}

impl<E> From<Box<E>> for Error where Error: From<E> {
fn from(err: Box<E>) -> Error {
Error::from(*err)
Expand Down
9 changes: 5 additions & 4 deletions ethcore/src/executive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,7 @@ impl<'a> Executive<'a> {
#[cfg(test)]
#[allow(dead_code)]
mod tests {
use ethkey::{Generator, Random};
use super::*;
use common::*;
use evm::{Factory, VMType};
Expand Down Expand Up @@ -1002,7 +1003,7 @@ mod tests {
// TODO: fix (preferred) or remove
evm_test_ignore!{test_transact_simple: test_transact_simple_jit, test_transact_simple_int}
fn test_transact_simple(factory: Factory) {
let keypair = KeyPair::create().unwrap();
let keypair = Random.generate().unwrap();
let t = Transaction {
action: Action::Create,
value: U256::from(17),
Expand Down Expand Up @@ -1069,7 +1070,7 @@ mod tests {

evm_test!{test_transact_invalid_nonce: test_transact_invalid_nonce_jit, test_transact_invalid_nonce_int}
fn test_transact_invalid_nonce(factory: Factory) {
let keypair = KeyPair::create().unwrap();
let keypair = Random.generate().unwrap();
let t = Transaction {
action: Action::Create,
value: U256::from(17),
Expand Down Expand Up @@ -1102,7 +1103,7 @@ mod tests {

evm_test!{test_transact_gas_limit_reached: test_transact_gas_limit_reached_jit, test_transact_gas_limit_reached_int}
fn test_transact_gas_limit_reached(factory: Factory) {
let keypair = KeyPair::create().unwrap();
let keypair = Random.generate().unwrap();
let t = Transaction {
action: Action::Create,
value: U256::from(17),
Expand Down Expand Up @@ -1137,7 +1138,7 @@ mod tests {
evm_test!{test_not_enough_cash: test_not_enough_cash_jit, test_not_enough_cash_int}
fn test_not_enough_cash(factory: Factory) {

let keypair = KeyPair::create().unwrap();
let keypair = Random.generate().unwrap();
let t = Transaction {
action: Action::Create,
value: U256::from(18),
Expand Down
1 change: 1 addition & 0 deletions ethcore/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ extern crate bloomchain;
extern crate rayon;
extern crate hyper;
extern crate ethash;
extern crate ethkey;
pub extern crate ethstore;
extern crate semver;
extern crate ethcore_ipc_nano as nanoipc;
Expand Down
5 changes: 3 additions & 2 deletions ethcore/src/miner/miner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -911,6 +911,7 @@ mod tests {
use super::super::MinerService;
use super::*;
use util::*;
use ethkey::{Generator, Random};
use client::{TestBlockChainClient, EachBlockWith};
use client::{TransactionImportResult};
use types::transaction::{Transaction, Action};
Expand Down Expand Up @@ -975,7 +976,7 @@ mod tests {
let client = TestBlockChainClient::default();
let miner = miner();
let transaction = {
let keypair = KeyPair::create().unwrap();
let keypair = Random.generate().unwrap();
Transaction {
action: Action::Create,
value: U256::zero(),
Expand Down Expand Up @@ -1005,7 +1006,7 @@ mod tests {
let client = TestBlockChainClient::default();
let miner = miner();
let transaction = {
let keypair = KeyPair::create().unwrap();
let keypair = Random.generate().unwrap();
Transaction {
action: Action::Create,
value: U256::zero(),
Expand Down
Loading

0 comments on commit b0d462c

Please sign in to comment.