Gav Wood
10 years ago
138 changed files with 554 additions and 357 deletions
@ -0,0 +1,9 @@ |
|||
#pragma once |
|||
|
|||
#include "Common.h" |
|||
#include "FileSystem.h" |
|||
#include "MemoryDB.h" |
|||
#include "OverlayDB.h" |
|||
#include "SHA3.h" |
|||
#include "TrieCommon.h" |
|||
#include "TrieDB.h" |
@ -0,0 +1,56 @@ |
|||
cmake_policy(SET CMP0015 NEW) |
|||
|
|||
aux_source_directory(. SRC_LIST) |
|||
|
|||
set(EXECUTABLE devcrypto) |
|||
|
|||
# set(CMAKE_INSTALL_PREFIX ../lib) |
|||
if(ETH_STATIC) |
|||
add_library(${EXECUTABLE} STATIC ${SRC_LIST}) |
|||
else() |
|||
add_library(${EXECUTABLE} SHARED ${SRC_LIST}) |
|||
endif() |
|||
|
|||
file(GLOB HEADERS "*.h") |
|||
|
|||
include_directories(..) |
|||
|
|||
target_link_libraries(${EXECUTABLE} devcore) |
|||
target_link_libraries(${EXECUTABLE} secp256k1) |
|||
target_link_libraries(${EXECUTABLE} gmp) |
|||
target_link_libraries(${EXECUTABLE} ${LEVELDB_LS}) |
|||
target_link_libraries(${EXECUTABLE} ${CRYPTOPP_LS}) |
|||
|
|||
if("${TARGET_PLATFORM}" STREQUAL "w64") |
|||
target_link_libraries(${EXECUTABLE} boost_system-mt-s) |
|||
target_link_libraries(${EXECUTABLE} boost_filesystem-mt-s) |
|||
target_link_libraries(${EXECUTABLE} boost_thread_win32-mt-s) |
|||
target_link_libraries(${EXECUTABLE} iphlpapi) |
|||
target_link_libraries(${EXECUTABLE} ws2_32) |
|||
target_link_libraries(${EXECUTABLE} mswsock) |
|||
target_link_libraries(${EXECUTABLE} shlwapi) |
|||
elseif (APPLE) |
|||
# Latest mavericks boost libraries only come with -mt |
|||
target_link_libraries(${EXECUTABLE} boost_system-mt) |
|||
target_link_libraries(${EXECUTABLE} boost_filesystem-mt) |
|||
target_link_libraries(${EXECUTABLE} boost_thread-mt) |
|||
find_package(Threads REQUIRED) |
|||
target_link_libraries(${EXECUTABLE} ${CMAKE_THREAD_LIBS_INIT}) |
|||
elseif (UNIX) |
|||
find_package(Boost 1.53 REQUIRED COMPONENTS filesystem) |
|||
target_link_libraries(${EXECUTABLE} ${Boost_SYSTEM_LIBRARY}) |
|||
target_link_libraries(${EXECUTABLE} ${Boost_FILESYSTEM_LIBRARY}) |
|||
target_link_libraries(${EXECUTABLE} ${Boost_THREAD_LIBRARY}) |
|||
target_link_libraries(${EXECUTABLE} ${Boost_DATE_TIME_LIBRARY}) |
|||
target_link_libraries(${EXECUTABLE} ${CMAKE_THREAD_LIBS_INIT}) |
|||
else () |
|||
target_link_libraries(${EXECUTABLE} boost_system) |
|||
target_link_libraries(${EXECUTABLE} boost_filesystem) |
|||
target_link_libraries(${EXECUTABLE} boost_thread) |
|||
find_package(Threads REQUIRED) |
|||
target_link_libraries(${EXECUTABLE} ${CMAKE_THREAD_LIBS_INIT}) |
|||
endif () |
|||
|
|||
install( TARGETS ${EXECUTABLE} ARCHIVE DESTINATION lib LIBRARY DESTINATION lib ) |
|||
install( FILES ${HEADERS} DESTINATION include/${EXECUTABLE} ) |
|||
|
@ -0,0 +1,104 @@ |
|||
/*
|
|||
This file is part of cpp-ethereum. |
|||
|
|||
cpp-ethereum is free software: you can redistribute it and/or modify |
|||
it under the terms of the GNU General Public License as published by |
|||
the Free Software Foundation, either version 3 of the License, or |
|||
(at your option) any later version. |
|||
|
|||
cpp-ethereum is distributed in the hope that it will be useful, |
|||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
GNU General Public License for more details. |
|||
|
|||
You should have received a copy of the GNU General Public License |
|||
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|||
*/ |
|||
/** @file CommonEth.cpp
|
|||
* @author Gav Wood <i@gavwood.com> |
|||
* @date 2014 |
|||
*/ |
|||
|
|||
#include "Common.h" |
|||
#include <random> |
|||
#include <secp256k1/secp256k1.h> |
|||
#include "SHA3.h" |
|||
using namespace std; |
|||
using namespace dev; |
|||
using namespace dev::eth; |
|||
|
|||
//#define ETH_ADDRESS_DEBUG 1
|
|||
|
|||
Address dev::toAddress(Secret _private) |
|||
{ |
|||
secp256k1_start(); |
|||
|
|||
byte pubkey[65]; |
|||
int pubkeylen = 65; |
|||
int ok = secp256k1_ecdsa_seckey_verify(_private.data()); |
|||
if (!ok) |
|||
return Address(); |
|||
ok = secp256k1_ecdsa_pubkey_create(pubkey, &pubkeylen, _private.data(), 0); |
|||
assert(pubkeylen == 65); |
|||
if (!ok) |
|||
return Address(); |
|||
ok = secp256k1_ecdsa_pubkey_verify(pubkey, 65); |
|||
if (!ok) |
|||
return Address(); |
|||
auto ret = right160(dev::eth::sha3(bytesConstRef(&(pubkey[1]), 64))); |
|||
#if ETH_ADDRESS_DEBUG |
|||
cout << "---- ADDRESS -------------------------------" << endl; |
|||
cout << "SEC: " << _private << endl; |
|||
cout << "PUB: " << toHex(bytesConstRef(&(pubkey[1]), 64)) << endl; |
|||
cout << "ADR: " << ret << endl; |
|||
#endif |
|||
return ret; |
|||
} |
|||
|
|||
KeyPair KeyPair::create() |
|||
{ |
|||
secp256k1_start(); |
|||
static std::mt19937_64 s_eng(time(0)); |
|||
std::uniform_int_distribution<uint16_t> d(0, 255); |
|||
|
|||
for (int i = 0; i < 100; ++i) |
|||
{ |
|||
h256 sec; |
|||
for (unsigned i = 0; i < 32; ++i) |
|||
sec[i] = (byte)d(s_eng); |
|||
|
|||
KeyPair ret(sec); |
|||
if (ret.address()) |
|||
return ret; |
|||
} |
|||
return KeyPair(); |
|||
} |
|||
|
|||
KeyPair::KeyPair(h256 _sec): |
|||
m_secret(_sec) |
|||
{ |
|||
int ok = secp256k1_ecdsa_seckey_verify(m_secret.data()); |
|||
if (!ok) |
|||
return; |
|||
|
|||
byte pubkey[65]; |
|||
int pubkeylen = 65; |
|||
ok = secp256k1_ecdsa_pubkey_create(pubkey, &pubkeylen, m_secret.data(), 0); |
|||
if (!ok || pubkeylen != 65) |
|||
return; |
|||
|
|||
ok = secp256k1_ecdsa_pubkey_verify(pubkey, 65); |
|||
if (!ok) |
|||
return; |
|||
|
|||
m_secret = m_secret; |
|||
memcpy(m_public.data(), &(pubkey[1]), 64); |
|||
m_address = right160(dev::eth::sha3(bytesConstRef(&(pubkey[1]), 64))); |
|||
|
|||
#if ETH_ADDRESS_DEBUG |
|||
cout << "---- ADDRESS -------------------------------" << endl; |
|||
cout << "SEC: " << m_secret << endl; |
|||
cout << "PUB: " << m_public << endl; |
|||
cout << "ADR: " << m_address << endl; |
|||
#endif |
|||
} |
@ -0,0 +1,86 @@ |
|||
/*
|
|||
This file is part of cpp-ethereum. |
|||
|
|||
cpp-ethereum is free software: you can redistribute it and/or modify |
|||
it under the terms of the GNU General Public License as published by |
|||
the Free Software Foundation, either version 3 of the License, or |
|||
(at your option) any later version. |
|||
|
|||
cpp-ethereum is distributed in the hope that it will be useful, |
|||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
GNU General Public License for more details. |
|||
|
|||
You should have received a copy of the GNU General Public License |
|||
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|||
*/ |
|||
/** @file CommonEth.h
|
|||
* @author Gav Wood <i@gavwood.com> |
|||
* @date 2014 |
|||
* |
|||
* Ethereum-specific data structures & algorithms. |
|||
*/ |
|||
|
|||
#pragma once |
|||
|
|||
#include <libdevcore/Common.h> |
|||
#include <libdevcore/FixedHash.h> |
|||
|
|||
namespace dev |
|||
{ |
|||
|
|||
/// A secret key: 32 bytes.
|
|||
/// @NOTE This is not endian-specific; it's just a bunch of bytes.
|
|||
using Secret = h256; |
|||
|
|||
/// A public key: 64 bytes.
|
|||
/// @NOTE This is not endian-specific; it's just a bunch of bytes.
|
|||
using Public = h512; |
|||
|
|||
/// An Ethereum address: 20 bytes.
|
|||
/// @NOTE This is not endian-specific; it's just a bunch of bytes.
|
|||
using Address = h160; |
|||
|
|||
/// A vector of Ethereum addresses.
|
|||
using Addresses = h160s; |
|||
|
|||
/// Convert a private key into the public key equivalent.
|
|||
/// @returns 0 if it's not a valid private key.
|
|||
Address toAddress(h256 _private); |
|||
|
|||
/// Simple class that represents a "key pair".
|
|||
/// All of the data of the class can be regenerated from the secret key (m_secret) alone.
|
|||
/// Actually stores a tuplet of secret, public and address (the right 160-bits of the public).
|
|||
class KeyPair |
|||
{ |
|||
public: |
|||
/// Null constructor.
|
|||
KeyPair() {} |
|||
|
|||
/// Normal constructor - populates object from the given secret key.
|
|||
KeyPair(Secret _k); |
|||
|
|||
/// Create a new, randomly generated object.
|
|||
static KeyPair create(); |
|||
|
|||
/// Retrieve the secret key.
|
|||
Secret const& secret() const { return m_secret; } |
|||
/// Retrieve the secret key.
|
|||
Secret const& sec() const { return m_secret; } |
|||
|
|||
/// Retrieve the public key.
|
|||
Public const& pub() const { return m_public; } |
|||
|
|||
/// Retrieve the associated address of the public key.
|
|||
Address const& address() const { return m_address; } |
|||
|
|||
bool operator==(KeyPair const& _c) const { return m_secret == _c.m_secret; } |
|||
bool operator!=(KeyPair const& _c) const { return m_secret != _c.m_secret; } |
|||
|
|||
private: |
|||
Secret m_secret; |
|||
Public m_public; |
|||
Address m_address; |
|||
}; |
|||
|
|||
} |
@ -0,0 +1,36 @@ |
|||
/*
|
|||
This file is part of cpp-ethereum. |
|||
|
|||
cpp-ethereum is free software: you can redistribute it and/or modify |
|||
it under the terms of the GNU General Public License as published by |
|||
the Free Software Foundation, either version 3 of the License, or |
|||
(at your option) any later version. |
|||
|
|||
cpp-ethereum is distributed in the hope that it will be useful, |
|||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
GNU General Public License for more details. |
|||
|
|||
You should have received a copy of the GNU General Public License |
|||
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|||
*/ |
|||
/** @file CryptoHeaders.h
|
|||
* @author Tim Hughes <tim@twistedfury.com> |
|||
* @date 2014 |
|||
*/ |
|||
#pragma once |
|||
|
|||
// need to leave this one disabled
|
|||
#pragma GCC diagnostic ignored "-Wunused-function" |
|||
|
|||
#pragma warning(push) |
|||
#pragma warning(disable:4100 4244) |
|||
#pragma GCC diagnostic push |
|||
#pragma GCC diagnostic ignored "-Wconversion" |
|||
#pragma GCC diagnostic ignored "-Wunused-parameter" |
|||
#include <sha.h> |
|||
#include <sha3.h> |
|||
#include <ripemd.h> |
|||
#include <secp256k1/secp256k1.h> |
|||
#pragma warning(pop) |
|||
#pragma GCC diagnostic pop |
Some files were not shown because too many files changed in this diff
Loading…
Reference in new issue