diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index e54f3e0e1..fa324dd11 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -109,3 +109,14 @@ cmake-test: coverage_report: coverage_format: cobertura path: coverage.xml + +amalgamation-test: + image: greenaddress/wallycore@sha256:956b107d688f549c6e3884424991b7d3d34d84173990d43046fd760d7918db7c + tags: + - ga + script: + - touch config.h + - gcc -Wall -W -Wextra -Werror -I. -I./src -I./src/ccan -I./src/secp256k1/include src/ctest/amalgamation_compile_test.c + - gcc -DBUILD_ELEMENTS -Wall -W -Wextra -Werror -I. -I./src -I./src/ccan -I./src/secp256k1/include src/ctest/amalgamation_compile_test.c + - clang -Wall -W -Wextra -Werror -I. -I./src -I./src/ccan -I./src/secp256k1/include src/ctest/amalgamation_compile_test.c + - clang -DBUILD_ELEMENTS -Wall -W -Wextra -Werror -I. -I./src -I./src/ccan -I./src/secp256k1/include src/ctest/amalgamation_compile_test.c diff --git a/setup.py b/setup.py index 4e4c47417..ab1a0e9e1 100644 --- a/setup.py +++ b/setup.py @@ -128,9 +128,7 @@ def _call(args, cwd=ABS_PATH): './src/secp256k1/include', ] sources += [ - 'src/amalgamation/combined.c', - 'src/amalgamation/combined_ccan.c', - 'src/amalgamation/combined_ccan2.c', + 'src/amalgamation/combined.c' ] else: # Compile the swig python wrapper file and link it with wally/secp diff --git a/src/amalgamation/combined.c b/src/amalgamation/combined.c index c60d4dcca..206544232 100644 --- a/src/amalgamation/combined.c +++ b/src/amalgamation/combined.c @@ -6,9 +6,9 @@ #define ENABLE_MODULE_SCHNORRSIG 1 #define ENABLE_MODULE_GENERATOR 1 #define ENABLE_MODULE_ECDSA_S2C 1 +#define ENABLE_MODULE_RECOVERY 1 #ifdef BUILD_ELEMENTS #define ENABLE_MODULE_RANGEPROOF 1 -#define ENABLE_MODULE_RECOVERY 1 #define ENABLE_MODULE_SURJECTIONPROOF 1 #define ENABLE_MODULE_WHITELIST 1 #endif @@ -33,38 +33,85 @@ #include "src/secp256k1/src/secp256k1.c" #include "src/secp256k1/src/precomputed_ecmult_gen.c" #include "src/secp256k1/src/precomputed_ecmult.c" -#include "ccan/ccan/crypto/sha256/sha256.c" -#include "internal.c" -#include "address.c" -#include "aes.c" -#include "anti_exfil.c" -#include "base_58.c" -#include "base_64.c" -#include "bech32.c" -#include "blech32.c" -#include "bip32.c" -#include "bip38.c" -#include "bip39.c" -#include "bip85.c" -#include "coins.c" -#include "descriptor.c" -#include "ecdh.c" -#include "elements.c" -#include "hex_.c" -#include "hmac.c" -#include "map.c" -#include "mnemonic.c" -#include "pbkdf2.c" -#include "pullpush.c" -#include "psbt.c" -#include "script.c" -#include "scrypt.c" -#include "sign.c" -#include "symmetric.c" -#include "transaction.c" -#include "wif.c" -#include "wordlist.c" +/* Force the inclusion of our internal header first, so that + * config.h (which must be provided by the amalgamation user) + * is included. + */ +#include "src/internal.h" + +/* The amalgamation user can provide their own defines and skip + * providing a ccan_config.h if they define _WALLY_CCAN_CONFIG_H_. + */ +#ifndef _WALLY_CCAN_CONFIG_H_ +#include "src/ccan_config.h" +#endif + +#include "src/internal.c" +#include "src/address.c" +#include "src/aes.c" +#include "src/anti_exfil.c" +#include "src/base_58.c" +#include "src/base_64.c" +#include "src/bech32.c" +#include "src/blech32.c" +#include "src/bip32.c" +#include "src/bip38.c" +#include "src/bip39.c" +#include "src/bip85.c" +#include "src/coins.c" +#include "src/descriptor.c" +#include "src/ecdh.c" +#include "src/elements.c" +#include "src/hex_.c" +#include "src/hmac.c" +#include "src/map.c" +#include "src/mnemonic.c" +#include "src/pbkdf2.c" +#include "src/pullpush.c" +#include "src/psbt.c" +#include "src/script.c" +#include "src/scrypt.c" +#include "src/sign.c" +#include "src/symmetric.c" +#include "src/transaction.c" +#include "src/wif.c" +#include "src/wordlist.c" + +/* ccan sources */ +#include "src/ccan/ccan/crypto/sha256/sha256.c" + +/* Redefine internal names so sha-512 can be included without conflicts */ +#define Round Round_512 +#define Transform Transform_512 +#define Maj Maj_512 +#define Sigma0 Sigma0_512 +#define sigma0 sigma0_512 +#define Sigma1 Sigma1_512 +#define sigma1 sigma1_512 +#define add add_512 +#define Ch Ch_512 +#include "src/ccan/ccan/crypto/sha512/sha512.c" +#undef Round +#undef Transform +#undef Sigma0 +#undef sigma0 +#undef sigma1 +#undef Sigma1 +#undef add +#undef Maj +#undef Ch +#include "src/ccan/ccan/str/hex/hex.c" + +/* Redefine internal names so ripemd-160 can be included without conflicts */ +#define Transform Transform_ripemd160 +#define add add_ripemd160 +#define Round Round_ripemd160 +#include "src/ccan/ccan/crypto/ripemd160/ripemd160.c" +#undef Transform +#undef add +#undef Round +#include "src/ccan/ccan/base64/base64.c" void wally_silence_unused_warnings(void) { diff --git a/src/amalgamation/combined_ccan.c b/src/amalgamation/combined_ccan.c deleted file mode 100644 index aad03f92a..000000000 --- a/src/amalgamation/combined_ccan.c +++ /dev/null @@ -1,4 +0,0 @@ -#include "internal.h" -#include "ccan/ccan/crypto/sha512/sha512.c" -#include "ccan/ccan/str/hex/hex.c" - diff --git a/src/amalgamation/combined_ccan2.c b/src/amalgamation/combined_ccan2.c deleted file mode 100644 index e74d46429..000000000 --- a/src/amalgamation/combined_ccan2.c +++ /dev/null @@ -1,3 +0,0 @@ -#include "internal.h" -#include "ccan/ccan/crypto/ripemd160/ripemd160.c" -#include "src/ccan/ccan/base64/base64.c" diff --git a/src/bip38.c b/src/bip38.c index 043ff2382..aec52d4ad 100644 --- a/src/bip38.c +++ b/src/bip38.c @@ -123,14 +123,14 @@ static int address_from_private_key(const unsigned char *bytes, return ret; } -static void aes_enc_impl(const unsigned char *src, const unsigned char *xor, +static void aes_enc_impl(const unsigned char *src, const unsigned char *xor_bytes, const unsigned char *key, unsigned char *bytes_out) { unsigned char plaintext[AES_BLOCK_LEN]; size_t i; for (i = 0; i < sizeof(plaintext); ++i) - plaintext[i] = src[i] ^ xor[i]; + plaintext[i] = src[i] ^ xor_bytes[i]; wally_aes(key, AES_KEY_LEN_256, plaintext, AES_BLOCK_LEN, AES_FLAG_ENCRYPT, bytes_out, AES_BLOCK_LEN); @@ -219,7 +219,7 @@ int bip38_from_private_key(const unsigned char *bytes, size_t bytes_len, } -static void aes_dec_impl(const unsigned char *cyphertext, const unsigned char *xor, +static void aes_dec_impl(const unsigned char *cyphertext, const unsigned char *xor_bytes, const unsigned char *key, unsigned char *bytes_out) { size_t i; @@ -230,7 +230,7 @@ static void aes_dec_impl(const unsigned char *cyphertext, const unsigned char *x bytes_out, AES_BLOCK_LEN); for (i = 0; i < AES_BLOCK_LEN; ++i) - bytes_out[i] ^= xor[i]; + bytes_out[i] ^= xor_bytes[i]; } static int to_private_key(const char *bip38, diff --git a/src/ccan_config.h b/src/ccan_config.h index 3556db6b0..87ea13607 100644 --- a/src/ccan_config.h +++ b/src/ccan_config.h @@ -1,4 +1,8 @@ +#ifndef _WALLY_CCAN_CONFIG_H_ +#define _WALLY_CCAN_CONFIG_H_ 1 + /* Config directives for ccan */ + #include #ifdef WORDS_BIGENDIAN @@ -55,3 +59,5 @@ void wally_clear(void *p, size_t len); #define CCAN_CLEAR_MEMORY(p, len) wally_clear(p, len) + +#endif /*_WALLY_CCAN_CONFIG_H_ */ diff --git a/src/ctest/amalgamation_compile_test.c b/src/ctest/amalgamation_compile_test.c new file mode 100644 index 000000000..7b0e331c6 --- /dev/null +++ b/src/ctest/amalgamation_compile_test.c @@ -0,0 +1,11 @@ +/* Tests that including the entire library as a single source file works */ +#include "../amalgamation/combined.c" + +int main(int argc, char *argv[]) +{ + (void) argc; + (void) argv; + + wally_init(0); + return wally_cleanup(0); +} diff --git a/src/transaction.c b/src/transaction.c index d132f0cd0..aa84279cb 100644 --- a/src/transaction.c +++ b/src/transaction.c @@ -1969,7 +1969,9 @@ int wally_tx_get_vsize(const struct wally_tx *tx, size_t *written) int wally_tx_get_elements_weight_discount(const struct wally_tx *tx, uint32_t flags, size_t *written) { +#ifdef BUILD_ELEMENTS size_t i, n = 0, is_elements = 0; +#endif /* BUILD_ELEMENTS */ if (written) *written = 0; @@ -2004,7 +2006,7 @@ int wally_tx_get_elements_weight_discount(const struct wally_tx *tx, } } *written = n; -#endif +#endif /* BUILD_ELEMENTS */ return WALLY_OK; } #endif /* WALLY_ABI_NO_ELEMENTS */ diff --git a/tools/msvc/build.bat b/tools/msvc/build.bat index b074e616b..5a9e753de 100644 --- a/tools/msvc/build.bat +++ b/tools/msvc/build.bat @@ -7,4 +7,4 @@ if "%ELEMENTS_BUILD%" == "elements" ( ) REM Compile everything (wally, ccan, libsecp256k) in one lump. -cl /utf-8 /DWALLY_CORE_BUILD %OPTS% /I%LIBWALLY_DIR% /I%LIBWALLY_DIR%\src /I%LIBWALLY_DIR%\include /I%LIBWALLY_DIR%\src\ccan /I%LIBWALLY_DIR%\src\ccan\base64 /I%LIBWALLY_DIR%\src\secp256k1 /Zi /LD src/amalgamation/combined.c src/amalgamation/combined_ccan.c src/amalgamation/combined_ccan2.c /Fewally.dll +cl /utf-8 /DWALLY_CORE_BUILD %OPTS% /I%LIBWALLY_DIR% /I%LIBWALLY_DIR%\src /I%LIBWALLY_DIR%\include /I%LIBWALLY_DIR%\src\ccan /I%LIBWALLY_DIR%\src\ccan\base64 /I%LIBWALLY_DIR%\src\secp256k1 /Zi /LD src/amalgamation/combined.c /Fewally.dll