From 59b358d256e1594d02923e95f081445d6eff2f6e Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Thu, 30 Jan 2020 08:03:05 +0100 Subject: [PATCH 1/7] src/types.h: cleanup EC_PUBKEY_SIZE abit. --- src/types.h | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/types.h b/src/types.h index eba5182..806b61f 100644 --- a/src/types.h +++ b/src/types.h @@ -25,6 +25,12 @@ #define TYPES_H #define EC_PRIVKEY_SIZE 32 -#define EC_PUBKEY_SIZE 33 /* Compressed: 32 bytes + 1 prefix */ + +/* + * 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) #endif /* TYPES_H */ From cc4d91579bd2959d11aa9141b20ba40c81a30c86 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Thu, 30 Jan 2020 08:03:39 +0100 Subject: [PATCH 2/7] src/checksum.h: Use a define for checksum length instead of magic number. --- src/checksum.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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); From 07a85e732f8781fdc1235970aa52888f45c7e216 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Thu, 30 Jan 2020 08:04:19 +0100 Subject: [PATCH 3/7] src/WIF.cpp: Use constants intead of magic number. --- src/WIF.cpp | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) 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)); } From 0bda9e3f2a3b924a1de880b69aaf041f7e095e41 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Thu, 30 Jan 2020 08:08:43 +0100 Subject: [PATCH 4/7] src/ec.h: document ec_generate_key() --- src/ec.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/ec.h b/src/ec.h index b29a8a4..a660ee8 100644 --- a/src/ec.h +++ b/src/ec.h @@ -35,6 +35,10 @@ struct ec_keypair { ec_pubkey_t pub; }; +/** + * Generates a keypair using the secp256k1 curve. + * public key is in compressed format. + */ int ec_generate_key(struct ec_keypair *pair); #endif /* EC_H */ From 8007a39c354df390852bc1922574bad5057de9b9 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Thu, 30 Jan 2020 08:16:41 +0100 Subject: [PATCH 5/7] src/ec.h: move typedefs and struct to types.h --- src/ec.h | 9 --------- src/types.h | 11 +++++++++++ 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/ec.h b/src/ec.h index a660ee8..29269e8 100644 --- a/src/ec.h +++ b/src/ec.h @@ -24,17 +24,8 @@ #ifndef EC_H #define EC_H -#include #include "types.h" -typedef std::array ec_privkey_t; -typedef std::array ec_pubkey_t; - -struct ec_keypair { - ec_privkey_t secret; - ec_pubkey_t pub; -}; - /** * Generates a keypair using the secp256k1 curve. * public key is in compressed format. diff --git a/src/types.h b/src/types.h index 806b61f..1977f5d 100644 --- a/src/types.h +++ b/src/types.h @@ -24,6 +24,8 @@ #ifndef TYPES_H #define TYPES_H +#include + #define EC_PRIVKEY_SIZE 32 /* @@ -33,4 +35,13 @@ */ #define EC_PUBKEY_SIZE (32 + 1) + +typedef std::array ec_privkey_t; +typedef std::array ec_pubkey_t; + +struct ec_keypair { + ec_privkey_t secret; + ec_pubkey_t pub; +}; + #endif /* TYPES_H */ From 8630f5c25560ab6107f267ea32040b5627591548 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Thu, 30 Jan 2020 08:19:28 +0100 Subject: [PATCH 6/7] include "types.h" instead of "ec.h" where it is appropriate --- src/WIF.h | 2 +- src/key_search_helpers.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/WIF.h b/src/WIF.h index 06b4a63..5102e65 100644 --- a/src/WIF.h +++ b/src/WIF.h @@ -25,7 +25,7 @@ #define WIF_H #include -#include "ec.h" +#include "types.h" std::string wif_priv_encode(ec_privkey_t priv); diff --git a/src/key_search_helpers.h b/src/key_search_helpers.h index 66e7783..b9679b9 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 "types.h" struct key_result { size_t pos; // position where the word was found. From c7802b2e56f3a65108f8f377bc2193d1427f3ff1 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Thu, 30 Jan 2020 08:41:30 +0100 Subject: [PATCH 7/7] move types.h and ec stuff to "ec" subdirectory. --- CMakeLists.txt | 2 +- src/WIF.h | 2 +- src/benchmark.cpp | 2 +- src/{ec.h => ec/generate.h} | 6 +++--- src/{ec.cpp => ec/openssl.cpp} | 2 +- src/{ => ec}/types.h | 7 +++---- src/key_search.cpp | 2 +- src/key_search_helpers.h | 2 +- src/key_search_mt.cpp | 2 +- src/main.cpp | 2 +- 10 files changed, 14 insertions(+), 15 deletions(-) rename src/{ec.h => ec/generate.h} (94%) rename src/{ec.cpp => ec/openssl.cpp} (98%) rename src/{ => ec}/types.h (96%) 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.h b/src/WIF.h index 5102e65..48eb6a8 100644 --- a/src/WIF.h +++ b/src/WIF.h @@ -25,7 +25,7 @@ #define WIF_H #include -#include "types.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/ec.h b/src/ec/generate.h similarity index 94% rename from src/ec.h rename to src/ec/generate.h index 29269e8..ccde10c 100644 --- a/src/ec.h +++ b/src/ec/generate.h @@ -21,8 +21,8 @@ * 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_GENERATE_H +#define EC_GENERATE_H #include "types.h" @@ -32,4 +32,4 @@ */ int ec_generate_key(struct ec_keypair *pair); -#endif /* EC_H */ +#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/types.h b/src/ec/types.h similarity index 96% rename from src/types.h rename to src/ec/types.h index 1977f5d..19a2312 100644 --- a/src/types.h +++ b/src/ec/types.h @@ -21,8 +21,8 @@ * 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_TYPES_H +#define EC_TYPES_H #include @@ -35,7 +35,6 @@ */ #define EC_PUBKEY_SIZE (32 + 1) - typedef std::array ec_privkey_t; typedef std::array ec_pubkey_t; @@ -44,4 +43,4 @@ struct ec_keypair { ec_pubkey_t pub; }; -#endif /* TYPES_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 b9679b9..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 "types.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 3a25014..cdc2dfe 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"