optional dependency on secp256k1 for ethcrypto (#8109)

* optional dependency on secp256k1 for ethcrypto

* README
This commit is contained in:
Robert Habermeier 2018-03-19 06:39:46 +01:00 committed by Marek Kotewicz
parent 373036bb7a
commit 249f81cbc5
4 changed files with 23 additions and 5 deletions

View File

@ -6,7 +6,11 @@ authors = ["Parity Technologies <admin@parity.io>"]
[dependencies] [dependencies]
rust-crypto = "0.2.36" rust-crypto = "0.2.36"
tiny-keccak = "1.3" tiny-keccak = "1.3"
eth-secp256k1 = { git = "https://github.com/paritytech/rust-secp256k1" } eth-secp256k1 = { git = "https://github.com/paritytech/rust-secp256k1", optional = true }
ethkey = { path = "../ethkey" } ethkey = { path = "../ethkey", optional = true }
ethereum-types = "0.2" ethereum-types = "0.2"
subtle = "0.5" subtle = "0.5"
[features]
default = ["secp256k1"]
secp256k1 = ["eth-secp256k1", "ethkey"]

5
ethcrypto/README.md Normal file
View File

@ -0,0 +1,5 @@
# Ethcrypto
General cryptographic utilities for Ethereum.
By default, this library is compiled with the `secp256k1` feature, which provides ECDH and ECIES capability on that curve. It can be compiled without to avoid a dependency on the `libsecp256k1` library.

View File

@ -18,17 +18,22 @@
extern crate crypto as rcrypto; extern crate crypto as rcrypto;
extern crate ethereum_types; extern crate ethereum_types;
extern crate ethkey;
extern crate secp256k1;
extern crate subtle; extern crate subtle;
extern crate tiny_keccak; extern crate tiny_keccak;
#[cfg(feature = "secp256k1")]
extern crate secp256k1;
#[cfg(feature = "secp256k1")]
extern crate ethkey;
use std::fmt; use std::fmt;
use tiny_keccak::Keccak; use tiny_keccak::Keccak;
use rcrypto::pbkdf2::pbkdf2; use rcrypto::pbkdf2::pbkdf2;
use rcrypto::scrypt::{scrypt, ScryptParams}; use rcrypto::scrypt::{scrypt, ScryptParams};
use rcrypto::sha2::Sha256; use rcrypto::sha2::Sha256;
use rcrypto::hmac::Hmac; use rcrypto::hmac::Hmac;
#[cfg(feature = "secp256k1")]
use secp256k1::Error as SecpError; use secp256k1::Error as SecpError;
pub const KEY_LENGTH: usize = 32; pub const KEY_LENGTH: usize = 32;
@ -59,6 +64,7 @@ impl fmt::Display for ScryptError {
#[derive(PartialEq, Debug)] #[derive(PartialEq, Debug)]
pub enum Error { pub enum Error {
#[cfg(feature = "secp256k1")]
Secp(SecpError), Secp(SecpError),
Scrypt(ScryptError), Scrypt(ScryptError),
InvalidMessage, InvalidMessage,
@ -73,6 +79,7 @@ impl From<ScryptError> for Error {
impl fmt::Display for Error { impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
let s = match *self { let s = match *self {
#[cfg(feature = "secp256k1")]
Error::Secp(ref err) => err.to_string(), Error::Secp(ref err) => err.to_string(),
Error::Scrypt(ref err) => err.to_string(), Error::Scrypt(ref err) => err.to_string(),
Error::InvalidMessage => "Invalid message".into(), Error::InvalidMessage => "Invalid message".into(),
@ -88,6 +95,7 @@ impl Into<String> for Error {
} }
} }
#[cfg(feature = "secp256k1")]
impl From<SecpError> for Error { impl From<SecpError> for Error {
fn from(e: SecpError) -> Self { fn from(e: SecpError) -> Self {
Error::Secp(e) Error::Secp(e)
@ -174,6 +182,7 @@ pub mod aes {
} }
/// ECDH functions /// ECDH functions
#[cfg(feature = "secp256k1")]
pub mod ecdh { pub mod ecdh {
use secp256k1::{ecdh, key, Error as SecpError}; use secp256k1::{ecdh, key, Error as SecpError};
use ethkey::{Secret, Public, SECP256K1}; use ethkey::{Secret, Public, SECP256K1};
@ -198,6 +207,7 @@ pub mod ecdh {
} }
/// ECIES function /// ECIES function
#[cfg(feature = "secp256k1")]
pub mod ecies { pub mod ecies {
use rcrypto::digest::Digest; use rcrypto::digest::Digest;
use rcrypto::sha2::Sha256; use rcrypto::sha2::Sha256;

View File

@ -207,7 +207,6 @@ impl ExtendedKeyPair {
// Work is based on BIP0032 // Work is based on BIP0032
// https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki // https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki
mod derivation { mod derivation {
use rcrypto::hmac::Hmac; use rcrypto::hmac::Hmac;
use rcrypto::mac::Mac; use rcrypto::mac::Mac;
use rcrypto::sha2::Sha512; use rcrypto::sha2::Sha512;