11/*******************************************************************************
2- * (c) 2018 - 2024 Zondax AG
2+ * (c) 2018 - 2025 Zondax AG
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
@@ -94,9 +94,11 @@ parser_error_t compute_tree_size(uint32_t num_right_siblings, uint32_t index_of_
9494}
9595
9696/**
97- * @brief Compute the hash of a leaf node in the proof .
97+ * @brief Compute the hash of a leaf node at a given index in the leaves data .
9898 *
99- * @param proof The proof structure containing the leaf node.
99+ * @param leaves Pointer to the merkle_leaves_data_t structure containing the leaves data.
100+ * @param index The index of the leaf to hash.
101+ * @param hash Output buffer for the computed hash (must be at least CX_SHA256_SIZE bytes).
100102 * @return parser_error_t Error code indicating the result of the operation.
101103 */
102104static parser_error_t hash_index_leaf (merkle_leaves_data_t * leaves , uint64_t index , uint8_t * hash ) {
@@ -164,28 +166,32 @@ static parser_error_t get_next_lemma_hash(merkle_lemmas_t *lemmas, uint8_t *hash
164166}
165167
166168/**
167- * @brief Check if there are leaves in the range.
169+ * @brief Check if there are leaves in the specified range.
168170 *
169- * @param start The start index.
170- * @param end The end index.
171- * @param indices The indices array.
172- * @return bool True if there are leaves in the range, false otherwise.
171+ * @param start The start index (inclusive).
172+ * @param end The end index (exclusive).
173+ * @param indices Pointer to the merkle_leaves_indices_t structure containing the leaf indices.
174+ * @param has_leaves Output pointer; set to true if there are leaves in the range, false otherwise.
175+ * @return parser_error_t Error code indicating the result of the operation.
173176 */
174- bool has_leaves (uint64_t start , uint64_t end , merkle_leaves_indices_t * indices ) {
177+ static parser_error_t has_leaves (uint64_t start , uint64_t end , merkle_leaves_indices_t * indices , bool * has_leaves ) {
175178 CHECK_INPUT (indices );
179+ CHECK_INPUT (has_leaves );
176180
177181 for (uint64_t i = 0 ; i < indices -> entries ; i ++ ) {
178182 uint64_t index_tmp = 0 ;
179183 CHECK_ERROR (read_u64 (& indices -> indices , & index_tmp ));
180184 if (index_tmp >= start && index_tmp < end ) {
181185 indices -> indices .offset = 0 ;
182- return true;
186+ * has_leaves = true;
187+ return parser_ok ;
183188 }
184189 }
185190
186191 // reset offset
187192 indices -> indices .offset = 0 ;
188- return false;
193+ * has_leaves = false;
194+ return parser_ok ;
189195}
190196
191197/**
@@ -194,6 +200,7 @@ bool has_leaves(uint64_t start, uint64_t end, merkle_leaves_indices_t *indices)
194200 * @param proof The proof structure containing the necessary data.
195201 * @param index_start The start index.
196202 * @param index_end The end index.
203+ * @param hash Output buffer for the computed hash (must be at least CX_SHA256_SIZE bytes).
197204 * @return parser_error_t Error code indicating the result of the operation.
198205 */
199206static parser_error_t verify_multiproof_inner (proof_t * proof , uint64_t index_start , uint64_t index_end , uint8_t * hash ) {
@@ -207,9 +214,10 @@ static parser_error_t verify_multiproof_inner(proof_t *proof, uint64_t index_sta
207214 // If this is a single node, return the hash of the node
208215 if (index_end - index_start == 1 ) {
209216 uint64_t index_vec = 0 ;
210- if (schema_find_index (index_start , & proof -> indices , & index_vec )) {
217+ bool found = false;
218+ CHECK_ERROR (schema_find_index (index_start , & proof -> indices , & index_vec , & found ));
219+ if (found ) {
211220 CHECK_ERROR (hash_index_leaf (& proof -> leaves , index_vec , hash ));
212-
213221 return parser_ok ;
214222 }
215223
@@ -223,8 +231,10 @@ static parser_error_t verify_multiproof_inner(proof_t *proof, uint64_t index_sta
223231 return parser_unexpected_buffer_end ;
224232 }
225233
226- bool left_has_leaves = has_leaves (index_start , mid , & proof -> indices );
227- bool right_has_leaves = has_leaves (mid , index_end , & proof -> indices );
234+ bool left_has_leaves = false;
235+ bool right_has_leaves = false;
236+ CHECK_ERROR (has_leaves (index_start , mid , & proof -> indices , & left_has_leaves ));
237+ CHECK_ERROR (has_leaves (mid , index_end , & proof -> indices , & right_has_leaves ));
228238
229239 // Check left subtree
230240 CHECK_ERROR (checkStack ());
@@ -253,8 +263,8 @@ static parser_error_t verify_multiproof_inner(proof_t *proof, uint64_t index_sta
253263/**
254264 * @brief Compute the root hash for a given metadata structure.
255265 *
256- * @param metadata The metadata structure containing the necessary data.
257- * @param metadataDigest The output buffer for the computed metadata digest .
266+ * @param metadata Pointer to the merkle_proof_t structure containing the necessary data.
267+ * @param hash Output buffer for the computed root hash (must be at least CX_SHA256_SIZE bytes) .
258268 * @return parser_error_t Error code indicating the result of the operation.
259269 */
260270parser_error_t get_root_hash (const merkle_proof_t * metadata , uint8_t * hash ) {
0 commit comments