Skip to content

Commit

Permalink
fix: align output merkle tree depth with solidity
Browse files Browse the repository at this point in the history
  • Loading branch information
diegonehab authored and vfusco committed Apr 12, 2024
1 parent 5b2a267 commit 60926e3
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
6 changes: 4 additions & 2 deletions sys-utils/libcmt/src/merkle.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include <strings.h>

// clang-format off
static const uint8_t pristine_hash[CMT_MERKLE_TREE_HEIGHT+1][CMT_KECCAK_LENGTH] = {
static const uint8_t pristine_hash[][CMT_KECCAK_LENGTH] = {
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0xad,0x32,0x28,0xb6,0x76,0xf7,0xd3,0xcd,0x42,0x84,0xa5,0x44,0x3f,0x17,0xf1,0x96,0x2b,0x36,0xe4,0x91,0xb3,0x0a,0x40,0xb2,0x40,0x58,0x49,0xe5,0x97,0xba,0x5f,0xb5},
{0xb4,0xc1,0x19,0x51,0x95,0x7c,0x6f,0x8f,0x64,0x2c,0x4a,0xf6,0x1c,0xd6,0xb2,0x46,0x40,0xfe,0xc6,0xdc,0x7f,0xc6,0x07,0xee,0x82,0x06,0xa9,0x9e,0x92,0x41,0x0d,0x30},
Expand Down Expand Up @@ -164,7 +164,9 @@ 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]) {
if (me->leaf_count == UINT64_MAX) {
const uint64_t max_count =
(CMT_MERKLE_TREE_HEIGHT < 8 * sizeof(uint64_t)) ? (UINT64_C(1) << CMT_MERKLE_TREE_HEIGHT) : UINT64_MAX;
if (me->leaf_count == max_count) {
return -ENOBUFS;
}
uint8_t right[CMT_KECCAK_LENGTH];
Expand Down
2 changes: 1 addition & 1 deletion sys-utils/libcmt/src/merkle.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#include "keccak.h"

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

/** Opaque Merkle tree state.
Expand Down
14 changes: 7 additions & 7 deletions sys-utils/libcmt/tests/merkle.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
static void print(int m, uint8_t md[CMT_KECCAK_LENGTH]) {
printf("%3d: ", m);
for (int i = 0, n = CMT_KECCAK_LENGTH; i < n; ++i)
printf("%02x%s", md[i], i + 1 == n ? "\n" : " ");
printf("0x%02x%s", md[i], i + 1 == n ? "\n" : ", ");
}
#endif

Expand Down Expand Up @@ -61,9 +61,9 @@ void test_merkle_push_back_and_get_root(void) {

uint8_t root[CMT_KECCAK_LENGTH];
cmt_merkle_get_root_hash(&merkle, root);
uint8_t expected_root[CMT_KECCAK_LENGTH] = {0xe2, 0xc3, 0xed, 0x40, 0x52, 0xee, 0xb1, 0xd6, 0x05, 0x14, 0xb4, 0xc3,
0x8e, 0xce, 0x8d, 0x73, 0xa2, 0x7f, 0x37, 0xfa, 0x5b, 0x36, 0xdc, 0xbf, 0x33, 0x8e, 0x70, 0xde, 0x95, 0x79,
0x8c, 0xaa};
uint8_t expected_root[CMT_KECCAK_LENGTH] = {0x27, 0x33, 0xe5, 0x0f, 0x52, 0x6e, 0xc2, 0xfa, 0x19, 0xa2, 0x2b, 0x31,
0xe8, 0xed, 0x50, 0xf2, 0x3c, 0xd1, 0xfd, 0xf9, 0x4c, 0x91, 0x54, 0xed, 0x3a, 0x76, 0x09, 0xa2, 0xf1, 0xff,
0x98, 0x1f};
for (int i = 0; i < CMT_KECCAK_LENGTH; ++i) {
assert(root[i] == expected_root[i]);
}
Expand All @@ -83,9 +83,9 @@ void test_cmt_merkle_push_back_data_and_get_root(void) {

uint8_t root[CMT_KECCAK_LENGTH];
cmt_merkle_get_root_hash(&merkle, root);
uint8_t expected_root[CMT_KECCAK_LENGTH] = {0x47, 0x7d, 0xf0, 0x98, 0x96, 0x8c, 0x1b, 0x89, 0x90, 0x6b, 0xcd, 0x68,
0x94, 0x90, 0x7d, 0xa2, 0x9d, 0xd9, 0xcb, 0x3c, 0xde, 0x87, 0x65, 0xba, 0x9c, 0x9e, 0xa0, 0x8e, 0xaa, 0x0f,
0x5f, 0xd2};
uint8_t expected_root[CMT_KECCAK_LENGTH] = {0xe8, 0xe0, 0x47, 0x71, 0x14, 0xcb, 0x63, 0x0c, 0x4d, 0x14, 0xee, 0xa2,
0x49, 0xeb, 0x2c, 0x63, 0xd8, 0x4c, 0x9c, 0x68, 0x5d, 0xdf, 0x35, 0xd1, 0x37, 0x01, 0x9e, 0x65, 0x9a, 0xe2,
0x04, 0x18};
for (int i = 0; i < CMT_KECCAK_LENGTH; ++i) {
assert(root[i] == expected_root[i]);
}
Expand Down

0 comments on commit 60926e3

Please sign in to comment.