diff --git a/src/commands/signtx/stx_output.c b/src/commands/signtx/stx_output.c index d0d15e9a..750a1f18 100644 --- a/src/commands/signtx/stx_output.c +++ b/src/commands/signtx/stx_output.c @@ -23,11 +23,11 @@ static inline ergo_tx_serializer_box_result_e maybe_finished( sign_transaction_output_info_ctx_t* ctx) { if (stx_output_info_is_finished(ctx)) { if (STX_OUTPUT_INFO_TYPE(ctx) == SIGN_TRANSACTION_OUTPUT_INFO_TYPE_SCRIPT) { - uint8_t hash[BLAKE2B_256_DIGEST_LEN]; + uint8_t hash[CX_BLAKE2B_256_SIZE]; if (!blake2b_256_finalize(&ctx->tree_hash_ctx, hash)) { return ERGO_TX_SERIALIZER_BOX_RES_ERR_HASHER; } - memmove(ctx->tree_hash, hash, BLAKE2B_256_DIGEST_LEN); + memmove(ctx->tree_hash, hash, CX_BLAKE2B_256_SIZE); } } return ERGO_TX_SERIALIZER_BOX_RES_OK; diff --git a/src/commands/signtx/stx_output.h b/src/commands/signtx/stx_output.h index 7a15807a..6f95ccd2 100644 --- a/src/commands/signtx/stx_output.h +++ b/src/commands/signtx/stx_output.h @@ -31,7 +31,7 @@ typedef struct { union { cx_blake2b_t tree_hash_ctx; uint8_t public_key[COMPRESSED_PUBLIC_KEY_LEN]; - uint8_t tree_hash[BLAKE2B_256_DIGEST_LEN]; + uint8_t tree_hash[CX_BLAKE2B_256_SIZE]; sign_transaction_bip32_path_t bip32_path; }; const token_table_t* tokens_table; diff --git a/src/ergo/address.c b/src/ergo/address.c index 53bbb92d..b4be68c9 100644 --- a/src/ergo/address.c +++ b/src/ergo/address.c @@ -41,7 +41,7 @@ static inline bool _ergo_address_from_pubkey(uint8_t network, } } - uint8_t hash[BLAKE2B_256_DIGEST_LEN] = {0}; + uint8_t hash[CX_BLAKE2B_256_SIZE] = {0}; if (!blake2b_256(rw_buffer_read_ptr(&buffer), rw_buffer_data_len(&buffer), hash)) { return false; @@ -80,7 +80,7 @@ bool ergo_address_from_script_hash(uint8_t network, if (!rw_buffer_write_bytes(&buffer, hash, P2SH_HASH_LEN)) { return false; } - uint8_t checksum[BLAKE2B_256_DIGEST_LEN] = {0}; + uint8_t checksum[CX_BLAKE2B_256_SIZE] = {0}; if (!blake2b_256(rw_buffer_read_ptr(&buffer), rw_buffer_data_len(&buffer), checksum)) { return false; } diff --git a/src/ergo/schnorr.c b/src/ergo/schnorr.c index 48651ff2..b703311d 100644 --- a/src/ergo/schnorr.c +++ b/src/ergo/schnorr.c @@ -97,10 +97,10 @@ bool ergo_secp256k1_schnorr_p2pk_sign_finish(uint8_t signature[static ERGO_SIGNA // build c // important: we only use the first 24 bytes of the hash output! - memset(buf, 0, BLAKE2B_256_DIGEST_LEN - ERGO_SOUNDNESS_BYTES); - memcpy(buf + BLAKE2B_256_DIGEST_LEN - ERGO_SOUNDNESS_BYTES, signature, ERGO_SOUNDNESS_BYTES); + memset(buf, 0, CX_BLAKE2B_256_SIZE - ERGO_SOUNDNESS_BYTES); + memcpy(buf + CX_BLAKE2B_256_SIZE - ERGO_SOUNDNESS_BYTES, signature, ERGO_SOUNDNESS_BYTES); - if (cx_math_is_zero(buf, BLAKE2B_256_DIGEST_LEN)) return false; + if (cx_math_is_zero(buf, CX_BLAKE2B_256_SIZE)) return false; // z = c * secret + key if (cx_math_multm_no_throw(buf, buf, secret, PIC(SECP256K1_N), PRIVATE_KEY_LEN) != 0) diff --git a/src/helpers/blake2b.c b/src/helpers/blake2b.c index 84ec8a65..ffa608b9 100644 --- a/src/helpers/blake2b.c +++ b/src/helpers/blake2b.c @@ -1,29 +1,22 @@ #include "blake2b.h" bool blake2b_256_init(cx_blake2b_t* ctx) { - return cx_blake2b_init_no_throw(ctx, 256) == 0; + return cx_blake2b_init_no_throw(ctx, 256) == CX_OK; } bool blake2b_update(cx_blake2b_t* ctx, const uint8_t* data, size_t len) { - return cx_hash_no_throw((cx_hash_t*) ctx, 0, data, len, NULL, 0) == 0; + return cx_hash_no_throw((cx_hash_t*) ctx, 0, data, len, NULL, 0) == CX_OK; } -bool blake2b_256_finalize(cx_blake2b_t* ctx, uint8_t out[static BLAKE2B_256_DIGEST_LEN]) { +bool blake2b_256_finalize(cx_blake2b_t* ctx, uint8_t out[static CX_BLAKE2B_256_SIZE]) { return cx_hash_no_throw((cx_hash_t*) ctx, CX_LAST | CX_NO_REINIT, NULL, 0, out, - BLAKE2B_256_DIGEST_LEN) == 0; + CX_BLAKE2B_256_SIZE) == CX_OK; } -bool blake2b_256(const uint8_t* data, size_t len, uint8_t out[static BLAKE2B_256_DIGEST_LEN]) { - cx_blake2b_t ctx; - if (!blake2b_256_init(&ctx)) return false; - return cx_hash_no_throw((cx_hash_t*) &ctx, - CX_LAST | CX_NO_REINIT, - data, - len, - out, - BLAKE2B_256_DIGEST_LEN) == 0; +bool blake2b_256(const uint8_t* data, size_t len, uint8_t out[static CX_BLAKE2B_256_SIZE]) { + return cx_blake2b_256_hash(data, len, out) == CX_OK; } \ No newline at end of file diff --git a/src/helpers/blake2b.h b/src/helpers/blake2b.h index a109bc44..b42ffcff 100644 --- a/src/helpers/blake2b.h +++ b/src/helpers/blake2b.h @@ -5,9 +5,7 @@ #include #include -#define BLAKE2B_256_DIGEST_LEN 32 - bool blake2b_256_init(cx_blake2b_t* ctx); bool blake2b_update(cx_blake2b_t* ctx, const uint8_t* data, size_t len); -bool blake2b_256_finalize(cx_blake2b_t* ctx, uint8_t out[static BLAKE2B_256_DIGEST_LEN]); -bool blake2b_256(const uint8_t* data, size_t len, uint8_t out[static BLAKE2B_256_DIGEST_LEN]); \ No newline at end of file +bool blake2b_256_finalize(cx_blake2b_t* ctx, uint8_t out[static CX_BLAKE2B_256_SIZE]); +bool blake2b_256(const uint8_t* data, size_t len, uint8_t out[static CX_BLAKE2B_256_SIZE]); \ No newline at end of file