Skip to content

Commit

Permalink
Obtain the shielded spend anchor from the Sapling bundle.
Browse files Browse the repository at this point in the history
  • Loading branch information
murisi committed Sep 10, 2024
1 parent b5edf3e commit 08e5685
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 33 deletions.
23 changes: 23 additions & 0 deletions app/src/parser_impl_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,29 @@ parser_error_t readFieldSizeU16(parser_context_t *ctx, uint16_t *size) {
return parser_ok;
}

parser_error_t readCompactSize(parser_context_t *ctx, uint64_t *result) {
uint8_t tag = 0;
uint16_t tmp16 = 0;
uint32_t tmp32 = 0;
CHECK_ERROR(readByte(ctx, &tag))
switch(tag) {
case 253:
CHECK_ERROR(readUint16(ctx, &tmp16))
*result = (uint64_t)tmp16;
break;
case 254:
CHECK_ERROR(readUint32(ctx, &tmp32))
*result = (uint64_t)tmp32;
break;
case 255:
CHECK_ERROR(readUint64(ctx, result))
break;
default:
*result = (uint64_t)tag;
}
return parser_ok;
}

parser_error_t checkTag(parser_context_t *ctx, uint8_t expectedTag) {
uint8_t tmpTag = 0;
CHECK_ERROR(readByte(ctx, &tmpTag))
Expand Down
1 change: 1 addition & 0 deletions app/src/parser_impl_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ parser_error_t readBytesSize(parser_context_t *ctx, uint8_t *output, uint16_t ou
parser_error_t readUint16(parser_context_t *ctx, uint16_t *value);
parser_error_t readUint32(parser_context_t *ctx, uint32_t *value);
parser_error_t readUint64(parser_context_t *ctx, uint64_t *value);
parser_error_t readCompactSize(parser_context_t *ctx, uint64_t *result);

parser_error_t readFieldSize(parser_context_t *ctx, uint32_t *size);
parser_error_t readFieldSizeU16(parser_context_t *ctx, uint16_t *size);
Expand Down
25 changes: 1 addition & 24 deletions app/src/parser_impl_masp.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,6 @@
#include "cx_blake2b.h"
#endif

static parser_error_t readCompactSize(parser_context_t *ctx, uint64_t *result) {
uint8_t tag = 0;
uint16_t tmp16 = 0;
uint32_t tmp32 = 0;
CHECK_ERROR(readByte(ctx, &tag))
switch(tag) {
case 253:
CHECK_ERROR(readUint16(ctx, &tmp16))
*result = (uint64_t)tmp16;
break;
case 254:
CHECK_ERROR(readUint32(ctx, &tmp32))
*result = (uint64_t)tmp32;
break;
case 255:
CHECK_ERROR(readUint64(ctx, result))
break;
default:
*result = (uint64_t)tag;
}
return parser_ok;
}

static parser_error_t readSaplingBundle(parser_context_t *ctx, masp_sapling_bundle_t *bundle) {
if (ctx == NULL || bundle == NULL) {
return parser_unexpected_error;
Expand Down Expand Up @@ -312,7 +289,7 @@ static parser_error_t readConvertDescriptionInfo(parser_context_t *ctx, masp_sap
CHECK_ERROR(readUint32(ctx, &builder->n_converts))
#if defined(LEDGER_SPECIFIC)
uint32_t rnd_converts = (uint32_t)transaction_get_n_converts();
if (rnd_converts != builder->n_converts) {
if (rnd_converts < builder->n_converts) {
return parser_invalid_number_of_converts;
}
#endif
Expand Down
6 changes: 4 additions & 2 deletions app/src/parser_impl_txn.c
Original file line number Diff line number Diff line change
Expand Up @@ -1422,7 +1422,9 @@ parser_error_t verifyShieldedHash(parser_context_t *ctx) {
#if defined(LEDGER_SPECIFIC)
// compute tx_id hash
uint8_t tx_id_hash[HASH_LEN] = {0};
tx_hash_txId(ctx->tx_obj, tx_id_hash);
if (tx_hash_txId(ctx->tx_obj, tx_id_hash) != zxerr_ok) {
return parser_unexpected_error;
}

if (ctx->tx_obj->transaction.sections.maspBuilder.target_hash.len == HASH_LEN) {
if (memcmp(tx_id_hash, ctx->tx_obj->transaction.sections.maspBuilder.target_hash.ptr, HASH_LEN) != 0) {
Expand All @@ -1432,4 +1434,4 @@ parser_error_t verifyShieldedHash(parser_context_t *ctx) {
#endif

return parser_ok;
}
}
9 changes: 3 additions & 6 deletions app/src/tx_hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,18 +125,15 @@ zxerr_t tx_hash_sapling_spends(const parser_tx_t *txObj, uint8_t *output) {

const uint8_t *spend = txObj->transaction.sections.maspTx.data.sapling_bundle.shielded_spends.ptr;
const uint64_t n_shielded_spends = txObj->transaction.sections.maspTx.data.sapling_bundle.n_shielded_spends;
const bool has_spend_anchor = txObj->transaction.sections.maspBuilder.builder.sapling_builder.has_spend_anchor;
const uint8_t *spend_anchor_ptr = txObj->transaction.sections.maspBuilder.builder.sapling_builder.spend_anchor.ptr;
const uint8_t *spend_anchor_ptr = txObj->transaction.sections.maspTx.data.sapling_bundle.anchor_shielded_spends.ptr;

for (uint64_t i = 0; i < n_shielded_spends; i++, spend += SHIELDED_SPENDS_LEN) {
shielded_spends_t *shielded_spends = (shielded_spends_t *)spend;

CHECK_CX_OK(cx_hash_no_throw(&nullifier_ctx.header, 0, shielded_spends->nullifier, NULLIFIER_LEN, NULL, 0));

CHECK_CX_OK(cx_hash_no_throw(&nc_ctx.header, 0, shielded_spends->cv, CV_LEN, NULL, 0));
if (has_spend_anchor) {
CHECK_CX_OK(cx_hash_no_throw(&nc_ctx.header, 0, spend_anchor_ptr, ANCHOR_LEN, NULL, 0));
}
CHECK_CX_OK(cx_hash_no_throw(&nc_ctx.header, 0, spend_anchor_ptr, ANCHOR_LEN, NULL, 0));
CHECK_CX_OK(cx_hash_no_throw(&nc_ctx.header, 0, shielded_spends->rk, RK_LEN, NULL, 0));
}

Expand Down Expand Up @@ -252,7 +249,7 @@ zxerr_t tx_hash_sapling_data(const parser_tx_t *txObj, uint8_t *output) {
uint8_t converts_hash[32] = {0};
uint8_t outputs_hash[32] = {0};

if (txObj->transaction.sections.maspTx.data.sapling_bundle.n_shielded_outputs != 0) {
if (txObj->transaction.sections.maspTx.data.sapling_bundle.n_shielded_spends != 0 || txObj->transaction.sections.maspTx.data.sapling_bundle.n_shielded_converts != 0 || txObj->transaction.sections.maspTx.data.sapling_bundle.n_shielded_outputs != 0) {
CHECK_ZXERR(tx_hash_sapling_spends(txObj, spends_hash));

// TODO: there is not an example to validate converts
Expand Down
2 changes: 1 addition & 1 deletion tests/testvectors.json
Original file line number Diff line number Diff line change
Expand Up @@ -50947,4 +50947,4 @@
],
"valid": true
}
]
]

0 comments on commit 08e5685

Please sign in to comment.