Skip to content

Commit

Permalink
fix: output root merkle hash
Browse files Browse the repository at this point in the history
fix computation
fix rollup utility use of merkle tree
simplify code
  • Loading branch information
diegonehab committed Mar 22, 2024
1 parent 4cb3971 commit cb4accd
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 161 deletions.
2 changes: 0 additions & 2 deletions sys-utils/libcmt/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ ioctl_SRC := \
src/abi.c \
src/keccak.c \
src/merkle.c \
src/merkle-table.c \
src/rollup.c \
src/ioctl/io.c

Expand Down Expand Up @@ -76,7 +75,6 @@ mock_SRC := \
src/buf.c \
src/keccak.c \
src/merkle.c \
src/merkle-table.c \
src/rollup.c \
src/mock/io.c

Expand Down
33 changes: 14 additions & 19 deletions sys-utils/libcmt/include/libcmt/merkle.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,26 @@
#include "keccak.h"

enum {
CMT_MERKLE_MAX_DEPTH = 64, /**< merkle tree height */
CMT_MERKLE_TREE_HEIGHT = 64, /**< merkle tree height */
};

/** Opaque Merkle tree state.
* initialize with: @ref cmt_merkle_init */
typedef struct {
uint64_t leaf_count;
uint8_t state[CMT_MERKLE_MAX_DEPTH][CMT_KECCAK_LENGTH];
const uint8_t (*zero)[CMT_KECCAK_LENGTH];
uint64_t leaf_count; /**< number of leaves in tree */
uint8_t state[CMT_MERKLE_TREE_HEIGHT][CMT_KECCAK_LENGTH]; /**< hashes of complete subtrees */
} cmt_merkle_t;

/** Initialize a @ref cmt_merkle_t tree state.
*
* @param [in] me uninitialized state */
void cmt_merkle_init(cmt_merkle_t *me);

/** Resets a @ref cmt_merkle_t to pristine conditions.
*
* @param [in] me initialized state */
void cmt_merkle_reset(cmt_merkle_t *me);

/** Finalize a @ref cmt_merkle_t tree state.
*
* @param [in] me initialized state
Expand All @@ -63,14 +67,12 @@ int cmt_merkle_load(cmt_merkle_t *me, const char *filepath);
* - 0 on success */
int cmt_merkle_save(cmt_merkle_t *me, const char *filepath);

/** Size in bytes required by merkle state save
/** Return number of leaves already in tree
*
* @param [in] me uninitialized state
* @param [in] length size of @p data in bytes
* @param [in] data array of bytes
* @param [in,out] me initialized state
* @return
* - size of the array required by @ref cmt_merkle_state_save */
size_t cmt_merkle_max_length(void);
* - leaf count */
uint64_t cmt_merkle_get_leaf_count(cmt_merkle_t *me);

/** Append a leaf node
*
Expand All @@ -79,14 +81,7 @@ size_t cmt_merkle_max_length(void);
* @return
* - 0 success
* - -ENOBUFS indicates the tree is full */
int cmt_merkle_push_back(cmt_merkle_t *me, uint8_t hash[CMT_KECCAK_LENGTH]);

/** Return number of leaves already in tree
*
* @param [in,out] me initialized state
* @return
* - leaf count */
uint64_t cmt_merkle_get_leaf_count(cmt_merkle_t *me);
int cmt_merkle_push_back(cmt_merkle_t *me, const uint8_t hash[CMT_KECCAK_LENGTH]);

/** Compute the keccak-256 hash of @p data and append it as a leaf node
*
Expand All @@ -98,7 +93,7 @@ uint64_t cmt_merkle_get_leaf_count(cmt_merkle_t *me);
* - -ENOBUFS indicates that the tree is full */
int cmt_merkle_push_back_data(cmt_merkle_t *me, size_t length, const void *data);

/** Retrieve the root hash of the merkle tree
/** Compute the root hash of the merkle tree
*
* @param [in] me initialized state
* @param [out] root root hash of the merkle tree */
Expand Down
5 changes: 5 additions & 0 deletions sys-utils/libcmt/include/libcmt/rollup.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,5 +209,10 @@ int cmt_rollup_load_merkle(cmt_rollup_t *me, const char *path);
* |< 0| failure with a -errno value | */
int cmt_rollup_save_merkle(cmt_rollup_t *me, const char *path);

/** Resets the merkle tree to pristine conditions
*
* @param [in,out] me initialized cmt_rollup_t instance */
int cmt_rollup_reset_merkle(cmt_rollup_t *me);

#endif /* CMT_ROLLUP_H */

71 changes: 0 additions & 71 deletions sys-utils/libcmt/src/merkle-table.c

This file was deleted.

Loading

0 comments on commit cb4accd

Please sign in to comment.