8000 Allow build with VS by sergey-chernikov · Pull Request #197 · libbtc/libbtc · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Allow build with VS #197

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

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
20 changes: 19 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ SET(CMAKE_C_STANDARD_REQUIRED TRUE)
SET(LIBBTC_NAME btc)
PROJECT(lib${LIBBTC_NAME} VERSION 0.1)

IF (MSVC)
add_definitions(-DWIN32)
ENDIF (MSVC)

FILE(GLOB SOURCES src/*.c)
#list(REMOVE_ITEM SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/src/net.c)

INCLUDE(CTest)
SET(USE_TESTS ${CMAKE_TESTING_ENABLED})
SET(WITH_TOOLS TRUE CACHE BOOL "enable bitcoin tool cli application")
Expand Down Expand Up @@ -54,6 +61,11 @@ IF(WITH_LOGDB)
ENDIF()
FILE(TOUCH src/libbtc-config.h)

INCLUDE_DIRECTORIES( include )
INCLUDE_DIRECTORIES( src/logdb/include )
INCLUDE_DIRECTORIES( src/secp256k1 )
INCLUDE_DIRECTORIES( ${GMP_INSTALL_DIR}/include )
INCLUDE_DIRECTORIES( ${GMP_INCLUDE_DIR} )

ADD_LIBRARY(${LIBBTC_NAME})

Expand Down Expand Up @@ -89,6 +101,7 @@ INSTALL(FILES

INSTALL(FILES
src/trezor-crypto/base58.h
src/trezor-crypto/bip39.h
src/trezor-crypto/blake2_common.h
src/trezor-crypto/blake2b.h
src/trezor-crypto/blake256.h
Expand All @@ -98,6 +111,8 @@ INSTALL(FILES
src/trezor-crypto/hmac.h
src/trezor-crypto/memzero.h
src/trezor-crypto/options.h
src/trezor-crypto/pbkdf2.h
src/trezor-crypto/rand.h
src/trezor-crypto/ripemd160.h
src/trezor-crypto/segwit_addr.h
src/trezor-crypto/sha2.h
Expand Down Expand Up @@ -129,12 +144,16 @@ TARGET_SOURCES(${LIBBTC_NAME} PRIVATE

TARGET_SOURCES(${LIBBTC_NAME} PRIVATE
src/trezor-crypto/base58.c
src/trezor-crypto/bip39.c
src/trezor-crypto/bip39_english.h
src/trezor-crypto/blake2b.c
src/trezor-crypto/blake256.c
src/trezor-crypto/groestl.c
src/trezor-crypto/hasher.c
src/trezor-crypto/hmac.c
src/trezor-crypto/memzero.c
src/trezor-crypto/pbkdf2.c
src/trezor-crypto/rand.c
src/trezor-crypto/ripemd160.c
src/trezor-crypto/segwit_addr.c
src/trezor-crypto/sha2.c
Expand Down Expand Up @@ -288,4 +307,3 @@ IF(WITH_TOOLS)
TARGET_INCLUDE_DIRECTORIES(bitcoin-spv PRIVATE src)
ENDIF()
ENDIF()

5 changes: 5 additions & 0 deletions include/btc/ecc.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,14 @@ LIBBTC_API void btc_ecc_get_pubkey(const uint8_t* private_key, uint8_t* public_k

//!ec mul tweak on given private key
LIBBTC_API btc_bool btc_ecc_private_key_tweak_add(uint8_t* private_key, const uint8_t* tweak);
LIBBTC_API btc_bool btc_ecc_private_key_tweak_mul(uint8_t* private_key, const uint8_t* tweak);

//!ec mul tweak on given public key
LIBBTC_API btc_bool btc_ecc_public_key_tweak_add(uint8_t* public_key_inout, const uint8_t* tweak);
LIBBTC_API btc_bool btc_ecc_public_key_tweak_mul(uint8_t* public_key_inout, const uint8_t* tweak);

LIBBTC_API btc_bool btc_ecc_public_key_compress(uint8_t* public_key_in, uint8_t* public_key_out);
LIBBTC_API btc_bool btc_ecc_public_key_uncompress(uint8_t* public_key_in, uint8_t* public_key_out);

//!verifies a given 32byte key
LIBBTC_API btc_bool btc_ecc_verify_privatekey(const uint8_t* private_key);
Expand Down
7 changes: 4 additions & 3 deletions src/ecc_key.c
Original file line number Diff line number Diff line change
Expand Up @@ -186,13 +186,14 @@ btc_bool btc_pubkey_get_hex(const btc_pubkey* pubkey, char* str, size_t* strsize

void btc_pubkey_from_key(const btc_key* privkey, btc_pubkey* pubkey_inout)
{
const btc_bool compressed = true;
if (pubkey_inout == NULL || privkey == NULL)
return;

size_t in_out_len = BTC_ECKEY_COMPRESSED_LENGTH;
size_t in_out_len = compressed ? BTC_ECKEY_COMPRESSED_LENGTH : BTC_ECKEY_UNCOMPRESSED_LENGTH;

btc_ecc_get_pubkey(privkey->privkey, pubkey_inout->pubkey, &in_out_len, true);
pubkey_inout->compressed = true;
btc_ecc_get_pubkey(privkey->privkey, pubkey_inout->pubkey, &in_out_len, compressed);
pubkey_inout->compressed = compressed;
}


Expand Down
53 changes: 53 additions & 0 deletions src/ecc_libsecp256k1.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,59 @@ btc_bool btc_ecc_public_key_tweak_add(uint8_t* public_key_inout, const uint8_t*
return true;
}

btc_bool btc_ecc_private_key_tweak_mul(uint8_t* private_key, const uint8_t* tweak)
{
assert(secp256k1_ctx);
return secp256k1_ec_privkey_tweak_mul(secp256k1_ctx, (unsigned char*)private_key, (const unsigned char*)tweak);
}

btc_bool btc_ecc_public_key_tweak_mul(uint8_t* public_key_inout, const uint8_t* tweak)
{
size_t out = BTC_ECKEY_UNCOMPRESSED_LENGTH;
secp256k1_pubkey pubkey;

assert(secp256k1_ctx);
if (!secp256k1_ec_pubkey_parse(secp256k1_ctx, &pubkey, public_key_inout, 65))
return false;

if (!secp256k1_ec_pubkey_tweak_mul(secp256k1_ctx, &pubkey, (const unsigned char*)tweak))
return false;

if (!secp256k1_ec_pubkey_serialize(secp256k1_ctx, public_key_inout, &out, &pubkey, SECP256K1_EC_UNCOMPRESSED))
return false;

return true;
}

btc_bool btc_ecc_public_key_compress(uint8_t* public_key_in, uint8_t* public_key_out)
{
secp256k1_pubkey pubkey;
assert(secp256k1_ctx);

if(!secp256k1_ec_pubkey_parse(secp256k1_ctx, &pubkey, public_key_in, 65))
return false;

size_t out = BTC_ECKEY_COMPRESSED_LENGTH;
if(!secp256k1_ec_pubkey_serialize(secp256k1_ctx, public_key_out, &out, &pubkey, SECP256K1_EC_COMPRESSED))
return false;

return true;
}

btc_bool btc_ecc_public_key_uncompress(uint8_t* public_key_in, uint8_t* public_key_out)
{
secp256k1_pubkey pubkey;
assert(secp256k1_ctx);

if(!secp256k1_ec_pubkey_parse(secp256k1_ctx, &pubkey, public_key_in, 33))
return false;

size_t out = BTC_ECKEY_UNCOMPRESSED_LENGTH;
if(!secp256k1_ec_pubkey_serialize(secp256k1_ctx, public_key_out, &out, &pubkey, SECP256K1_EC_UNCOMPRESSED))
return false;

return true;
}

btc_bool btc_ecc_verify_privatekey(const uint8_t* private_key)
{
Expand Down
2 changes: 1 addition & 1 deletion src/logdb/test/logdb_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ void test_logdb(logdb_log_db* (*new_func)())
fsize = ftell(f);
fseek(f, 0, SEEK_SET);

buf = malloc(fsize + 1);
buf = safe_malloc(fsize + 1);
fread(buf, fsize, 1, f);
fclose(f);

Expand Down
6 changes: 5 additions & 1 deletion src/secp256k1/src/bench.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "sys/time.h"
#ifdef WIN32
# include "winsock.h"
#else
# include "sys/time.h"
#endif

static int64_t gettime_i64(void) {
struct timeval tv;
Expand Down
24 changes: 12 additions & 12 deletions src/secp256k1/src/tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,10 @@ void run_context_tests(int use_prealloc) {
secp256k1_scalar sigr, sigs;

if (use_prealloc) {
none_prealloc = malloc(secp256k1_context_preallocated_size(SECP256K1_CONTEXT_NONE));
sign_prealloc = malloc(secp256k1_context_preallocated_size(SECP256K1_CONTEXT_SIGN));
vrfy_prealloc = malloc(secp256k1_context_preallocated_size(SECP256K1_CONTEXT_VERIFY));
both_prealloc = malloc(secp256k1_context_preallocated_size(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY));
none_prealloc = checked_malloc(secp256k1_context_preallocated_size(SECP256K1_CONTEXT_NONE));
sign_prealloc = checked_malloc(secp256k1_context_preallocated_size(SECP256K1_CONTEXT_SIGN));
vrfy_prealloc = checked_malloc(secp256k1_context_preallocated_size(SECP256K1_CONTEXT_VERIFY));
both_prealloc = checked_malloc(secp256k1_context_preallocated_size(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY));
CHECK(none_prealloc != NULL);
CHECK(sign_prealloc != NULL);
CHECK(vrfy_prealloc != NULL);
Expand Down Expand Up @@ -207,40 +207,40 @@ void run_context_tests(int use_prealloc) {
if (use_prealloc) {
/* clone into a non-preallocated context and then again into a new preallocated one. */
ctx_tmp = none; none = secp256k1_context_clone(none); secp256k1_context_preallocated_destroy(ctx_tmp);
free(none_prealloc); none_prealloc = malloc(secp256k1_context_preallocated_size(SECP256K1_CONTEXT_NONE)); CHECK(none_prealloc != NULL);
free(none_prealloc); none_prealloc = checked_malloc(secp256k1_context_preallocated_size(SECP256K1_CONTEXT_NONE)); CHECK(none_prealloc != NULL);
ctx_tmp = none; none = secp256k1_context_preallocated_clone(none, none_prealloc); secp256k1_context_destroy(ctx_tmp);

ctx_tmp = sign; sign = secp256k1_context_clone(sign); secp256k1_context_preallocated_destroy(ctx_tmp);
free(sign_prealloc); sign_prealloc = malloc(secp256k1_context_preallocated_size(SECP256K1_CONTEXT_SIGN)); CHECK(sign_prealloc != NULL);
free(sign_prealloc); sign_prealloc = checked_malloc(secp256k1_context_preallocated_size(SECP256K1_CONTEXT_SIGN)); CHECK(sign_prealloc != NULL);
ctx_tmp = sign; sign = secp256k1_context_preallocated_clone(sign, sign_prealloc); secp256k1_context_destroy(ctx_tmp);

ctx_tmp = vrfy; vrfy = secp256k1_context_clone(vrfy); secp256k1_context_preallocated_destroy(ctx_tmp);
free(vrfy_prealloc); vrfy_prealloc = malloc(secp256k1_context_preallocated_size(SECP256K1_CONTEXT_VERIFY)); CHECK(vrfy_prealloc != NULL);
free(vrfy_prealloc); vrfy_prealloc = checked_malloc(secp256k1_context_preallocated_size(SECP256K1_CONTEXT_VERIFY)); CHECK(vrfy_prealloc != NULL);
ctx_tmp = vrfy; vrfy = secp256k1_context_preallocated_clone(vrfy, vrfy_prealloc); secp256k1_context_destroy(ctx_tmp);

ctx_tmp = both; both = secp256k1_context_clone(both); secp256k1_context_preallocated_destroy(ctx_tmp);
free(both_prealloc); both_prealloc = malloc(secp256k1_context_preallocated_size(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY)); CHECK(both_prealloc != NULL);
free(both_prealloc); both_prealloc = checked_malloc(secp256k1_context_preallocated_size(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY)); CHECK(both_prealloc != NULL);
ctx_tmp = both; both = secp256k1_context_preallocated_clone(both, both_prealloc); secp256k1_context_destroy(ctx_tmp);
} else {
/* clone into a preallocated context and then again into a new non-preallocated one. */
void *prealloc_tmp;

prealloc_tmp = malloc(secp256k1_context_preallocated_size(SECP256K1_CONTEXT_NONE)); CHECK(prealloc_tmp != NULL);
prealloc_tmp = checked_malloc(secp256k1_context_preallocated_size(SECP256K1_CONTEXT_NONE)); CHECK(prealloc_tmp != NULL);
ctx_tmp = none; none = secp256k1_context_preallocated_clone(none, prealloc_tmp); secp256k1_context_destroy(ctx_tmp);
ctx_tmp = none; none = secp256k1_context_clone(none); secp256k1_context_preallocated_destroy(ctx_tmp);
free(prealloc_tmp);

prealloc_tmp = malloc(secp256k1_context_preallocated_size(SECP256K1_CONTEXT_SIGN)); CHECK(prealloc_tmp != NULL);
prealloc_tmp = checked_malloc(secp256k1_context_preallocated_size(SECP256K1_CONTEXT_SIGN)); CHECK(prealloc_tmp != NULL);
ctx_tmp = sign; sign = secp256k1_context_preallocated_clone(sign, prealloc_tmp); secp256k1_context_destroy(ctx_tmp);
ctx_tmp = sign; sign = secp256k1_context_clone(sign); secp256k1_context_preallocated_destroy(ctx_tmp);
free(prealloc_tmp);

prealloc_tmp = malloc(secp256k1_context_preallocated_size(SECP256K1_CONTEXT_VERIFY)); CHECK(prealloc_tmp != NULL);
prealloc_tmp = checked_malloc(secp256k1_context_preallocated_size(SECP256K1_CONTEXT_VERIFY)); CHECK(prealloc_tmp != NULL);
ctx_tmp = vrfy; vrfy = secp256k1_context_preallocated_clone(vrfy, prealloc_tmp); secp256k1_context_destroy(ctx_tmp);
ctx_tmp = vrfy; vrfy = secp256k1_context_clone(vrfy); secp256k1_ 10000 context_preallocated_destroy(ctx_tmp);
free(prealloc_tmp);

prealloc_tmp = malloc(secp256k1_context_preallocated_size(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY)); CHECK(prealloc_tmp != NULL);
prealloc_tmp = checked_malloc(secp256k1_context_preallocated_size(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY)); CHECK(prealloc_tmp != NULL);
ctx_tmp = both; both = secp256k1_context_preallocated_clone(both, prealloc_tmp); secp256k1_context_destroy(ctx_tmp);
ctx_tmp = both; both = secp256k1_context_clone(both); secp256k1_context_preallocated_destroy(ctx_tmp);
free(prealloc_tmp);
Expand Down
58 changes: 41 additions & 17 deletions src/trezor-crypto/base58.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,16 @@ bool b58tobin(void *bin, size_t *binszp, const char *b58) {

const unsigned char *b58u = (const unsigned char *)b58;
unsigned char *binu = bin;
size_t outisz =
(binsz + sizeof(b58_almostmaxint_t) - 1) / sizeof(b58_almostmaxint_t);
const size_t outisz = (binsz + sizeof(b58_almostmaxint_t) - 1) / sizeof(b58_almostmaxint_t);

#ifdef _MSC_VER
if (outisz > 1024) { // avoid buffer overflow
return false;
}
b58_almostmaxint_t outi[1024];
#else
b58_almostmaxint_t outi[outisz];
#endif
b58_maxint_t t = 0;
b58_almostmaxint_t c = 0;
size_t i = 0, j = 0;
Expand All @@ -71,7 +78,7 @@ bool b58tobin(void *bin, size_t *binszp, const char *b58) {

size_t b58sz = strlen(b58);

memzero(outi, sizeof(outi));
memzero(outi, outisz * sizeof(b58_almostmaxint_t));

// Leading zeros, just count
for (i = 0; i < b58sz && b58u[i] == '1'; ++i) ++zerocount;
Expand All @@ -80,21 +87,24 @@ bool b58tobin(void *bin, size_t *binszp, const char *b58) {
if (b58u[i] & 0x80)
// High-bit set on invalid digit
return false;
if (b58digits_map[b58u[i]] == -1)
// Invalid base58 digit
return false;
if (b58digits_map[b58u[i]] == -1) {
// Invalid base58 digit
return false;
}
c = (unsigned)b58digits_map[b58u[i]];
for (j = outisz; j--;) {
t = ((b58_maxint_t)outi[j]) * 58 + c;
c = t >> b58_almostmaxint_bits;
outi[j] = t & b58_almostmaxint_mask;
}
if (c)
// Output number too big (carry to the next int32)
return false;
if (outi[0] & zeromask)
// Output number too big (last int32 filled too far)
return false;
if (c) {
// Output number too big (carry to the next int32)
return false;
}
if (outi[0] & zeromask) {
// Output number too big (last int32 filled too far)
return false;
}
}

j = 0;
Expand Down Expand Up @@ -149,12 +159,18 @@ bool b58enc(char *b58, size_t *b58sz, const void *data, size_t binsz) {
const uint8_t *bin = data;
int carry = 0;
size_t i = 0, j = 0, high = 0, zcount = 0;
size_t size = 0;

while (zcount < binsz && !bin[zcount]) ++zcount;

size = (binsz - zcount) * 138 / 100 + 1;
const size_t size = (binsz - zcount) * 138 / 100 + 1;
#ifdef _MSC_VER
if (size > 4096) { // avoid buffer overflow
return false;
}
uint8_t buf[4096];
#else
uint8_t buf[size];
#endif
memzero(buf, size);

for (i = zcount, high = size - 1; i < binsz; ++i, high = j) {
Expand Down Expand Up @@ -190,14 +206,18 @@ int base58_encode_check(const uint8_t *data, int datalen,
if (datalen > 128) {
return 0;
}
#ifdef _MSC_VER
uint8_t buf[128 + 32];
#else
uint8_t buf[datalen + 32];
memset(buf, 0, sizeof(buf));
#endif
memset(buf, 0, datalen + 32);
uint8_t *hash = buf + datalen;
memcpy(buf, data, datalen);
hasher_Raw(hasher_type, data, datalen, hash);
size_t res = strsize;
bool success = b58enc(str, &res, buf, datalen + 4);
memzero(buf, sizeof(buf));
memzero(buf, datalen + 32);
return success ? res : 0;
}

Expand All @@ -206,8 +226,12 @@ int base58_decode_check(const char *str, HasherType hasher_type, uint8_t *data,
if (datalen > 128) {
return 0;
}
#ifdef _MSC_VER
uint8_t d[128 + 4];
#else
uint8_t d[datalen + 4];
memset(d, 0, sizeof(d));
#endif
memset(d, 0, datalen + 4);
size_t res = datalen + 4;
if (b58tobin(d, &res, str) != true) {
return 0;
Expand Down
4 changes: 4 additions & 0 deletions src/trezor-crypto/blake2b.c
3D11
Original file line number Diff line numberDiff line change
Expand Up @@ -33,7 +33,11 @@ typedef struct blake2b_param__
uint8_t reserved[14]; /* 32 */
uint8_t salt[BLAKE2B_SALTBYTES]; /* 48 */
uint8_t personal[BLAKE2B_PERSONALBYTES]; /* 64 */
#ifdef __GNUC__
} __attribute__((packed)) blake2b_param;
#else
} blake2b_param;
#endif

static const uint64_t blake2b_IV[8] =
{
Expand Down
6 changes: 0 additions & 6 deletions src/trezor-crypto/groestl_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@
#error This code requires 8-bit bytes
#endif

#if defined __STDC__ && __STDC_VERSION__ >= 199901L

#include <stdint.h>

typedef uint32_t sph_u32;
Expand All @@ -70,10 +68,6 @@ typedef int64_t sph_s64;
#define SPH_C32(x) ((sph_u32)(x))
#define SPH_C64(x) ((sph_u64)(x))

#else
#error We need at least C99 compiler
#endif

#define SPH_T32(x) ((x) & SPH_C32(0xFFFFFFFF))
#define SPH_ROTL32(x, n) SPH_T32(((x) << (n)) | ((x) >> (32 - (n))))
#define SPH_ROTR32(x, n) SPH_ROTL32(x, (32 - (n)))
Expand Down
Loading
0