Skip to content

Commit

Permalink
switched to the new blake2 call
Browse files Browse the repository at this point in the history
  • Loading branch information
ypopovych committed May 1, 2024
1 parent b9278a9 commit d784e5a
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 25 deletions.
4 changes: 2 additions & 2 deletions src/commands/signtx/stx_output.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/commands/signtx/stx_output.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/ergo/address.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
6 changes: 3 additions & 3 deletions src/ergo/schnorr.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
19 changes: 6 additions & 13 deletions src/helpers/blake2b.c
Original file line number Diff line number Diff line change
@@ -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;
}
6 changes: 2 additions & 4 deletions src/helpers/blake2b.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
#include <stddef.h>
#include <cx.h>

#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]);
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]);

0 comments on commit d784e5a

Please sign in to comment.