Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Amalgamation updates #472

Merged
merged 6 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 1 addition & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
111 changes: 79 additions & 32 deletions src/amalgamation/combined.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
{
Expand Down
4 changes: 0 additions & 4 deletions src/amalgamation/combined_ccan.c

This file was deleted.

3 changes: 0 additions & 3 deletions src/amalgamation/combined_ccan2.c

This file was deleted.

8 changes: 4 additions & 4 deletions src/bip38.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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,
Expand Down
6 changes: 6 additions & 0 deletions src/ccan_config.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
#ifndef _WALLY_CCAN_CONFIG_H_
#define _WALLY_CCAN_CONFIG_H_ 1

/* Config directives for ccan */

#include <stddef.h>

#ifdef WORDS_BIGENDIAN
Expand Down Expand Up @@ -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_ */
11 changes: 11 additions & 0 deletions src/ctest/amalgamation_compile_test.c
Original file line number Diff line number Diff line change
@@ -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);
}
4 changes: 3 additions & 1 deletion src/transaction.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 */
Expand Down
2 changes: 1 addition & 1 deletion tools/msvc/build.bat
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading