diff --git a/CMakeLists.txt b/CMakeLists.txt index a5edd61..e821d8d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,7 +18,7 @@ set (PROGRAM_EXE ${CMAKE_PROJECT_NAME}) set (PROGRAM_SOURCE src/string.cpp - src/ec.cpp + src/ec/openssl.cpp src/base58.cpp src/checksum.cpp src/WIF.cpp diff --git a/src/WIF.cpp b/src/WIF.cpp index 618de62..5577d8e 100644 --- a/src/WIF.cpp +++ b/src/WIF.cpp @@ -27,16 +27,19 @@ #include "checksum.h" #include "WIF.h" +#define PRIV_KEY_PREFIX 0x80 /* 0x80 for "Bitcoin mainnet". Always used by EOS. */ + std::string wif_priv_encode(ec_privkey_t priv) { checksum_t check; - unsigned char buf[37] = { 0x80 }; + // 1 byte extra for prefix. + unsigned char buf[1 + EC_PRIVKEY_SIZE + CHECKSUM_SIZE] = { PRIV_KEY_PREFIX }; memcpy(buf + 1, priv.data(), priv.size()); // Checksum - check = checksum_sha256d(buf, 33); - memcpy(buf + 33, check.data(), check.size()); + check = checksum_sha256d(buf, 1 + EC_PRIVKEY_SIZE); + memcpy(buf + 1 + EC_PRIVKEY_SIZE, check.data(), check.size()); return base58_encode(buf, buf + sizeof(buf)); } @@ -44,10 +47,10 @@ std::string wif_priv_encode(ec_privkey_t priv) { std::string wif_pub_encode(ec_pubkey_t pub) { checksum_t check = checksum_ripemd160(pub.data(), pub.size()); - unsigned char buf[37]; + unsigned char buf[EC_PUBKEY_SIZE + CHECKSUM_SIZE]; memcpy(buf, pub.data(), pub.size()); - memcpy(buf + 33, check.data(), check.size()); + memcpy(buf + EC_PUBKEY_SIZE, check.data(), check.size()); return "EOS" + base58_encode(buf, buf + sizeof(buf)); } diff --git a/src/WIF.h b/src/WIF.h index 06b4a63..48eb6a8 100644 --- a/src/WIF.h +++ b/src/WIF.h @@ -25,7 +25,7 @@ #define WIF_H #include -#include "ec.h" +#include "ec/types.h" std::string wif_priv_encode(ec_privkey_t priv); diff --git a/src/benchmark.cpp b/src/benchmark.cpp index 6fb6c66..228b045 100644 --- a/src/benchmark.cpp +++ b/src/benchmark.cpp @@ -22,7 +22,7 @@ * SOFTWARE. */ #include -#include "ec.h" +#include "ec/generate.h" #include "benchmark.h" using std::chrono::steady_clock; diff --git a/src/checksum.h b/src/checksum.h index 57f50e8..1ac08a2 100644 --- a/src/checksum.h +++ b/src/checksum.h @@ -27,7 +27,9 @@ #include #include -typedef std::array checksum_t; +#define CHECKSUM_SIZE 4 + +typedef std::array checksum_t; checksum_t checksum_sha256d(const unsigned char *data, std::size_t len); diff --git a/src/types.h b/src/ec/generate.h similarity index 82% rename from src/types.h rename to src/ec/generate.h index eba5182..ccde10c 100644 --- a/src/types.h +++ b/src/ec/generate.h @@ -21,10 +21,15 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -#ifndef TYPES_H -#define TYPES_H +#ifndef EC_GENERATE_H +#define EC_GENERATE_H -#define EC_PRIVKEY_SIZE 32 -#define EC_PUBKEY_SIZE 33 /* Compressed: 32 bytes + 1 prefix */ +#include "types.h" -#endif /* TYPES_H */ +/** + * Generates a keypair using the secp256k1 curve. + * public key is in compressed format. + */ +int ec_generate_key(struct ec_keypair *pair); + +#endif /* EC_GENERATE_H */ diff --git a/src/ec.cpp b/src/ec/openssl.cpp similarity index 98% rename from src/ec.cpp rename to src/ec/openssl.cpp index d277eda..40e0cf3 100644 --- a/src/ec.cpp +++ b/src/ec/openssl.cpp @@ -24,7 +24,7 @@ #include #include #include -#include "ec.h" +#include "generate.h" static int ec_generate_pair(unsigned char *priv, unsigned char *pub) { diff --git a/src/ec.h b/src/ec/types.h similarity index 83% rename from src/ec.h rename to src/ec/types.h index b29a8a4..19a2312 100644 --- a/src/ec.h +++ b/src/ec/types.h @@ -21,11 +21,19 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -#ifndef EC_H -#define EC_H +#ifndef EC_TYPES_H +#define EC_TYPES_H #include -#include "types.h" + +#define EC_PRIVKEY_SIZE 32 + +/* + * Compressed format! + * z||x, where byte z specifies which (of the 2) solutions of the quadratic equation y is. + * Each cordinate is 32 bytes. + */ +#define EC_PUBKEY_SIZE (32 + 1) typedef std::array ec_privkey_t; typedef std::array ec_pubkey_t; @@ -35,6 +43,4 @@ struct ec_keypair { ec_pubkey_t pub; }; -int ec_generate_key(struct ec_keypair *pair); - -#endif /* EC_H */ +#endif /* EC_TYPES_H */ diff --git a/src/key_search.cpp b/src/key_search.cpp index 285962b..9e1a259 100644 --- a/src/key_search.cpp +++ b/src/key_search.cpp @@ -22,7 +22,7 @@ * SOFTWARE. */ #include -#include "ec.h" +#include "ec/generate.h" #include "key_search_helpers.h" #include "key_search.h" diff --git a/src/key_search_helpers.h b/src/key_search_helpers.h index 66e7783..1b80105 100644 --- a/src/key_search_helpers.h +++ b/src/key_search_helpers.h @@ -25,7 +25,7 @@ #define KEY_SEARCH_HELPERS_H #include "string.h" -#include "ec.h" +#include "ec/types.h" struct key_result { size_t pos; // position where the word was found. diff --git a/src/key_search_mt.cpp b/src/key_search_mt.cpp index 3d7dc5e..85f3e2a 100644 --- a/src/key_search_mt.cpp +++ b/src/key_search_mt.cpp @@ -25,7 +25,7 @@ #include #include #include -#include "ec.h" +#include "ec/generate.h" #include "key_search_helpers.h" #include "key_search.h" diff --git a/src/main.cpp b/src/main.cpp index d83e9de..002c05d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -28,7 +28,7 @@ #include #include "string.h" #include "WIF.h" -#include "ec.h" +#include "ec/generate.h" #include "key_search.h" #include "benchmark.h"