Skip to content

Commit

Permalink
improve hash computation tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
janmazak committed May 4, 2021
1 parent 4d2dffa commit a29028e
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 16 deletions.
51 changes: 40 additions & 11 deletions src/auxDataHashBuilder.c
Expand Up @@ -22,39 +22,54 @@ enum {
HC_CATALYST_PAYLOAD = (1u << 1) // catalyst voting registration payload hash context
};

// Syntactic sugar
/*
The following macros and functions have dual purpose:
1. syntactic sugar for neat recording of hash computations;
2. thorough tracing of hash computations.
*/

#define APPEND_CBOR(hashContexts, type, value) \
if (hashContexts & HC_AUX_DATA) { \
blake2b_256_append_cbor(&builder->auxDataHash, type, value, true); \
blake2b_256_append_cbor_aux_data(&builder->auxDataHash, type, value); \
} \
if (hashContexts & HC_CATALYST_PAYLOAD) { \
blake2b_256_append_cbor(&builder->catalystRegistrationData.payloadHash, type, value, false); \
blake2b_256_append_cbor_catalyst(&builder->catalystRegistrationData.payloadHash, type, value); \
}

#define APPEND_DATA(hashContexts, buffer, bufferSize) \
if (hashContexts & HC_AUX_DATA) { \
blake2b_256_append_and_trace(&builder->auxDataHash, buffer, bufferSize); \
blake2b_256_append_buffer_aux_data(&builder->auxDataHash, buffer, bufferSize); \
} \
if (hashContexts & HC_CATALYST_PAYLOAD) { \
blake2b_256_append(&builder->catalystRegistrationData.payloadHash, buffer, bufferSize); \
blake2b_256_append_buffer_catalyst(&builder->catalystRegistrationData.payloadHash, buffer, bufferSize); \
}


__noinline_due_to_stack__
static void blake2b_256_append_cbor(
static void blake2b_256_append_cbor_aux_data(
blake2b_256_context_t* hashCtx,
uint8_t type, uint64_t value, bool trace
uint8_t type, uint64_t value
)
{
uint8_t buffer[10];
size_t size = cbor_writeToken(type, value, buffer, SIZEOF(buffer));
if (trace) {
TRACE_BUFFER(buffer, size);
}
TRACE_BUFFER(buffer, size);
blake2b_256_append(hashCtx, buffer, size);
}

__noinline_due_to_stack__
static void blake2b_256_append_cbor_catalyst(
blake2b_256_context_t* hashCtx,
uint8_t type, uint64_t value
)
{
uint8_t buffer[10];
size_t size = cbor_writeToken(type, value, buffer, SIZEOF(buffer));
TRACE_BUFFER(buffer, size);
blake2b_256_append(hashCtx, buffer, size);
}

static void blake2b_256_append_and_trace(
static void blake2b_256_append_buffer_aux_data(
blake2b_256_context_t* hashCtx,
const uint8_t* buffer,
size_t bufferSize
Expand All @@ -66,6 +81,20 @@ static void blake2b_256_append_and_trace(
blake2b_256_append(hashCtx, buffer, bufferSize);
}

static void blake2b_256_append_buffer_catalyst(
blake2b_256_context_t* hashCtx,
const uint8_t* buffer,
size_t bufferSize
)
{
// keeping tracing within a function to be able to extract the serialized data
// by matching the function name where the tracing is invoked
TRACE_BUFFER(buffer, bufferSize);
blake2b_256_append(hashCtx, buffer, bufferSize);
}

/* End of hash computation utilities. */

void auxDataHashBuilder_init(
aux_data_hash_builder_t* builder
)
Expand Down
17 changes: 12 additions & 5 deletions src/txHashBuilder.c
Expand Up @@ -18,15 +18,20 @@
#endif // DEVEL


// Syntactic sugar
/*
The following macros and functions have dual purpose:
1. syntactic sugar for neat recording of hash computations;
2. thorough tracing of hash computations.
*/

#define BUILDER_APPEND_CBOR(type, value) \
blake2b_256_append_cbor(&builder->txHash, type, value)
blake2b_256_append_cbor_tx_body(&builder->txHash, type, value)

#define BUILDER_APPEND_DATA(buffer, bufferSize) \
blake2b_256_append_and_trace(&builder->txHash, buffer, bufferSize)
blake2b_256_append_buffer_tx_body(&builder->txHash, buffer, bufferSize)


static void blake2b_256_append_and_trace(
static void blake2b_256_append_buffer_tx_body(
blake2b_256_context_t* hashCtx,
const uint8_t* buffer,
size_t bufferSize
Expand All @@ -37,7 +42,7 @@ static void blake2b_256_append_and_trace(
}

__noinline_due_to_stack__
static void blake2b_256_append_cbor(
static void blake2b_256_append_cbor_tx_body(
blake2b_256_context_t* hashCtx,
uint8_t type, uint64_t value
)
Expand All @@ -48,6 +53,8 @@ static void blake2b_256_append_cbor(
blake2b_256_append(hashCtx, buffer, size);
}

/* End of hash computation utilities. */

void txHashBuilder_init(
tx_hash_builder_t* builder,
uint16_t numInputs,
Expand Down

0 comments on commit a29028e

Please sign in to comment.