From 2b68d7ec327be7da8998ffc65a5c7c246f3df964 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Thu, 23 Mar 2023 14:19:37 +0100 Subject: [PATCH 01/58] src/WIF.cpp: move _calculate_sig_checksum() to top of file, so we dont need to add a function declaration. --- src/WIF.cpp | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/WIF.cpp b/src/WIF.cpp index 7a6f5c8..2c2ca4b 100644 --- a/src/WIF.cpp +++ b/src/WIF.cpp @@ -31,6 +31,20 @@ namespace libeosio { #define PRIV_KEY_PREFIX 0x80 /* 0x80 for "Bitcoin mainnet". Always used by EOS. */ +// Just to make it "harder" the calculated checksum for a signature +// has a "K1" suffix that is not present in the WIF encoded string. +// So this function is a quick hack to calculate it. +// +// Should implement and use Init/Update/Finalize hash functions to do it inplace. +checksum_t _calculate_sig_checksum(const unsigned char *in) { + unsigned char buf[EC_SIGNATURE_SIZE + 2]; + + memcpy(buf, in, EC_SIGNATURE_SIZE); + memcpy(buf + EC_SIGNATURE_SIZE, "K1", 2); + + return checksum_ripemd160(buf, EC_SIGNATURE_SIZE + 2); +} + std::string wif_priv_encode(const ec_privkey_t& priv) { checksum_t check; @@ -112,20 +126,6 @@ void wif_print_key(const struct ec_keypair *key, const std::string& prefix) { std::cout << "Private: " << wif_priv_encode(key->secret) << std::endl; } -// Just to make it "harder" the calculated checksum for a signature -// has a "K1" suffix that is not present in the WIF encoded string. -// So this function is a quick hack to calculate it. -// -// Should implement and use Init/Update/Finalize hash functions to do it inplace. -checksum_t _calculate_sig_checksum(const unsigned char *in) { - unsigned char buf[EC_SIGNATURE_SIZE + 2]; - - memcpy(buf, in, EC_SIGNATURE_SIZE); - memcpy(buf + EC_SIGNATURE_SIZE, "K1", 2); - - return checksum_ripemd160(buf, EC_SIGNATURE_SIZE + 2); -} - bool wif_sig_decode(ec_signature_t& sig, const std::string& data) { checksum_t checksum; From f606f7e263a96e005cf6153a6db8fe00d465640f Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Thu, 23 Mar 2023 17:17:43 +0100 Subject: [PATCH 02/58] README.md: Update to reflect the use of libsecp256k1 --- README.md | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 40bb6d7..b9ded7c 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,19 @@ NOTE: This repository has no connection to the official EOS code. You will need `openssl` development files (version 1.1 or later) to compile and `cmake 3.15` or later to compile this project. +### Elliptic curve backend + +There is two different backend implementation for the elliptic curve part of the library: + +* `OpenSSL` as mentioned before. however you still need to link to openssl even if it is not used as the EC backend + because more of the codebase uses it. + +* `libsecp256k1` + +Default is to use `libsecp256k1` as it is more optimized. + +You can switch implementation by modifing the cmake variable `EC_LIB`. + ### CMake You can install `cmake` by reading the [official guide](https://cmake.org/install). @@ -96,13 +109,14 @@ C:\repo> cmake --build build --config Release ## Security notice -Keys are generated by `OpenSSL`'s `EC_KEY_generate_key` function. The library will -never expose keys to anything but the computers memory (and standard output if you call such a function of course). +Elliptic curve crypthographic operations is done using either `OpenSSL` or `libsecp256k1` libraries. +This library (libeosio) will never expose sensitve cryptographic information +to anything but the computers memory. You are free to inspect the source code and compile yourself to verify. However, use this at your own risk. we cannot guarantee that the keys are -cryptographically secure as this depends on OpenSSL's implementation (alto it is -widely used and should be safe) +cryptographically secure as this depends on the elliptic curve +implementation (alto both OpenSSL and libsecp256k1 are widely used and should be safe) Please read the `LICENSE` file. From f8630de09812df63b70adaed7444d3f4f1050232 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Thu, 23 Mar 2023 18:26:55 +0100 Subject: [PATCH 03/58] tests/WIF/pub_decode.cpp: fix test name. --- tests/WIF/pub_decode.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/WIF/pub_decode.cpp b/tests/WIF/pub_decode.cpp index 7898918..8004407 100644 --- a/tests/WIF/pub_decode.cpp +++ b/tests/WIF/pub_decode.cpp @@ -4,7 +4,7 @@ #include #include -TEST_CASE("WIF::wif_pub_encode") { +TEST_CASE("WIF::wif_pub_decode [legacy]") { struct testcase { const char* name; std::string key; From 9114c173150bdf07c16623c0479f435566e8b1e2 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Thu, 23 Mar 2023 19:14:51 +0100 Subject: [PATCH 04/58] include/libeosio/WIF.hpp: adding prefixes constants. --- include/libeosio/WIF.hpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/include/libeosio/WIF.hpp b/include/libeosio/WIF.hpp index 9eca7fc..a402607 100644 --- a/include/libeosio/WIF.hpp +++ b/include/libeosio/WIF.hpp @@ -29,6 +29,14 @@ namespace libeosio { +/** + * Key prefixes. (strings that is not equal to these prefixes are treated as legacy format.) + */ +const std::string WIF_PUB_LEG = "EOS"; +const std::string WIF_PUB_K1 = "PUB_K1_"; +const std::string WIF_PVT_K1 = "PVT_K1_"; + + /** * Encode an EC private key to WIF String. */ From ebb421902464f127f0be18f7d38d60104019643b Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Thu, 23 Mar 2023 19:15:25 +0100 Subject: [PATCH 05/58] WIF: Support PUB_K1 format. --- include/libeosio/WIF.hpp | 6 ++--- src/WIF.cpp | 56 +++++++++++++++++++++++++++++----------- tests/WIF/pub_decode.cpp | 28 ++++++++++++++++++++ tests/WIF/pub_encode.cpp | 36 +++++++++++++++++++++----- 4 files changed, 101 insertions(+), 25 deletions(-) diff --git a/include/libeosio/WIF.hpp b/include/libeosio/WIF.hpp index a402607..dffee92 100644 --- a/include/libeosio/WIF.hpp +++ b/include/libeosio/WIF.hpp @@ -50,17 +50,17 @@ bool wif_priv_decode(ec_privkey_t& priv, const std::string& data); /** * Encode an EC public key to WIF String. */ -std::string wif_pub_encode(const ec_pubkey_t& pub, const std::string& prefix = "EOS"); +std::string wif_pub_encode(const ec_pubkey_t& pub, const std::string& prefix = WIF_PUB_LEG); /** * Decode an WIF String to EC public key */ -bool wif_pub_decode(ec_pubkey_t& pub, const std::string& data, size_t prefix_length = 3); +bool wif_pub_decode(ec_pubkey_t& pub, const std::string& data); /** * Prints an EC keypair in WIF format to standard out. */ -void wif_print_key(const struct ec_keypair *key, const std::string& prefix = "EOS"); +void wif_print_key(const struct ec_keypair *key, const std::string& prefix = WIF_PUB_LEG); /** * Signatures diff --git a/src/WIF.cpp b/src/WIF.cpp index 2c2ca4b..0d8fd2a 100644 --- a/src/WIF.cpp +++ b/src/WIF.cpp @@ -31,18 +31,18 @@ namespace libeosio { #define PRIV_KEY_PREFIX 0x80 /* 0x80 for "Bitcoin mainnet". Always used by EOS. */ -// Just to make it "harder" the calculated checksum for a signature -// has a "K1" suffix that is not present in the WIF encoded string. +// Just to make it "harder" the calculated checksum for a signature (k1) and pub/priv keys in k1/r1 format. +// has a suffix that is not present in the WIF encoded string. // So this function is a quick hack to calculate it. // // Should implement and use Init/Update/Finalize hash functions to do it inplace. -checksum_t _calculate_sig_checksum(const unsigned char *in) { - unsigned char buf[EC_SIGNATURE_SIZE + 2]; +checksum_t _checksum_suffix(const unsigned char *in, size_t size, const char *suffix) { + unsigned char buf[size + 2]; - memcpy(buf, in, EC_SIGNATURE_SIZE); - memcpy(buf + EC_SIGNATURE_SIZE, "K1", 2); + memcpy(buf, in, size); + memcpy(buf + size, suffix, 2); - return checksum_ripemd160(buf, EC_SIGNATURE_SIZE + 2); + return checksum_ripemd160(buf, size + 2); } std::string wif_priv_encode(const ec_privkey_t& priv) { @@ -89,20 +89,42 @@ bool wif_priv_decode(ec_privkey_t& priv, const std::string& data) { std::string wif_pub_encode(const ec_pubkey_t& pub, const std::string& prefix) { - checksum_t check = checksum_ripemd160(pub.data(), pub.size()); + checksum_t check; unsigned char buf[EC_PUBKEY_SIZE + CHECKSUM_SIZE]; memcpy(buf, pub.data(), pub.size()); + + + if (prefix == WIF_PUB_K1) { + check = _checksum_suffix(buf, EC_PUBKEY_SIZE, "K1"); + } + // Legacy + else { + check = checksum_ripemd160(pub.data(), pub.size()); + } + memcpy(buf + EC_PUBKEY_SIZE, check.data(), check.size()); - return prefix.substr(0, 3) + base58_encode(buf, buf + sizeof(buf)); + return prefix + base58_encode(buf, buf + sizeof(buf)); } -bool wif_pub_decode(ec_pubkey_t& pub, const std::string& data, size_t prefix_length) { +bool wif_pub_decode(ec_pubkey_t& pub, const std::string& data) { + const char *suffix; + int offset; std::vector buf; - if (!base58_decode(data.c_str() + prefix_length, buf)) { + // Check prefix + if (data.substr(0, WIF_PUB_K1.size()) == WIF_PUB_K1) { + suffix = "K1"; + offset = WIF_PUB_K1.size(); + } else { + // Legacy + suffix = ""; + offset = 3; + } + + if (!base58_decode(data.c_str() + offset, buf)) { return false; } @@ -110,8 +132,12 @@ bool wif_pub_decode(ec_pubkey_t& pub, const std::string& data, size_t prefix_len return false; } - // Calculate and validate checksum - if (!checksum_validate(buf.data(), buf.size())) { + if (suffix[0] != '\0') { + checksum_t check = _checksum_suffix(buf.data(), EC_PUBKEY_SIZE, suffix); + if (memcmp(buf.data() + EC_PUBKEY_SIZE, check.data(), CHECKSUM_SIZE)) { + return false; + } + } else if (!checksum_validate(buf.data(), buf.size())) { return false; } @@ -145,7 +171,7 @@ bool wif_sig_decode(ec_signature_t& sig, const std::string& data) { } // Calculate checksum - checksum = _calculate_sig_checksum(buf.data()); + checksum = _checksum_suffix(buf.data(), EC_SIGNATURE_SIZE, "K1"); // And validate if (memcmp(buf.data() + EC_SIGNATURE_SIZE, checksum.data(), CHECKSUM_SIZE)) { @@ -160,7 +186,7 @@ bool wif_sig_decode(ec_signature_t& sig, const std::string& data) { std::string wif_sig_encode(const ec_signature_t& sig) { unsigned char buf[EC_SIGNATURE_SIZE + CHECKSUM_SIZE]; - checksum_t check = _calculate_sig_checksum(sig.data()); + checksum_t check = _checksum_suffix(sig.data(), EC_SIGNATURE_SIZE, "K1"); memcpy(buf, sig.data(), sig.size()); memcpy(buf + EC_SIGNATURE_SIZE, check.data(), check.size()); diff --git a/tests/WIF/pub_decode.cpp b/tests/WIF/pub_decode.cpp index 8004407..36ccad1 100644 --- a/tests/WIF/pub_decode.cpp +++ b/tests/WIF/pub_decode.cpp @@ -31,3 +31,31 @@ TEST_CASE("WIF::wif_pub_decode [legacy]") { } } } + +TEST_CASE("WIF::wif_pub_decode [k1]") { + struct testcase { + const char* name; + std::string key; + libeosio::ec_pubkey_t expected; + bool expectedRet; + }; + + std::vector tests { + { "one", "PUB_K1_7kzJ5iFBmQWWT1LiWgAiocESD7TTNuuPCdYREUQysruq7AxzWu", { 0x03, 0x7a, 0x0e, 0x6b, 0xfd, 0xe4, 0xf1, 0xad, 0x36, 0x3f, 0x3a, 0xf9, 0xe0, 0x93, 0x63, 0x5a, 0xa9, 0x99, 0x21, 0x15, 0xbc, 0x23, 0x35, 0x75, 0x13, 0x69, 0x55, 0xee, 0x3f, 0xf8, 0xfd, 0x97, 0xec }, true }, + { "two", "PUB_K1_5c9HkNCJLDebe2Wvapp8bpB38Pf1QWNpkrsFy3mshg7DViSUUa", { 0x02, 0x5e, 0x94, 0xa5, 0xe7, 0x9f, 0x66, 0x37, 0x55, 0x7e, 0xc2, 0x28, 0x30, 0x40, 0x82, 0x9a, 0x38, 0x72, 0x10, 0x96, 0x6e, 0x15, 0xb7, 0xa5, 0x8a, 0x27, 0x9a, 0x71, 0x06, 0xa7, 0x64, 0x23, 0x30 }, true }, + { "three", "PUB_K1_8SwZMY8DChbbmRKS3wdHCAbv1VWgTRmQEDSaLyJk8pG4wKBXpw", { 0x03, 0xd4, 0xc6, 0x2a, 0xdc, 0x11, 0x1c, 0x65, 0x7a, 0x9f, 0x5b, 0xba, 0x96, 0x3f, 0xbb, 0x2a, 0x69, 0x2e, 0xc5, 0x4a, 0x48, 0x3b, 0xa3, 0x5f, 0x2a, 0x37, 0x6c, 0x59, 0x95, 0xb1, 0x95, 0x1c, 0xc9 }, true }, + { "wrong_checksum", "PUB_K1_8SwZMY8DChbbmRKS3wdHCAbv1VWgTRmQEDSaLyJk8pG4wKBXgE", { 0x0 }, false }, + { "wrong_length", "PUB_K1_7kzJ5iFBmQWWT1LiWgAiocESD7TT", { 0x0 }, false }, + { "not_base58", "PUB_K1_7IIIIIOOOO", { 0x0 }, false } + }; + + for(auto it = tests.begin(); it != tests.end(); it++) { + + SUBCASE(it->name) { + libeosio::ec_pubkey_t result = { 0x0 }; + + CHECK( libeosio::wif_pub_decode(result, it->key) == it->expectedRet ); + CHECK( result == it->expected ); + } + } +} diff --git a/tests/WIF/pub_encode.cpp b/tests/WIF/pub_encode.cpp index cb6d627..d3e548f 100644 --- a/tests/WIF/pub_encode.cpp +++ b/tests/WIF/pub_encode.cpp @@ -4,29 +4,29 @@ #include #include -TEST_CASE("WIF::wif_pub_encode") { +TEST_CASE("WIF::wif_pub_encode [legacy]") { struct testcase { std::string name; + const std::string prefix; libeosio::ec_pubkey_t key; std::string expected; }; std::vector tests { - { "one", { 0x03, 0x7a, 0x0e, 0x6b, 0xfd, 0xe4, 0xf1, 0xad, 0x36, 0x3f, 0x3a, 0xf9, 0xe0, 0x93, 0x63, 0x5a, 0xa9, 0x99, 0x21, 0x15, 0xbc, 0x23, 0x35, 0x75, 0x13, 0x69, 0x55, 0xee, 0x3f, 0xf8, 0xfd, 0x97, 0xec }, "EOS7kzJ5iFBmQWWT1LiWgAiocESD7TTNuuPCdYREUQysruq8VeFKy" }, - { "two", { 0x02, 0x5e, 0x94, 0xa5, 0xe7, 0x9f, 0x66, 0x37, 0x55, 0x7e, 0xc2, 0x28, 0x30, 0x40, 0x82, 0x9a, 0x38, 0x72, 0x10, 0x96, 0x6e, 0x15, 0xb7, 0xa5, 0x8a, 0x27, 0x9a, 0x71, 0x06, 0xa7, 0x64, 0x23, 0x30 }, "EOS5c9HkNCJLDebe2Wvapp8bpB38Pf1QWNpkrsFy3mshg7DZfPNeA" }, - { "three", { 0x03, 0xd4, 0xc6, 0x2a, 0xdc, 0x11, 0x1c, 0x65, 0x7a, 0x9f, 0x5b, 0xba, 0x96, 0x3f, 0xbb, 0x2a, 0x69, 0x2e, 0xc5, 0x4a, 0x48, 0x3b, 0xa3, 0x5f, 0x2a, 0x37, 0x6c, 0x59, 0x95, 0xb1, 0x95, 0x1c, 0xc9 }, "EOS8SwZMY8DChbbmRKS3wdHCAbv1VWgTRmQEDSaLyJk8pG4wm8BJF" } + { "one", libeosio::WIF_PUB_LEG, { 0x03, 0x7a, 0x0e, 0x6b, 0xfd, 0xe4, 0xf1, 0xad, 0x36, 0x3f, 0x3a, 0xf9, 0xe0, 0x93, 0x63, 0x5a, 0xa9, 0x99, 0x21, 0x15, 0xbc, 0x23, 0x35, 0x75, 0x13, 0x69, 0x55, 0xee, 0x3f, 0xf8, 0xfd, 0x97, 0xec }, "EOS7kzJ5iFBmQWWT1LiWgAiocESD7TTNuuPCdYREUQysruq8VeFKy" }, + { "two", libeosio::WIF_PUB_LEG, { 0x02, 0x5e, 0x94, 0xa5, 0xe7, 0x9f, 0x66, 0x37, 0x55, 0x7e, 0xc2, 0x28, 0x30, 0x40, 0x82, 0x9a, 0x38, 0x72, 0x10, 0x96, 0x6e, 0x15, 0xb7, 0xa5, 0x8a, 0x27, 0x9a, 0x71, 0x06, 0xa7, 0x64, 0x23, 0x30 }, "EOS5c9HkNCJLDebe2Wvapp8bpB38Pf1QWNpkrsFy3mshg7DZfPNeA" }, + { "three", libeosio::WIF_PUB_LEG, { 0x03, 0xd4, 0xc6, 0x2a, 0xdc, 0x11, 0x1c, 0x65, 0x7a, 0x9f, 0x5b, 0xba, 0x96, 0x3f, 0xbb, 0x2a, 0x69, 0x2e, 0xc5, 0x4a, 0x48, 0x3b, 0xa3, 0x5f, 0x2a, 0x37, 0x6c, 0x59, 0x95, 0xb1, 0x95, 0x1c, 0xc9 }, "EOS8SwZMY8DChbbmRKS3wdHCAbv1VWgTRmQEDSaLyJk8pG4wm8BJF" } }; for(auto it = tests.begin(); it != tests.end(); it++) { SUBCASE(it->name.c_str()) { - CHECK( libeosio::wif_pub_encode(it->key) == it->expected ); + CHECK( libeosio::wif_pub_encode(it->key, it->prefix) == it->expected ); } } } - -TEST_CASE("WIF::wif_pub_encode [prefix]") { +TEST_CASE("WIF::wif_pub_encode [custom prefix]") { struct testcase { std::string name; @@ -47,3 +47,25 @@ TEST_CASE("WIF::wif_pub_encode [prefix]") { } } } + +TEST_CASE("WIF::wif_pub_encode [k1]") { + struct testcase { + std::string name; + const std::string prefix; + libeosio::ec_pubkey_t key; + std::string expected; + }; + + std::vector tests { + { "one", libeosio::WIF_PUB_K1, { 0x03, 0x7a, 0x0e, 0x6b, 0xfd, 0xe4, 0xf1, 0xad, 0x36, 0x3f, 0x3a, 0xf9, 0xe0, 0x93, 0x63, 0x5a, 0xa9, 0x99, 0x21, 0x15, 0xbc, 0x23, 0x35, 0x75, 0x13, 0x69, 0x55, 0xee, 0x3f, 0xf8, 0xfd, 0x97, 0xec }, "PUB_K1_7kzJ5iFBmQWWT1LiWgAiocESD7TTNuuPCdYREUQysruq7AxzWu" }, + { "two", libeosio::WIF_PUB_K1, { 0x02, 0x5e, 0x94, 0xa5, 0xe7, 0x9f, 0x66, 0x37, 0x55, 0x7e, 0xc2, 0x28, 0x30, 0x40, 0x82, 0x9a, 0x38, 0x72, 0x10, 0x96, 0x6e, 0x15, 0xb7, 0xa5, 0x8a, 0x27, 0x9a, 0x71, 0x06, 0xa7, 0x64, 0x23, 0x30 }, "PUB_K1_5c9HkNCJLDebe2Wvapp8bpB38Pf1QWNpkrsFy3mshg7DViSUUa" }, + { "three", libeosio::WIF_PUB_K1, { 0x03, 0xd4, 0xc6, 0x2a, 0xdc, 0x11, 0x1c, 0x65, 0x7a, 0x9f, 0x5b, 0xba, 0x96, 0x3f, 0xbb, 0x2a, 0x69, 0x2e, 0xc5, 0x4a, 0x48, 0x3b, 0xa3, 0x5f, 0x2a, 0x37, 0x6c, 0x59, 0x95, 0xb1, 0x95, 0x1c, 0xc9 }, "PUB_K1_8SwZMY8DChbbmRKS3wdHCAbv1VWgTRmQEDSaLyJk8pG4wKBXpw" } + }; + + for(auto it = tests.begin(); it != tests.end(); it++) { + + SUBCASE(it->name.c_str()) { + CHECK( libeosio::wif_pub_encode(it->key, it->prefix) == it->expected ); + } + } +} \ No newline at end of file From 3abfc488e8ed4a5c3d875351dc64ac6b0589dac1 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Sat, 25 Mar 2023 15:24:05 +0100 Subject: [PATCH 06/58] Adding src/wif/codec.hpp --- src/wif/codec.hpp | 52 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 src/wif/codec.hpp diff --git a/src/wif/codec.hpp b/src/wif/codec.hpp new file mode 100644 index 0000000..2c38d44 --- /dev/null +++ b/src/wif/codec.hpp @@ -0,0 +1,52 @@ +/** + * MIT License + * + * Copyright (c) 2019-2021 EOS Sw/eden + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef LIBEOSIO_CODEC_H +#define LIBEOSIO_CODEC_H + +#include +#include + +namespace libeosio { namespace internal { + +/** + * Public-key encoders + */ +typedef void (*pub_encoder_t)(const ec_pubkey_t& key, unsigned char *buf); + +void pub_encoder_legacy(const ec_pubkey_t& key, unsigned char *buf); + +void pub_encoder_k1(const ec_pubkey_t& key, unsigned char *buf); + +/** + * Public-key decoders + */ +typedef bool (*pub_decoder_t)(const std::vector& buf, ec_pubkey_t& key); + +bool pub_decoder_legacy(const std::vector& buf, ec_pubkey_t& key); + +bool pub_decoder_k1(const std::vector& buf, ec_pubkey_t& key); + +}} // namespace libeosio::internal + +#endif /* LIBEOSIO_CODEC_H */ From 6793762fbb91e8e5a8ad72326ef1d9c380e2dd49 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Sat, 25 Mar 2023 15:24:49 +0100 Subject: [PATCH 07/58] Adding src/wif/legacy.cpp --- src/wif/legacy.cpp | 48 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 src/wif/legacy.cpp diff --git a/src/wif/legacy.cpp b/src/wif/legacy.cpp new file mode 100644 index 0000000..47a8251 --- /dev/null +++ b/src/wif/legacy.cpp @@ -0,0 +1,48 @@ +/** + * MIT License + * + * Copyright (c) 2019-2021 EOS Sw/eden + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include +#include "codec.hpp" + +namespace libeosio { namespace internal { + +void pub_encoder_legacy(const ec_pubkey_t& key, unsigned char *buf) { + + checksum_t check = checksum_ripemd160(key.data(), EC_PUBKEY_SIZE); + + memcpy(buf, key.data(), EC_PUBKEY_SIZE); + memcpy(buf + EC_PUBKEY_SIZE, check.data(), check.size()); +} + +bool pub_decoder_legacy(const std::vector& buf, ec_pubkey_t& key) { + + if (!checksum_validate(buf.data(), buf.size())) { + return false; + } + + memcpy(key.data(), buf.data(), EC_PUBKEY_SIZE); + return true; +} + +}} // namespace libeosio::internal \ No newline at end of file From 053f91c74b86123904dd9dec72097ec8c9e68525 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Sat, 25 Mar 2023 15:24:57 +0100 Subject: [PATCH 08/58] Adding src/wif/k1.cpp --- src/wif/k1.cpp | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 src/wif/k1.cpp diff --git a/src/wif/k1.cpp b/src/wif/k1.cpp new file mode 100644 index 0000000..ea29f4e --- /dev/null +++ b/src/wif/k1.cpp @@ -0,0 +1,64 @@ +/** + * MIT License + * + * Copyright (c) 2019-2021 EOS Sw/eden + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include +#include "codec.hpp" + +namespace libeosio { namespace internal { + +// Just to make it "harder" the calculated checksum for a signature (k1) and pub/priv keys in k1/r1 format. +// has a suffix that is not present in the WIF encoded string. +// So this function is a quick hack to calculate it. +// +// Should implement and use Init/Update/Finalize hash functions to do it inplace. +checksum_t _checksum_suffix(const unsigned char *in, size_t size, const char *suffix) { + unsigned char buf[size + 2]; + + memcpy(buf, in, size); + memcpy(buf + size, suffix, 2); + + return checksum_ripemd160(buf, size + 2); +} + +void pub_encoder_k1(const ec_pubkey_t& key, unsigned char *buf) { + + checksum_t check = _checksum_suffix(key.data(), EC_PUBKEY_SIZE, "K1"); + + memcpy(buf, key.data(), EC_PUBKEY_SIZE); + memcpy(buf + EC_PUBKEY_SIZE, check.data(), check.size()); +} + +bool pub_decoder_k1(const std::vector& buf, ec_pubkey_t& key) { + + checksum_t check = _checksum_suffix(buf.data(), EC_PUBKEY_SIZE, "K1"); + + if (memcmp(buf.data() + EC_PUBKEY_SIZE, check.data(), CHECKSUM_SIZE)) { + return false; + } + + memcpy(key.data(), buf.data(), EC_PUBKEY_SIZE); + return true; +} + +}} // namespace libeosio::internal \ No newline at end of file From ea411793a219d611d7dd0d808b6c8e09d2b1a1e4 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Sat, 25 Mar 2023 16:02:09 +0100 Subject: [PATCH 09/58] src/WIF.cpp: use wif/codec.hpp --- CMakeLists.txt | 2 ++ src/WIF.cpp | 31 +++++++++---------------------- 2 files changed, 11 insertions(+), 22 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 46dec36..990af40 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -53,6 +53,8 @@ add_library( ${LIB_NAME} STATIC src/base58.cpp src/ec.cpp src/WIF.cpp + src/wif/k1.cpp + src/wif/legacy.cpp src/openssl/hash.cpp ) diff --git a/src/WIF.cpp b/src/WIF.cpp index 0d8fd2a..9e904c6 100644 --- a/src/WIF.cpp +++ b/src/WIF.cpp @@ -26,6 +26,7 @@ #include #include #include +#include "wif/codec.hpp" namespace libeosio { @@ -91,36 +92,33 @@ std::string wif_pub_encode(const ec_pubkey_t& pub, const std::string& prefix) { checksum_t check; unsigned char buf[EC_PUBKEY_SIZE + CHECKSUM_SIZE]; - - memcpy(buf, pub.data(), pub.size()); - + internal::pub_encoder_t encoder; if (prefix == WIF_PUB_K1) { - check = _checksum_suffix(buf, EC_PUBKEY_SIZE, "K1"); + encoder = internal::pub_encoder_k1; } // Legacy else { - check = checksum_ripemd160(pub.data(), pub.size()); + encoder = internal::pub_encoder_legacy; } - memcpy(buf + EC_PUBKEY_SIZE, check.data(), check.size()); + encoder(pub, buf); return prefix + base58_encode(buf, buf + sizeof(buf)); } bool wif_pub_decode(ec_pubkey_t& pub, const std::string& data) { - const char *suffix; + internal::pub_decoder_t decoder = internal::pub_decoder_legacy; int offset; std::vector buf; // Check prefix if (data.substr(0, WIF_PUB_K1.size()) == WIF_PUB_K1) { - suffix = "K1"; - offset = WIF_PUB_K1.size(); + decoder = internal::pub_decoder_k1; + offset = WIF_PUB_K1.size(); } else { // Legacy - suffix = ""; offset = 3; } @@ -132,18 +130,7 @@ bool wif_pub_decode(ec_pubkey_t& pub, const std::string& data) { return false; } - if (suffix[0] != '\0') { - checksum_t check = _checksum_suffix(buf.data(), EC_PUBKEY_SIZE, suffix); - if (memcmp(buf.data() + EC_PUBKEY_SIZE, check.data(), CHECKSUM_SIZE)) { - return false; - } - } else if (!checksum_validate(buf.data(), buf.size())) { - return false; - } - - // Copy data to output - memcpy(pub.data(), buf.data(), pub.size()); - return true; + return decoder(buf, pub); } void wif_print_key(const struct ec_keypair *key, const std::string& prefix) { From 1aa6906ba24a04ab0ab9cdb88927ceb9d4797ebb Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Sat, 25 Mar 2023 16:25:02 +0100 Subject: [PATCH 10/58] WIF: Support PVT_K1 format. --- include/libeosio/WIF.hpp | 3 ++- src/WIF.cpp | 57 ++++++++++++++++++--------------------- src/wif/codec.hpp | 18 +++++++++++++ src/wif/k1.cpp | 24 +++++++++++++++++ src/wif/legacy.cpp | 30 +++++++++++++++++++++ tests/WIF/priv_decode.cpp | 29 +++++++++++++++++++- tests/WIF/priv_encode.cpp | 34 +++++++++++++++++++---- 7 files changed, 157 insertions(+), 38 deletions(-) diff --git a/include/libeosio/WIF.hpp b/include/libeosio/WIF.hpp index dffee92..07f2a94 100644 --- a/include/libeosio/WIF.hpp +++ b/include/libeosio/WIF.hpp @@ -34,13 +34,14 @@ namespace libeosio { */ const std::string WIF_PUB_LEG = "EOS"; const std::string WIF_PUB_K1 = "PUB_K1_"; +const std::string WIF_PVT_LEG = ""; const std::string WIF_PVT_K1 = "PVT_K1_"; /** * Encode an EC private key to WIF String. */ -std::string wif_priv_encode(const ec_privkey_t& priv); +std::string wif_priv_encode(const ec_privkey_t& priv, const std::string& prefix = WIF_PVT_LEG); /** * Decode an WIF String to EC private key diff --git a/src/WIF.cpp b/src/WIF.cpp index 9e904c6..0ace329 100644 --- a/src/WIF.cpp +++ b/src/WIF.cpp @@ -30,8 +30,6 @@ namespace libeosio { -#define PRIV_KEY_PREFIX 0x80 /* 0x80 for "Bitcoin mainnet". Always used by EOS. */ - // Just to make it "harder" the calculated checksum for a signature (k1) and pub/priv keys in k1/r1 format. // has a suffix that is not present in the WIF encoded string. // So this function is a quick hack to calculate it. @@ -46,51 +44,48 @@ checksum_t _checksum_suffix(const unsigned char *in, size_t size, const char *su return checksum_ripemd160(buf, size + 2); } -std::string wif_priv_encode(const ec_privkey_t& priv) { +std::string wif_priv_encode(const ec_privkey_t& priv, const std::string& prefix) { checksum_t check; - // 1 byte extra for prefix. - unsigned char buf[1 + EC_PRIVKEY_SIZE + CHECKSUM_SIZE] = { PRIV_KEY_PREFIX }; + // 1 byte extra for legacy prefix prefix. + unsigned char buf[1 + EC_PRIVKEY_SIZE + CHECKSUM_SIZE] = { 0 }; + size_t len; - memcpy(buf + 1, priv.data(), priv.size()); + if (prefix == WIF_PVT_K1) { + len = internal::priv_encoder_k1(priv, buf); + } else if (prefix == WIF_PVT_LEG) { + len = internal::priv_encoder_legacy(priv, buf); + } else { + return ""; + } - // Checksum - 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)); + return prefix + base58_encode(buf, buf + len); } bool wif_priv_decode(ec_privkey_t& priv, const std::string& data) { + uint8_t offset; std::vector buf; + internal::priv_decoder_t decoder = internal::priv_decoder_legacy; - if (!base58_decode(data, buf)) { + // Check prefix + if (data.substr(0, WIF_PVT_K1.size()) == WIF_PVT_K1) { + offset = WIF_PVT_K1.size(); + decoder = internal::priv_decoder_k1; + } else { + // Legacy + offset = 0; + } + + if (!base58_decode(data.c_str() + offset, buf)) { return false; } - if (buf.size() != 1 + EC_PRIVKEY_SIZE + CHECKSUM_SIZE) { - return false; - } - - // First byte is the prefix - if (buf[0] != PRIV_KEY_PREFIX) { - return false; - } - - // Calculate and validate checksum - if (!checksum_validate(buf.data(), buf.size())) { - return false; - } - - // Copy data to output - memcpy(priv.data(), buf.data() + 1, priv.size()); - return true; + return decoder(buf, priv); } std::string wif_pub_encode(const ec_pubkey_t& pub, const std::string& prefix) { - checksum_t check; unsigned char buf[EC_PUBKEY_SIZE + CHECKSUM_SIZE]; internal::pub_encoder_t encoder; @@ -136,7 +131,7 @@ bool wif_pub_decode(ec_pubkey_t& pub, const std::string& data) { void wif_print_key(const struct ec_keypair *key, const std::string& prefix) { std::cout << "Public: " << wif_pub_encode(key->pub, prefix) << std::endl; - std::cout << "Private: " << wif_priv_encode(key->secret) << std::endl; + std::cout << "Private: " << wif_priv_encode(key->secret, prefix) << std::endl; } bool wif_sig_decode(ec_signature_t& sig, const std::string& data) { diff --git a/src/wif/codec.hpp b/src/wif/codec.hpp index 2c38d44..3e235d3 100644 --- a/src/wif/codec.hpp +++ b/src/wif/codec.hpp @@ -47,6 +47,24 @@ bool pub_decoder_legacy(const std::vector& buf, ec_pubkey_t& key) bool pub_decoder_k1(const std::vector& buf, ec_pubkey_t& key); +/** + * Private-key encoders + */ +typedef size_t (*priv_encoder_t)(const ec_privkey_t&, unsigned char *); + +size_t priv_encoder_legacy(const ec_privkey_t& priv, unsigned char *buf); + +size_t priv_encoder_k1(const ec_privkey_t& priv, unsigned char *buf); + +/** + * Private-key decoders + */ +typedef bool (*priv_decoder_t)(const std::vector&, ec_privkey_t&); + +bool priv_decoder_legacy(const std::vector& buf, ec_privkey_t& priv); + +bool priv_decoder_k1(const std::vector& buf, ec_privkey_t& priv); + }} // namespace libeosio::internal #endif /* LIBEOSIO_CODEC_H */ diff --git a/src/wif/k1.cpp b/src/wif/k1.cpp index ea29f4e..3792dd1 100644 --- a/src/wif/k1.cpp +++ b/src/wif/k1.cpp @@ -61,4 +61,28 @@ bool pub_decoder_k1(const std::vector& buf, ec_pubkey_t& key) { return true; } +size_t priv_encoder_k1(const ec_privkey_t& priv, unsigned char *buf) { + checksum_t check = _checksum_suffix(priv.data(), EC_PRIVKEY_SIZE, "K1"); + + memcpy(buf, priv.data(), priv.size()); + memcpy(buf + EC_PRIVKEY_SIZE, check.data(), check.size()); + + return EC_PRIVKEY_SIZE + CHECKSUM_SIZE; +} + +bool priv_decoder_k1(const std::vector& buf, ec_privkey_t& priv) { + + if (buf.size() != EC_PRIVKEY_SIZE + CHECKSUM_SIZE) { + return false; + } + + checksum_t check = _checksum_suffix(buf.data(), EC_PRIVKEY_SIZE, "K1"); + if (memcmp(buf.data() + EC_PRIVKEY_SIZE, check.data(), CHECKSUM_SIZE)) { + return false; + } + + memcpy(priv.data(), buf.data(), priv.size()); + return true; +} + }} // namespace libeosio::internal \ No newline at end of file diff --git a/src/wif/legacy.cpp b/src/wif/legacy.cpp index 47a8251..96578e6 100644 --- a/src/wif/legacy.cpp +++ b/src/wif/legacy.cpp @@ -27,6 +27,8 @@ namespace libeosio { namespace internal { +#define PRIV_KEY_PREFIX 0x80 /* 0x80 for "Bitcoin mainnet". Always used by EOS. */ + void pub_encoder_legacy(const ec_pubkey_t& key, unsigned char *buf) { checksum_t check = checksum_ripemd160(key.data(), EC_PUBKEY_SIZE); @@ -45,4 +47,32 @@ bool pub_decoder_legacy(const std::vector& buf, ec_pubkey_t& key) return true; } +size_t priv_encoder_legacy(const ec_privkey_t& priv, unsigned char *buf) { + checksum_t check; + + buf[0] = PRIV_KEY_PREFIX; + memcpy(buf + 1, priv.data(), EC_PRIVKEY_SIZE); + check = checksum_sha256d(buf, 1 + EC_PRIVKEY_SIZE); + memcpy(buf + 1 + EC_PRIVKEY_SIZE, check.data(), check.size()); + + return 1 + EC_PRIVKEY_SIZE + CHECKSUM_SIZE; +} + +bool priv_decoder_legacy(const std::vector& buf, ec_privkey_t& priv) { + if (buf[0] != PRIV_KEY_PREFIX) { + return false; + } + + if (buf.size() != 1 + EC_PRIVKEY_SIZE + CHECKSUM_SIZE) { + return false; + } + + if (!checksum_validate(buf.data(), buf.size())) { + return false; + } + + memcpy(priv.data(), buf.data() + 1, priv.size()); + return true; +} + }} // namespace libeosio::internal \ No newline at end of file diff --git a/tests/WIF/priv_decode.cpp b/tests/WIF/priv_decode.cpp index d3d8c6a..5105e66 100644 --- a/tests/WIF/priv_decode.cpp +++ b/tests/WIF/priv_decode.cpp @@ -4,7 +4,7 @@ #include #include -TEST_CASE("WIF::wif_priv_decode") { +TEST_CASE("WIF::wif_priv_decode [legacy]") { struct testcase { std::string name; std::string key; @@ -29,4 +29,31 @@ TEST_CASE("WIF::wif_priv_decode") { } } +} + +TEST_CASE("WIF::wif_priv_decode [K1]") { + struct testcase { + std::string name; + std::string key; + libeosio::ec_privkey_t expected; + }; + + + std::vector tests { + { "one", "PVT_K1_6Mcb23muAxyXaSMhmB6B1mqkvLdWhtuFZmnZsxDczHRvQdp32", { 0x0C, 0x28, 0xFC, 0xA3, 0x86, 0xC7, 0xA2, 0x27, 0x60, 0x0B, 0x2F, 0xE5, 0x0B, 0x7C, 0xAE, 0x11, 0xEC, 0x86, 0xD3, 0xBF, 0x1F, 0xBE, 0x47, 0x1B, 0xE8, 0x98, 0x27, 0xE1, 0x9D, 0x72, 0xAA, 0x1D } }, + { "two", "PVT_K1_2DRBT8jmXT8k9ywNSSbufvhk1hLFhPzWJBpsE2jo12CDoFhcc1", { 0x9F, 0xE3, 0xE3, 0x2B, 0x3C, 0x4B, 0x6B, 0x91, 0x6E, 0x20, 0x6C, 0xB0, 0x91, 0xDF, 0x1F, 0x5E, 0x34, 0x32, 0x88, 0x0B, 0x41, 0x33, 0x86, 0xBD, 0xF2, 0x92, 0xFF, 0x23, 0x06, 0x43, 0xF2, 0x8C } }, + { "three", "PVT_K1_gJCsP4CwMv4gTkDXiZT8QFhs3NrSB7Sv22ANGrc8Svun9uC9C", { 0x59, 0x3A, 0x51, 0xB5, 0x5D, 0x56, 0xAA, 0xF0, 0x5B, 0xD9, 0xD1, 0x0E, 0x6B, 0x88, 0x6D, 0xF9, 0xC4, 0x37, 0x09, 0xB2, 0x4C, 0xEC, 0xBB, 0x63, 0x68, 0x92, 0xC2, 0x94, 0x31, 0x48, 0x71, 0x8C } } + }; + + for(auto it = tests.begin(); it != tests.end(); it++) { + + SUBCASE(it->name.c_str()) { + + libeosio::ec_privkey_t result; + + CHECK( libeosio::wif_priv_decode(result, it->key) ); + CHECK( result == it->expected ); + } + } + } \ No newline at end of file diff --git a/tests/WIF/priv_encode.cpp b/tests/WIF/priv_encode.cpp index 57a7173..bb08bd1 100644 --- a/tests/WIF/priv_encode.cpp +++ b/tests/WIF/priv_encode.cpp @@ -4,24 +4,48 @@ #include #include -TEST_CASE("WIF::wif_priv_encode") { +TEST_CASE("WIF::wif_priv_encode [Legacy]") { struct testcase { std::string name; + const std::string prefix; libeosio::ec_privkey_t key; std::string expected; }; std::vector tests { - { "one", { 0x0C, 0x28, 0xFC, 0xA3, 0x86, 0xC7, 0xA2, 0x27, 0x60, 0x0B, 0x2F, 0xE5, 0x0B, 0x7C, 0xAE, 0x11, 0xEC, 0x86, 0xD3, 0xBF, 0x1F, 0xBE, 0x47, 0x1B, 0xE8, 0x98, 0x27, 0xE1, 0x9D,0x72,0xAA,0x1D}, "5HueCGU8rMjxEXxiPuD5BDku4MkFqeZyd4dZ1jvhTVqvbTLvyTJ" }, - { "two", { 0x9F, 0xE3, 0xE3, 0x2B, 0x3C, 0x4B, 0x6B, 0x91, 0x6E, 0x20, 0x6C, 0xB0, 0x91, 0xDF, 0x1F, 0x5E, 0x34, 0x32, 0x88, 0x0B, 0x41, 0x33, 0x86, 0xBD, 0xF2, 0x92, 0xFF, 0x23, 0x06, 0x43, 0xF2, 0x8C}, "5K2hm8apqz281ANDQdtVzifpxcXFTqG5E7Fc6Q5V2ssqPRQ3urJ" }, - { "three", { 0x59, 0x3A, 0x51, 0xB5, 0x5D, 0x56, 0xAA, 0xF0, 0x5B, 0xD9, 0xD1, 0x0E, 0x6B, 0x88, 0x6D, 0xF9, 0xC4, 0x37, 0x09, 0xB2, 0x4C, 0xEC, 0xBB, 0x63, 0x68, 0x92, 0xC2, 0x94, 0x31, 0x48, 0x71, 0x8C}, "5JVanYq9HPvuKgr2FjATYB9NvTsJ4a3CAj5oPYKbr1Ja5MRLsZX" } + { "one", libeosio::WIF_PVT_LEG, { 0x0C, 0x28, 0xFC, 0xA3, 0x86, 0xC7, 0xA2, 0x27, 0x60, 0x0B, 0x2F, 0xE5, 0x0B, 0x7C, 0xAE, 0x11, 0xEC, 0x86, 0xD3, 0xBF, 0x1F, 0xBE, 0x47, 0x1B, 0xE8, 0x98, 0x27, 0xE1, 0x9D,0x72,0xAA,0x1D}, "5HueCGU8rMjxEXxiPuD5BDku4MkFqeZyd4dZ1jvhTVqvbTLvyTJ" }, + { "two", libeosio::WIF_PVT_LEG, { 0x9F, 0xE3, 0xE3, 0x2B, 0x3C, 0x4B, 0x6B, 0x91, 0x6E, 0x20, 0x6C, 0xB0, 0x91, 0xDF, 0x1F, 0x5E, 0x34, 0x32, 0x88, 0x0B, 0x41, 0x33, 0x86, 0xBD, 0xF2, 0x92, 0xFF, 0x23, 0x06, 0x43, 0xF2, 0x8C}, "5K2hm8apqz281ANDQdtVzifpxcXFTqG5E7Fc6Q5V2ssqPRQ3urJ" }, + { "three", libeosio::WIF_PVT_LEG, { 0x59, 0x3A, 0x51, 0xB5, 0x5D, 0x56, 0xAA, 0xF0, 0x5B, 0xD9, 0xD1, 0x0E, 0x6B, 0x88, 0x6D, 0xF9, 0xC4, 0x37, 0x09, 0xB2, 0x4C, 0xEC, 0xBB, 0x63, 0x68, 0x92, 0xC2, 0x94, 0x31, 0x48, 0x71, 0x8C}, "5JVanYq9HPvuKgr2FjATYB9NvTsJ4a3CAj5oPYKbr1Ja5MRLsZX" } }; for(auto it = tests.begin(); it != tests.end(); it++) { SUBCASE(it->name.c_str()) { - CHECK( libeosio::wif_priv_encode(it->key) == it->expected ); + CHECK( libeosio::wif_priv_encode(it->key, it->prefix) == it->expected ); + } + } +} + +TEST_CASE("WIF::wif_priv_encode [K1]") { + + struct testcase { + std::string name; + const std::string prefix; + libeosio::ec_privkey_t key; + std::string expected; + }; + + std::vector tests { + { "one", libeosio::WIF_PVT_K1, { 0x0C, 0x28, 0xFC, 0xA3, 0x86, 0xC7, 0xA2, 0x27, 0x60, 0x0B, 0x2F, 0xE5, 0x0B, 0x7C, 0xAE, 0x11, 0xEC, 0x86, 0xD3, 0xBF, 0x1F, 0xBE, 0x47, 0x1B, 0xE8, 0x98, 0x27, 0xE1, 0x9D,0x72,0xAA,0x1D}, "PVT_K1_6Mcb23muAxyXaSMhmB6B1mqkvLdWhtuFZmnZsxDczHRvQdp32" }, + { "two", libeosio::WIF_PVT_K1, { 0x9F, 0xE3, 0xE3, 0x2B, 0x3C, 0x4B, 0x6B, 0x91, 0x6E, 0x20, 0x6C, 0xB0, 0x91, 0xDF, 0x1F, 0x5E, 0x34, 0x32, 0x88, 0x0B, 0x41, 0x33, 0x86, 0xBD, 0xF2, 0x92, 0xFF, 0x23, 0x06, 0x43, 0xF2, 0x8C}, "PVT_K1_2DRBT8jmXT8k9ywNSSbufvhk1hLFhPzWJBpsE2jo12CDoFhcc1" }, + { "three", libeosio::WIF_PVT_K1, { 0x59, 0x3A, 0x51, 0xB5, 0x5D, 0x56, 0xAA, 0xF0, 0x5B, 0xD9, 0xD1, 0x0E, 0x6B, 0x88, 0x6D, 0xF9, 0xC4, 0x37, 0x09, 0xB2, 0x4C, 0xEC, 0xBB, 0x63, 0x68, 0x92, 0xC2, 0x94, 0x31, 0x48, 0x71, 0x8C}, "PVT_K1_gJCsP4CwMv4gTkDXiZT8QFhs3NrSB7Sv22ANGrc8Svun9uC9C" } + }; + + for(auto it = tests.begin(); it != tests.end(); it++) { + + SUBCASE(it->name.c_str()) { + CHECK( libeosio::wif_priv_encode(it->key, it->prefix) == it->expected ); } } } From 171db63de40e8916541066f612361d4af9fbc118 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Sat, 25 Mar 2023 18:14:51 +0100 Subject: [PATCH 11/58] include/libeosio/WIF.hpp: Adding WIF_SIG_K1 constant. --- include/libeosio/WIF.hpp | 2 +- src/WIF.cpp | 10 +++------- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/include/libeosio/WIF.hpp b/include/libeosio/WIF.hpp index 07f2a94..040c392 100644 --- a/include/libeosio/WIF.hpp +++ b/include/libeosio/WIF.hpp @@ -36,7 +36,7 @@ const std::string WIF_PUB_LEG = "EOS"; const std::string WIF_PUB_K1 = "PUB_K1_"; const std::string WIF_PVT_LEG = ""; const std::string WIF_PVT_K1 = "PVT_K1_"; - +const std::string WIF_SIG_K1 = "SIG_K1_"; /** * Encode an EC private key to WIF String. diff --git a/src/WIF.cpp b/src/WIF.cpp index 0ace329..aa07c3b 100644 --- a/src/WIF.cpp +++ b/src/WIF.cpp @@ -139,16 +139,12 @@ bool wif_sig_decode(ec_signature_t& sig, const std::string& data) { checksum_t checksum; std::vector buf; - if (data.substr(0, 7) != "SIG_K1_") { + if (data.substr(0, WIF_SIG_K1.length()) != WIF_SIG_K1) { // Invalid prefix return false; } - if (!base58_decode(data.c_str() + 7, buf)) { - return false; - } - - if (buf.size() != EC_SIGNATURE_SIZE + CHECKSUM_SIZE) { + if (!base58_decode(data.c_str() + WIF_SIG_K1.length(), buf)) { return false; } @@ -173,7 +169,7 @@ std::string wif_sig_encode(const ec_signature_t& sig) { memcpy(buf, sig.data(), sig.size()); memcpy(buf + EC_SIGNATURE_SIZE, check.data(), check.size()); - return "SIG_K1_" + base58_encode(buf, buf + sizeof(buf)); + return WIF_SIG_K1_" + base58_encode(buf, buf + sizeof(buf)); } } // namespace libeosio From abdd84f257326856ccbcf173726d4e5f955b4c70 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Sat, 25 Mar 2023 18:15:44 +0100 Subject: [PATCH 12/58] src/wif/codec.hpp: Adding signature encoders/decoders --- src/wif/codec.hpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/wif/codec.hpp b/src/wif/codec.hpp index 3e235d3..eb3e5ca 100644 --- a/src/wif/codec.hpp +++ b/src/wif/codec.hpp @@ -65,6 +65,20 @@ bool priv_decoder_legacy(const std::vector& buf, ec_privkey_t& pr bool priv_decoder_k1(const std::vector& buf, ec_privkey_t& priv); +/** + * Signature encoders + */ +typedef void (*sig_encoder_t)(const ec_signature_t& sig, unsigned char *buf); + +void sig_encoder_k1(const ec_signature_t& sig, unsigned char *buf); + +/** + * Signature decoders + */ +typedef bool (*sig_decoder_t)(const std::vector& buf, ec_signature_t& sig); + +bool sig_decoder_k1(const std::vector& buf, ec_signature_t& sig); + }} // namespace libeosio::internal #endif /* LIBEOSIO_CODEC_H */ From be6c98f1bd5574cc174f8d15a77a2e26dd4fd949 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Sat, 25 Mar 2023 18:16:10 +0100 Subject: [PATCH 13/58] src/wif/k1.cpp: implement sig_encoder_k1 and sig_decoder_k1 --- src/wif/k1.cpp | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/wif/k1.cpp b/src/wif/k1.cpp index 3792dd1..507ce99 100644 --- a/src/wif/k1.cpp +++ b/src/wif/k1.cpp @@ -85,4 +85,34 @@ bool priv_decoder_k1(const std::vector& buf, ec_privkey_t& priv) return true; } +void sig_encoder_k1(const ec_signature_t& sig, unsigned char *buf) { + + checksum_t check = _checksum_suffix(sig.data(), EC_SIGNATURE_SIZE, "K1"); + + memcpy(buf, sig.data(), sig.size()); + memcpy(buf + EC_SIGNATURE_SIZE, check.data(), check.size()); +} + +bool sig_decoder_k1(const std::vector& buf, ec_signature_t& sig) { + + checksum_t check; + + if (buf.size() != EC_SIGNATURE_SIZE + CHECKSUM_SIZE) { + return false; + } + + // Calculate checksum + check = _checksum_suffix(buf.data(), EC_SIGNATURE_SIZE, "K1"); + + // And validate + if (memcmp(buf.data() + EC_SIGNATURE_SIZE, check.data(), CHECKSUM_SIZE)) { + return false; + } + + // Copy data to output + memcpy(sig.data(), buf.data(), sig.size()); + return true; +} + + }} // namespace libeosio::internal \ No newline at end of file From 9819b2b94df9bc069bc76b5209c1ee6759004f47 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Sat, 25 Mar 2023 18:16:32 +0100 Subject: [PATCH 14/58] src/WIF.cpp: use signature encoder/decoder. --- src/WIF.cpp | 33 +++------------------------------ 1 file changed, 3 insertions(+), 30 deletions(-) diff --git a/src/WIF.cpp b/src/WIF.cpp index aa07c3b..44869f2 100644 --- a/src/WIF.cpp +++ b/src/WIF.cpp @@ -30,20 +30,6 @@ namespace libeosio { -// Just to make it "harder" the calculated checksum for a signature (k1) and pub/priv keys in k1/r1 format. -// has a suffix that is not present in the WIF encoded string. -// So this function is a quick hack to calculate it. -// -// Should implement and use Init/Update/Finalize hash functions to do it inplace. -checksum_t _checksum_suffix(const unsigned char *in, size_t size, const char *suffix) { - unsigned char buf[size + 2]; - - memcpy(buf, in, size); - memcpy(buf + size, suffix, 2); - - return checksum_ripemd160(buf, size + 2); -} - std::string wif_priv_encode(const ec_privkey_t& priv, const std::string& prefix) { checksum_t check; @@ -148,28 +134,15 @@ bool wif_sig_decode(ec_signature_t& sig, const std::string& data) { return false; } - // Calculate checksum - checksum = _checksum_suffix(buf.data(), EC_SIGNATURE_SIZE, "K1"); - - // And validate - if (memcmp(buf.data() + EC_SIGNATURE_SIZE, checksum.data(), CHECKSUM_SIZE)) { - return false; - } - - // Copy data to output - memcpy(sig.data(), buf.data(), sig.size()); - return true; + return internal::sig_decoder_k1(buf, sig); } std::string wif_sig_encode(const ec_signature_t& sig) { unsigned char buf[EC_SIGNATURE_SIZE + CHECKSUM_SIZE]; - checksum_t check = _checksum_suffix(sig.data(), EC_SIGNATURE_SIZE, "K1"); + internal::sig_encoder_k1(sig, buf); - memcpy(buf, sig.data(), sig.size()); - memcpy(buf + EC_SIGNATURE_SIZE, check.data(), check.size()); - - return WIF_SIG_K1_" + base58_encode(buf, buf + sizeof(buf)); + return WIF_SIG_K1 + base58_encode(buf, buf + sizeof(buf)); } } // namespace libeosio From 815ab2569f39e7e780b7f087ece1dd2fc8c9618f Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Sat, 25 Mar 2023 19:54:05 +0100 Subject: [PATCH 15/58] src/wif/k1.cpp: in _checksum_suffix() change array to std::vector as MSVC does not like variable size c-arrays. --- src/wif/k1.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/wif/k1.cpp b/src/wif/k1.cpp index 507ce99..cb32283 100644 --- a/src/wif/k1.cpp +++ b/src/wif/k1.cpp @@ -23,6 +23,7 @@ */ #include +#include #include "codec.hpp" namespace libeosio { namespace internal { @@ -33,12 +34,12 @@ namespace libeosio { namespace internal { // // Should implement and use Init/Update/Finalize hash functions to do it inplace. checksum_t _checksum_suffix(const unsigned char *in, size_t size, const char *suffix) { - unsigned char buf[size + 2]; + std::vector buf(size + 2); - memcpy(buf, in, size); - memcpy(buf + size, suffix, 2); + memcpy(buf.data(), in, size); + memcpy(buf.data() + size, suffix, 2); - return checksum_ripemd160(buf, size + 2); + return checksum_ripemd160(buf.data(), buf.size()); } void pub_encoder_k1(const ec_pubkey_t& key, unsigned char *buf) { From abecabba995c647a387266f6264d3f89426bd6b7 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Sun, 26 Mar 2023 19:25:55 +0200 Subject: [PATCH 16/58] include/libeosio/hash.hpp: remove nested struct from ripemd160_t and sha256_t and just use a array directly. --- include/libeosio/hash.hpp | 4 +- src/libsecp256k1/ecdsa.cpp | 6 +-- src/openssl/ecdsa.cpp | 8 ++-- src/openssl/hash.cpp | 8 ++-- tests/ec/ecdsa_recover.cpp | 40 +++++++---------- tests/ec/ecdsa_sign.cpp | 60 ++++++++++--------------- tests/ec/ecdsa_verify.cpp | 90 +++++++++++++++----------------------- 7 files changed, 89 insertions(+), 127 deletions(-) diff --git a/include/libeosio/hash.hpp b/include/libeosio/hash.hpp index 6503b29..be9ac75 100644 --- a/include/libeosio/hash.hpp +++ b/include/libeosio/hash.hpp @@ -31,8 +31,8 @@ namespace libeosio { /** * Hashes */ -typedef struct { unsigned char data[20]; } ripemd160_t; -typedef struct { unsigned char data[32]; } sha256_t; +typedef unsigned char ripemd160_t[20]; +typedef unsigned char sha256_t[32]; /** * sha256 hashing function. diff --git a/src/libsecp256k1/ecdsa.cpp b/src/libsecp256k1/ecdsa.cpp index ab09fa2..2a5e235 100644 --- a/src/libsecp256k1/ecdsa.cpp +++ b/src/libsecp256k1/ecdsa.cpp @@ -50,7 +50,7 @@ int ecdsa_sign(const ec_privkey_t& key, const sha256_t* digest, ec_signature_t& int v = 0; secp256k1_ecdsa_recoverable_signature s; - if (!secp256k1_ecdsa_sign_recoverable(ctx, &s, digest->data, key.data(), extended_nonce_function, &counter)) { + if (!secp256k1_ecdsa_sign_recoverable(ctx, &s, (const unsigned char*) digest, key.data(), extended_nonce_function, &counter)) { return -1; } @@ -86,7 +86,7 @@ int ecdsa_verify(const sha256_t* digest, const ec_signature_t& sig, const ec_pub // Verify secp256k1_ecdsa_recoverable_signature_convert(ctx, &ec_sig, &ec_rec_sig); - return secp256k1_ecdsa_verify(ctx, &ec_sig, digest->data, &pubkey) > 0 ? 0 : -1; + return secp256k1_ecdsa_verify(ctx, &ec_sig, (const unsigned char*) digest, &pubkey) > 0 ? 0 : -1; } int ecdsa_recover(const sha256_t* digest, const ec_signature_t& sig, ec_pubkey_t& pubkey) { @@ -106,7 +106,7 @@ int ecdsa_recover(const sha256_t* digest, const ec_signature_t& sig, ec_pubkey_t // Recover public key - if (!secp256k1_ecdsa_recover(ctx, &ec_pubkey, &ec_sig, digest->data)) { + if (!secp256k1_ecdsa_recover(ctx, &ec_pubkey, &ec_sig, (const unsigned char*) digest)) { return -1; } diff --git a/src/openssl/ecdsa.cpp b/src/openssl/ecdsa.cpp index a897166..cf5c2f4 100644 --- a/src/openssl/ecdsa.cpp +++ b/src/openssl/ecdsa.cpp @@ -61,7 +61,7 @@ int ecdsa_sign(const ec_privkey_t& key, const sha256_t* digest, ec_signature_t& const BIGNUM *r, *s; EC_KEY* tmpk; - ecdsa_sig = ECDSA_do_sign(digest->data, 32, ec_key); + ecdsa_sig = ECDSA_do_sign((const unsigned char*) digest, 32, ec_key); if (ecdsa_sig == NULL) { goto err2; } @@ -72,7 +72,7 @@ int ecdsa_sign(const ec_privkey_t& key, const sha256_t* digest, ec_signature_t& tmpk = EC_KEY_new_by_curve_name( NID_secp256k1 ); for (int i = 0; i < 4; i++) { - if (ECDSA_SIG_recover_key_GFp(tmpk, r, s, digest->data, sizeof(digest->data), i, 1) == 1) { + if (ECDSA_SIG_recover_key_GFp(tmpk, r, s, (const unsigned char*) digest, 32, i, 1) == 1) { const EC_POINT *p = EC_KEY_get0_public_key(tmpk); // Compare public keys @@ -140,7 +140,7 @@ int ecdsa_verify(const sha256_t* digest, const ec_signature_t& sig, const ec_pub goto err3; } - if (ECDSA_do_verify(digest->data, 32, ecdsa_sig, ec_key) == 1) { + if (ECDSA_do_verify((const unsigned char*) digest, 32, ecdsa_sig, ec_key) == 1) { ret = 0; } @@ -164,7 +164,7 @@ int ecdsa_recover(const sha256_t* digest, const ec_signature_t& sig, ec_pubkey_t ECDSA_SIG_unserialize_rs(sig.data(), &r, &s, &recid); // Recover public key. - if (ECDSA_SIG_recover_key_GFp(ec_key, r, s, digest->data, 32, recid, 1) == 1) { + if (ECDSA_SIG_recover_key_GFp(ec_key, r, s, (const unsigned char*) digest, 32, recid, 1) == 1) { // Encode point to binary compressed format. const EC_POINT *p = EC_KEY_get0_public_key(ec_key); diff --git a/src/openssl/hash.cpp b/src/openssl/hash.cpp index 33426f7..a8dc0cd 100644 --- a/src/openssl/hash.cpp +++ b/src/openssl/hash.cpp @@ -28,16 +28,16 @@ namespace libeosio { sha256_t* sha256(const unsigned char *data, std::size_t len, sha256_t* out) { - return (sha256_t *) SHA256(data, len, out->data); + return (sha256_t *) SHA256(data, len, (unsigned char*) out); } sha256_t* sha256d(const unsigned char *data, std::size_t len, sha256_t* out) { - SHA256(data, len, out->data); - return (sha256_t *) SHA256(out->data, 32, out->data); + SHA256(data, len, (unsigned char*) out); + return (sha256_t *) SHA256((unsigned char*) out, 32, (unsigned char*) out); } ripemd160_t* ripemd160(const unsigned char *data, std::size_t len, ripemd160_t* out) { - return (ripemd160_t *) RIPEMD160(data, len, out->data); + return (ripemd160_t *) RIPEMD160(data, len, (unsigned char*) out); } } // namespace libeosio diff --git a/tests/ec/ecdsa_recover.cpp b/tests/ec/ecdsa_recover.cpp index 4ade153..59b7a37 100644 --- a/tests/ec/ecdsa_recover.cpp +++ b/tests/ec/ecdsa_recover.cpp @@ -16,12 +16,10 @@ TEST_CASE("ec::ecdsa_recover") { { "valid #1", { - { - 0xab, 0x53, 0x0a, 0x13, 0xe4, 0x59, 0x14, 0x98, - 0x2b, 0x79, 0xf9, 0xb7, 0xe3, 0xfb, 0xa9, 0x94, - 0xcf, 0xd1, 0xf3, 0xfb, 0x22, 0xf7, 0x1c, 0xea, - 0x1a, 0xfb, 0xf0, 0x2b, 0x46, 0x0c, 0x6d, 0x1d - }, + 0xab, 0x53, 0x0a, 0x13, 0xe4, 0x59, 0x14, 0x98, + 0x2b, 0x79, 0xf9, 0xb7, 0xe3, 0xfb, 0xa9, 0x94, + 0xcf, 0xd1, 0xf3, 0xfb, 0x22, 0xf7, 0x1c, 0xea, + 0x1a, 0xfb, 0xf0, 0x2b, 0x46, 0x0c, 0x6d, 0x1d }, // SIG_K1_KdgBih1poWj8DYZXwLxMdjaHMzYhuAVp7XshR9ZjrZSubZwsgSpiyUKXu44NmCtKgRFswmqKaioWLTuGZrXwYPsSNCSyyr { @@ -36,12 +34,10 @@ TEST_CASE("ec::ecdsa_recover") { { "valid #2", { - { - 0x19, 0xd3, 0xe0, 0x8b, 0xbb, 0xad, 0x5f, 0x02, - 0x35, 0xa8, 0xa8, 0xf8, 0x1a, 0x7f, 0xa1, 0xe0, - 0xf8, 0x50, 0xdd, 0x39, 0x12, 0xe3, 0xc6, 0x55, - 0xb4, 0x35, 0xd4, 0x78, 0x6b, 0x93, 0x64, 0xa6 - }, + 0x19, 0xd3, 0xe0, 0x8b, 0xbb, 0xad, 0x5f, 0x02, + 0x35, 0xa8, 0xa8, 0xf8, 0x1a, 0x7f, 0xa1, 0xe0, + 0xf8, 0x50, 0xdd, 0x39, 0x12, 0xe3, 0xc6, 0x55, + 0xb4, 0x35, 0xd4, 0x78, 0x6b, 0x93, 0x64, 0xa6 }, // SIG_K1_K4XXx6oSYBzcwzscMstvSxruxdkTCinyN9dnRo4DuBkCCpQbCJQcJmbE7aAmNueBYCccHyyDK5JDfMpvewRF2rGUFtSE2y { @@ -56,12 +52,10 @@ TEST_CASE("ec::ecdsa_recover") { { "valid #3", { - { - 0x1b, 0x01, 0x0b, 0xe5, 0xce, 0x6a, 0x49, 0xc7, - 0xcd, 0x04, 0x86, 0x0d, 0xef, 0x63, 0x1c, 0x6a, - 0xcc, 0xd5, 0x17, 0x47, 0x2e, 0x74, 0x5b, 0xa6, - 0xc8, 0xaf, 0x26, 0x1b, 0x15, 0x7e, 0x11, 0xec - }, + 0x1b, 0x01, 0x0b, 0xe5, 0xce, 0x6a, 0x49, 0xc7, + 0xcd, 0x04, 0x86, 0x0d, 0xef, 0x63, 0x1c, 0x6a, + 0xcc, 0xd5, 0x17, 0x47, 0x2e, 0x74, 0x5b, 0xa6, + 0xc8, 0xaf, 0x26, 0x1b, 0x15, 0x7e, 0x11, 0xec }, // SIG_K1_K54CVeQjFREm9Z92jutWESZWb9WQfCRZ2KfMtisfsnxedppeSMxTrZ9fYDLiJTfE79zvLCHb5NysAEcNdh7HiBvtU4Ahhh { @@ -76,12 +70,10 @@ TEST_CASE("ec::ecdsa_recover") { { "not valid #1 (non valid signature)", { - { - 0xde, 0x01, 0x64, 0x03, 0x39, 0x01, 0x66, 0x8b, - 0xa0, 0x39, 0xef, 0x31, 0x61, 0xc7, 0xc8, 0x9d, - 0x15, 0x4b, 0xc6, 0x7b, 0x99, 0x5c, 0xba, 0x9b, - 0x23, 0x8a, 0x76, 0x4b, 0x81, 0xf2, 0xff, 0xeb - }, + 0xde, 0x01, 0x64, 0x03, 0x39, 0x01, 0x66, 0x8b, + 0xa0, 0x39, 0xef, 0x31, 0x61, 0xc7, 0xc8, 0x9d, + 0x15, 0x4b, 0xc6, 0x7b, 0x99, 0x5c, 0xba, 0x9b, + 0x23, 0x8a, 0x76, 0x4b, 0x81, 0xf2, 0xff, 0xeb }, { 0x1f, 0x4b, 0xe9, 0x04, 0x20, 0xfa, 0x7b, 0x9d, 0x56, 0xc6, 0x00, 0x5c, 0x83, 0x70, 0xa9, 0x26, 0x41, 0x7d, 0xe8, 0xeb, 0xe7, 0x75, 0xea, 0x6f, 0x75, 0xa7, 0x7c, 0x98, 0x10, 0x27, 0xbf, 0xce, diff --git a/tests/ec/ecdsa_sign.cpp b/tests/ec/ecdsa_sign.cpp index 993e5c4..7c4b0e2 100644 --- a/tests/ec/ecdsa_sign.cpp +++ b/tests/ec/ecdsa_sign.cpp @@ -19,12 +19,10 @@ TEST_CASE("ec::ecdsa_sign") { // Public key: EOS6zjfj9Xjk9CYoucZDptdDZ6317eZd622pVvaYtv5q6gwEs9icD { 0x03, 0x15, 0x93, 0x8a, 0x8e, 0x1d, 0x57, 0x84, 0x9f, 0xab, 0x07, 0x18, 0x67, 0xb5, 0x0c, 0xda, 0xb0, 0x77, 0x62, 0x29, 0xb6, 0x43, 0xb8, 0x67, 0x56, 0xc7, 0xb3, 0xe8, 0x7f, 0xe6, 0x08, 0xf8, 0x4b }, { - { - 0xab, 0x53, 0x0a, 0x13, 0xe4, 0x59, 0x14, 0x98, - 0x2b, 0x79, 0xf9, 0xb7, 0xe3, 0xfb, 0xa9, 0x94, - 0xcf, 0xd1, 0xf3, 0xfb, 0x22, 0xf7, 0x1c, 0xea, - 0x1a, 0xfb, 0xf0, 0x2b, 0x46, 0x0c, 0x6d, 0x1d - }, + 0xab, 0x53, 0x0a, 0x13, 0xe4, 0x59, 0x14, 0x98, + 0x2b, 0x79, 0xf9, 0xb7, 0xe3, 0xfb, 0xa9, 0x94, + 0xcf, 0xd1, 0xf3, 0xfb, 0x22, 0xf7, 0x1c, 0xea, + 0x1a, 0xfb, 0xf0, 0x2b, 0x46, 0x0c, 0x6d, 0x1d }, }, { @@ -34,12 +32,10 @@ TEST_CASE("ec::ecdsa_sign") { // Public key: EOS6tVtKhTpM6yU7kkiRz1AecDJPcBQo2w4x4oytJbJi5PMV2Rcw2 { 0x03, 0x07, 0x69, 0xbb, 0xa5, 0x2c, 0xd2, 0xe1, 0x3b, 0x3e, 0x0a, 0x40, 0xb3, 0xa2, 0x44, 0xad, 0x71, 0x6e, 0x32, 0x64, 0x9c, 0x3a, 0x64, 0x27, 0x4f, 0x31, 0x86, 0x8a, 0x4c, 0x69, 0x58, 0x86, 0x49 }, { - { - 0x19, 0xd3, 0xe0, 0x8b, 0xbb, 0xad, 0x5f, 0x02, - 0x35, 0xa8, 0xa8, 0xf8, 0x1a, 0x7f, 0xa1, 0xe0, - 0xf8, 0x50, 0xdd, 0x39, 0x12, 0xe3, 0xc6, 0x55, - 0xb4, 0x35, 0xd4, 0x78, 0x6b, 0x93, 0x64, 0xa6 - }, + 0x19, 0xd3, 0xe0, 0x8b, 0xbb, 0xad, 0x5f, 0x02, + 0x35, 0xa8, 0xa8, 0xf8, 0x1a, 0x7f, 0xa1, 0xe0, + 0xf8, 0x50, 0xdd, 0x39, 0x12, 0xe3, 0xc6, 0x55, + 0xb4, 0x35, 0xd4, 0x78, 0x6b, 0x93, 0x64, 0xa6 }, }, { @@ -49,12 +45,10 @@ TEST_CASE("ec::ecdsa_sign") { // Public key: EOS7Xtaa4y44gYapth4MH5bdtCvdtQvVLdsW7a8thVAuvNAkj8X7i { 0x03, 0x5c, 0x50, 0x81, 0xef, 0xa6, 0x46, 0x00, 0x5a, 0xb9, 0xd8, 0x2b, 0xfe, 0xd8, 0xe1, 0x6d, 0x15, 0x42, 0x9e, 0x9a, 0xcb, 0xc9, 0xd6, 0xb3, 0x2e, 0x5a, 0xe3, 0xed, 0xa5, 0x8d, 0x6a, 0x42, 0x6c }, { - { - 0x1b, 0x01, 0x0b, 0xe5, 0xce, 0x6a, 0x49, 0xc7, - 0xcd, 0x04, 0x86, 0x0d, 0xef, 0x63, 0x1c, 0x6a, - 0xcc, 0xd5, 0x17, 0x47, 0x2e, 0x74, 0x5b, 0xa6, - 0xc8, 0xaf, 0x26, 0x1b, 0x15, 0x7e, 0x11, 0xec - }, + 0x1b, 0x01, 0x0b, 0xe5, 0xce, 0x6a, 0x49, 0xc7, + 0xcd, 0x04, 0x86, 0x0d, 0xef, 0x63, 0x1c, 0x6a, + 0xcc, 0xd5, 0x17, 0x47, 0x2e, 0x74, 0x5b, 0xa6, + 0xc8, 0xaf, 0x26, 0x1b, 0x15, 0x7e, 0x11, 0xec }, }, { @@ -64,12 +58,10 @@ TEST_CASE("ec::ecdsa_sign") { // Public key: EOS6E12fqQqWLYJS32ffB6LaQYxyDXUQSPfMTMnj6tc5bgntZKcBy { 0x02, 0xaf, 0xff, 0xeb, 0xef, 0x47, 0x70, 0x58, 0x2f, 0x9b, 0x66, 0x6c, 0xe0, 0xea, 0x84, 0x32, 0x41, 0xa0, 0x94, 0x36, 0x30, 0x9b, 0xfc, 0xdb, 0x9a, 0x58, 0xdd, 0x0e, 0xe6, 0x3e, 0xd4, 0x5a, 0xcd }, { - { - 0xbc, 0x83, 0xbe, 0xe1, 0x73, 0x82, 0xfb, 0x02, - 0x71, 0x25, 0x3b, 0xf5, 0x39, 0x32, 0x55, 0x4e, - 0x01, 0x28, 0x5d, 0xf4, 0x02, 0xe8, 0xa2, 0x92, - 0x04, 0xf2, 0x95, 0xbc, 0xfa, 0xed, 0x8f, 0xaa - }, + 0xbc, 0x83, 0xbe, 0xe1, 0x73, 0x82, 0xfb, 0x02, + 0x71, 0x25, 0x3b, 0xf5, 0x39, 0x32, 0x55, 0x4e, + 0x01, 0x28, 0x5d, 0xf4, 0x02, 0xe8, 0xa2, 0x92, + 0x04, 0xf2, 0x95, 0xbc, 0xfa, 0xed, 0x8f, 0xaa }, }, { @@ -79,12 +71,10 @@ TEST_CASE("ec::ecdsa_sign") { // Public key: EOS6uqJC6F7eEMq7SHREhTzRoAT7uumrmHDDYC595CJKeBgPvPojd { 0x03, 0x0a, 0x71, 0x8e, 0x48, 0x1a, 0x7a, 0x55, 0x84, 0xb9, 0xaf, 0x24, 0xca, 0x8f, 0x85, 0xcd, 0x0f, 0x55, 0x5c, 0xcf, 0xb3, 0x7b, 0x39, 0x5b, 0xa5, 0xfc, 0xb9, 0xaf, 0x26, 0xc7, 0xc7, 0x88, 0x7b }, { - { - 0xfc, 0xb2, 0x0d, 0xa1, 0x22, 0x6c, 0xcc, 0x59, - 0x26, 0x66, 0xba, 0x57, 0xeb, 0x4a, 0xfa, 0x20, - 0xff, 0x40, 0x7b, 0xc7, 0x3c, 0x35, 0xb8, 0xaa, - 0x72, 0x3e, 0x94, 0xcc, 0x05, 0xe1, 0x51, 0xc7 - }, + 0xfc, 0xb2, 0x0d, 0xa1, 0x22, 0x6c, 0xcc, 0x59, + 0x26, 0x66, 0xba, 0x57, 0xeb, 0x4a, 0xfa, 0x20, + 0xff, 0x40, 0x7b, 0xc7, 0x3c, 0x35, 0xb8, 0xaa, + 0x72, 0x3e, 0x94, 0xcc, 0x05, 0xe1, 0x51, 0xc7 }, }, { @@ -94,12 +84,10 @@ TEST_CASE("ec::ecdsa_sign") { // Public key: EOS5YiBwqnFXqeb5hCmwV9bLHp6Jg5hVnRjzYVApf2DXyRGr7B7kZ { 0x02, 0x56, 0xc9, 0x41, 0x90, 0x44, 0x8a, 0xcc, 0x89, 0x91, 0x79, 0xaf, 0x4e, 0x3a, 0x72, 0xa7, 0x24, 0x86, 0x7f, 0xd8, 0x03, 0x07, 0x04, 0x30, 0xd3, 0xf3, 0x6b, 0x20, 0x94, 0x85, 0x78, 0xfc, 0x38 }, { - { - 0xd2, 0xfa, 0xa6, 0x97, 0x12, 0xd7, 0x04, 0x05, - 0xe8, 0x60, 0x7e, 0x86, 0x73, 0x69, 0x05, 0x90, - 0x97, 0xa2, 0x57, 0xee, 0x12, 0x4b, 0x80, 0x13, - 0x04, 0xfa, 0x7d, 0x70, 0xe7, 0xdc, 0x86, 0xb2 - }, + 0xd2, 0xfa, 0xa6, 0x97, 0x12, 0xd7, 0x04, 0x05, + 0xe8, 0x60, 0x7e, 0x86, 0x73, 0x69, 0x05, 0x90, + 0x97, 0xa2, 0x57, 0xee, 0x12, 0x4b, 0x80, 0x13, + 0x04, 0xfa, 0x7d, 0x70, 0xe7, 0xdc, 0x86, 0xb2 }, }, }; diff --git a/tests/ec/ecdsa_verify.cpp b/tests/ec/ecdsa_verify.cpp index 9f0e896..7959f12 100644 --- a/tests/ec/ecdsa_verify.cpp +++ b/tests/ec/ecdsa_verify.cpp @@ -16,12 +16,10 @@ TEST_CASE("ec::ecdsa_verify") { { "valid #1", { - { - 0xab, 0x53, 0x0a, 0x13, 0xe4, 0x59, 0x14, 0x98, - 0x2b, 0x79, 0xf9, 0xb7, 0xe3, 0xfb, 0xa9, 0x94, - 0xcf, 0xd1, 0xf3, 0xfb, 0x22, 0xf7, 0x1c, 0xea, - 0x1a, 0xfb, 0xf0, 0x2b, 0x46, 0x0c, 0x6d, 0x1d - }, + 0xab, 0x53, 0x0a, 0x13, 0xe4, 0x59, 0x14, 0x98, + 0x2b, 0x79, 0xf9, 0xb7, 0xe3, 0xfb, 0xa9, 0x94, + 0xcf, 0xd1, 0xf3, 0xfb, 0x22, 0xf7, 0x1c, 0xea, + 0x1a, 0xfb, 0xf0, 0x2b, 0x46, 0x0c, 0x6d, 0x1d }, // Public Key: EOS6zjfj9Xjk9CYoucZDptdDZ6317eZd622pVvaYtv5q6gwEs9icD { 0x03, 0x15, 0x93, 0x8a, 0x8e, 0x1d, 0x57, 0x84, 0x9f, 0xab, 0x07, 0x18, 0x67, 0xb5, 0x0c, 0xda, 0xb0, 0x77, 0x62, 0x29, 0xb6, 0x43, 0xb8, 0x67, 0x56, 0xc7, 0xb3, 0xe8, 0x7f, 0xe6, 0x08, 0xf8, 0x4b }, @@ -36,12 +34,10 @@ TEST_CASE("ec::ecdsa_verify") { { "valid #2 (generated by openssl)", { - { - 0xab, 0x53, 0x0a, 0x13, 0xe4, 0x59, 0x14, 0x98, - 0x2b, 0x79, 0xf9, 0xb7, 0xe3, 0xfb, 0xa9, 0x94, - 0xcf, 0xd1, 0xf3, 0xfb, 0x22, 0xf7, 0x1c, 0xea, - 0x1a, 0xfb, 0xf0, 0x2b, 0x46, 0x0c, 0x6d, 0x1d - }, + 0xab, 0x53, 0x0a, 0x13, 0xe4, 0x59, 0x14, 0x98, + 0x2b, 0x79, 0xf9, 0xb7, 0xe3, 0xfb, 0xa9, 0x94, + 0xcf, 0xd1, 0xf3, 0xfb, 0x22, 0xf7, 0x1c, 0xea, + 0x1a, 0xfb, 0xf0, 0x2b, 0x46, 0x0c, 0x6d, 0x1d }, // Public Key: EOS6zjfj9Xjk9CYoucZDptdDZ6317eZd622pVvaYtv5q6gwEs9icD { 0x03, 0x15, 0x93, 0x8a, 0x8e, 0x1d, 0x57, 0x84, 0x9f, 0xab, 0x07, 0x18, 0x67, 0xb5, 0x0c, 0xda, 0xb0, 0x77, 0x62, 0x29, 0xb6, 0x43, 0xb8, 0x67, 0x56, 0xc7, 0xb3, 0xe8, 0x7f, 0xe6, 0x08, 0xf8, 0x4b }, @@ -56,12 +52,10 @@ TEST_CASE("ec::ecdsa_verify") { { "valid #3 (generated by eos-go)", { - { - 0xab, 0x53, 0x0a, 0x13, 0xe4, 0x59, 0x14, 0x98, - 0x2b, 0x79, 0xf9, 0xb7, 0xe3, 0xfb, 0xa9, 0x94, - 0xcf, 0xd1, 0xf3, 0xfb, 0x22, 0xf7, 0x1c, 0xea, - 0x1a, 0xfb, 0xf0, 0x2b, 0x46, 0x0c, 0x6d, 0x1d - }, + 0xab, 0x53, 0x0a, 0x13, 0xe4, 0x59, 0x14, 0x98, + 0x2b, 0x79, 0xf9, 0xb7, 0xe3, 0xfb, 0xa9, 0x94, + 0xcf, 0xd1, 0xf3, 0xfb, 0x22, 0xf7, 0x1c, 0xea, + 0x1a, 0xfb, 0xf0, 0x2b, 0x46, 0x0c, 0x6d, 0x1d }, // Public Key: EOS6zjfj9Xjk9CYoucZDptdDZ6317eZd622pVvaYtv5q6gwEs9icD { 0x03, 0x15, 0x93, 0x8a, 0x8e, 0x1d, 0x57, 0x84, 0x9f, 0xab, 0x07, 0x18, 0x67, 0xb5, 0x0c, 0xda, 0xb0, 0x77, 0x62, 0x29, 0xb6, 0x43, 0xb8, 0x67, 0x56, 0xc7, 0xb3, 0xe8, 0x7f, 0xe6, 0x08, 0xf8, 0x4b }, @@ -76,12 +70,10 @@ TEST_CASE("ec::ecdsa_verify") { { "valid #4 (generated by eosjs)", { - { - 0xab, 0x53, 0x0a, 0x13, 0xe4, 0x59, 0x14, 0x98, - 0x2b, 0x79, 0xf9, 0xb7, 0xe3, 0xfb, 0xa9, 0x94, - 0xcf, 0xd1, 0xf3, 0xfb, 0x22, 0xf7, 0x1c, 0xea, - 0x1a, 0xfb, 0xf0, 0x2b, 0x46, 0x0c, 0x6d, 0x1d - }, + 0xab, 0x53, 0x0a, 0x13, 0xe4, 0x59, 0x14, 0x98, + 0x2b, 0x79, 0xf9, 0xb7, 0xe3, 0xfb, 0xa9, 0x94, + 0xcf, 0xd1, 0xf3, 0xfb, 0x22, 0xf7, 0x1c, 0xea, + 0x1a, 0xfb, 0xf0, 0x2b, 0x46, 0x0c, 0x6d, 0x1d }, // Public Key: EOS6zjfj9Xjk9CYoucZDptdDZ6317eZd622pVvaYtv5q6gwEs9icD { 0x03, 0x15, 0x93, 0x8a, 0x8e, 0x1d, 0x57, 0x84, 0x9f, 0xab, 0x07, 0x18, 0x67, 0xb5, 0x0c, 0xda, 0xb0, 0x77, 0x62, 0x29, 0xb6, 0x43, 0xb8, 0x67, 0x56, 0xc7, 0xb3, 0xe8, 0x7f, 0xe6, 0x08, 0xf8, 0x4b }, @@ -95,12 +87,10 @@ TEST_CASE("ec::ecdsa_verify") { { "valid #4", { - { - 0x19, 0xd3, 0xe0, 0x8b, 0xbb, 0xad, 0x5f, 0x02, - 0x35, 0xa8, 0xa8, 0xf8, 0x1a, 0x7f, 0xa1, 0xe0, - 0xf8, 0x50, 0xdd, 0x39, 0x12, 0xe3, 0xc6, 0x55, - 0xb4, 0x35, 0xd4, 0x78, 0x6b, 0x93, 0x64, 0xa6 - }, + 0x19, 0xd3, 0xe0, 0x8b, 0xbb, 0xad, 0x5f, 0x02, + 0x35, 0xa8, 0xa8, 0xf8, 0x1a, 0x7f, 0xa1, 0xe0, + 0xf8, 0x50, 0xdd, 0x39, 0x12, 0xe3, 0xc6, 0x55, + 0xb4, 0x35, 0xd4, 0x78, 0x6b, 0x93, 0x64, 0xa6 }, // Public Key: EOS6tVtKhTpM6yU7kkiRz1AecDJPcBQo2w4x4oytJbJi5PMV2Rcw2 { 0x03, 0x07, 0x69, 0xbb, 0xa5, 0x2c, 0xd2, 0xe1, 0x3b, 0x3e, 0x0a, 0x40, 0xb3, 0xa2, 0x44, 0xad, 0x71, 0x6e, 0x32, 0x64, 0x9c, 0x3a, 0x64, 0x27, 0x4f, 0x31, 0x86, 0x8a, 0x4c, 0x69, 0x58, 0x86, 0x49 }, @@ -115,12 +105,10 @@ TEST_CASE("ec::ecdsa_verify") { { "valid #5", { - { - 0x1b, 0x01, 0x0b, 0xe5, 0xce, 0x6a, 0x49, 0xc7, - 0xcd, 0x04, 0x86, 0x0d, 0xef, 0x63, 0x1c, 0x6a, - 0xcc, 0xd5, 0x17, 0x47, 0x2e, 0x74, 0x5b, 0xa6, - 0xc8, 0xaf, 0x26, 0x1b, 0x15, 0x7e, 0x11, 0xec - }, + 0x1b, 0x01, 0x0b, 0xe5, 0xce, 0x6a, 0x49, 0xc7, + 0xcd, 0x04, 0x86, 0x0d, 0xef, 0x63, 0x1c, 0x6a, + 0xcc, 0xd5, 0x17, 0x47, 0x2e, 0x74, 0x5b, 0xa6, + 0xc8, 0xaf, 0x26, 0x1b, 0x15, 0x7e, 0x11, 0xec }, // Public Key: EOS7Xtaa4y44gYapth4MH5bdtCvdtQvVLdsW7a8thVAuvNAkj8X7i { 0x03, 0x5c, 0x50, 0x81, 0xef, 0xa6, 0x46, 0x00, 0x5a, 0xb9, 0xd8, 0x2b, 0xfe, 0xd8, 0xe1, 0x6d, 0x15, 0x42, 0x9e, 0x9a, 0xcb, 0xc9, 0xd6, 0xb3, 0x2e, 0x5a, 0xe3, 0xed, 0xa5, 0x8d, 0x6a, 0x42, 0x6c }, @@ -135,12 +123,10 @@ TEST_CASE("ec::ecdsa_verify") { { "not valid #1", { - { - 0xde, 0x01, 0x64, 0x03, 0x39, 0x01, 0x66, 0x8b, - 0xa0, 0x39, 0xef, 0x31, 0x61, 0xc7, 0xc8, 0x9d, - 0x15, 0x4b, 0xc6, 0x7b, 0x99, 0x5c, 0xba, 0x9b, - 0x23, 0x8a, 0x76, 0x4b, 0x81, 0xf2, 0xff, 0xeb - }, + 0xde, 0x01, 0x64, 0x03, 0x39, 0x01, 0x66, 0x8b, + 0xa0, 0x39, 0xef, 0x31, 0x61, 0xc7, 0xc8, 0x9d, + 0x15, 0x4b, 0xc6, 0x7b, 0x99, 0x5c, 0xba, 0x9b, + 0x23, 0x8a, 0x76, 0x4b, 0x81, 0xf2, 0xff, 0xeb }, // Public Key: EOS7Xtaa4y44gYapth4MH5bdtCvdtQvVLdsW7a8thVAuvNAkj8X7i { 0x03, 0x5c, 0x50, 0x81, 0xef, 0xa6, 0x46, 0x00, 0x5a, 0xb9, 0xd8, 0x2b, 0xfe, 0xd8, 0xe1, 0x6d, 0x15, 0x42, 0x9e, 0x9a, 0xcb, 0xc9, 0xd6, 0xb3, 0x2e, 0x5a, 0xe3, 0xed, 0xa5, 0x8d, 0x6a, 0x42, 0x6c }, @@ -155,12 +141,10 @@ TEST_CASE("ec::ecdsa_verify") { { "not valid #2", { - { - 0xa7, 0xf7, 0x89, 0x36, 0xea, 0xb7, 0x95, 0xa7, - 0x71, 0xaa, 0x73, 0xb5, 0xf6, 0xb8, 0xa0, 0x40, - 0xe5, 0x4f, 0xb3, 0x87, 0xff, 0xd9, 0xb6, 0x4e, - 0x30, 0x4c, 0xa3, 0x78, 0xab, 0x68, 0x86, 0x24 - }, + 0xa7, 0xf7, 0x89, 0x36, 0xea, 0xb7, 0x95, 0xa7, + 0x71, 0xaa, 0x73, 0xb5, 0xf6, 0xb8, 0xa0, 0x40, + 0xe5, 0x4f, 0xb3, 0x87, 0xff, 0xd9, 0xb6, 0x4e, + 0x30, 0x4c, 0xa3, 0x78, 0xab, 0x68, 0x86, 0x24 }, // EOS5AxTzvLZ7mRPvo1Ju9nCdB31PruYHE9uar8pF6D3CvZQGWcHq8 { 0x02, 0x25, 0x64, 0x31, 0x9d, 0x41, 0x46, 0x82, 0xeb, 0x60, 0xed, 0x17, 0xe9, 0x8a, 0xd1, 0x21, 0x60, 0xc4, 0x65, 0xe7, 0x7e, 0x73, 0x2e, 0x45, 0xf0, 0x78, 0x8b, 0x7f, 0x43, 0x30, 0x71, 0xbc, 0x34 }, @@ -175,12 +159,10 @@ TEST_CASE("ec::ecdsa_verify") { { "not valid #3", { - { - 0x48, 0xd7, 0xd3, 0x83,0x9c, 0xa2, 0x82, 0xde, - 0xb6, 0x9a, 0xb8, 0x34,0x36, 0xb0, 0x9f, 0x19, - 0xbb, 0xdf, 0x2b, 0xb5,0x39, 0x42, 0x92, 0x32, - 0x33, 0x34, 0x84, 0xdd,0xba, 0xbd, 0x95, 0x43 - }, + 0x48, 0xd7, 0xd3, 0x83,0x9c, 0xa2, 0x82, 0xde, + 0xb6, 0x9a, 0xb8, 0x34,0x36, 0xb0, 0x9f, 0x19, + 0xbb, 0xdf, 0x2b, 0xb5,0x39, 0x42, 0x92, 0x32, + 0x33, 0x34, 0x84, 0xdd,0xba, 0xbd, 0x95, 0x43 }, // EOS5vhJWLeBjQAiTZxWdnFkttUDWANurEka69La2nu8fp2gSi5eQk { 0x02, 0x88, 0xb4, 0x83, 0x3a, 0x86, 0x18, 0xd3, 0xb2, 0x2b, 0xbe, 0xe6, 0x59, 0x3d, 0xf2, 0x41, 0xf6, 0xed, 0x86, 0x40, 0xe6, 0x19, 0xc8, 0x45, 0x03, 0x78, 0x57, 0xde, 0xcb, 0x2a, 0xd7, 0xc2, 0xf0 }, From 33d3440f53293924dc2e4a1f58e1a3780368e32c Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Mon, 27 Mar 2023 15:20:10 +0200 Subject: [PATCH 17/58] include/libeosio/checksum.hpp: Use plain c-array instead of std::array --- include/libeosio/checksum.hpp | 16 +++++++-------- src/wif/k1.cpp | 37 ++++++++++++++++++++++------------- src/wif/legacy.cpp | 10 ++++++---- 3 files changed, 36 insertions(+), 27 deletions(-) diff --git a/include/libeosio/checksum.hpp b/include/libeosio/checksum.hpp index 6bedac9..376ca25 100644 --- a/include/libeosio/checksum.hpp +++ b/include/libeosio/checksum.hpp @@ -26,7 +26,6 @@ #include #include -#include #include namespace libeosio { @@ -39,7 +38,7 @@ namespace libeosio { /** * Checksum datatype */ -typedef std::array checksum_t; +typedef unsigned char checksum_t[CHECKSUM_SIZE]; /** * Checksum template function. @@ -48,19 +47,18 @@ typedef std::array checksum_t; * - F: Hash calculation function, should have the signature `T* F(const unsigned char *, std::size_t, T*)` */ template -inline checksum_t checksum(const unsigned char* data, std::size_t len) { - checksum_t crc; +inline void checksum(const unsigned char* data, std::size_t len, checksum_t crc) { T hash; F(data, len, &hash); - std::memcpy(crc.data(), &hash, crc.size()); - return crc; + std::memcpy(crc, &hash, CHECKSUM_SIZE); } -template +template inline bool checksum_validate(const unsigned char* data, std::size_t len) { - checksum_t check = F(data, len - CHECKSUM_SIZE); - return !memcmp(check.data(), data + (len - CHECKSUM_SIZE), CHECKSUM_SIZE); + checksum_t crc; + F(data, len - CHECKSUM_SIZE, crc); + return !memcmp(crc, data + (len - CHECKSUM_SIZE), CHECKSUM_SIZE); } /** diff --git a/src/wif/k1.cpp b/src/wif/k1.cpp index cb32283..fcabac5 100644 --- a/src/wif/k1.cpp +++ b/src/wif/k1.cpp @@ -33,28 +33,32 @@ namespace libeosio { namespace internal { // So this function is a quick hack to calculate it. // // Should implement and use Init/Update/Finalize hash functions to do it inplace. -checksum_t _checksum_suffix(const unsigned char *in, size_t size, const char *suffix) { +void _checksum_suffix(const unsigned char *in, size_t size, const char *suffix, checksum_t check) { std::vector buf(size + 2); memcpy(buf.data(), in, size); memcpy(buf.data() + size, suffix, 2); - return checksum_ripemd160(buf.data(), buf.size()); + return checksum_ripemd160(buf.data(), buf.size(), (unsigned char*) check); } void pub_encoder_k1(const ec_pubkey_t& key, unsigned char *buf) { - checksum_t check = _checksum_suffix(key.data(), EC_PUBKEY_SIZE, "K1"); + checksum_t check; + + _checksum_suffix(key.data(), EC_PUBKEY_SIZE, "K1", check); memcpy(buf, key.data(), EC_PUBKEY_SIZE); - memcpy(buf + EC_PUBKEY_SIZE, check.data(), check.size()); + memcpy(buf + EC_PUBKEY_SIZE, check, CHECKSUM_SIZE); } bool pub_decoder_k1(const std::vector& buf, ec_pubkey_t& key) { - checksum_t check = _checksum_suffix(buf.data(), EC_PUBKEY_SIZE, "K1"); + checksum_t check; - if (memcmp(buf.data() + EC_PUBKEY_SIZE, check.data(), CHECKSUM_SIZE)) { + _checksum_suffix(buf.data(), EC_PUBKEY_SIZE, "K1", check); + + if (memcmp(buf.data() + EC_PUBKEY_SIZE, check, CHECKSUM_SIZE)) { return false; } @@ -63,10 +67,12 @@ bool pub_decoder_k1(const std::vector& buf, ec_pubkey_t& key) { } size_t priv_encoder_k1(const ec_privkey_t& priv, unsigned char *buf) { - checksum_t check = _checksum_suffix(priv.data(), EC_PRIVKEY_SIZE, "K1"); + checksum_t check; + + _checksum_suffix(priv.data(), EC_PRIVKEY_SIZE, "K1", check); memcpy(buf, priv.data(), priv.size()); - memcpy(buf + EC_PRIVKEY_SIZE, check.data(), check.size()); + memcpy(buf + EC_PRIVKEY_SIZE, check, CHECKSUM_SIZE); return EC_PRIVKEY_SIZE + CHECKSUM_SIZE; } @@ -77,8 +83,9 @@ bool priv_decoder_k1(const std::vector& buf, ec_privkey_t& priv) return false; } - checksum_t check = _checksum_suffix(buf.data(), EC_PRIVKEY_SIZE, "K1"); - if (memcmp(buf.data() + EC_PRIVKEY_SIZE, check.data(), CHECKSUM_SIZE)) { + checksum_t check; + _checksum_suffix(buf.data(), EC_PRIVKEY_SIZE, "K1", check); + if (memcmp(buf.data() + EC_PRIVKEY_SIZE, check, CHECKSUM_SIZE)) { return false; } @@ -88,10 +95,12 @@ bool priv_decoder_k1(const std::vector& buf, ec_privkey_t& priv) void sig_encoder_k1(const ec_signature_t& sig, unsigned char *buf) { - checksum_t check = _checksum_suffix(sig.data(), EC_SIGNATURE_SIZE, "K1"); + checksum_t check; + + _checksum_suffix(sig.data(), EC_SIGNATURE_SIZE, "K1", check); memcpy(buf, sig.data(), sig.size()); - memcpy(buf + EC_SIGNATURE_SIZE, check.data(), check.size()); + memcpy(buf + EC_SIGNATURE_SIZE, check, CHECKSUM_SIZE); } bool sig_decoder_k1(const std::vector& buf, ec_signature_t& sig) { @@ -103,10 +112,10 @@ bool sig_decoder_k1(const std::vector& buf, ec_signature_t& sig) } // Calculate checksum - check = _checksum_suffix(buf.data(), EC_SIGNATURE_SIZE, "K1"); + _checksum_suffix(buf.data(), EC_SIGNATURE_SIZE, "K1", check); // And validate - if (memcmp(buf.data() + EC_SIGNATURE_SIZE, check.data(), CHECKSUM_SIZE)) { + if (memcmp(buf.data() + EC_SIGNATURE_SIZE, check, CHECKSUM_SIZE)) { return false; } diff --git a/src/wif/legacy.cpp b/src/wif/legacy.cpp index 96578e6..aca47a7 100644 --- a/src/wif/legacy.cpp +++ b/src/wif/legacy.cpp @@ -31,10 +31,12 @@ namespace libeosio { namespace internal { void pub_encoder_legacy(const ec_pubkey_t& key, unsigned char *buf) { - checksum_t check = checksum_ripemd160(key.data(), EC_PUBKEY_SIZE); + checksum_t check; + + checksum_ripemd160(key.data(), EC_PUBKEY_SIZE, check); memcpy(buf, key.data(), EC_PUBKEY_SIZE); - memcpy(buf + EC_PUBKEY_SIZE, check.data(), check.size()); + memcpy(buf + EC_PUBKEY_SIZE, check, CHECKSUM_SIZE); } bool pub_decoder_legacy(const std::vector& buf, ec_pubkey_t& key) { @@ -52,8 +54,8 @@ size_t priv_encoder_legacy(const ec_privkey_t& priv, unsigned char *buf) { buf[0] = PRIV_KEY_PREFIX; memcpy(buf + 1, priv.data(), EC_PRIVKEY_SIZE); - check = checksum_sha256d(buf, 1 + EC_PRIVKEY_SIZE); - memcpy(buf + 1 + EC_PRIVKEY_SIZE, check.data(), check.size()); + checksum_sha256d(buf, 1 + EC_PRIVKEY_SIZE, check); + memcpy(buf + 1 + EC_PRIVKEY_SIZE, check, CHECKSUM_SIZE); return 1 + EC_PRIVKEY_SIZE + CHECKSUM_SIZE; } From 86d75df2ec996ee864b48dcc88d62d9b25373a12 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Tue, 4 Apr 2023 13:26:45 +0200 Subject: [PATCH 18/58] include/libeosio/WIF.hpp: declare all WIF_ constant strings as extern and set them in WIF.cpp --- include/libeosio/WIF.hpp | 10 +++++----- src/WIF.cpp | 6 ++++++ 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/include/libeosio/WIF.hpp b/include/libeosio/WIF.hpp index 040c392..767da80 100644 --- a/include/libeosio/WIF.hpp +++ b/include/libeosio/WIF.hpp @@ -32,11 +32,11 @@ namespace libeosio { /** * Key prefixes. (strings that is not equal to these prefixes are treated as legacy format.) */ -const std::string WIF_PUB_LEG = "EOS"; -const std::string WIF_PUB_K1 = "PUB_K1_"; -const std::string WIF_PVT_LEG = ""; -const std::string WIF_PVT_K1 = "PVT_K1_"; -const std::string WIF_SIG_K1 = "SIG_K1_"; +extern const std::string WIF_PUB_LEG; +extern const std::string WIF_PUB_K1; +extern const std::string WIF_PVT_LEG; +extern const std::string WIF_PVT_K1; +extern const std::string WIF_SIG_K1; /** * Encode an EC private key to WIF String. diff --git a/src/WIF.cpp b/src/WIF.cpp index 44869f2..9d8712d 100644 --- a/src/WIF.cpp +++ b/src/WIF.cpp @@ -30,6 +30,12 @@ namespace libeosio { +const std::string WIF_PUB_LEG = "EOS"; +const std::string WIF_PUB_K1 = "PUB_K1_"; +const std::string WIF_PVT_LEG = ""; +const std::string WIF_PVT_K1 = "PVT_K1_"; +const std::string WIF_SIG_K1 = "SIG_K1_"; + std::string wif_priv_encode(const ec_privkey_t& priv, const std::string& prefix) { checksum_t check; From 18f35c66b5496ca3fac8e23d06375e579316ef3a Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Tue, 4 Apr 2023 13:29:12 +0200 Subject: [PATCH 19/58] include/libeosio/WIF.hpp: add wif_codec_t struct and related functions/constants. --- include/libeosio/WIF.hpp | 17 +++++++++++++++++ src/WIF.cpp | 3 +++ 2 files changed, 20 insertions(+) diff --git a/include/libeosio/WIF.hpp b/include/libeosio/WIF.hpp index 767da80..2978c88 100644 --- a/include/libeosio/WIF.hpp +++ b/include/libeosio/WIF.hpp @@ -38,6 +38,23 @@ extern const std::string WIF_PVT_LEG; extern const std::string WIF_PVT_K1; extern const std::string WIF_SIG_K1; +/** + * Codecs + */ + +// A WIF Codec is an public and private key prefix pair. +typedef struct { + std::string pub; + std::string pvt; +} wif_codec_t; + +extern const wif_codec_t WIF_CODEC_K1; +extern const wif_codec_t WIF_CODEC_LEG; + +inline wif_codec_t wif_create_legacy_codec(const std::string& pub_prefix) { + return { pub_prefix, WIF_PVT_LEG }; +} + /** * Encode an EC private key to WIF String. */ diff --git a/src/WIF.cpp b/src/WIF.cpp index 9d8712d..7de1c6b 100644 --- a/src/WIF.cpp +++ b/src/WIF.cpp @@ -36,6 +36,9 @@ const std::string WIF_PVT_LEG = ""; const std::string WIF_PVT_K1 = "PVT_K1_"; const std::string WIF_SIG_K1 = "SIG_K1_"; +const wif_codec_t WIF_CODEC_K1 = { WIF_PUB_K1, WIF_PVT_K1 }; +const wif_codec_t WIF_CODEC_LEG = wif_create_legacy_codec(WIF_PUB_LEG); + std::string wif_priv_encode(const ec_privkey_t& priv, const std::string& prefix) { checksum_t check; From 2f56e8b43d63f24c9a3e6bbd8da7d8070a78c85a Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Tue, 4 Apr 2023 13:30:07 +0200 Subject: [PATCH 20/58] include/libeosio/WIF.hpp: pass wif_codec_t to wif_print_key() and make it default to K1. --- include/libeosio/WIF.hpp | 2 +- src/WIF.cpp | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/include/libeosio/WIF.hpp b/include/libeosio/WIF.hpp index 2978c88..83ae7e4 100644 --- a/include/libeosio/WIF.hpp +++ b/include/libeosio/WIF.hpp @@ -78,7 +78,7 @@ bool wif_pub_decode(ec_pubkey_t& pub, const std::string& data); /** * Prints an EC keypair in WIF format to standard out. */ -void wif_print_key(const struct ec_keypair *key, const std::string& prefix = WIF_PUB_LEG); +void wif_print_key(const struct ec_keypair *key, const wif_codec_t& codec = WIF_CODEC_K1); /** * Signatures diff --git a/src/WIF.cpp b/src/WIF.cpp index 7de1c6b..cf19c16 100644 --- a/src/WIF.cpp +++ b/src/WIF.cpp @@ -123,10 +123,10 @@ bool wif_pub_decode(ec_pubkey_t& pub, const std::string& data) { return decoder(buf, pub); } -void wif_print_key(const struct ec_keypair *key, const std::string& prefix) { +void wif_print_key(const struct ec_keypair *key, const wif_codec_t& codec) { - std::cout << "Public: " << wif_pub_encode(key->pub, prefix) << std::endl; - std::cout << "Private: " << wif_priv_encode(key->secret, prefix) << std::endl; + std::cout << "Public: " << wif_pub_encode(key->pub, codec.pub) << std::endl; + std::cout << "Private: " << wif_priv_encode(key->secret, codec.pvt) << std::endl; } bool wif_sig_decode(ec_signature_t& sig, const std::string& data) { From 424fe4702d8b08ba5401068259dc857d83d05f54 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Tue, 4 Apr 2023 13:30:39 +0200 Subject: [PATCH 21/58] include/libeosio/WIF.hpp: Make wif_priv_encode and wif_pub_encode default to K1 prefix. --- include/libeosio/WIF.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/libeosio/WIF.hpp b/include/libeosio/WIF.hpp index 83ae7e4..1b35aa8 100644 --- a/include/libeosio/WIF.hpp +++ b/include/libeosio/WIF.hpp @@ -58,7 +58,7 @@ inline wif_codec_t wif_create_legacy_codec(const std::string& pub_prefix) { /** * Encode an EC private key to WIF String. */ -std::string wif_priv_encode(const ec_privkey_t& priv, const std::string& prefix = WIF_PVT_LEG); +std::string wif_priv_encode(const ec_privkey_t& priv, const std::string& prefix = WIF_PVT_K1); /** * Decode an WIF String to EC private key @@ -68,7 +68,7 @@ bool wif_priv_decode(ec_privkey_t& priv, const std::string& data); /** * Encode an EC public key to WIF String. */ -std::string wif_pub_encode(const ec_pubkey_t& pub, const std::string& prefix = WIF_PUB_LEG); +std::string wif_pub_encode(const ec_pubkey_t& pub, const std::string& prefix = WIF_PUB_K1); /** * Decode an WIF String to EC public key From 9d113574902d52ba45ea10e925c87bedfb91e936 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Tue, 4 Apr 2023 19:00:45 +0200 Subject: [PATCH 22/58] Version 0.1.7 --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 990af40..6ad0f0c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,7 +6,7 @@ cmake_minimum_required(VERSION 3.15) # Project name and version project(libeosio - VERSION 0.1.6 + VERSION 0.1.7 DESCRIPTION "C++ library for EOSIO" HOMEPAGE_URL "https://github.com/eosswedenorg/libeosio" LANGUAGES C CXX From 0cfd459c71ea5672e1cb3abbd91cecaa9827b6b2 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Thu, 6 Apr 2023 09:29:48 +0200 Subject: [PATCH 23/58] Change project name from libeosio to libantelope --- .github/workflows/package.yml | 2 +- CMakeLists.txt | 6 +++--- README.md | 12 ++++++------ cmake/libantelopeConfig.cmake.in | 26 ++++++++++++++++++++++++++ cmake/libeosioConfig.cmake.in | 26 -------------------------- 5 files changed, 36 insertions(+), 36 deletions(-) create mode 100644 cmake/libantelopeConfig.cmake.in delete mode 100644 cmake/libeosioConfig.cmake.in diff --git a/.github/workflows/package.yml b/.github/workflows/package.yml index 5ee2909..b6b6eb4 100644 --- a/.github/workflows/package.yml +++ b/.github/workflows/package.yml @@ -62,7 +62,7 @@ jobs: id: package run: | cmake --build build --config Release --target package - $FILE=(ls build/libeosio*.zip) + $FILE=(ls build/libantelope*.zip) echo "::set-output name=filename::$FILE" echo "::set-output name=name::$(([io.fileinfo]"$FILE").basename).zip" diff --git a/CMakeLists.txt b/CMakeLists.txt index 6ad0f0c..8fa4f8a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,10 +5,10 @@ cmake_minimum_required(VERSION 3.15) # -------------------------------- # Project name and version -project(libeosio +project(libantelope VERSION 0.1.7 - DESCRIPTION "C++ library for EOSIO" - HOMEPAGE_URL "https://github.com/eosswedenorg/libeosio" + DESCRIPTION "C++ library for Antelope IO" + HOMEPAGE_URL "https://github.com/eosswedenorg/libantelope" LANGUAGES C CXX ) diff --git a/README.md b/README.md index b9ded7c..b1a33fd 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,12 @@ -![](https://github.com/eosswedenorg/libeosio/workflows/CI/badge.svg) -[![GitHub release](https://img.shields.io/github/v/release/eosswedenorg/libeosio?include_prereleases)](https://github.com/eosswedenorg/libeosio/releases/latest) +![](https://github.com/eosswedenorg/libantelope/workflows/CI/badge.svg) +[![GitHub release](https://img.shields.io/github/v/release/eosswedenorg/libleap?include_prereleases)](https://github.com/eosswedenorg/libantelope/releases/latest) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) -# libeosio +# libleap -Independent C++ library for [EOS](https://eos.io/) +Independent C++ library for [Antelope IO](https://antelope.io) (former [libeosio](https://github.com/eosswedenorg/libeosio)) -NOTE: This repository has no connection to the official EOS code. +NOTE: This repository has no connection to the official Antelope code. ## Compiling the library @@ -110,7 +110,7 @@ C:\repo> cmake --build build --config Release ## Security notice Elliptic curve crypthographic operations is done using either `OpenSSL` or `libsecp256k1` libraries. -This library (libeosio) will never expose sensitve cryptographic information +This library (libleap) will never expose sensitve cryptographic information to anything but the computers memory. You are free to inspect the source code and compile yourself to verify. diff --git a/cmake/libantelopeConfig.cmake.in b/cmake/libantelopeConfig.cmake.in new file mode 100644 index 0000000..fcb6456 --- /dev/null +++ b/cmake/libantelopeConfig.cmake.in @@ -0,0 +1,26 @@ +# This script provides the libantelope as an import target +# ---------------------------------------------------------- +# +# Use find_package() so cmake will find libantelope: +# +# find_package(libantelope) # No specific version +# find_package(libantelope REQUIRED) # No specific version, but the library must be found. +# find_package(libantelope 0.1) # any 0.1.x, but the library is optional. +# find_package(libeantelope 0.1.0) # 0.1.0 or greater, but the library is optional. +# +# Then you just link the you target with eoskeygen target: +# +# target_link_libraries( ${PROGRAM_EXE} PUBLIC libantelope ) +# +# if you do not specify REQUIRED. you must check the variable libantelope_FOUND +# and and only link to it if it's defined: +# +# if (libantelope_FOUND) +# ... +# target_link_libraries( ${PROGRAM_EXE} PUBLIC libantelope ) +# .. +# endif() + +set(LIBANTELOPE_VERSION "@PROJECT_VERSION@") + +include ( "${CMAKE_CURRENT_LIST_DIR}/@PROJECT_NAME@Targets.cmake" ) diff --git a/cmake/libeosioConfig.cmake.in b/cmake/libeosioConfig.cmake.in deleted file mode 100644 index dfc2ca9..0000000 --- a/cmake/libeosioConfig.cmake.in +++ /dev/null @@ -1,26 +0,0 @@ -# This script provides the libeosio as an import target -# ---------------------------------------------------------- -# -# Use find_package() so cmake will find libeosio: -# -# find_package(libeosio) # No specific version -# find_package(libeosio REQUIRED) # No specific version, but the library must be found. -# find_package(libeosio 0.1) # any 0.1.x, but the library is optional. -# find_package(libeosio 0.1.0) # 0.1.0 or greater, but the library is optional. -# -# Then you just link the you target with eoskeygen target: -# -# target_link_libraries( ${PROGRAM_EXE} PUBLIC libeosio ) -# -# if you do not specify REQUIRED. you must check the variable libeosio_FOUND -# and and only link to it if it's defined: -# -# if (libeosio_FOUND) -# ... -# target_link_libraries( ${PROGRAM_EXE} PUBLIC libeosio ) -# .. -# endif() - -set(LIBEOSIO_VERSION "@PROJECT_VERSION@") - -include ( "${CMAKE_CURRENT_LIST_DIR}/@PROJECT_NAME@Targets.cmake" ) From 6824a2f49eb5d0e73bfca0e40da54b1ffa1d0379 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Thu, 6 Apr 2023 14:23:05 +0200 Subject: [PATCH 24/58] Change namespace and header guards from libeosio to libantelope --- include/{libeosio => libantelope}/WIF.hpp | 12 ++++---- include/{libeosio => libantelope}/base58.hpp | 10 +++---- .../{libeosio => libantelope}/checksum.hpp | 12 ++++---- include/{libeosio => libantelope}/ec.hpp | 16 +++++------ include/{libeosio => libantelope}/hash.hpp | 10 +++---- src/WIF.cpp | 10 +++---- src/base58.cpp | 6 ++-- src/ec.cpp | 6 ++-- src/libsecp256k1/ec.cpp | 6 ++-- src/libsecp256k1/ecdsa.cpp | 6 ++-- src/openssl/ec.cpp | 6 ++-- src/openssl/ecdsa.cpp | 6 ++-- src/openssl/hash.cpp | 6 ++-- src/openssl/internal.h | 6 ++-- src/wif/codec.hpp | 12 ++++---- src/wif/k1.cpp | 6 ++-- src/wif/legacy.cpp | 6 ++-- tests/WIF/priv_decode.cpp | 16 +++++------ tests/WIF/priv_encode.cpp | 24 ++++++++-------- tests/WIF/pub_decode.cpp | 16 +++++------ tests/WIF/pub_encode.cpp | 28 +++++++++---------- tests/WIF/sig_decode.cpp | 10 +++---- tests/WIF/sig_encode.cpp | 8 +++--- tests/base58/decode.cpp | 4 +-- tests/base58/encode.cpp | 4 +-- tests/base58/is_base58.cpp | 8 +++--- tests/benchmark/ec.cpp | 12 ++++---- tests/ec/ecdsa_recover.cpp | 16 +++++------ tests/ec/ecdsa_sign.cpp | 20 ++++++------- tests/ec/ecdsa_verify.cpp | 14 +++++----- tests/ec/generate.cpp | 14 +++++----- tests/ec/pubkey.cpp | 16 +++++------ 32 files changed, 176 insertions(+), 176 deletions(-) rename include/{libeosio => libantelope}/WIF.hpp (94%) rename include/{libeosio => libantelope}/base58.hpp (93%) rename include/{libeosio => libantelope}/checksum.hpp (92%) rename include/{libeosio => libantelope}/ec.hpp (91%) rename include/{libeosio => libantelope}/hash.hpp (93%) diff --git a/include/libeosio/WIF.hpp b/include/libantelope/WIF.hpp similarity index 94% rename from include/libeosio/WIF.hpp rename to include/libantelope/WIF.hpp index 1b35aa8..3f055bd 100644 --- a/include/libeosio/WIF.hpp +++ b/include/libantelope/WIF.hpp @@ -21,13 +21,13 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -#ifndef LIBEOSIO_WIF_H -#define LIBEOSIO_WIF_H +#ifndef LIBANTELOPE_WIF_H +#define LIBANTELOPE_WIF_H #include -#include +#include -namespace libeosio { +namespace libantelope { /** * Key prefixes. (strings that is not equal to these prefixes are treated as legacy format.) @@ -94,6 +94,6 @@ std::string wif_sig_encode(const ec_signature_t& sig); */ bool wif_sig_decode(ec_signature_t& sig, const std::string& data); -} // namespace libeosio +} // namespace libantelope -#endif /* LIBEOSIO_WIF_H */ +#endif /* LIBANTELOPE_WIF_H */ diff --git a/include/libeosio/base58.hpp b/include/libantelope/base58.hpp similarity index 93% rename from include/libeosio/base58.hpp rename to include/libantelope/base58.hpp index 6975412..9d3cc58 100644 --- a/include/libeosio/base58.hpp +++ b/include/libantelope/base58.hpp @@ -21,13 +21,13 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -#ifndef LIBEOSIO_BASE58_H -#define LIBEOSIO_BASE58_H +#ifndef LIBANTELOPE_BASE58_H +#define LIBANTELOPE_BASE58_H #include #include -namespace libeosio { +namespace libantelope { /** * Base58 Encoding functions. @@ -61,6 +61,6 @@ size_t is_base58(const std::string& str); */ std::string& base58_strip(std::string& str); -} //namespace libeosio +} //namespace libantelope -#endif /* LIBEOSIO_BASE58_H */ +#endif /* LIBANTELOPE_BASE58_H */ diff --git a/include/libeosio/checksum.hpp b/include/libantelope/checksum.hpp similarity index 92% rename from include/libeosio/checksum.hpp rename to include/libantelope/checksum.hpp index 376ca25..9da4b8e 100644 --- a/include/libeosio/checksum.hpp +++ b/include/libantelope/checksum.hpp @@ -21,14 +21,14 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -#ifndef LIBEOSIO_CHECKSUM_H -#define LIBEOSIO_CHECKSUM_H +#ifndef LIBANTELOPE_CHECKSUM_H +#define LIBANTELOPE_CHECKSUM_H #include #include -#include +#include -namespace libeosio { +namespace libantelope { /** * Checksum size (in bytes) @@ -68,6 +68,6 @@ inline bool checksum_validate(const unsigned char* data, std::size_t len) { #define checksum_sha256d checksum #define checksum_ripemd160 checksum -} // namespace libeosio +} // namespace libantelope -#endif /* LIBEOSIO_CHECKSUM_H */ +#endif /* LIBANTELOPE_CHECKSUM_H */ diff --git a/include/libeosio/ec.hpp b/include/libantelope/ec.hpp similarity index 91% rename from include/libeosio/ec.hpp rename to include/libantelope/ec.hpp index ff7c57b..3df6fe7 100644 --- a/include/libeosio/ec.hpp +++ b/include/libantelope/ec.hpp @@ -21,14 +21,14 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -#ifndef LIBEOSIO_EC_H -#define LIBEOSIO_EC_H +#ifndef LIBANTELOPE_EC_H +#define LIBANTELOPE_EC_H -#include +#include #include #include -namespace libeosio { +namespace libantelope { /** * Elliptic curve private key size (in bytes) @@ -128,13 +128,13 @@ int ecdsa_recover(const sha256_t* digest, const ec_signature_t& sig, ec_pubkey_t */ void ec_shutdown(); -} // namespace libeosio +} // namespace libantelope // Stream operators -std::ostream& operator<<(std::ostream& os, const libeosio::ec_privkey_t& pk); +std::ostream& operator<<(std::ostream& os, const libantelope::ec_privkey_t& pk); -std::ostream& operator<<(std::ostream& os, const libeosio::ec_pubkey_t& pk); +std::ostream& operator<<(std::ostream& os, const libantelope::ec_pubkey_t& pk); -#endif /* LIBEOSIO_EC_H */ +#endif /* LIBANTELOPE_EC_H */ diff --git a/include/libeosio/hash.hpp b/include/libantelope/hash.hpp similarity index 93% rename from include/libeosio/hash.hpp rename to include/libantelope/hash.hpp index be9ac75..4ad57d7 100644 --- a/include/libeosio/hash.hpp +++ b/include/libantelope/hash.hpp @@ -21,12 +21,12 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -#ifndef LIBEOSIO_HASH_H -#define LIBEOSIO_HASH_H +#ifndef LIBANTELOPE_HASH_H +#define LIBANTELOPE_HASH_H #include -namespace libeosio { +namespace libantelope { /** * Hashes @@ -55,6 +55,6 @@ sha256_t* sha256d(const unsigned char *data, std::size_t len, sha256_t* out); */ ripemd160_t* ripemd160(const unsigned char *data, std::size_t len, ripemd160_t* out); -} // namespace libeosio +} // namespace libantelope -#endif /* LIBEOSIO_HASH_H */ +#endif /* LIBANTELOPE_HASH_H */ diff --git a/src/WIF.cpp b/src/WIF.cpp index cf19c16..e04f8be 100644 --- a/src/WIF.cpp +++ b/src/WIF.cpp @@ -23,12 +23,12 @@ */ #include #include -#include -#include -#include +#include +#include +#include #include "wif/codec.hpp" -namespace libeosio { +namespace libantelope { const std::string WIF_PUB_LEG = "EOS"; const std::string WIF_PUB_K1 = "PUB_K1_"; @@ -154,4 +154,4 @@ std::string wif_sig_encode(const ec_signature_t& sig) { return WIF_SIG_K1 + base58_encode(buf, buf + sizeof(buf)); } -} // namespace libeosio +} // namespace libantelope diff --git a/src/base58.cpp b/src/base58.cpp index 998c4c0..3979dc1 100644 --- a/src/base58.cpp +++ b/src/base58.cpp @@ -28,9 +28,9 @@ #include #include #include -#include +#include -namespace libeosio { +namespace libantelope { static const char charmap[59] = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"; static const int8_t table[256] = { @@ -190,4 +190,4 @@ std::string& base58_strip(std::string &str) { return str; } -} // namespace libeosio +} // namespace libantelope diff --git a/src/ec.cpp b/src/ec.cpp index 8493f02..c6ceca6 100644 --- a/src/ec.cpp +++ b/src/ec.cpp @@ -21,7 +21,7 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -#include +#include std::ostream& _hex(std::ostream& os, const unsigned char *b, std::size_t sz) { os << "[ " << std::hex; @@ -38,10 +38,10 @@ std::ostream& _hex(std::ostream& os, const unsigned char *b, std::size_t sz) { return os << std::oct << " ]"; } -std::ostream& operator<<(std::ostream& os, const libeosio::ec_privkey_t& k) { +std::ostream& operator<<(std::ostream& os, const libantelope::ec_privkey_t& k) { return _hex(os, k.data(), k.size()); } -std::ostream& operator<<(std::ostream& os, const libeosio::ec_pubkey_t& k) { +std::ostream& operator<<(std::ostream& os, const libantelope::ec_pubkey_t& k) { return _hex(os, k.data(), k.size()); } \ No newline at end of file diff --git a/src/libsecp256k1/ec.cpp b/src/libsecp256k1/ec.cpp index 9c6388d..190f5d6 100644 --- a/src/libsecp256k1/ec.cpp +++ b/src/libsecp256k1/ec.cpp @@ -23,10 +23,10 @@ */ #include #include -#include +#include #include "rng.h" -namespace libeosio { +namespace libantelope { secp256k1_context* ctx; @@ -90,4 +90,4 @@ int ec_generate_key(struct ec_keypair *pair) { return ec_get_publickey(&pair->secret, &pair->pub); } -} // namespace libeosio +} // namespace libantelope diff --git a/src/libsecp256k1/ecdsa.cpp b/src/libsecp256k1/ecdsa.cpp index 2a5e235..838809a 100644 --- a/src/libsecp256k1/ecdsa.cpp +++ b/src/libsecp256k1/ecdsa.cpp @@ -23,10 +23,10 @@ */ #include #include -#include +#include #include "rng.h" -namespace libeosio { +namespace libantelope { extern secp256k1_context* ctx; @@ -115,4 +115,4 @@ int ecdsa_recover(const sha256_t* digest, const ec_signature_t& sig, ec_pubkey_t return len != EC_PUBKEY_SIZE ? -1 : 0; } -} // namespace libeosio +} // namespace libantelope diff --git a/src/openssl/ec.cpp b/src/openssl/ec.cpp index c001c82..1ab7afe 100644 --- a/src/openssl/ec.cpp +++ b/src/openssl/ec.cpp @@ -24,10 +24,10 @@ #include #include #include -#include +#include #include "internal.h" -namespace libeosio { +namespace libantelope { BN_CTX *ctx = NULL; EC_KEY *k = NULL; @@ -121,4 +121,4 @@ int ec_generate_key(struct ec_keypair *pair) { return 0; } -} // namespace libeosio +} // namespace libantelope diff --git a/src/openssl/ecdsa.cpp b/src/openssl/ecdsa.cpp index cf5c2f4..9582e21 100644 --- a/src/openssl/ecdsa.cpp +++ b/src/openssl/ecdsa.cpp @@ -24,10 +24,10 @@ #include #include #include -#include +#include #include "internal.h" -namespace libeosio { +namespace libantelope { extern BN_CTX *ctx; @@ -182,4 +182,4 @@ err2: EC_KEY_free(ec_key); err1: return ret; } -} // namespace libeosio \ No newline at end of file +} // namespace libantelope \ No newline at end of file diff --git a/src/openssl/hash.cpp b/src/openssl/hash.cpp index a8dc0cd..492e23a 100644 --- a/src/openssl/hash.cpp +++ b/src/openssl/hash.cpp @@ -23,9 +23,9 @@ */ #include #include -#include +#include -namespace libeosio { +namespace libantelope { sha256_t* sha256(const unsigned char *data, std::size_t len, sha256_t* out) { return (sha256_t *) SHA256(data, len, (unsigned char*) out); @@ -40,4 +40,4 @@ ripemd160_t* ripemd160(const unsigned char *data, std::size_t len, ripemd160_t* return (ripemd160_t *) RIPEMD160(data, len, (unsigned char*) out); } -} // namespace libeosio +} // namespace libantelope diff --git a/src/openssl/internal.h b/src/openssl/internal.h index 9a5d6c1..6ceb1e5 100644 --- a/src/openssl/internal.h +++ b/src/openssl/internal.h @@ -24,8 +24,8 @@ #include #include -#ifndef LIBEOSIO_OPENSSL_INTERNAL_H -#define LIBEOSIO_OPENSSL_INTERNAL_H +#ifndef LIBANTELOPE_OPENSSL_INTERNAL_H +#define LIBANTELOPE_OPENSSL_INTERNAL_H #define EC_KEY_new_secp256k1() (EC_KEY_new_by_curve_name( NID_secp256k1 )) @@ -62,4 +62,4 @@ int ECDSA_SIG_unserialize_rs(const unsigned char *sig, BIGNUM **r, BIGNUM **s, i } #endif -#endif /* LIBEOSIO_OPENSSL_INTERNAL_H */ \ No newline at end of file +#endif /* LIBANTELOPE_OPENSSL_INTERNAL_H */ \ No newline at end of file diff --git a/src/wif/codec.hpp b/src/wif/codec.hpp index eb3e5ca..c927647 100644 --- a/src/wif/codec.hpp +++ b/src/wif/codec.hpp @@ -21,13 +21,13 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -#ifndef LIBEOSIO_CODEC_H -#define LIBEOSIO_CODEC_H +#ifndef LIBANTELOPE_CODEC_H +#define LIBANTELOPE_CODEC_H -#include +#include #include -namespace libeosio { namespace internal { +namespace libantelope { namespace internal { /** * Public-key encoders @@ -79,6 +79,6 @@ typedef bool (*sig_decoder_t)(const std::vector& buf, ec_signatur bool sig_decoder_k1(const std::vector& buf, ec_signature_t& sig); -}} // namespace libeosio::internal +}} // namespace libantelope::internal -#endif /* LIBEOSIO_CODEC_H */ +#endif /* LIBANTELOPE_CODEC_H */ diff --git a/src/wif/k1.cpp b/src/wif/k1.cpp index fcabac5..f7e8af2 100644 --- a/src/wif/k1.cpp +++ b/src/wif/k1.cpp @@ -22,11 +22,11 @@ * SOFTWARE. */ -#include +#include #include #include "codec.hpp" -namespace libeosio { namespace internal { +namespace libantelope { namespace internal { // Just to make it "harder" the calculated checksum for a signature (k1) and pub/priv keys in k1/r1 format. // has a suffix that is not present in the WIF encoded string. @@ -125,4 +125,4 @@ bool sig_decoder_k1(const std::vector& buf, ec_signature_t& sig) } -}} // namespace libeosio::internal \ No newline at end of file +}} // namespace libantelope::internal \ No newline at end of file diff --git a/src/wif/legacy.cpp b/src/wif/legacy.cpp index aca47a7..aa9dafb 100644 --- a/src/wif/legacy.cpp +++ b/src/wif/legacy.cpp @@ -22,10 +22,10 @@ * SOFTWARE. */ -#include +#include #include "codec.hpp" -namespace libeosio { namespace internal { +namespace libantelope { namespace internal { #define PRIV_KEY_PREFIX 0x80 /* 0x80 for "Bitcoin mainnet". Always used by EOS. */ @@ -77,4 +77,4 @@ bool priv_decoder_legacy(const std::vector& buf, ec_privkey_t& pr return true; } -}} // namespace libeosio::internal \ No newline at end of file +}} // namespace libantelope::internal \ No newline at end of file diff --git a/tests/WIF/priv_decode.cpp b/tests/WIF/priv_decode.cpp index 5105e66..b99a94e 100644 --- a/tests/WIF/priv_decode.cpp +++ b/tests/WIF/priv_decode.cpp @@ -1,5 +1,5 @@ -#include -#include +#include +#include #include #include #include @@ -8,7 +8,7 @@ TEST_CASE("WIF::wif_priv_decode [legacy]") { struct testcase { std::string name; std::string key; - libeosio::ec_privkey_t expected; + libantelope::ec_privkey_t expected; }; @@ -22,9 +22,9 @@ TEST_CASE("WIF::wif_priv_decode [legacy]") { SUBCASE(it->name.c_str()) { - libeosio::ec_privkey_t result; + libantelope::ec_privkey_t result; - CHECK( libeosio::wif_priv_decode(result, it->key) ); + CHECK( libantelope::wif_priv_decode(result, it->key) ); CHECK( result == it->expected ); } } @@ -35,7 +35,7 @@ TEST_CASE("WIF::wif_priv_decode [K1]") { struct testcase { std::string name; std::string key; - libeosio::ec_privkey_t expected; + libantelope::ec_privkey_t expected; }; @@ -49,9 +49,9 @@ TEST_CASE("WIF::wif_priv_decode [K1]") { SUBCASE(it->name.c_str()) { - libeosio::ec_privkey_t result; + libantelope::ec_privkey_t result; - CHECK( libeosio::wif_priv_decode(result, it->key) ); + CHECK( libantelope::wif_priv_decode(result, it->key) ); CHECK( result == it->expected ); } } diff --git a/tests/WIF/priv_encode.cpp b/tests/WIF/priv_encode.cpp index bb08bd1..5030e3f 100644 --- a/tests/WIF/priv_encode.cpp +++ b/tests/WIF/priv_encode.cpp @@ -1,5 +1,5 @@ -#include -#include +#include +#include #include #include #include @@ -9,20 +9,20 @@ TEST_CASE("WIF::wif_priv_encode [Legacy]") { struct testcase { std::string name; const std::string prefix; - libeosio::ec_privkey_t key; + libantelope::ec_privkey_t key; std::string expected; }; std::vector tests { - { "one", libeosio::WIF_PVT_LEG, { 0x0C, 0x28, 0xFC, 0xA3, 0x86, 0xC7, 0xA2, 0x27, 0x60, 0x0B, 0x2F, 0xE5, 0x0B, 0x7C, 0xAE, 0x11, 0xEC, 0x86, 0xD3, 0xBF, 0x1F, 0xBE, 0x47, 0x1B, 0xE8, 0x98, 0x27, 0xE1, 0x9D,0x72,0xAA,0x1D}, "5HueCGU8rMjxEXxiPuD5BDku4MkFqeZyd4dZ1jvhTVqvbTLvyTJ" }, - { "two", libeosio::WIF_PVT_LEG, { 0x9F, 0xE3, 0xE3, 0x2B, 0x3C, 0x4B, 0x6B, 0x91, 0x6E, 0x20, 0x6C, 0xB0, 0x91, 0xDF, 0x1F, 0x5E, 0x34, 0x32, 0x88, 0x0B, 0x41, 0x33, 0x86, 0xBD, 0xF2, 0x92, 0xFF, 0x23, 0x06, 0x43, 0xF2, 0x8C}, "5K2hm8apqz281ANDQdtVzifpxcXFTqG5E7Fc6Q5V2ssqPRQ3urJ" }, - { "three", libeosio::WIF_PVT_LEG, { 0x59, 0x3A, 0x51, 0xB5, 0x5D, 0x56, 0xAA, 0xF0, 0x5B, 0xD9, 0xD1, 0x0E, 0x6B, 0x88, 0x6D, 0xF9, 0xC4, 0x37, 0x09, 0xB2, 0x4C, 0xEC, 0xBB, 0x63, 0x68, 0x92, 0xC2, 0x94, 0x31, 0x48, 0x71, 0x8C}, "5JVanYq9HPvuKgr2FjATYB9NvTsJ4a3CAj5oPYKbr1Ja5MRLsZX" } + { "one", libantelope::WIF_PVT_LEG, { 0x0C, 0x28, 0xFC, 0xA3, 0x86, 0xC7, 0xA2, 0x27, 0x60, 0x0B, 0x2F, 0xE5, 0x0B, 0x7C, 0xAE, 0x11, 0xEC, 0x86, 0xD3, 0xBF, 0x1F, 0xBE, 0x47, 0x1B, 0xE8, 0x98, 0x27, 0xE1, 0x9D,0x72,0xAA,0x1D}, "5HueCGU8rMjxEXxiPuD5BDku4MkFqeZyd4dZ1jvhTVqvbTLvyTJ" }, + { "two", libantelope::WIF_PVT_LEG, { 0x9F, 0xE3, 0xE3, 0x2B, 0x3C, 0x4B, 0x6B, 0x91, 0x6E, 0x20, 0x6C, 0xB0, 0x91, 0xDF, 0x1F, 0x5E, 0x34, 0x32, 0x88, 0x0B, 0x41, 0x33, 0x86, 0xBD, 0xF2, 0x92, 0xFF, 0x23, 0x06, 0x43, 0xF2, 0x8C}, "5K2hm8apqz281ANDQdtVzifpxcXFTqG5E7Fc6Q5V2ssqPRQ3urJ" }, + { "three", libantelope::WIF_PVT_LEG, { 0x59, 0x3A, 0x51, 0xB5, 0x5D, 0x56, 0xAA, 0xF0, 0x5B, 0xD9, 0xD1, 0x0E, 0x6B, 0x88, 0x6D, 0xF9, 0xC4, 0x37, 0x09, 0xB2, 0x4C, 0xEC, 0xBB, 0x63, 0x68, 0x92, 0xC2, 0x94, 0x31, 0x48, 0x71, 0x8C}, "5JVanYq9HPvuKgr2FjATYB9NvTsJ4a3CAj5oPYKbr1Ja5MRLsZX" } }; for(auto it = tests.begin(); it != tests.end(); it++) { SUBCASE(it->name.c_str()) { - CHECK( libeosio::wif_priv_encode(it->key, it->prefix) == it->expected ); + CHECK( libantelope::wif_priv_encode(it->key, it->prefix) == it->expected ); } } } @@ -32,20 +32,20 @@ TEST_CASE("WIF::wif_priv_encode [K1]") { struct testcase { std::string name; const std::string prefix; - libeosio::ec_privkey_t key; + libantelope::ec_privkey_t key; std::string expected; }; std::vector tests { - { "one", libeosio::WIF_PVT_K1, { 0x0C, 0x28, 0xFC, 0xA3, 0x86, 0xC7, 0xA2, 0x27, 0x60, 0x0B, 0x2F, 0xE5, 0x0B, 0x7C, 0xAE, 0x11, 0xEC, 0x86, 0xD3, 0xBF, 0x1F, 0xBE, 0x47, 0x1B, 0xE8, 0x98, 0x27, 0xE1, 0x9D,0x72,0xAA,0x1D}, "PVT_K1_6Mcb23muAxyXaSMhmB6B1mqkvLdWhtuFZmnZsxDczHRvQdp32" }, - { "two", libeosio::WIF_PVT_K1, { 0x9F, 0xE3, 0xE3, 0x2B, 0x3C, 0x4B, 0x6B, 0x91, 0x6E, 0x20, 0x6C, 0xB0, 0x91, 0xDF, 0x1F, 0x5E, 0x34, 0x32, 0x88, 0x0B, 0x41, 0x33, 0x86, 0xBD, 0xF2, 0x92, 0xFF, 0x23, 0x06, 0x43, 0xF2, 0x8C}, "PVT_K1_2DRBT8jmXT8k9ywNSSbufvhk1hLFhPzWJBpsE2jo12CDoFhcc1" }, - { "three", libeosio::WIF_PVT_K1, { 0x59, 0x3A, 0x51, 0xB5, 0x5D, 0x56, 0xAA, 0xF0, 0x5B, 0xD9, 0xD1, 0x0E, 0x6B, 0x88, 0x6D, 0xF9, 0xC4, 0x37, 0x09, 0xB2, 0x4C, 0xEC, 0xBB, 0x63, 0x68, 0x92, 0xC2, 0x94, 0x31, 0x48, 0x71, 0x8C}, "PVT_K1_gJCsP4CwMv4gTkDXiZT8QFhs3NrSB7Sv22ANGrc8Svun9uC9C" } + { "one", libantelope::WIF_PVT_K1, { 0x0C, 0x28, 0xFC, 0xA3, 0x86, 0xC7, 0xA2, 0x27, 0x60, 0x0B, 0x2F, 0xE5, 0x0B, 0x7C, 0xAE, 0x11, 0xEC, 0x86, 0xD3, 0xBF, 0x1F, 0xBE, 0x47, 0x1B, 0xE8, 0x98, 0x27, 0xE1, 0x9D,0x72,0xAA,0x1D}, "PVT_K1_6Mcb23muAxyXaSMhmB6B1mqkvLdWhtuFZmnZsxDczHRvQdp32" }, + { "two", libantelope::WIF_PVT_K1, { 0x9F, 0xE3, 0xE3, 0x2B, 0x3C, 0x4B, 0x6B, 0x91, 0x6E, 0x20, 0x6C, 0xB0, 0x91, 0xDF, 0x1F, 0x5E, 0x34, 0x32, 0x88, 0x0B, 0x41, 0x33, 0x86, 0xBD, 0xF2, 0x92, 0xFF, 0x23, 0x06, 0x43, 0xF2, 0x8C}, "PVT_K1_2DRBT8jmXT8k9ywNSSbufvhk1hLFhPzWJBpsE2jo12CDoFhcc1" }, + { "three", libantelope::WIF_PVT_K1, { 0x59, 0x3A, 0x51, 0xB5, 0x5D, 0x56, 0xAA, 0xF0, 0x5B, 0xD9, 0xD1, 0x0E, 0x6B, 0x88, 0x6D, 0xF9, 0xC4, 0x37, 0x09, 0xB2, 0x4C, 0xEC, 0xBB, 0x63, 0x68, 0x92, 0xC2, 0x94, 0x31, 0x48, 0x71, 0x8C}, "PVT_K1_gJCsP4CwMv4gTkDXiZT8QFhs3NrSB7Sv22ANGrc8Svun9uC9C" } }; for(auto it = tests.begin(); it != tests.end(); it++) { SUBCASE(it->name.c_str()) { - CHECK( libeosio::wif_priv_encode(it->key, it->prefix) == it->expected ); + CHECK( libantelope::wif_priv_encode(it->key, it->prefix) == it->expected ); } } } diff --git a/tests/WIF/pub_decode.cpp b/tests/WIF/pub_decode.cpp index 36ccad1..0754249 100644 --- a/tests/WIF/pub_decode.cpp +++ b/tests/WIF/pub_decode.cpp @@ -1,5 +1,5 @@ -#include -#include +#include +#include #include #include #include @@ -8,7 +8,7 @@ TEST_CASE("WIF::wif_pub_decode [legacy]") { struct testcase { const char* name; std::string key; - libeosio::ec_pubkey_t expected; + libantelope::ec_pubkey_t expected; bool expectedRet; }; @@ -24,9 +24,9 @@ TEST_CASE("WIF::wif_pub_decode [legacy]") { for(auto it = tests.begin(); it != tests.end(); it++) { SUBCASE(it->name) { - libeosio::ec_pubkey_t result = { 0x0 }; + libantelope::ec_pubkey_t result = { 0x0 }; - CHECK( libeosio::wif_pub_decode(result, it->key) == it->expectedRet ); + CHECK( libantelope::wif_pub_decode(result, it->key) == it->expectedRet ); CHECK( result == it->expected ); } } @@ -36,7 +36,7 @@ TEST_CASE("WIF::wif_pub_decode [k1]") { struct testcase { const char* name; std::string key; - libeosio::ec_pubkey_t expected; + libantelope::ec_pubkey_t expected; bool expectedRet; }; @@ -52,9 +52,9 @@ TEST_CASE("WIF::wif_pub_decode [k1]") { for(auto it = tests.begin(); it != tests.end(); it++) { SUBCASE(it->name) { - libeosio::ec_pubkey_t result = { 0x0 }; + libantelope::ec_pubkey_t result = { 0x0 }; - CHECK( libeosio::wif_pub_decode(result, it->key) == it->expectedRet ); + CHECK( libantelope::wif_pub_decode(result, it->key) == it->expectedRet ); CHECK( result == it->expected ); } } diff --git a/tests/WIF/pub_encode.cpp b/tests/WIF/pub_encode.cpp index d3e548f..c88476d 100644 --- a/tests/WIF/pub_encode.cpp +++ b/tests/WIF/pub_encode.cpp @@ -1,5 +1,5 @@ -#include -#include +#include +#include #include #include #include @@ -8,20 +8,20 @@ TEST_CASE("WIF::wif_pub_encode [legacy]") { struct testcase { std::string name; const std::string prefix; - libeosio::ec_pubkey_t key; + libantelope::ec_pubkey_t key; std::string expected; }; std::vector tests { - { "one", libeosio::WIF_PUB_LEG, { 0x03, 0x7a, 0x0e, 0x6b, 0xfd, 0xe4, 0xf1, 0xad, 0x36, 0x3f, 0x3a, 0xf9, 0xe0, 0x93, 0x63, 0x5a, 0xa9, 0x99, 0x21, 0x15, 0xbc, 0x23, 0x35, 0x75, 0x13, 0x69, 0x55, 0xee, 0x3f, 0xf8, 0xfd, 0x97, 0xec }, "EOS7kzJ5iFBmQWWT1LiWgAiocESD7TTNuuPCdYREUQysruq8VeFKy" }, - { "two", libeosio::WIF_PUB_LEG, { 0x02, 0x5e, 0x94, 0xa5, 0xe7, 0x9f, 0x66, 0x37, 0x55, 0x7e, 0xc2, 0x28, 0x30, 0x40, 0x82, 0x9a, 0x38, 0x72, 0x10, 0x96, 0x6e, 0x15, 0xb7, 0xa5, 0x8a, 0x27, 0x9a, 0x71, 0x06, 0xa7, 0x64, 0x23, 0x30 }, "EOS5c9HkNCJLDebe2Wvapp8bpB38Pf1QWNpkrsFy3mshg7DZfPNeA" }, - { "three", libeosio::WIF_PUB_LEG, { 0x03, 0xd4, 0xc6, 0x2a, 0xdc, 0x11, 0x1c, 0x65, 0x7a, 0x9f, 0x5b, 0xba, 0x96, 0x3f, 0xbb, 0x2a, 0x69, 0x2e, 0xc5, 0x4a, 0x48, 0x3b, 0xa3, 0x5f, 0x2a, 0x37, 0x6c, 0x59, 0x95, 0xb1, 0x95, 0x1c, 0xc9 }, "EOS8SwZMY8DChbbmRKS3wdHCAbv1VWgTRmQEDSaLyJk8pG4wm8BJF" } + { "one", libantelope::WIF_PUB_LEG, { 0x03, 0x7a, 0x0e, 0x6b, 0xfd, 0xe4, 0xf1, 0xad, 0x36, 0x3f, 0x3a, 0xf9, 0xe0, 0x93, 0x63, 0x5a, 0xa9, 0x99, 0x21, 0x15, 0xbc, 0x23, 0x35, 0x75, 0x13, 0x69, 0x55, 0xee, 0x3f, 0xf8, 0xfd, 0x97, 0xec }, "EOS7kzJ5iFBmQWWT1LiWgAiocESD7TTNuuPCdYREUQysruq8VeFKy" }, + { "two", libantelope::WIF_PUB_LEG, { 0x02, 0x5e, 0x94, 0xa5, 0xe7, 0x9f, 0x66, 0x37, 0x55, 0x7e, 0xc2, 0x28, 0x30, 0x40, 0x82, 0x9a, 0x38, 0x72, 0x10, 0x96, 0x6e, 0x15, 0xb7, 0xa5, 0x8a, 0x27, 0x9a, 0x71, 0x06, 0xa7, 0x64, 0x23, 0x30 }, "EOS5c9HkNCJLDebe2Wvapp8bpB38Pf1QWNpkrsFy3mshg7DZfPNeA" }, + { "three", libantelope::WIF_PUB_LEG, { 0x03, 0xd4, 0xc6, 0x2a, 0xdc, 0x11, 0x1c, 0x65, 0x7a, 0x9f, 0x5b, 0xba, 0x96, 0x3f, 0xbb, 0x2a, 0x69, 0x2e, 0xc5, 0x4a, 0x48, 0x3b, 0xa3, 0x5f, 0x2a, 0x37, 0x6c, 0x59, 0x95, 0xb1, 0x95, 0x1c, 0xc9 }, "EOS8SwZMY8DChbbmRKS3wdHCAbv1VWgTRmQEDSaLyJk8pG4wm8BJF" } }; for(auto it = tests.begin(); it != tests.end(); it++) { SUBCASE(it->name.c_str()) { - CHECK( libeosio::wif_pub_encode(it->key, it->prefix) == it->expected ); + CHECK( libantelope::wif_pub_encode(it->key, it->prefix) == it->expected ); } } } @@ -30,7 +30,7 @@ TEST_CASE("WIF::wif_pub_encode [custom prefix]") { struct testcase { std::string name; - libeosio::ec_pubkey_t key; + libantelope::ec_pubkey_t key; std::string expected; }; @@ -43,7 +43,7 @@ TEST_CASE("WIF::wif_pub_encode [custom prefix]") { for(auto it = tests.begin(); it != tests.end(); it++) { SUBCASE(it->name.c_str()) { - CHECK( libeosio::wif_pub_encode(it->key, "ZYX") == it->expected ); + CHECK( libantelope::wif_pub_encode(it->key, "ZYX") == it->expected ); } } } @@ -52,20 +52,20 @@ TEST_CASE("WIF::wif_pub_encode [k1]") { struct testcase { std::string name; const std::string prefix; - libeosio::ec_pubkey_t key; + libantelope::ec_pubkey_t key; std::string expected; }; std::vector tests { - { "one", libeosio::WIF_PUB_K1, { 0x03, 0x7a, 0x0e, 0x6b, 0xfd, 0xe4, 0xf1, 0xad, 0x36, 0x3f, 0x3a, 0xf9, 0xe0, 0x93, 0x63, 0x5a, 0xa9, 0x99, 0x21, 0x15, 0xbc, 0x23, 0x35, 0x75, 0x13, 0x69, 0x55, 0xee, 0x3f, 0xf8, 0xfd, 0x97, 0xec }, "PUB_K1_7kzJ5iFBmQWWT1LiWgAiocESD7TTNuuPCdYREUQysruq7AxzWu" }, - { "two", libeosio::WIF_PUB_K1, { 0x02, 0x5e, 0x94, 0xa5, 0xe7, 0x9f, 0x66, 0x37, 0x55, 0x7e, 0xc2, 0x28, 0x30, 0x40, 0x82, 0x9a, 0x38, 0x72, 0x10, 0x96, 0x6e, 0x15, 0xb7, 0xa5, 0x8a, 0x27, 0x9a, 0x71, 0x06, 0xa7, 0x64, 0x23, 0x30 }, "PUB_K1_5c9HkNCJLDebe2Wvapp8bpB38Pf1QWNpkrsFy3mshg7DViSUUa" }, - { "three", libeosio::WIF_PUB_K1, { 0x03, 0xd4, 0xc6, 0x2a, 0xdc, 0x11, 0x1c, 0x65, 0x7a, 0x9f, 0x5b, 0xba, 0x96, 0x3f, 0xbb, 0x2a, 0x69, 0x2e, 0xc5, 0x4a, 0x48, 0x3b, 0xa3, 0x5f, 0x2a, 0x37, 0x6c, 0x59, 0x95, 0xb1, 0x95, 0x1c, 0xc9 }, "PUB_K1_8SwZMY8DChbbmRKS3wdHCAbv1VWgTRmQEDSaLyJk8pG4wKBXpw" } + { "one", libantelope::WIF_PUB_K1, { 0x03, 0x7a, 0x0e, 0x6b, 0xfd, 0xe4, 0xf1, 0xad, 0x36, 0x3f, 0x3a, 0xf9, 0xe0, 0x93, 0x63, 0x5a, 0xa9, 0x99, 0x21, 0x15, 0xbc, 0x23, 0x35, 0x75, 0x13, 0x69, 0x55, 0xee, 0x3f, 0xf8, 0xfd, 0x97, 0xec }, "PUB_K1_7kzJ5iFBmQWWT1LiWgAiocESD7TTNuuPCdYREUQysruq7AxzWu" }, + { "two", libantelope::WIF_PUB_K1, { 0x02, 0x5e, 0x94, 0xa5, 0xe7, 0x9f, 0x66, 0x37, 0x55, 0x7e, 0xc2, 0x28, 0x30, 0x40, 0x82, 0x9a, 0x38, 0x72, 0x10, 0x96, 0x6e, 0x15, 0xb7, 0xa5, 0x8a, 0x27, 0x9a, 0x71, 0x06, 0xa7, 0x64, 0x23, 0x30 }, "PUB_K1_5c9HkNCJLDebe2Wvapp8bpB38Pf1QWNpkrsFy3mshg7DViSUUa" }, + { "three", libantelope::WIF_PUB_K1, { 0x03, 0xd4, 0xc6, 0x2a, 0xdc, 0x11, 0x1c, 0x65, 0x7a, 0x9f, 0x5b, 0xba, 0x96, 0x3f, 0xbb, 0x2a, 0x69, 0x2e, 0xc5, 0x4a, 0x48, 0x3b, 0xa3, 0x5f, 0x2a, 0x37, 0x6c, 0x59, 0x95, 0xb1, 0x95, 0x1c, 0xc9 }, "PUB_K1_8SwZMY8DChbbmRKS3wdHCAbv1VWgTRmQEDSaLyJk8pG4wKBXpw" } }; for(auto it = tests.begin(); it != tests.end(); it++) { SUBCASE(it->name.c_str()) { - CHECK( libeosio::wif_pub_encode(it->key, it->prefix) == it->expected ); + CHECK( libantelope::wif_pub_encode(it->key, it->prefix) == it->expected ); } } } \ No newline at end of file diff --git a/tests/WIF/sig_decode.cpp b/tests/WIF/sig_decode.cpp index e0c77e6..eb25af5 100644 --- a/tests/WIF/sig_decode.cpp +++ b/tests/WIF/sig_decode.cpp @@ -1,5 +1,5 @@ -#include -#include +#include +#include #include #include @@ -8,7 +8,7 @@ TEST_CASE("WIF::wif_sig_decode") { struct testcase { const char *name; std::string input; - libeosio::ec_signature_t expected; + libantelope::ec_signature_t expected; bool expectedRet; }; @@ -87,9 +87,9 @@ TEST_CASE("WIF::wif_sig_decode") { for(auto it = tests.begin(); it != tests.end(); it++) { SUBCASE(it->name) { - libeosio::ec_signature_t result; + libantelope::ec_signature_t result; - CHECK( libeosio::wif_sig_decode(result, it->input) == it->expectedRet ); + CHECK( libantelope::wif_sig_decode(result, it->input) == it->expectedRet ); if (it->expectedRet == true) { CHECK( result == it->expected ); diff --git a/tests/WIF/sig_encode.cpp b/tests/WIF/sig_encode.cpp index 000fd19..a7a6cbd 100644 --- a/tests/WIF/sig_encode.cpp +++ b/tests/WIF/sig_encode.cpp @@ -1,5 +1,5 @@ -#include -#include +#include +#include #include #include @@ -7,7 +7,7 @@ TEST_CASE("WIF::wif_sig_encode") { struct testcase { const char *name; - libeosio::ec_signature_t sig; + libantelope::ec_signature_t sig; std::string expected; }; @@ -47,7 +47,7 @@ TEST_CASE("WIF::wif_sig_encode") { for(auto it = tests.begin(); it != tests.end(); it++) { SUBCASE(it->name) { - CHECK( libeosio::wif_sig_encode(it->sig) == it->expected ); + CHECK( libantelope::wif_sig_encode(it->sig) == it->expected ); } } } \ No newline at end of file diff --git a/tests/base58/decode.cpp b/tests/base58/decode.cpp index e482597..cd044b4 100644 --- a/tests/base58/decode.cpp +++ b/tests/base58/decode.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include #include @@ -41,7 +41,7 @@ TEST_CASE("base58_decode") { std::vector expectedOut(it->expectedOut.begin(), it->expectedOut.end()); - CHECK( libeosio::base58_decode(it->in, result) == it->expectedReturn ); + CHECK( libantelope::base58_decode(it->in, result) == it->expectedReturn ); CHECK( result == expectedOut ); } } diff --git a/tests/base58/encode.cpp b/tests/base58/encode.cpp index 6d47ed4..6ce6c2c 100644 --- a/tests/base58/encode.cpp +++ b/tests/base58/encode.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include #include @@ -28,7 +28,7 @@ TEST_CASE("base58::base58_encode") { for(auto it = tests.begin(); it != tests.end(); it++) { SUBCASE(it->name) { - CHECK( libeosio::base58_encode(it->in) == it->expected ); + CHECK( libantelope::base58_encode(it->in) == it->expected ); } } } \ No newline at end of file diff --git a/tests/base58/is_base58.cpp b/tests/base58/is_base58.cpp index 5eeb56c..e210f10 100644 --- a/tests/base58/is_base58.cpp +++ b/tests/base58/is_base58.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include #include @@ -23,7 +23,7 @@ TEST_CASE("base58::is_base58 [string]") { for(auto it = tests.begin(); it != tests.end(); it++) { SUBCASE(it->name) { - CHECK(libeosio::is_base58(it->input) == it->expected); + CHECK(libantelope::is_base58(it->input) == it->expected); } } @@ -38,7 +38,7 @@ TEST_CASE("base58::is_base58 [char]") { for(int i = 0; i < valid_alphabet.length(); i++) { char ch = valid_alphabet[i]; - CHECK(libeosio::is_base58(ch)); + CHECK(libantelope::is_base58(ch)); } } @@ -46,7 +46,7 @@ TEST_CASE("base58::is_base58 [char]") { for(int i = 0; i < invalid_alphabet.length(); i++) { char ch = invalid_alphabet[i]; - CHECK_FALSE(libeosio::is_base58(ch)); + CHECK_FALSE(libantelope::is_base58(ch)); } } } \ No newline at end of file diff --git a/tests/benchmark/ec.cpp b/tests/benchmark/ec.cpp index 79d1409..ccff750 100644 --- a/tests/benchmark/ec.cpp +++ b/tests/benchmark/ec.cpp @@ -22,15 +22,15 @@ * SOFTWARE. */ #include -#include -#include +#include +#include std::chrono::duration _run(size_t num_keys) { auto start = std::chrono::steady_clock::now(); for(size_t i = 0; i < num_keys; i++) { - struct libeosio::ec_keypair k; - libeosio::ec_generate_key(&k); + struct libantelope::ec_keypair k; + libantelope::ec_generate_key(&k); } return std::chrono::steady_clock::now() - start; } @@ -48,13 +48,13 @@ void test(size_t num_keys) { } int main() { - libeosio::ec_init(); + libantelope::ec_init(); test(1000); test(10000); test(100000); - libeosio::ec_shutdown(); + libantelope::ec_shutdown(); return 0; } \ No newline at end of file diff --git a/tests/ec/ecdsa_recover.cpp b/tests/ec/ecdsa_recover.cpp index 59b7a37..40a95d8 100644 --- a/tests/ec/ecdsa_recover.cpp +++ b/tests/ec/ecdsa_recover.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include @@ -6,9 +6,9 @@ TEST_CASE("ec::ecdsa_recover") { struct testcase { const char *name; - libeosio::sha256_t dgst; - libeosio::ec_signature_t sig; - libeosio::ec_pubkey_t expected; + libantelope::sha256_t dgst; + libantelope::ec_signature_t sig; + libantelope::ec_pubkey_t expected; int expectedRet; }; @@ -85,19 +85,19 @@ TEST_CASE("ec::ecdsa_recover") { }, }; - libeosio::ec_init(); + libantelope::ec_init(); for(auto it = tests.begin(); it != tests.end(); it++) { SUBCASE(it->name) { - libeosio::ec_pubkey_t result; + libantelope::ec_pubkey_t result; - CHECK( libeosio::ecdsa_recover(&it->dgst, it->sig, result) == it->expectedRet ); + CHECK( libantelope::ecdsa_recover(&it->dgst, it->sig, result) == it->expectedRet ); if (it->expectedRet == 0) { CHECK( result == it->expected ); } } } - libeosio::ec_shutdown(); + libantelope::ec_shutdown(); } diff --git a/tests/ec/ecdsa_sign.cpp b/tests/ec/ecdsa_sign.cpp index 7c4b0e2..743a8d2 100644 --- a/tests/ec/ecdsa_sign.cpp +++ b/tests/ec/ecdsa_sign.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include @@ -6,9 +6,9 @@ TEST_CASE("ec::ecdsa_sign") { struct testcase { const char *name; - libeosio::ec_privkey_t key; - libeosio::ec_pubkey_t pub; - libeosio::sha256_t dgst; + libantelope::ec_privkey_t key; + libantelope::ec_pubkey_t pub; + libantelope::sha256_t dgst; }; std::vector tests = { @@ -92,24 +92,24 @@ TEST_CASE("ec::ecdsa_sign") { }, }; - libeosio::ec_init(); + libantelope::ec_init(); for(auto it = tests.begin(); it != tests.end(); it++) { SUBCASE(it->name) { - libeosio::ec_signature_t result; + libantelope::ec_signature_t result; - CHECK( libeosio::ecdsa_sign(it->key, &it->dgst, result) == 0 ); + CHECK( libantelope::ecdsa_sign(it->key, &it->dgst, result) == 0 ); // Need to use verify here as different implemententations produces different signatures. - // (i have tested eosjs, eos-go and ofc libeosio) + // (i have tested eosjs, eos-go and ofc libantelope) // However, the signatures are correct and can be validated by all implementations. // // Now, how do we know that ecdsa_verify is correct? // well, in escdsa_verify.cpp there are tests that checks hardcoded signatures generated by different implementations and should be fine. - CHECK( libeosio::ecdsa_verify(&it->dgst, result, it->pub) == 0); + CHECK( libantelope::ecdsa_verify(&it->dgst, result, it->pub) == 0); } } - libeosio::ec_shutdown(); + libantelope::ec_shutdown(); } diff --git a/tests/ec/ecdsa_verify.cpp b/tests/ec/ecdsa_verify.cpp index 7959f12..12c71d0 100644 --- a/tests/ec/ecdsa_verify.cpp +++ b/tests/ec/ecdsa_verify.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include @@ -6,9 +6,9 @@ TEST_CASE("ec::ecdsa_verify") { struct testcase { const char *name; - libeosio::sha256_t dgst; - libeosio::ec_pubkey_t pubkey; - libeosio::ec_signature_t sig; + libantelope::sha256_t dgst; + libantelope::ec_pubkey_t pubkey; + libantelope::ec_signature_t sig; int expected; }; @@ -176,14 +176,14 @@ TEST_CASE("ec::ecdsa_verify") { }, }; - libeosio::ec_init(); + libantelope::ec_init(); for(auto it = tests.begin(); it != tests.end(); it++) { SUBCASE(it->name) { - CHECK( libeosio::ecdsa_verify(&it->dgst, it->sig, it->pubkey) == it->expected ); + CHECK( libantelope::ecdsa_verify(&it->dgst, it->sig, it->pubkey) == it->expected ); } } - libeosio::ec_shutdown(); + libantelope::ec_shutdown(); } diff --git a/tests/ec/generate.cpp b/tests/ec/generate.cpp index 4fe610d..6eb7f35 100644 --- a/tests/ec/generate.cpp +++ b/tests/ec/generate.cpp @@ -1,18 +1,18 @@ -#include +#include #include TEST_CASE("ec::generate") { - libeosio::ec_init(); + libantelope::ec_init(); - libeosio::ec_pubkey_t result; - libeosio::ec_keypair pair; - CHECK(libeosio::ec_generate_key(&pair) == 0); + libantelope::ec_pubkey_t result; + libantelope::ec_keypair pair; + CHECK(libantelope::ec_generate_key(&pair) == 0); // Can't test much because... well the private key should be random :) // But alteast verify that the public key belongs to the private key. - CHECK(libeosio::ec_get_publickey(&pair.secret, &result) == 0); + CHECK(libantelope::ec_get_publickey(&pair.secret, &result) == 0); CHECK( result == pair.pub ); - libeosio::ec_shutdown(); + libantelope::ec_shutdown(); } \ No newline at end of file diff --git a/tests/ec/pubkey.cpp b/tests/ec/pubkey.cpp index 8215f25..fa2b744 100644 --- a/tests/ec/pubkey.cpp +++ b/tests/ec/pubkey.cpp @@ -1,5 +1,5 @@ -#include -#include +#include +#include #include #include @@ -8,8 +8,8 @@ TEST_CASE("ec::pubkey") { struct testcase { std::string name; - libeosio::ec_privkey_t priv; - libeosio::ec_pubkey_t expected; + libantelope::ec_privkey_t priv; + libantelope::ec_pubkey_t expected; }; std::vector tests { @@ -125,16 +125,16 @@ TEST_CASE("ec::pubkey") { } }; - libeosio::ec_init(); + libantelope::ec_init(); for(auto it = tests.begin(); it != tests.end(); it++) { SUBCASE(it->name.c_str()) { - libeosio::ec_pubkey_t result; - CHECK( libeosio::ec_get_publickey(&it->priv, &result) == 0 ); + libantelope::ec_pubkey_t result; + CHECK( libantelope::ec_get_publickey(&it->priv, &result) == 0 ); CHECK( result == it->expected ); } } - libeosio::ec_shutdown(); + libantelope::ec_shutdown(); } \ No newline at end of file From 923384025ae4d6d351a9d95d351158873da5582c Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Thu, 6 Apr 2023 14:39:21 +0200 Subject: [PATCH 25/58] cmake/libantelopeConfig.cmake.in: fix a typo in the documentation. --- cmake/libantelopeConfig.cmake.in | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmake/libantelopeConfig.cmake.in b/cmake/libantelopeConfig.cmake.in index fcb6456..5250615 100644 --- a/cmake/libantelopeConfig.cmake.in +++ b/cmake/libantelopeConfig.cmake.in @@ -8,7 +8,7 @@ # find_package(libantelope 0.1) # any 0.1.x, but the library is optional. # find_package(libeantelope 0.1.0) # 0.1.0 or greater, but the library is optional. # -# Then you just link the you target with eoskeygen target: +# Then you just link the you target with libantelope target: # # target_link_libraries( ${PROGRAM_EXE} PUBLIC libantelope ) # From aa6bff9a84012e88481e4e5df3eb7323d859f652 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Thu, 6 Apr 2023 14:39:36 +0200 Subject: [PATCH 26/58] cmake/libantelopeConfig.cmake.in: minor style fix. --- cmake/libantelopeConfig.cmake.in | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmake/libantelopeConfig.cmake.in b/cmake/libantelopeConfig.cmake.in index 5250615..5c81b1e 100644 --- a/cmake/libantelopeConfig.cmake.in +++ b/cmake/libantelopeConfig.cmake.in @@ -21,6 +21,6 @@ # .. # endif() -set(LIBANTELOPE_VERSION "@PROJECT_VERSION@") +set( LIBANTELOPE_VERSION "@PROJECT_VERSION@" ) include ( "${CMAKE_CURRENT_LIST_DIR}/@PROJECT_NAME@Targets.cmake" ) From 98a1ce7fcc8f3a0401a4144ea7fdb58146186001 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Thu, 6 Apr 2023 14:48:17 +0200 Subject: [PATCH 27/58] src/libsecp256k1/rng.h: add header guard. --- src/libsecp256k1/rng.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/libsecp256k1/rng.h b/src/libsecp256k1/rng.h index 13daf08..103583f 100644 --- a/src/libsecp256k1/rng.h +++ b/src/libsecp256k1/rng.h @@ -4,6 +4,9 @@ * EXAMPLES_COPYING or https://creativecommons.org/publicdomain/zero/1.0 * *************************************************************************/ +#ifndef LIBANTELOPE_LIBSECP256K1_RNG_H +#define LIBANTELOPE_LIBSECP256K1_RNG_H + /* * This file is an attempt at collecting best practice methods for obtaining randomness with different operating systems. * It may be out-of-date. Consult the documentation of the operating system before considering to use the methods below. @@ -64,4 +67,6 @@ static int fill_random(unsigned char* data, size_t size) { } #endif return 0; -} \ No newline at end of file +} + +#endif /* LIBANTELOPE_LIBSECP256K1_RNG_H */ \ No newline at end of file From 3e38c587d3e65b987c9d37499068885eb2104822 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Thu, 6 Apr 2023 14:49:48 +0200 Subject: [PATCH 28/58] Version 0.2.0 --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8fa4f8a..16deb91 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,7 +6,7 @@ cmake_minimum_required(VERSION 3.15) # Project name and version project(libantelope - VERSION 0.1.7 + VERSION 0.2.0 DESCRIPTION "C++ library for Antelope IO" HOMEPAGE_URL "https://github.com/eosswedenorg/libantelope" LANGUAGES C CXX From a22825db9bfc4d5325c56cc63d337c0d15b892dd Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Mon, 10 Apr 2023 16:42:32 +0200 Subject: [PATCH 29/58] README.md: fix some places where "libleap" was used instead of "libantelope" --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index b1a33fd..6c22bcc 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,8 @@ ![](https://github.com/eosswedenorg/libantelope/workflows/CI/badge.svg) -[![GitHub release](https://img.shields.io/github/v/release/eosswedenorg/libleap?include_prereleases)](https://github.com/eosswedenorg/libantelope/releases/latest) +[![GitHub release](https://img.shields.io/github/v/release/eosswedenorg/libantelope?include_prereleases)](https://github.com/eosswedenorg/libantelope/releases/latest) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) -# libleap +# libantelope Independent C++ library for [Antelope IO](https://antelope.io) (former [libeosio](https://github.com/eosswedenorg/libeosio)) @@ -110,7 +110,7 @@ C:\repo> cmake --build build --config Release ## Security notice Elliptic curve crypthographic operations is done using either `OpenSSL` or `libsecp256k1` libraries. -This library (libleap) will never expose sensitve cryptographic information +This library (libantelope) will never expose sensitve cryptographic information to anything but the computers memory. You are free to inspect the source code and compile yourself to verify. From c06846f7edb03d6a0bbedd9c4a8947ab3a50f9ba Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Mon, 10 Apr 2023 16:47:30 +0200 Subject: [PATCH 30/58] CMakeLists.txt: configure compiler flags for different build types. --- CMakeLists.txt | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 16deb91..c79e705 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -34,6 +34,23 @@ set( CMAKE_CXX_STANDARD 11 ) set( CMAKE_CXX_STANDARD_REQUIRED ON ) set( CMAKE_CXX_EXTENSIONS OFF ) +add_compile_options( + "$<$:-Wall;-Wconversion;-Wno-sign-conversion;-Wextra>" + "$<$:/W3;-D_CRT_SECURE_NO_WARNINGS=1>" + + # Debug + "$<$:$<$:-O0;-g>>" + "$<$:$<$:/Od;/Zi>>" + + # Release + "$<$:$<$:-O3>>" + "$<$:$<$:/O2>>" + + # MinSizeRel + "$<$:$<$:-Os>>" + "$<$:$<$:/O1>>" +) + # OpenSSL 3.0 deprecates some functions we use. # Adding this flag makes the compiler not spam warnings. add_compile_options(-D OPENSSL_API_COMPAT=0x10100000L) From 7fc83991bcb14e5817d24b39be6b50e682ec6b8c Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Mon, 10 Apr 2023 16:47:44 +0200 Subject: [PATCH 31/58] src/WIF.cpp: Remove unused variables. --- src/WIF.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/WIF.cpp b/src/WIF.cpp index e04f8be..c13bdd4 100644 --- a/src/WIF.cpp +++ b/src/WIF.cpp @@ -41,7 +41,6 @@ const wif_codec_t WIF_CODEC_LEG = wif_create_legacy_codec(WIF_PUB_LEG); std::string wif_priv_encode(const ec_privkey_t& priv, const std::string& prefix) { - checksum_t check; // 1 byte extra for legacy prefix prefix. unsigned char buf[1 + EC_PRIVKEY_SIZE + CHECKSUM_SIZE] = { 0 }; size_t len; @@ -131,7 +130,6 @@ void wif_print_key(const struct ec_keypair *key, const wif_codec_t& codec) { bool wif_sig_decode(ec_signature_t& sig, const std::string& data) { - checksum_t checksum; std::vector buf; if (data.substr(0, WIF_SIG_K1.length()) != WIF_SIG_K1) { From ac3facf30b4d22393f97ad74d8777c221dddf745 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Mon, 10 Apr 2023 16:50:23 +0200 Subject: [PATCH 32/58] src/WIF.cpp: fix integer comparison between different sizes warning. --- src/WIF.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/WIF.cpp b/src/WIF.cpp index c13bdd4..64be109 100644 --- a/src/WIF.cpp +++ b/src/WIF.cpp @@ -58,7 +58,7 @@ std::string wif_priv_encode(const ec_privkey_t& priv, const std::string& prefix) bool wif_priv_decode(ec_privkey_t& priv, const std::string& data) { - uint8_t offset; + std::size_t offset; std::vector buf; internal::priv_decoder_t decoder = internal::priv_decoder_legacy; @@ -99,7 +99,7 @@ std::string wif_pub_encode(const ec_pubkey_t& pub, const std::string& prefix) { bool wif_pub_decode(ec_pubkey_t& pub, const std::string& data) { internal::pub_decoder_t decoder = internal::pub_decoder_legacy; - int offset; + std::size_t offset; std::vector buf; // Check prefix From b7063f3f3aa1586a1f43e4d5f570f015b26e3c09 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Mon, 10 Apr 2023 17:22:04 +0200 Subject: [PATCH 33/58] src/libsecp256k1/ecdsa.cpp: don't include "rng.h" here. --- src/libsecp256k1/ecdsa.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/libsecp256k1/ecdsa.cpp b/src/libsecp256k1/ecdsa.cpp index 838809a..e667308 100644 --- a/src/libsecp256k1/ecdsa.cpp +++ b/src/libsecp256k1/ecdsa.cpp @@ -24,7 +24,6 @@ #include #include #include -#include "rng.h" namespace libantelope { From fa7d3cb2e9410e310de79aa1f77cac1b5001d857 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Mon, 10 Apr 2023 17:23:42 +0200 Subject: [PATCH 34/58] src/ec.cpp: fix integer size comparison warning. --- src/ec.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ec.cpp b/src/ec.cpp index c6ceca6..86bd3ef 100644 --- a/src/ec.cpp +++ b/src/ec.cpp @@ -25,7 +25,7 @@ std::ostream& _hex(std::ostream& os, const unsigned char *b, std::size_t sz) { os << "[ " << std::hex; - for (int i = 0; i < sz; i++) { + for (std::size_t i = 0; i < sz; i++) { unsigned int v = b[i]; os << "0x"; From 000876176a6fc00b37f6e4aeb9128073fa392ebb Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Mon, 10 Apr 2023 17:25:15 +0200 Subject: [PATCH 35/58] src/base58.cpp: fix integer size comparison warning. --- src/base58.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/base58.cpp b/src/base58.cpp index 3979dc1..bd2da9a 100644 --- a/src/base58.cpp +++ b/src/base58.cpp @@ -120,7 +120,7 @@ bool base58_decode(const char* psz, std::vector& out) { psz++; } // Allocate enough space in big-endian base256 representation. - int size = strlen(psz) * 733 /1000 + 1; // log(58) / log(256), rounded up. + std::size_t size = strlen(psz) * 733 /1000 + 1; // log(58) / log(256), rounded up. std::vector b256(size); // Process the characters. @@ -132,7 +132,7 @@ bool base58_decode(const char* psz, std::vector& out) { int i = 0; for (std::vector::reverse_iterator it = b256.rbegin(); (carry != 0 || i < length) && (it != b256.rend()); ++it, ++i) { carry += 58 * (*it); - *it = carry % 256; + *it = (unsigned char) (carry % 256); carry /= 256; } assert(carry == 0); From 62b677d0e0428dc542f35d7cd43fa83446a30252 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Mon, 10 Apr 2023 17:29:12 +0200 Subject: [PATCH 36/58] tests/base58/is_base58.cpp: fix integer size comparison warning. --- tests/base58/is_base58.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/base58/is_base58.cpp b/tests/base58/is_base58.cpp index e210f10..1dad426 100644 --- a/tests/base58/is_base58.cpp +++ b/tests/base58/is_base58.cpp @@ -35,7 +35,7 @@ TEST_CASE("base58::is_base58 [char]") { SUBCASE("valid") { - for(int i = 0; i < valid_alphabet.length(); i++) { + for(std::size_t i = 0; i < valid_alphabet.length(); i++) { char ch = valid_alphabet[i]; CHECK(libantelope::is_base58(ch)); @@ -43,7 +43,7 @@ TEST_CASE("base58::is_base58 [char]") { } SUBCASE("invalid") { - for(int i = 0; i < invalid_alphabet.length(); i++) { + for(std::size_t i = 0; i < invalid_alphabet.length(); i++) { char ch = invalid_alphabet[i]; CHECK_FALSE(libantelope::is_base58(ch)); From ee4705e8586a075d0903c2b5f22137f057b57a8f Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Mon, 10 Apr 2023 17:31:52 +0200 Subject: [PATCH 37/58] src/libsecp256k1/ecdsa.cpp: fix warning about assigning an int value to unsigned char. --- src/libsecp256k1/ecdsa.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libsecp256k1/ecdsa.cpp b/src/libsecp256k1/ecdsa.cpp index e667308..3aa8a5c 100644 --- a/src/libsecp256k1/ecdsa.cpp +++ b/src/libsecp256k1/ecdsa.cpp @@ -56,7 +56,7 @@ int ecdsa_sign(const ec_privkey_t& key, const sha256_t* digest, ec_signature_t& secp256k1_ecdsa_recoverable_signature_serialize_compact(ctx, sig.data() + 1, &v, &s); if (is_canonical(sig.data())) { - sig[0] = 27 + 4 + v; + sig[0] = (unsigned char) (27 + 4 + v); return 0; } } From b7a1298615a30afec16afea5a336301787404972 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Mon, 10 Apr 2023 17:32:22 +0200 Subject: [PATCH 38/58] src/libsecp256k1/ecdsa.cpp: fix unused variable warning in extended_nonce_function() --- src/libsecp256k1/ecdsa.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/libsecp256k1/ecdsa.cpp b/src/libsecp256k1/ecdsa.cpp index 3aa8a5c..8984fe7 100644 --- a/src/libsecp256k1/ecdsa.cpp +++ b/src/libsecp256k1/ecdsa.cpp @@ -39,6 +39,7 @@ int is_canonical(const unsigned char *d) { static int extended_nonce_function( unsigned char *nonce32, const unsigned char *msg32, const unsigned char *key32, const unsigned char* algo16, void* data, unsigned int attempt ) { + (void)attempt; // "use" the variable here to shutup compiler about unsed variable. return secp256k1_nonce_function_rfc6979(nonce32, msg32, key32, algo16, nullptr, *(unsigned int*) data); } From 596cf8c7744b0bac02549fe336e188054de885e5 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Tue, 30 May 2023 13:45:35 +0200 Subject: [PATCH 39/58] Adding src/config.in.h --- src/config.in.h | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 src/config.in.h diff --git a/src/config.in.h b/src/config.in.h new file mode 100644 index 0000000..27c830d --- /dev/null +++ b/src/config.in.h @@ -0,0 +1,38 @@ +/* + * MIT License + * + * Copyright (c) 2019-2023 EOS Sw/eden + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef LIBANTELOPE_CONFIG_H +#define LIBANTELOPE_CONFIG_H + +#ifdef __cplusplus +extern "C" { +#endif + +/* Hash implementation */ +#cmakedefine LIBANTELOPE_HASHIMPL_OPENSSL + +#ifdef __cplusplus +} +#endif + +#endif /* LIBANTELOPE_CONFIG_H */ \ No newline at end of file From 682ea069a24ff0603f25a10f5d93b9d97e2e9795 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Tue, 30 May 2023 13:46:23 +0200 Subject: [PATCH 40/58] CMakeLists.txt: use src/config.in.h as configuration file. --- CMakeLists.txt | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c79e705..14c12d6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -79,6 +79,7 @@ add_library( ${LIB_NAME} STATIC target_include_directories( ${LIB_NAME} PUBLIC $ + $ $ ) @@ -124,6 +125,9 @@ endif() message("-- Using Elliptic curve library: ${EC_LIB}") +# Generate config file +configure_file(src/config.in.h ${CMAKE_BINARY_DIR}/include/libantelope/config.h) + # -------------------------------- # Tests # -------------------------------- @@ -160,7 +164,7 @@ install(TARGETS ${LIB_NAME} EXPORT ${PROJECT_NAME}Targets LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} ) -install(DIRECTORY include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) +install(DIRECTORY include/ ${CMAKE_BINARY_DIR}/include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) # Readme and license install(FILES README.md LICENSE LICENSE.bitcoin From be8096ed0f860e83c2fc795925244157c26156be Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Tue, 30 May 2023 13:46:42 +0200 Subject: [PATCH 41/58] CMakeLists.txt: set LIBANTELOPE_HASHIMPL_OPENSSL --- CMakeLists.txt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 14c12d6..30ba605 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -92,6 +92,9 @@ endif() include(OpenSSL) target_link_libraries( ${LIB_NAME} PRIVATE OpenSSL::Crypto) +# Hash implementation +set( LIBANTELOPE_HASHIMPL_OPENSSL 1 ) + # EC Implementation if (${EC_LIB} STREQUAL "libsecp256k1") add_subdirectory( vendor/secp256k1 ) From 891d2e970d73b75b40f4854dd0bce83c6e70d188 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Tue, 30 May 2023 13:47:02 +0200 Subject: [PATCH 42/58] Adding include/libantelope/internal/hash.hpp --- include/libantelope/internal/hash.hpp | 45 +++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 include/libantelope/internal/hash.hpp diff --git a/include/libantelope/internal/hash.hpp b/include/libantelope/internal/hash.hpp new file mode 100644 index 0000000..d2a889d --- /dev/null +++ b/include/libantelope/internal/hash.hpp @@ -0,0 +1,45 @@ + +/** + * MIT License + * + * Copyright (c) 2019-2023 EOS Sw/eden + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef LIBANTELOPE_INTERNAL_HASH_H +#define LIBANTELOPE_INTERNAL_HASH_H + +#include + +#ifdef LIBANTELOPE_HASHIMPL_OPENSSL + +#include +#include + +namespace libantelope { namespace internal { + + typedef SHA256_CTX sha256_state; + typedef RIPEMD160_CTX ripemd160_state; +} } // namespace libantelope::internal + +#else +#error "Missing hash implementation" +#endif + +#endif /* LIBANTELOPE_INTERNAL_INTERNAL_H */ \ No newline at end of file From 610c32c17110e83cd4eb53def284ea1d6c7d988e Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Tue, 30 May 2023 13:47:38 +0200 Subject: [PATCH 43/58] Hash: Define init/update/final functions for sha256 and ripemd160 --- include/libantelope/hash.hpp | 38 ++++++++++++++++++++++++++++++++++++ src/openssl/hash.cpp | 24 +++++++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/include/libantelope/hash.hpp b/include/libantelope/hash.hpp index 4ad57d7..67dd698 100644 --- a/include/libantelope/hash.hpp +++ b/include/libantelope/hash.hpp @@ -25,6 +25,7 @@ #define LIBANTELOPE_HASH_H #include +#include namespace libantelope { @@ -34,6 +35,26 @@ namespace libantelope { typedef unsigned char ripemd160_t[20]; typedef unsigned char sha256_t[32]; +typedef internal::sha256_state sha256_ctx_t; +typedef internal::ripemd160_state ripemd160_ctx_t; + +/** + * Initialize a sha256_ctx_t structure + */ +int sha256_init(sha256_ctx_t* ctx); + +/** + * Update the sha256 hash value with the contents in `data` up to `len` bytes. + * This can be called repeatedly to hash chunks of data. + */ +int sha256_update(sha256_ctx_t* ctx, const void *data, std::size_t len); + +/** + * Place the message digest in out variable. + * The ctx's internal state is reset after this operation. + */ +int sha256_final(sha256_ctx_t* ctx, sha256_t* out); + /** * sha256 hashing function. * Hashes the content in `data` up to `len` bytes. The result is stored in `out`. @@ -48,6 +69,23 @@ sha256_t* sha256(const unsigned char *data, std::size_t len, sha256_t* out); */ sha256_t* sha256d(const unsigned char *data, std::size_t len, sha256_t* out); +/** + * Initialize a ripmemd160_ctx_t structure + */ +int ripemd160_init(ripemd160_ctx_t* ctx); + +/** + * Update the RipeMD160 hash value with the contents in `data` up to `len` bytes. + * This can be called repeatedly to hash chunks of data. + */ +int ripemd160_update(ripemd160_ctx_t* ctx, const void *data, std::size_t len); + +/** + * Places the RipeMD160 message digest in out variable. + * The ctx's internal state is reset after this operation. + */ +int ripemd160_final(ripemd160_ctx_t* ctx, ripemd160_t* out); + /** * RipeMD160 hashing function. * Hashes the content in `data` up to `len` bytes. The result is stored in `out`. diff --git a/src/openssl/hash.cpp b/src/openssl/hash.cpp index 492e23a..1fa07b3 100644 --- a/src/openssl/hash.cpp +++ b/src/openssl/hash.cpp @@ -27,6 +27,18 @@ namespace libantelope { +int sha256_init(struct sha256_ctx* ctx) { + return SHA256_Init((SHA256_CTX*)ctx); +} + +int sha256_update(sha256_ctx_t* ctx, const void *data, std::size_t len) { + return SHA256_Update((SHA256_CTX*)ctx, data, len); +} + +int sha256_final(sha256_ctx_t* ctx, sha256_t* out) { + return SHA256_Final((unsigned char*) out, (SHA256_CTX*)ctx); +} + sha256_t* sha256(const unsigned char *data, std::size_t len, sha256_t* out) { return (sha256_t *) SHA256(data, len, (unsigned char*) out); } @@ -36,6 +48,18 @@ sha256_t* sha256d(const unsigned char *data, std::size_t len, sha256_t* out) { return (sha256_t *) SHA256((unsigned char*) out, 32, (unsigned char*) out); } +int ripemd160_init(ripemd160_ctx_t* ctx) { + return RIPEMD160_Init((RIPEMD160_CTX*)ctx); +} + +int ripemd160_update(ripemd160_ctx_t* ctx, const void *data, std::size_t len) { + return RIPEMD160_Update((RIPEMD160_CTX*)ctx, data, len); +} + +int ripemd160_final(ripemd160_ctx_t* ctx, ripemd160_t* out) { + return RIPEMD160_Final((unsigned char*) out, (RIPEMD160_CTX*)ctx); +} + ripemd160_t* ripemd160(const unsigned char *data, std::size_t len, ripemd160_t* out) { return (ripemd160_t *) RIPEMD160(data, len, (unsigned char*) out); } From 225a1947ae4948d5228f2378f6a0e956295248a5 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Tue, 30 May 2023 13:55:07 +0200 Subject: [PATCH 44/58] include/libantelope/hash.hpp: split into hash/ripemd160.hpp and hash/sha256.hpp --- include/libantelope/hash.hpp | 72 +------------------------- include/libantelope/hash/ripemd160.hpp | 62 ++++++++++++++++++++++ include/libantelope/hash/sha256.hpp | 69 ++++++++++++++++++++++++ 3 files changed, 133 insertions(+), 70 deletions(-) create mode 100644 include/libantelope/hash/ripemd160.hpp create mode 100644 include/libantelope/hash/sha256.hpp diff --git a/include/libantelope/hash.hpp b/include/libantelope/hash.hpp index 67dd698..a471c67 100644 --- a/include/libantelope/hash.hpp +++ b/include/libantelope/hash.hpp @@ -24,75 +24,7 @@ #ifndef LIBANTELOPE_HASH_H #define LIBANTELOPE_HASH_H -#include -#include - -namespace libantelope { - -/** - * Hashes - */ -typedef unsigned char ripemd160_t[20]; -typedef unsigned char sha256_t[32]; - -typedef internal::sha256_state sha256_ctx_t; -typedef internal::ripemd160_state ripemd160_ctx_t; - -/** - * Initialize a sha256_ctx_t structure - */ -int sha256_init(sha256_ctx_t* ctx); - -/** - * Update the sha256 hash value with the contents in `data` up to `len` bytes. - * This can be called repeatedly to hash chunks of data. - */ -int sha256_update(sha256_ctx_t* ctx, const void *data, std::size_t len); - -/** - * Place the message digest in out variable. - * The ctx's internal state is reset after this operation. - */ -int sha256_final(sha256_ctx_t* ctx, sha256_t* out); - -/** - * sha256 hashing function. - * Hashes the content in `data` up to `len` bytes. The result is stored in `out`. - * Returns the same pointer as `out`. - */ -sha256_t* sha256(const unsigned char *data, std::size_t len, sha256_t* out); - -/** - * sha256 double hashing function. - * Hashes the content in `data` up to `len` bytes. The result is stored in `out`. - * Returns the same pointer as `out`. - */ -sha256_t* sha256d(const unsigned char *data, std::size_t len, sha256_t* out); - -/** - * Initialize a ripmemd160_ctx_t structure - */ -int ripemd160_init(ripemd160_ctx_t* ctx); - -/** - * Update the RipeMD160 hash value with the contents in `data` up to `len` bytes. - * This can be called repeatedly to hash chunks of data. - */ -int ripemd160_update(ripemd160_ctx_t* ctx, const void *data, std::size_t len); - -/** - * Places the RipeMD160 message digest in out variable. - * The ctx's internal state is reset after this operation. - */ -int ripemd160_final(ripemd160_ctx_t* ctx, ripemd160_t* out); - -/** - * RipeMD160 hashing function. - * Hashes the content in `data` up to `len` bytes. The result is stored in `out`. - * Returns the same pointer as `out`. - */ -ripemd160_t* ripemd160(const unsigned char *data, std::size_t len, ripemd160_t* out); - -} // namespace libantelope +#include +#include #endif /* LIBANTELOPE_HASH_H */ diff --git a/include/libantelope/hash/ripemd160.hpp b/include/libantelope/hash/ripemd160.hpp new file mode 100644 index 0000000..45ba4ff --- /dev/null +++ b/include/libantelope/hash/ripemd160.hpp @@ -0,0 +1,62 @@ +/** + * MIT License + * + * Copyright (c) 2019-2023 EOS Sw/eden + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef LIBANTELOPE_HASH_RIPEMD160_H +#define LIBANTELOPE_HASH_RIPEMD160_H + +#include +#include + +namespace libantelope { + +typedef unsigned char ripemd160_t[20]; + +typedef internal::ripemd160_state ripemd160_ctx_t; + +/** + * Initialize a ripmemd160_ctx_t structure + */ +int ripemd160_init(ripemd160_ctx_t* ctx); + +/** + * Update the RipeMD160 hash value with the contents in `data` up to `len` bytes. + * This can be called repeatedly to hash chunks of data. + */ +int ripemd160_update(ripemd160_ctx_t* ctx, const void *data, std::size_t len); + +/** + * Places the RipeMD160 message digest in out variable. + * The ctx's internal state is reset after this operation. + */ +int ripemd160_final(ripemd160_ctx_t* ctx, ripemd160_t* out); + +/** + * RipeMD160 hashing function. + * Hashes the content in `data` up to `len` bytes. The result is stored in `out`. + * Returns the same pointer as `out`. + */ +ripemd160_t* ripemd160(const unsigned char *data, std::size_t len, ripemd160_t* out); + +} // namespace libantelope + +#endif /* LIBANTELOPE_RIPEMD160_H */ diff --git a/include/libantelope/hash/sha256.hpp b/include/libantelope/hash/sha256.hpp new file mode 100644 index 0000000..3f79d4a --- /dev/null +++ b/include/libantelope/hash/sha256.hpp @@ -0,0 +1,69 @@ +/** + * MIT License + * + * Copyright (c) 2019-2023 EOS Sw/eden + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef LIBANTELOPE_HASH_SHA256_H +#define LIBANTELOPE_HASH_SHA256_H + +#include +#include + +namespace libantelope { + +typedef unsigned char sha256_t[32]; + +typedef internal::sha256_state sha256_ctx_t; + +/** + * Initialize a sha256_ctx_t structure + */ +int sha256_init(sha256_ctx_t* ctx); + +/** + * Update the sha256 hash value with the contents in `data` up to `len` bytes. + * This can be called repeatedly to hash chunks of data. + */ +int sha256_update(sha256_ctx_t* ctx, const void *data, std::size_t len); + +/** + * Place the message digest in out variable. + * The ctx's internal state is reset after this operation. + */ +int sha256_final(sha256_ctx_t* ctx, sha256_t* out); + +/** + * sha256 hashing function. + * Hashes the content in `data` up to `len` bytes. The result is stored in `out`. + * Returns the same pointer as `out`. + */ +sha256_t* sha256(const unsigned char *data, std::size_t len, sha256_t* out); + +/** + * sha256 double hashing function. + * Hashes the content in `data` up to `len` bytes. The result is stored in `out`. + * Returns the same pointer as `out`. + */ +sha256_t* sha256d(const unsigned char *data, std::size_t len, sha256_t* out); + +} // namespace libantelope + +#endif /* LIBANTELOPE_HASH_SHA256_H */ From c035a804b229494c3a9c88b92b053becc7de2692 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Tue, 30 May 2023 13:59:05 +0200 Subject: [PATCH 45/58] src/wif/k1.cpp: implement _checksum_suffix() using init/update/final ripemd160 functions. --- src/wif/k1.cpp | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/src/wif/k1.cpp b/src/wif/k1.cpp index f7e8af2..881e519 100644 --- a/src/wif/k1.cpp +++ b/src/wif/k1.cpp @@ -23,7 +23,7 @@ */ #include -#include +#include #include "codec.hpp" namespace libantelope { namespace internal { @@ -31,22 +31,23 @@ namespace libantelope { namespace internal { // Just to make it "harder" the calculated checksum for a signature (k1) and pub/priv keys in k1/r1 format. // has a suffix that is not present in the WIF encoded string. // So this function is a quick hack to calculate it. -// -// Should implement and use Init/Update/Finalize hash functions to do it inplace. -void _checksum_suffix(const unsigned char *in, size_t size, const char *suffix, checksum_t check) { - std::vector buf(size + 2); +void _checksum_suffix(const unsigned char *in, size_t size, checksum_t check) { + ripemd160_ctx_t ctx; + ripemd160_t md; - memcpy(buf.data(), in, size); - memcpy(buf.data() + size, suffix, 2); + ripemd160_init(&ctx); + ripemd160_update(&ctx, in, size); + ripemd160_update(&ctx, "K1", 2); + ripemd160_final(&ctx, &md); - return checksum_ripemd160(buf.data(), buf.size(), (unsigned char*) check); + std::memcpy(check, md, CHECKSUM_SIZE); } void pub_encoder_k1(const ec_pubkey_t& key, unsigned char *buf) { checksum_t check; - _checksum_suffix(key.data(), EC_PUBKEY_SIZE, "K1", check); + _checksum_suffix(key.data(), EC_PUBKEY_SIZE, check); memcpy(buf, key.data(), EC_PUBKEY_SIZE); memcpy(buf + EC_PUBKEY_SIZE, check, CHECKSUM_SIZE); @@ -56,7 +57,7 @@ bool pub_decoder_k1(const std::vector& buf, ec_pubkey_t& key) { checksum_t check; - _checksum_suffix(buf.data(), EC_PUBKEY_SIZE, "K1", check); + _checksum_suffix(buf.data(), EC_PUBKEY_SIZE, check); if (memcmp(buf.data() + EC_PUBKEY_SIZE, check, CHECKSUM_SIZE)) { return false; @@ -69,7 +70,7 @@ bool pub_decoder_k1(const std::vector& buf, ec_pubkey_t& key) { size_t priv_encoder_k1(const ec_privkey_t& priv, unsigned char *buf) { checksum_t check; - _checksum_suffix(priv.data(), EC_PRIVKEY_SIZE, "K1", check); + _checksum_suffix(priv.data(), EC_PRIVKEY_SIZE, check); memcpy(buf, priv.data(), priv.size()); memcpy(buf + EC_PRIVKEY_SIZE, check, CHECKSUM_SIZE); @@ -84,7 +85,7 @@ bool priv_decoder_k1(const std::vector& buf, ec_privkey_t& priv) } checksum_t check; - _checksum_suffix(buf.data(), EC_PRIVKEY_SIZE, "K1", check); + _checksum_suffix(buf.data(), EC_PRIVKEY_SIZE, check); if (memcmp(buf.data() + EC_PRIVKEY_SIZE, check, CHECKSUM_SIZE)) { return false; } @@ -97,7 +98,7 @@ void sig_encoder_k1(const ec_signature_t& sig, unsigned char *buf) { checksum_t check; - _checksum_suffix(sig.data(), EC_SIGNATURE_SIZE, "K1", check); + _checksum_suffix(sig.data(), EC_SIGNATURE_SIZE, check); memcpy(buf, sig.data(), sig.size()); memcpy(buf + EC_SIGNATURE_SIZE, check, CHECKSUM_SIZE); @@ -112,7 +113,7 @@ bool sig_decoder_k1(const std::vector& buf, ec_signature_t& sig) } // Calculate checksum - _checksum_suffix(buf.data(), EC_SIGNATURE_SIZE, "K1", check); + _checksum_suffix(buf.data(), EC_SIGNATURE_SIZE, check); // And validate if (memcmp(buf.data() + EC_SIGNATURE_SIZE, check, CHECKSUM_SIZE)) { From ec7a67ad883a9a00612c7f0fd2e85be9c08f01b6 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Wed, 31 May 2023 10:42:12 +0200 Subject: [PATCH 46/58] src/openssl/hash.cpp: fix sha256_init() signature. --- src/openssl/hash.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/openssl/hash.cpp b/src/openssl/hash.cpp index 1fa07b3..ce86071 100644 --- a/src/openssl/hash.cpp +++ b/src/openssl/hash.cpp @@ -27,7 +27,7 @@ namespace libantelope { -int sha256_init(struct sha256_ctx* ctx) { +int sha256_init(sha256_ctx_t* ctx) { return SHA256_Init((SHA256_CTX*)ctx); } From 93f6b1b0300fb9e3fcea523581535525f3721500 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Wed, 31 May 2023 10:45:25 +0200 Subject: [PATCH 47/58] Adding tests/include/testing.h --- tests/include/testing.h | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 tests/include/testing.h diff --git a/tests/include/testing.h b/tests/include/testing.h new file mode 100644 index 0000000..6a7206e --- /dev/null +++ b/tests/include/testing.h @@ -0,0 +1,8 @@ +#ifndef LIBANTELOPE_TESTING_H +#define LIBANTELOPE_TESTING_H + +#include + +#define CHECK_PRED(a,b,pred) if (pred) { CHECK_EQ((a), (b)); } else { CHECK_NE((a), (b)); } + +#endif /* LIBANTELOPE_TESTING_H */ \ No newline at end of file From 84645175c1c319b840a4b006a5ab55f07f6f404f Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Wed, 31 May 2023 10:46:14 +0200 Subject: [PATCH 48/58] tests: Adding tests for sha256 and ripemd160 hashing functions. --- tests/CMakeLists.txt | 4 + tests/hash/ripemd160.cpp | 115 ++++++++++++++++++++++++++ tests/hash/sha256.cpp | 174 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 293 insertions(+) create mode 100644 tests/hash/ripemd160.cpp create mode 100644 tests/hash/sha256.cpp diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index c1a8429..5776caf 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -2,6 +2,10 @@ set(TEST_SRC main.cpp + # hash + hash/sha256.cpp + hash/ripemd160.cpp + # ec ec/generate.cpp ec/pubkey.cpp diff --git a/tests/hash/ripemd160.cpp b/tests/hash/ripemd160.cpp new file mode 100644 index 0000000..086605a --- /dev/null +++ b/tests/hash/ripemd160.cpp @@ -0,0 +1,115 @@ +#include +#include +#include +#include + +TEST_CASE("hash::ripemd160::ripemd160") { + + struct testcase { + const char *name; + std::string input; + libantelope::ripemd160_t expected; + bool valid; + }; + + std::vector tests = { + { + "valid #1", + "Morbi at egestas risus. Praesent blandit pharetra urna, nec porttitor risus sodales eu. Cras et volutpat elit, porta dapibus ipsum. Donec facilisis, eros nec imperdiet tristique, purus eros malesuada neque, quis interdum nisl risus nec leo.", + { + 0xd9, 0x6a, 0x48, 0xf8, 0x2b, 0x39, 0xa9, 0x9f, + 0x22, 0xba, 0x3e, 0x01, 0x58, 0x5b, 0x15, 0xc7, + 0x7b, 0x0e, 0x5f, 0x50, + }, + true + }, + { + "valid #2", + "Donec eget mattis velit, vel vulputate sem. Suspendisse vulputate dolor vel est facilisis congue. Nulla non leo nulla. Proin lorem elit, sagittis eget congue in, pellentesque sed nisi. In pulvinar tortor fermentum suscipit varius.", + { + 0x87, 0x1b, 0x87, 0xde, 0x2e, 0xb6, 0x8b, 0xb6, + 0xdc, 0x29, 0xe7, 0x40, 0xc3, 0xd1, 0x99, 0x42, + 0xad, 0x1a, 0xe3, 0x57 + }, + true + } + }; + + for(auto it = tests.begin(); it != tests.end(); it++) { + SUBCASE(it->name) { + libantelope::ripemd160_t dgst; + + CHECK( libantelope::ripemd160((const unsigned char*) it->input.c_str(), it->input.size(), &dgst) == &dgst ); + + CHECK_PRED(doctest::toString(dgst), doctest::toString(it->expected), it->valid); + } + } +} + +TEST_CASE("hash::ripemd160::init/update/final") { + + struct testcase { + const char *name; + std::vector inputs; + libantelope::ripemd160_t expected; + }; + + std::vector tests = { + { + "valid #1", + { + "tortor in congue luctus, tortor sapien condimentum quam, ac congue enim lacus vitae erat. Mauris dapibus eros bibendum", + "Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia curae", + }, + { + 0x30, 0x77, 0xaf, 0x6b, 0x43, 0x0b, 0x94, 0x8d, + 0x59, 0x4e, 0xc7, 0xbb, 0x1a, 0x2b, 0xc3, 0x08, + 0xaa, 0xf0, 0x75, 0x3a + } + }, + { + "valid #2", + { + "Cras suscipit, mi sit amet pretium blandit, massa felis aliquet eros", + "Aenean efficitur nibh quis enim mollis blandit", + "Vestibulum posuere tempus mi nec cursus" + }, + { + 0xf1, 0xcf, 0xea, 0xf7, 0xef, 0x3a, 0x0f, 0x80, + 0x26, 0x75, 0x40, 0x75, 0xe0, 0x9d, 0x89, 0x05, + 0xd1, 0x29, 0xe5, 0xf6 + } + }, + { + "valid #3", + { + "Donec nec blandit dui. Nulla et tempus odio, id fermentum neque. Nam vitae nunc leo. Aliquam dictum velit nec neque dignissim maximus nec at tellus", + "Proin elementum porttitor odio, ut ullamcorper justo rutrum in. Proin dignissim nec diam a eleifend. Duis consequat ultrices purus sed finibus", + "Donec eget ante dictum, scelerisque metus eget, mollis velit. Curabitur elementum fermentum lorem, a fringilla velit ultrices non" + }, + { + 0xbb, 0x25, 0x58, 0xa9, 0xd0, 0xc1, 0x23, 0xef, + 0x55, 0xac, 0x2d, 0x8c, 0xd5, 0xd6, 0xe1, 0x49, + 0x00, 0x5d, 0x86, 0xe8 + } + }, + }; + + for(auto it = tests.begin(); it != tests.end(); it++) { + SUBCASE(it->name) { + libantelope::ripemd160_ctx_t ctx; + libantelope::ripemd160_t dgst; + + CHECK_EQ(libantelope::ripemd160_init(&ctx), 1); + + for (auto in_it = it->inputs.begin(); in_it != it->inputs.end(); in_it++ ) { + CHECK_EQ(libantelope::ripemd160_update(&ctx, (const unsigned char*) in_it->c_str(), in_it->size()), 1); + } + + CHECK_EQ(libantelope::ripemd160_final(&ctx, &dgst), 1); + + CHECK( doctest::toString(dgst) == doctest::toString(it->expected) ); + } + } + +} \ No newline at end of file diff --git a/tests/hash/sha256.cpp b/tests/hash/sha256.cpp new file mode 100644 index 0000000..88b2744 --- /dev/null +++ b/tests/hash/sha256.cpp @@ -0,0 +1,174 @@ +#include +#include +#include +#include + +TEST_CASE("hash::sha256::sha256") { + + struct testcase { + const char *name; + std::string input; + libantelope::sha256_t expected; + bool valid; + }; + + std::vector tests = { + { + "valid #1", + "Suspendisse ut tincidunt quam. Praesent scelerisque risus vitae est consectetur, sed facilisis sem luctus. Praesent aliquet eros quis leo sodales, eget blandit diam scelerisque.", + { + 0x1e, 0x54, 0x96, 0x86, 0x2f, 0x39, 0x44, 0xea, + 0x42, 0xa9, 0x0f, 0xad, 0x56, 0x79, 0x4b, 0x77, + 0x8f, 0xcc, 0x54, 0xf7, 0x7a, 0x32, 0x60, 0x37, + 0x4b, 0xac, 0xd5, 0x65, 0x74, 0xf7, 0xcf, 0x6c + }, + true + }, + { + "valid #2", + "Phasellus consectetur augue vitae massa vulputate placerat. Pellentesque nec eros a velit bibendum venenatis sit amet et augue. Morbi malesuada facilisis consequat.", + { + 0x7c, 0x79, 0x4a, 0xf4, 0x9b, 0x5b, 0xb4, 0x0c, + 0xef, 0x4f, 0xaa, 0x65, 0xa4, 0x7c, 0x5f, 0xc5, + 0x95, 0x69, 0x49, 0x99, 0x6b, 0x08, 0x9b, 0xc0, + 0x40, 0x2d, 0x57, 0x8a, 0x90, 0x02, 0x42, 0x32, + }, + true + } + }; + + for(auto it = tests.begin(); it != tests.end(); it++) { + SUBCASE(it->name) { + libantelope::sha256_t dgst; + + CHECK( libantelope::sha256((const unsigned char*) it->input.c_str(), it->input.size(), &dgst) == &dgst ); + + CHECK_PRED(doctest::toString(dgst), doctest::toString(it->expected), it->valid); + } + } +} + +TEST_CASE("hash::sha256::sha256d") { + + struct testcase { + const char *name; + std::string input; + libantelope::sha256_t expected; + }; + + std::vector tests = { + { + "valid #1", + "Suspendisse ut tincidunt quam. Praesent scelerisque risus vitae est consectetur, sed facilisis sem luctus. Praesent aliquet eros quis leo sodales, eget blandit diam scelerisque.", + { + 0x4b, 0x6f, 0xa1, 0xf6, 0x30, 0x1e, 0xbe, 0x4a, + 0xc7, 0xef, 0x1e, 0x55, 0x3e, 0xdb, 0xc1, 0x31, + 0x1f, 0x6b, 0xf5, 0xc8, 0x04, 0xe9, 0x0e, 0xe3, + 0xbe, 0x66, 0x01, 0xbf, 0x70, 0x9f, 0x8e, 0x80, + } + }, + { + "valid #2", + "Vivamus ut elementum justo. Vestibulum lobortis rutrum libero sollicitudin aliquet. Nullam tempor urna non odio iaculis, sed pretium quam porttitor. Pellentesque pretium, justo vitae tristique porttitor, diam massa pulvinar neque, sed lacinia mi nulla sed nisi.", + { + 0x1f, 0x3f, 0x1c, 0x48, 0xf6, 0xee, 0x24, 0x1f, + 0x6c, 0x41, 0x86, 0x69, 0xe3, 0x2f, 0x5e, 0x4d, + 0xa5, 0x51, 0x04, 0x8b, 0x11, 0x35, 0x47, 0xad, + 0x7e, 0xd9, 0xfb, 0x2e, 0x59, 0xee, 0x66, 0x21, + } + }, + { + "valid #3", + "Praesent ultrices consequat risus luctus faucibus.", + { + 0xd5, 0x5f, 0x9c, 0xda, 0x2d, 0x93, 0x32, 0xc2, + 0x9b, 0xb1, 0xbb, 0x14, 0x55, 0x80, 0x72, 0xb7, + 0xba, 0x13, 0xa8, 0xc6, 0xa6, 0xbc, 0x65, 0xfc, + 0x49, 0xe0, 0x3b, 0x23, 0x04, 0x2a, 0x92, 0x8d, + } + } + }; + + for(auto it = tests.begin(); it != tests.end(); it++) { + SUBCASE(it->name) { + libantelope::sha256_t dgst; + + CHECK( libantelope::sha256d((const unsigned char*) it->input.c_str(), it->input.size(), &dgst) == &dgst ); + + CHECK( doctest::toString(dgst) == doctest::toString(it->expected) ); + } + } +} + +TEST_CASE("hash::sha256::init/update/final") { + + struct testcase { + const char *name; + std::vector inputs; + libantelope::sha256_t expected; + }; + + std::vector tests = { + { + "valid #1", + { + "Donec vestibulum enim commodo, faucibus nisi non, mattis quam.", + "Nam sed nunc dapibus, auctor risus placerat, aliquet dolor", + }, + { + 0x48, 0xc2, 0x34, 0x93, 0x3d, 0xae, 0x0d, 0xd0, + 0x28, 0xff, 0x5c, 0xa0, 0xca, 0xb1, 0x0a, 0xa3, + 0xe2, 0xa0, 0xa4, 0x7e, 0xb2, 0x71, 0xa5, 0x28, + 0x41, 0x03, 0x72, 0x20, 0xb5, 0x23, 0xc3, 0x67, + } + }, + { + "valid #2", + { + "In tempus, lectus ac molestie venenatis, enim purus suscipit tortor", + "sed sodales massa condimentum a", + "Integer sit amet pretium magna", + "Aenean non accumsan eros. Donec imperdiet justo tempor magna tincidunt malesuada", + "Duis eu tortor ac massa sagittis elementum" + }, + { + 0xfb, 0x12, 0x31, 0x9c, 0x2c, 0xe4, 0x94, 0x29, + 0xc9, 0xd3, 0xc7, 0x84, 0x0c, 0x58, 0x3d, 0x4c, + 0xde, 0xb5, 0x36, 0x59, 0x46, 0x69, 0xe1, 0x63, + 0xc5, 0x75, 0xb6, 0x94, 0x41, 0x5a, 0xd4, 0x62, + } + }, + { + "valid #3", + { + "Donec tempus pellentesque lobortis. Integer pellentesque feugiat enim ac suscipit. Curabitur urna quam, condimentum sed bibendum eu", + "Nullam lacinia ligula at ex gravida fermentum. Integer scelerisque accumsan iaculis. Suspendisse quis eros ut orci sollicitudin dignissim", + "Nulla ligula tortor, tristique eget feugiat non, vehicula sit amet velit. Proin fermentum sagittis tincidunt. Nullam condimentum dapibus magna", + }, + { + 0x19, 0xfb, 0x71, 0xb1, 0x47, 0x01, 0x7f, 0xf5, + 0xeb, 0xda, 0xc2, 0xd8, 0xe7, 0xab, 0xc9, 0xcb, + 0xea, 0x7d, 0x13, 0xa0, 0x2e, 0xe8, 0x48, 0x94, + 0x67, 0xc5, 0x14, 0xbf, 0x7d, 0x6f, 0x96, 0x83, + } + }, + }; + + for(auto it = tests.begin(); it != tests.end(); it++) { + SUBCASE(it->name) { + libantelope::sha256_ctx_t ctx; + libantelope::sha256_t dgst; + + CHECK_EQ(libantelope::sha256_init(&ctx), 1); + + for (auto in_it = it->inputs.begin(); in_it != it->inputs.end(); in_it++ ) { + CHECK_EQ(libantelope::sha256_update(&ctx, (const unsigned char*) in_it->c_str(), in_it->size()), 1); + } + + CHECK_EQ(libantelope::sha256_final(&ctx, &dgst), 1); + + CHECK( doctest::toString(dgst) == doctest::toString(it->expected) ); + } + } + +} \ No newline at end of file From e0d0bcb0cb7d554b2cc74dcbb8425ccb10bd15ea Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Wed, 31 May 2023 10:47:44 +0200 Subject: [PATCH 49/58] include/libantelope/internal/hash.hpp: don't include OpenSSL headers here. instead we declare the state variables as plain arrays. --- include/libantelope/internal/hash.hpp | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/include/libantelope/internal/hash.hpp b/include/libantelope/internal/hash.hpp index d2a889d..62107ff 100644 --- a/include/libantelope/internal/hash.hpp +++ b/include/libantelope/internal/hash.hpp @@ -29,13 +29,10 @@ #ifdef LIBANTELOPE_HASHIMPL_OPENSSL -#include -#include - namespace libantelope { namespace internal { - typedef SHA256_CTX sha256_state; - typedef RIPEMD160_CTX ripemd160_state; + typedef unsigned char sha256_state[112]; + typedef unsigned char ripemd160_state[96]; } } // namespace libantelope::internal #else From 7e9bd41a83710b4b68abca1dfa524796b264398d Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Wed, 31 May 2023 10:48:52 +0200 Subject: [PATCH 50/58] src/libsecp256k1/rng.h: fix C467 warning on MSVC compilers. --- src/libsecp256k1/rng.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/libsecp256k1/rng.h b/src/libsecp256k1/rng.h index 103583f..f476963 100644 --- a/src/libsecp256k1/rng.h +++ b/src/libsecp256k1/rng.h @@ -42,12 +42,17 @@ static int fill_random(unsigned char* data, size_t size) { #if defined(_WIN32) +/* Disable C4267 Warning (dataloss when casting variable to smaller size) temporarily */ +#pragma warning( push ) +#pragma warning( disable: 4267 ) NTSTATUS res = BCryptGenRandom(NULL, data, size, BCRYPT_USE_SYSTEM_PREFERRED_RNG); +#pragma warning( pop ) if (res != STATUS_SUCCESS || size > ULONG_MAX) { return 0; } else { return 1; } + #elif defined(__linux__) || defined(__FreeBSD__) /* If `getrandom(2)` is not available you should fallback to /dev/urandom */ ssize_t res = getrandom(data, size, 0); From e15a5ede18481f28f17b12ebaf318ea2cce82ff6 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Wed, 31 May 2023 14:41:36 +0200 Subject: [PATCH 51/58] tests/hash: fix bug in MSVC compiler. --- tests/hash/ripemd160.cpp | 3 +++ tests/hash/sha256.cpp | 6 ++++++ 2 files changed, 9 insertions(+) diff --git a/tests/hash/ripemd160.cpp b/tests/hash/ripemd160.cpp index 086605a..383a231 100644 --- a/tests/hash/ripemd160.cpp +++ b/tests/hash/ripemd160.cpp @@ -52,6 +52,9 @@ TEST_CASE("hash::ripemd160::init/update/final") { const char *name; std::vector inputs; libantelope::ripemd160_t expected; +#ifdef _MSC_VER + char _; // ripemd160_t can't be last, wierd compiler bug on MSVC +#endif }; std::vector tests = { diff --git a/tests/hash/sha256.cpp b/tests/hash/sha256.cpp index 88b2744..8477cea 100644 --- a/tests/hash/sha256.cpp +++ b/tests/hash/sha256.cpp @@ -54,6 +54,9 @@ TEST_CASE("hash::sha256::sha256d") { const char *name; std::string input; libantelope::sha256_t expected; +#ifdef _MSC_VER + char _; // sha256_t can't be last, wierd compiler bug on MSVC +#endif }; std::vector tests = { @@ -106,6 +109,9 @@ TEST_CASE("hash::sha256::init/update/final") { const char *name; std::vector inputs; libantelope::sha256_t expected; +#ifdef _MSC_VER + char _; // sha256_t can't be last, wierd compiler bug on MSVC +#endif }; std::vector tests = { From 9ad2e3cc007c3aff87a645fcac0eacda9a82a685 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Wed, 31 May 2023 15:03:07 +0200 Subject: [PATCH 52/58] Adding tests/cmake/doctest.cmake --- tests/cmake/doctest.cmake | 189 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 189 insertions(+) create mode 100644 tests/cmake/doctest.cmake diff --git a/tests/cmake/doctest.cmake b/tests/cmake/doctest.cmake new file mode 100644 index 0000000..f0bc593 --- /dev/null +++ b/tests/cmake/doctest.cmake @@ -0,0 +1,189 @@ +# Distributed under the OSI-approved BSD 3-Clause License. See accompanying +# file Copyright.txt or https://cmake.org/licensing for details. + +#[=======================================================================[.rst: +doctest +----- + +This module defines a function to help use the doctest test framework. + +The :command:`doctest_discover_tests` discovers tests by asking the compiled test +executable to enumerate its tests. This does not require CMake to be re-run +when tests change. However, it may not work in a cross-compiling environment, +and setting test properties is less convenient. + +This command is intended to replace use of :command:`add_test` to register +tests, and will create a separate CTest test for each doctest test case. Note +that this is in some cases less efficient, as common set-up and tear-down logic +cannot be shared by multiple test cases executing in the same instance. +However, it provides more fine-grained pass/fail information to CTest, which is +usually considered as more beneficial. By default, the CTest test name is the +same as the doctest name; see also ``TEST_PREFIX`` and ``TEST_SUFFIX``. + +.. command:: doctest_discover_tests + + Automatically add tests with CTest by querying the compiled test executable + for available tests:: + + doctest_discover_tests(target + [TEST_SPEC arg1...] + [EXTRA_ARGS arg1...] + [WORKING_DIRECTORY dir] + [TEST_PREFIX prefix] + [TEST_SUFFIX suffix] + [PROPERTIES name1 value1...] + [ADD_LABELS value] + [TEST_LIST var] + [JUNIT_OUTPUT_DIR dir] + ) + + ``doctest_discover_tests`` sets up a post-build command on the test executable + that generates the list of tests by parsing the output from running the test + with the ``--list-test-cases`` argument. This ensures that the full + list of tests is obtained. Since test discovery occurs at build time, it is + not necessary to re-run CMake when the list of tests changes. + However, it requires that :prop_tgt:`CROSSCOMPILING_EMULATOR` is properly set + in order to function in a cross-compiling environment. + + Additionally, setting properties on tests is somewhat less convenient, since + the tests are not available at CMake time. Additional test properties may be + assigned to the set of tests as a whole using the ``PROPERTIES`` option. If + more fine-grained test control is needed, custom content may be provided + through an external CTest script using the :prop_dir:`TEST_INCLUDE_FILES` + directory property. The set of discovered tests is made accessible to such a + script via the ``_TESTS`` variable. + + The options are: + + ``target`` + Specifies the doctest executable, which must be a known CMake executable + target. CMake will substitute the location of the built executable when + running the test. + + ``TEST_SPEC arg1...`` + Specifies test cases, wildcarded test cases, tags and tag expressions to + pass to the doctest executable with the ``--list-test-cases`` argument. + + ``EXTRA_ARGS arg1...`` + Any extra arguments to pass on the command line to each test case. + + ``WORKING_DIRECTORY dir`` + Specifies the directory in which to run the discovered test cases. If this + option is not provided, the current binary directory is used. + + ``TEST_PREFIX prefix`` + Specifies a ``prefix`` to be prepended to the name of each discovered test + case. This can be useful when the same test executable is being used in + multiple calls to ``doctest_discover_tests()`` but with different + ``TEST_SPEC`` or ``EXTRA_ARGS``. + + ``TEST_SUFFIX suffix`` + Similar to ``TEST_PREFIX`` except the ``suffix`` is appended to the name of + every discovered test case. Both ``TEST_PREFIX`` and ``TEST_SUFFIX`` may + be specified. + + ``PROPERTIES name1 value1...`` + Specifies additional properties to be set on all tests discovered by this + invocation of ``doctest_discover_tests``. + + ``ADD_LABELS value`` + Specifies if the test labels should be set automatically. + + ``TEST_LIST var`` + Make the list of tests available in the variable ``var``, rather than the + default ``_TESTS``. This can be useful when the same test + executable is being used in multiple calls to ``doctest_discover_tests()``. + Note that this variable is only available in CTest. + + ``JUNIT_OUTPUT_DIR dir`` + If specified, the parameter is passed along with ``--reporters=junit`` + and ``--out=`` to the test executable. The actual file name is the same + as the test target, including prefix and suffix. This should be used + instead of EXTRA_ARGS to avoid race conditions writing the XML result + output when using parallel test execution. + +#]=======================================================================] + +#------------------------------------------------------------------------------ +function(doctest_discover_tests TARGET) + cmake_parse_arguments( + "" + "" + "TEST_PREFIX;TEST_SUFFIX;WORKING_DIRECTORY;TEST_LIST;JUNIT_OUTPUT_DIR" + "TEST_SPEC;EXTRA_ARGS;PROPERTIES;ADD_LABELS" + ${ARGN} + ) + + if(NOT _WORKING_DIRECTORY) + set(_WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}") + endif() + if(NOT _TEST_LIST) + set(_TEST_LIST ${TARGET}_TESTS) + endif() + + ## Generate a unique name based on the extra arguments + string(SHA1 args_hash "${_TEST_SPEC} ${_EXTRA_ARGS}") + string(SUBSTRING ${args_hash} 0 7 args_hash) + + # Define rule to generate test list for aforementioned test executable + set(ctest_include_file "${CMAKE_CURRENT_BINARY_DIR}/${TARGET}_include-${args_hash}.cmake") + set(ctest_tests_file "${CMAKE_CURRENT_BINARY_DIR}/${TARGET}_tests-${args_hash}.cmake") + get_property(crosscompiling_emulator + TARGET ${TARGET} + PROPERTY CROSSCOMPILING_EMULATOR + ) + add_custom_command( + TARGET ${TARGET} POST_BUILD + BYPRODUCTS "${ctest_tests_file}" + COMMAND "${CMAKE_COMMAND}" + -D "TEST_TARGET=${TARGET}" + -D "TEST_EXECUTABLE=$" + -D "TEST_EXECUTOR=${crosscompiling_emulator}" + -D "TEST_WORKING_DIR=${_WORKING_DIRECTORY}" + -D "TEST_SPEC=${_TEST_SPEC}" + -D "TEST_EXTRA_ARGS=${_EXTRA_ARGS}" + -D "TEST_PROPERTIES=${_PROPERTIES}" + -D "TEST_ADD_LABELS=${_ADD_LABELS}" + -D "TEST_PREFIX=${_TEST_PREFIX}" + -D "TEST_SUFFIX=${_TEST_SUFFIX}" + -D "TEST_LIST=${_TEST_LIST}" + -D "TEST_JUNIT_OUTPUT_DIR=${_JUNIT_OUTPUT_DIR}" + -D "CTEST_FILE=${ctest_tests_file}" + -P "${_DOCTEST_DISCOVER_TESTS_SCRIPT}" + VERBATIM + ) + + file(WRITE "${ctest_include_file}" + "if(EXISTS \"${ctest_tests_file}\")\n" + " include(\"${ctest_tests_file}\")\n" + "else()\n" + " add_test(${TARGET}_NOT_BUILT-${args_hash} ${TARGET}_NOT_BUILT-${args_hash})\n" + "endif()\n" + ) + + if(NOT CMAKE_VERSION VERSION_LESS 3.10) + # Add discovered tests to directory TEST_INCLUDE_FILES + set_property(DIRECTORY + APPEND PROPERTY TEST_INCLUDE_FILES "${ctest_include_file}" + ) + else() + # Add discovered tests as directory TEST_INCLUDE_FILE if possible + get_property(test_include_file_set DIRECTORY PROPERTY TEST_INCLUDE_FILE SET) + if(NOT ${test_include_file_set}) + set_property(DIRECTORY + PROPERTY TEST_INCLUDE_FILE "${ctest_include_file}" + ) + else() + message(FATAL_ERROR + "Cannot set more than one TEST_INCLUDE_FILE" + ) + endif() + endif() + +endfunction() + +############################################################################### + +set(_DOCTEST_DISCOVER_TESTS_SCRIPT + ${CMAKE_CURRENT_LIST_DIR}/doctestAddTests.cmake +) \ No newline at end of file From 12e779c8cfcb1f6d842a25b11ed7eb2533133e6e Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Wed, 31 May 2023 15:03:19 +0200 Subject: [PATCH 53/58] Adding tests/cmake/doctestAddTests.cmake --- tests/cmake/doctestAddTests.cmake | 120 ++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 tests/cmake/doctestAddTests.cmake diff --git a/tests/cmake/doctestAddTests.cmake b/tests/cmake/doctestAddTests.cmake new file mode 100644 index 0000000..b9e2b2e --- /dev/null +++ b/tests/cmake/doctestAddTests.cmake @@ -0,0 +1,120 @@ +# Distributed under the OSI-approved BSD 3-Clause License. See accompanying +# file Copyright.txt or https://cmake.org/licensing for details. + +set(prefix "${TEST_PREFIX}") +set(suffix "${TEST_SUFFIX}") +set(spec ${TEST_SPEC}) +set(extra_args ${TEST_EXTRA_ARGS}) +set(properties ${TEST_PROPERTIES}) +set(add_labels ${TEST_ADD_LABELS}) +set(junit_output_dir "${TEST_JUNIT_OUTPUT_DIR}") +set(script) +set(suite) +set(tests) + +function(add_command NAME) + set(_args "") + foreach(_arg ${ARGN}) + if(_arg MATCHES "[^-./:a-zA-Z0-9_]") + set(_args "${_args} [==[${_arg}]==]") # form a bracket_argument + else() + set(_args "${_args} ${_arg}") + endif() + endforeach() + set(script "${script}${NAME}(${_args})\n" PARENT_SCOPE) +endfunction() + +# Run test executable to get list of available tests +if(NOT EXISTS "${TEST_EXECUTABLE}") + message(FATAL_ERROR + "Specified test executable '${TEST_EXECUTABLE}' does not exist" + ) +endif() + +if("${spec}" MATCHES .) + set(spec "--test-case=${spec}") +endif() + +execute_process( + COMMAND ${TEST_EXECUTOR} "${TEST_EXECUTABLE}" ${spec} --list-test-cases + OUTPUT_VARIABLE output + RESULT_VARIABLE result + WORKING_DIRECTORY "${TEST_WORKING_DIR}" +) +if(NOT ${result} EQUAL 0) + message(FATAL_ERROR + "Error running test executable '${TEST_EXECUTABLE}':\n" + " Result: ${result}\n" + " Output: ${output}\n" + ) +endif() + +string(REPLACE "\n" ";" output "${output}") + +# Parse output +foreach(line ${output}) + if("${line}" STREQUAL "===============================================================================" OR "${line}" MATCHES [==[^\[doctest\] ]==]) + continue() + endif() + set(test ${line}) + set(labels "") + if(${add_labels}) + # get test suite that test belongs to + execute_process( + COMMAND ${TEST_EXECUTOR} "${TEST_EXECUTABLE}" --test-case=${test} --list-test-suites + OUTPUT_VARIABLE labeloutput + RESULT_VARIABLE labelresult + WORKING_DIRECTORY "${TEST_WORKING_DIR}" + ) + if(NOT ${labelresult} EQUAL 0) + message(FATAL_ERROR + "Error running test executable '${TEST_EXECUTABLE}':\n" + " Result: ${labelresult}\n" + " Output: ${labeloutput}\n" + ) + endif() + + string(REPLACE "\n" ";" labeloutput "${labeloutput}") + foreach(labelline ${labeloutput}) + if("${labelline}" STREQUAL "===============================================================================" OR "${labelline}" MATCHES [==[^\[doctest\] ]==]) + continue() + endif() + list(APPEND labels ${labelline}) + endforeach() + endif() + + if(NOT "${junit_output_dir}" STREQUAL "") + # turn testname into a valid filename by replacing all special characters with "-" + string(REGEX REPLACE "[/\\:\"|<>]" "-" test_filename "${test}") + set(TEST_JUNIT_OUTPUT_PARAM "--reporters=junit" "--out=${junit_output_dir}/${prefix}${test_filename}${suffix}.xml") + else() + unset(TEST_JUNIT_OUTPUT_PARAM) + endif() + # use escape commas to handle properly test cases with commas inside the name + string(REPLACE "," "\\," test_name ${test}) + # ...and add to script + add_command(add_test + "${prefix}${test}${suffix}" + ${TEST_EXECUTOR} + "${TEST_EXECUTABLE}" + "--test-case=${test_name}" + "${TEST_JUNIT_OUTPUT_PARAM}" + ${extra_args} + ) + add_command(set_tests_properties + "${prefix}${test}${suffix}" + PROPERTIES + WORKING_DIRECTORY "${TEST_WORKING_DIR}" + ${properties} + LABELS ${labels} + ) + unset(labels) + list(APPEND tests "${prefix}${test}${suffix}") +endforeach() + +# Create a list of all discovered tests, which users may use to e.g. set +# properties on the tests +add_command(set ${TEST_LIST} ${tests}) + +# Write CTest script +file(WRITE "${CTEST_FILE}" "${script}") \ No newline at end of file From afc199e739a3f329ca6d51b427446baa3f8f7bfb Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Wed, 31 May 2023 15:05:35 +0200 Subject: [PATCH 54/58] tests/CMakeLists.txt: use doctest_discover_tests() macro to add all doctest test cases with a separate CTest test. --- tests/CMakeLists.txt | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 5776caf..3360fbe 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,4 +1,6 @@ +include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/doctest.cmake) + set(TEST_SRC main.cpp @@ -30,10 +32,7 @@ add_executable(doctest ${TEST_SRC}) target_link_libraries(doctest PRIVATE ${LIB_NAME}) target_include_directories(doctest PRIVATE ${CMAKE_CURRENT_LIST_DIR}/include) -add_test( - NAME doctest - COMMAND $ -ni -fc -) +doctest_discover_tests(doctest) if (WITH_BENCHMARK) add_subdirectory( benchmark ) From 1801c8424a356bf1308e3847e9362151f505c262 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Wed, 31 May 2023 15:06:58 +0200 Subject: [PATCH 55/58] Version 0.2.1 --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 30ba605..172508b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,7 +6,7 @@ cmake_minimum_required(VERSION 3.15) # Project name and version project(libantelope - VERSION 0.2.0 + VERSION 0.2.1 DESCRIPTION "C++ library for Antelope IO" HOMEPAGE_URL "https://github.com/eosswedenorg/libantelope" LANGUAGES C CXX From cadb1ff984e1907488a591506b04fdfc7d798c49 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Thu, 1 Jun 2023 14:45:00 +0200 Subject: [PATCH 56/58] Rework README.md --- README.md | 109 +++++++++++++++++++++--------------------------------- 1 file changed, 43 insertions(+), 66 deletions(-) diff --git a/README.md b/README.md index 6c22bcc..93b8c36 100644 --- a/README.md +++ b/README.md @@ -4,130 +4,107 @@ # libantelope -Independent C++ library for [Antelope IO](https://antelope.io) (former [libeosio](https://github.com/eosswedenorg/libeosio)) - -NOTE: This repository has no connection to the official Antelope code. +libantelope is an independent C++ library designed for Antelope IO (formerly known as libeosio). Please note that this repository is not affiliated with the official Antelope code. ## Compiling the library -You will need `openssl` development files (version 1.1 or later) to compile and `cmake 3.15` or later to compile this project. +To compile this project, you will need the following: + +- `openssl` development files (version 1.1 or later) +- `cmake 3.15` or later ### Elliptic curve backend -There is two different backend implementation for the elliptic curve part of the library: +The library offers two different backend implementations for the elliptic curve functionality: -* `OpenSSL` as mentioned before. however you still need to link to openssl even if it is not used as the EC backend - because more of the codebase uses it. +- `libsecp256k1` +- `OpenSSL`: Although the default is to use `libsecp256k1` for optimization, you still need to link to OpenSSL as other parts of the codebase rely on it. -* `libsecp256k1` - -Default is to use `libsecp256k1` as it is more optimized. - -You can switch implementation by modifing the cmake variable `EC_LIB`. +To switch the implementation, modify the `EC_LIB` variable in the cmake. ### CMake -You can install `cmake` by reading the [official guide](https://cmake.org/install). +You can install `cmake` by referring to the [official guide](https://cmake.org/install). ### Linux -**NOTE:** Only Ubuntu `20.04` and `22.04` is officially supported. +**NOTE:** Only Ubuntu versions `20.04` and `22.04` are officially supported. -The project should compile fine on most versions/distros but it is only tested -and distributed for Ubuntu `20.04` and `22.04` by [Sw/eden](https://www.eossweden.org). +While the project should compile fine on most versions/distros, it is only tested and distributed for Ubuntu `20.04` and `22.04` by [Sw/eden](https://www.eossweden.org). #### Dependencies -**Ubuntu (or other debian based distros)** +**Ubuntu (or other Debian-based distros)** -First you need to have a compiler, `openssl` and `cmake`. this can be installed with apt. +To install the necessary dependencies (compiler, `openssl`, and `cmake`), use the following `apt` command: ```sh -$ apt-get install gcc g++ cmake libssl-dev +apt-get install gcc g++ cmake libssl-dev ``` -If you need a newer version of cmake then apt provides. -Checkout the official [CMake APT repository](https://apt.kitware.com/). + +If you require a newer version of `cmake`, you can refer to the [official CMake APT repository](https://apt.kitware.com/). **Other** -Consult your package manager's manual for getting `openssl`,`g++` and `cmake` installed. - -If you need a newer version of cmake then your package manager provides. checkout the [official guide](https://cmake.org/install). +For other distros, please consult your package manager's manual to install `openssl`, `g++`, and `cmake`. If you need a newer version of `cmake`, you can follow the [official installation guide](https://cmake.org/install). ### MacOS #### Dependencies -You must have a compiler installed. This project is known to build with `Xcode 11.0` but other versions should work. +Ensure that you have a compiler installed. This project is known to build with `Xcode 11.0`, but other versions should work as well. + +To install `openssl` and `cmake`, you can use the following `brew` command: -You need to have openssl and cmake installed also, this can be done with this `brew` command: ```sh -$ brew install openssl cmake +brew install openssl cmake ``` -If you need a newer version of cmake then brew provides. checkout the [official guide](https://cmake.org/install) +If you require a newer version of `cmake`, refer to the [official installation guide](https://cmake.org/install). #### Build ```sh -$ mkdir build && cd build -$ cmake .. && make +mkdir build && cd build +cmake .. && make ``` -**MacOS:** You may need to point `cmake` to `openssl` by passing the argument -`-D OPENSSL_ROOT_DIR=/usr/local/opt/openssl@1.1` if openssl is not under `/usr/local/opt/openssl@1.1` you need to find the correct path. +**MacOS:** If your `openssl` installation is not located at `/usr/local/opt/openssl@1.1`, you may need to pass the argument `-D OPENSSL_ROOT_DIR=/path/to/openssl` to `cmake` and specify the correct path. ### Windows #### Dependencies -First you will need a compiler. +First, ensure that you have a compiler installed. -[Build Tools for Visual Studio 2019](https://visualstudio.microsoft.com/thank-you-downloading-visual-studio/?sku=BuildTools&rel=16) (Selecting C++ during installation) is recommended. +It is recommended to use [Build Tools for Visual Studio 2019](https://visualstudio.microsoft.com/thank-you-downloading-visual-studio/?sku=BuildTools&rel=16) and select C++ during installation. -By default `cmake` will use the bundled openssl package located at `vendor/openssl-1.1.1e-win-static.zip` +By default, `cmake` will utilize the bundled OpenSSL package located at `vendor/openssl-1.1.1e-win-static.zip`. If you prefer to use a different version of OpenSSL, set the `OPENSSL_ROOT_DIR` to the directory where OpenSSL is located on your system: -If you like to use an other version of OpenSSL then the static one bundled with this repo -you need to set `OPENSSL_ROOT_DIR` to the directory where OpenSSL is located on the system. - -For example: - -``` -C:\repo> cmake -D OPENSSL_ROOT_DIR=C:/path/to/openssl -B build +```sh +cmake -D OPENSSL_ROOT_DIR=C:/path/to/openssl -B build ``` -**NOTE:** `cmake` uses forward slash `/` for path even for windows. so make sure you use that when setting `OPENSSL_ROOT_DIR` +**NOTE:** `cmake` uses forward slashes `/` for paths, even on Windows, so ensure that you use them when setting -#### Build. + `OPENSSL_ROOT_DIR`. -Run cmake +#### Build -``` -C:\repo> cmake -B build -C:\repo> cmake --build build --config Release +Run `cmake`: + +```sh +cmake -B build +cmake --build build --config Release ``` -## Security notice +## Security Notice -Elliptic curve crypthographic operations is done using either `OpenSSL` or `libsecp256k1` libraries. -This library (libantelope) will never expose sensitve cryptographic information -to anything but the computers memory. -You are free to inspect the source code and compile yourself to verify. +The library performs elliptic curve cryptographic operations using either the `OpenSSL` or `libsecp256k1` libraries. The `libantelope` library ensures that sensitive cryptographic information is only stored in computer memory and not exposed to external sources. You are encouraged to inspect the source code and compile it yourself for verification purposes. -However, use this at your own risk. we cannot guarantee that the keys are -cryptographically secure as this depends on the elliptic curve -implementation (alto both OpenSSL and libsecp256k1 are widely used and should be safe) +However, please use this library at your own risk. While both OpenSSL and libsecp256k1 are widely used and considered safe, we cannot guarantee the cryptographic security of the keys as it depends on the elliptic curve implementation. -Please read the `LICENSE` file. - -``` -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, -INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A -PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT -HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF -CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE -OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -``` +Please refer to the `LICENSE` file for more information. ## Author From 11e086ee1225b452e6e861b1edacd8912d872a2f Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Wed, 16 Aug 2023 15:49:12 +0200 Subject: [PATCH 57/58] src/base58.cpp: need to include cstdint for uint8_t type --- src/base58.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/base58.cpp b/src/base58.cpp index bd2da9a..a63c909 100644 --- a/src/base58.cpp +++ b/src/base58.cpp @@ -25,6 +25,7 @@ * Based on code from https://github.com/bitcoin/bitcoin/blob/f1e2f2a85962c1664e4e55471061af0eaa798d40/src/base58.cpp */ #include +#include #include #include #include From 4787573a760dcdf0ef7c4b39857b61e87e4f3397 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Wed, 16 Aug 2023 15:52:14 +0200 Subject: [PATCH 58/58] Version 0.2.2 --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 172508b..d360a31 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,7 +6,7 @@ cmake_minimum_required(VERSION 3.15) # Project name and version project(libantelope - VERSION 0.2.1 + VERSION 0.2.2 DESCRIPTION "C++ library for Antelope IO" HOMEPAGE_URL "https://github.com/eosswedenorg/libantelope" LANGUAGES C CXX