Skip to content

Commit

Permalink
magic number clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
chcmedeiros committed Sep 24, 2024
1 parent bf43a4b commit 3fa70cb
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 20 deletions.
2 changes: 2 additions & 0 deletions app/src/common/parser_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ typedef enum {
parser_unexpected_chain,
parser_missing_field,
paser_unknown_transaction,

parser_invalid_tx_version,
} parser_error_t;

typedef struct {
Expand Down
23 changes: 10 additions & 13 deletions app/src/crypto_helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,19 +105,17 @@ parser_error_t transaction_signature_hash(parser_tx_t *txObj, uint8_t output[HAS
#endif

// Spends
const uint16_t SPENDLEN = 32 + 192 + 32 + 32 + 4 + 32 + 64;
for (uint64_t i = 0; i < txObj->spends.elements; i++) {
const uint8_t *spend_i = txObj->spends.data.ptr + (SPENDLEN * i) + 32;
const uint8_t *spend_i = txObj->spends.data.ptr + (SPENDLEN * i) + PUBKEY_RANDONMESS_LEN;
// Don't hash neither public_key_randomness(32) nor binding_signature(64)
#if defined(LEDGER_SPECIFIC)
ASSERT_CX_OK(cx_blake2b_update(&ctx, spend_i, SPENDLEN - (32 + 64)));
ASSERT_CX_OK(cx_blake2b_update(&ctx, spend_i, SPENDLEN - (PUBKEY_RANDONMESS_LEN + BINDING_SIGNATURE_LEN)));
#else
blake2b_update(&state, spend_i, SPENDLEN - (32 + 64));
blake2b_update(&state, spend_i, SPENDLEN - (PUBKEY_RANDONMESS_LEN + BINDING_SIGNATURE_LEN));
#endif
}

// Outputs
const uint16_t OUTPUTLEN = 192 + 328;
for (uint64_t i = 0; i < txObj->outputs.elements; i++) {
const uint8_t *output_i = txObj->outputs.data.ptr + (OUTPUTLEN * i);
#if defined(LEDGER_SPECIFIC)
Expand All @@ -128,7 +126,6 @@ parser_error_t transaction_signature_hash(parser_tx_t *txObj, uint8_t output[HAS
}

// Mints
const uint16_t MINTLEN = 32 + 192 + 193 + 8;
uint16_t tmpOffset = 0;
for (uint64_t i = 0; i < txObj->mints.elements; i++) {
const uint8_t *mint_i = txObj->mints.data.ptr + tmpOffset; // + 32;
Expand All @@ -137,16 +134,16 @@ parser_error_t transaction_signature_hash(parser_tx_t *txObj, uint8_t output[HAS

// Don't hash neither public_key_randomness(32) nor binding_signature(64)
#if defined(LEDGER_SPECIFIC)
ASSERT_CX_OK(cx_blake2b_update(&ctx, mint_i + 32, tmpMintLen - (32 + 64)));
ASSERT_CX_OK(cx_blake2b_update(&ctx, mint_i + PUBKEY_RANDONMESS_LEN,
tmpMintLen - (PUBKEY_RANDONMESS_LEN + BINDING_SIGNATURE_LEN)));
#else
blake2b_update(&state, mint_i + 32, tmpMintLen - (32 + 64));
blake2b_update(&state, mint_i + PUBKEY_RANDONMESS_LEN, tmpMintLen - (PUBKEY_RANDONMESS_LEN + BINDING_SIGNATURE_LEN));
#endif

tmpOffset += tmpMintLen;
}

// Burns
const uint16_t BURNLEN = 32 + 8;
for (uint64_t i = 0; i < txObj->burns.elements; i++) {
const uint8_t *burn_i = txObj->burns.data.ptr + (BURNLEN * i);
#if defined(LEDGER_SPECIFIC)
Expand Down Expand Up @@ -175,16 +172,16 @@ static parser_error_t h_star(bytes_t a, const uint8_t randomizedPublicKey[32], c
ASSERT_CX_OK(cx_blake2b_init2_no_throw(&ctx, BLAKE2B_OUTPUT_LEN, NULL, 0, (uint8_t *)SIGNING_REDJUBJUB,
sizeof(SIGNING_REDJUBJUB)));
ASSERT_CX_OK(cx_blake2b_update(&ctx, a.ptr, a.len));
ASSERT_CX_OK(cx_blake2b_update(&ctx, randomizedPublicKey, 32));
ASSERT_CX_OK(cx_blake2b_update(&ctx, transactionHash, 32));
ASSERT_CX_OK(cx_blake2b_update(&ctx, randomizedPublicKey, PUBKEY_RANDONMESS_LEN));
ASSERT_CX_OK(cx_blake2b_update(&ctx, transactionHash, HASH_LEN));
cx_blake2b_final(&ctx, hash);
#else
blake2b_state state = {0};
blake2b_init_with_personalization(&state, BLAKE2B_OUTPUT_LEN, (const uint8_t *)SIGNING_REDJUBJUB,
sizeof(SIGNING_REDJUBJUB));
blake2b_update(&state, a.ptr, a.len);
blake2b_update(&state, randomizedPublicKey, 32);
blake2b_update(&state, transactionHash, 32);
blake2b_update(&state, randomizedPublicKey, PUBKEY_RANDONMESS_LEN);
blake2b_update(&state, transactionHash, HASH_LEN);
blake2b_final(&state, hash, BLAKE2B_OUTPUT_LEN);
#endif

Expand Down
11 changes: 6 additions & 5 deletions app/src/parser_impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ static parser_error_t readTransactionVersion(parser_context_t *ctx, transaction_
CHECK_ERROR(readByte(ctx, &tmpVersion));

if (tmpVersion != V1 && tmpVersion != V2) {
return parser_value_out_of_range;
return parser_invalid_tx_version;
}
*txVersion = (transaction_version_e)tmpVersion;
return parser_ok;
Expand All @@ -42,7 +42,6 @@ static parser_error_t readSpends(parser_context_t *ctx, vec_spend_description_t
return parser_no_data;
}

const uint16_t SPENDLEN = 32 + 192 + 32 + 32 + 4 + 32 + 64;
spends->data.ptr = ctx->buffer + ctx->offset;
spends->data.len = 0;
const uint8_t *tmpPtr = NULL;
Expand All @@ -57,7 +56,7 @@ static parser_error_t readOutputs(parser_context_t *ctx, vec_output_description_
if (ctx == NULL || outputs == NULL) {
return parser_no_data;
}
const uint16_t OUTPUTLEN = 192 + 328;

outputs->data.ptr = ctx->buffer + ctx->offset;
outputs->data.len = 0;
const uint8_t *tmpPtr = NULL;
Expand All @@ -73,7 +72,6 @@ static parser_error_t readMints(parser_context_t *ctx, vec_mint_description_t *m
return parser_no_data;
}

const uint16_t MINTLEN = 32 + 192 + 193 + 8;
mints->data.ptr = ctx->buffer + ctx->offset;
mints->data.len = 0;
const uint8_t *tmpPtr = NULL;
Expand All @@ -97,7 +95,6 @@ static parser_error_t readBurns(parser_context_t *ctx, vec_burn_description_t *b
return parser_no_data;
}

const uint16_t BURNLEN = 32 + 8;
burns->data.ptr = ctx->buffer + ctx->offset;
burns->data.len = 0;
const uint8_t *tmpPtr = NULL;
Expand All @@ -110,10 +107,14 @@ static parser_error_t readBurns(parser_context_t *ctx, vec_burn_description_t *b

parser_error_t _read(parser_context_t *ctx, parser_tx_t *v) {
CHECK_ERROR(readTransactionVersion(ctx, &v->transactionVersion));

// Now read the number of spends, outputs, mints and burns
CHECK_ERROR(readUint64(ctx, &v->spends.elements));
CHECK_ERROR(readUint64(ctx, &v->outputs.elements));
CHECK_ERROR(readUint64(ctx, &v->mints.elements));
CHECK_ERROR(readUint64(ctx, &v->burns.elements));

// now read the fee and expiration
CHECK_ERROR(readInt64(ctx, &v->fee));
CHECK_ERROR(readUint32(ctx, &v->expiration));

Expand Down
27 changes: 25 additions & 2 deletions app/src/parser_txdef.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,31 @@ extern "C" {
// Move bytes_t definition to a different place?
#include "keys_def.h"

#define NAME_LENGTH 32
#define METADATA_LENGTH 96
#define NAME_LENGTH 32
#define METADATA_LENGTH 96
#define PUBKEY_RANDONMESS_LEN 32
#define BINDING_SIGNATURE_LEN 64

// 32-bytes public_key randomness
// 192-byes proof
// 32-bytes value_commitment
// 32-bytes root hash
// 4-bytes tree_size
// 32-bytes nullifier
// 64-bytes authorize signatures
#define SPENDLEN 32 + 192 + 32 + 32 + 4 + 32 + 64

// 192-bytes proof + 328-bytes Merkle Note
#define OUTPUTLEN 192 + 328

// 33-bytes public_key randomness
// 192-byes proof
// 193-bytes asset_len (creator addr(32) + name (32) + metadata (96) + nonce (1))
// 8-bytes value
// + optional values 32 bytes owner, opion flag + 32_bytes new owner
#define MINTLEN 32 + 192 + 193 + 8

#define BURNLEN 32 + 8

typedef enum {
V1 = 1,
Expand Down

0 comments on commit 3fa70cb

Please sign in to comment.