diff --git a/sys-utils/libcmt/Makefile b/sys-utils/libcmt/Makefile index 969aea88..067839ac 100644 --- a/sys-utils/libcmt/Makefile +++ b/sys-utils/libcmt/Makefile @@ -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 @@ -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 diff --git a/sys-utils/libcmt/include/libcmt/merkle.h b/sys-utils/libcmt/include/libcmt/merkle.h index b1342144..08a231eb 100644 --- a/sys-utils/libcmt/include/libcmt/merkle.h +++ b/sys-utils/libcmt/include/libcmt/merkle.h @@ -24,15 +24,14 @@ #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. @@ -40,6 +39,11 @@ typedef struct { * @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 @@ -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 * @@ -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 * @@ -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 */ diff --git a/sys-utils/libcmt/include/libcmt/rollup.h b/sys-utils/libcmt/include/libcmt/rollup.h index e6255caf..1c0d72d4 100644 --- a/sys-utils/libcmt/include/libcmt/rollup.h +++ b/sys-utils/libcmt/include/libcmt/rollup.h @@ -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 */ diff --git a/sys-utils/libcmt/src/merkle-table.c b/sys-utils/libcmt/src/merkle-table.c deleted file mode 100644 index 6aa9de8f..00000000 --- a/sys-utils/libcmt/src/merkle-table.c +++ /dev/null @@ -1,71 +0,0 @@ -/** Precomputed merkle roots for various tree heights */ -#ifndef CMT_MERKLE_TABLE_H -#define CMT_MERKLE_TABLE_H -#include -const uint8_t cmt_merkle_table[CMT_MERKLE_MAX_DEPTH][CMT_KECCAK_LENGTH] = { - {0xc5,0xd2,0x46,0x01,0x86,0xf7,0x23,0x3c,0x92,0x7e,0x7d,0xb2,0xdc,0xc7,0x03,0xc0,0xe5,0x00,0xb6,0x53,0xca,0x82,0x27,0x3b,0x7b,0xfa,0xd8,0x04,0x5d,0x85,0xa4,0x70}, - {0x9c,0x6b,0x2c,0x1b,0x0d,0x0b,0x25,0xa0,0x08,0xe6,0xc8,0x82,0xcc,0x7b,0x41,0x5f,0x30,0x99,0x65,0xc7,0x2a,0xd2,0xb9,0x44,0xac,0x09,0x31,0x04,0x8c,0xa3,0x1c,0xd5}, - {0xfa,0xdb,0xd3,0xc7,0xf7,0x9f,0xa2,0xbd,0xc4,0xf2,0x48,0x57,0x70,0x9c,0xd4,0xa4,0xe8,0x70,0x62,0x3d,0xc9,0xe9,0xab,0xcd,0xfd,0x6e,0x44,0x80,0x33,0xe3,0x52,0x12}, - {0xec,0xa0,0x78,0x60,0x5c,0x1b,0x0a,0xd6,0xff,0x43,0x23,0xf7,0xc2,0x33,0x07,0x58,0x5d,0x3d,0xdd,0xd5,0x04,0xf9,0x6e,0x7a,0x7f,0x72,0x2f,0x98,0x02,0xd2,0xa1,0xb7}, - {0x0f,0xfa,0x09,0x00,0xc8,0x38,0xd1,0x73,0x41,0xdf,0x2d,0x00,0xfa,0x48,0x32,0x75,0x5d,0xe6,0x19,0xe6,0x46,0x13,0x78,0x44,0x70,0x06,0x68,0xad,0x54,0x4c,0x8a,0xae}, - {0xf3,0xac,0xe1,0x89,0x6f,0x86,0xf9,0x16,0x27,0xcc,0x1c,0x09,0xee,0xab,0xa2,0xcd,0x76,0xd8,0x2a,0x75,0xbe,0x6f,0x09,0xb9,0x4c,0x86,0x15,0x24,0xfa,0x5e,0x52,0x89}, - {0x89,0x43,0x76,0xe0,0x4f,0x93,0x2d,0xea,0xdc,0x9a,0xb2,0x12,0xac,0x51,0x4f,0x37,0xb4,0x1e,0x67,0x0b,0xe2,0xf8,0x00,0x2b,0xab,0xde,0x1f,0xaf,0x20,0x93,0x54,0x61}, - {0x28,0xed,0x10,0x3e,0xef,0x2c,0x52,0x5c,0x95,0x61,0x19,0x60,0xb0,0x1a,0x0a,0xf4,0x89,0x82,0x55,0xaa,0x8e,0x8f,0x50,0xcf,0xe4,0x8a,0x86,0x61,0xc4,0x33,0x6d,0x51}, - {0xc7,0x5a,0x4c,0x92,0xcc,0x03,0x06,0xb9,0x76,0xea,0x3d,0x1c,0x38,0x9f,0x01,0xea,0xc5,0xd7,0x86,0xb3,0xa2,0x77,0x80,0x27,0xbe,0x39,0xb0,0x27,0xba,0xea,0x4a,0xc8}, - {0x7b,0xef,0xe8,0x35,0xef,0xb5,0x39,0xbc,0x3b,0x6b,0xc4,0x8a,0x8f,0xcd,0x3f,0xd7,0xda,0x58,0xd7,0x70,0x66,0xf1,0x92,0xd6,0xca,0x45,0x0c,0xb1,0x66,0x87,0x21,0x53}, - {0x84,0x60,0xcc,0xfb,0xf1,0xcf,0x67,0x75,0x70,0xdc,0x77,0x3f,0x4d,0x4b,0x1d,0xc6,0x09,0x9a,0x72,0x43,0xd2,0xc2,0x96,0xaa,0xed,0x66,0x53,0x80,0x0a,0x38,0xc1,0x71}, - {0x46,0xda,0x32,0x7e,0x0a,0xfa,0xa5,0xc8,0x7c,0x99,0x5f,0x6c,0xe4,0xb1,0x1d,0x47,0x36,0xcd,0x50,0x0c,0x9f,0x95,0x0b,0xa8,0xd5,0x99,0x3e,0xa2,0x8d,0xba,0x8c,0x51}, - {0xf7,0xad,0x65,0xa6,0x0a,0xcb,0x7f,0xd4,0x57,0xae,0xeb,0x06,0xe9,0x93,0xca,0x22,0xbb,0x50,0xd4,0x04,0x09,0xba,0x35,0x9b,0x30,0x45,0xb6,0x36,0xb5,0x6f,0x7a,0xfd}, - {0xf3,0x30,0x87,0x5a,0xd2,0x65,0xc8,0xd8,0x24,0xc0,0x06,0x50,0xd5,0x65,0x5c,0x45,0xcc,0xb2,0x17,0x0a,0x86,0xdf,0x24,0x30,0x3d,0x67,0xee,0x0f,0xf8,0xd0,0xd4,0xb0}, - {0xbc,0xb6,0xb3,0x19,0xfb,0x8c,0x49,0x95,0x87,0xe7,0x62,0x32,0xbf,0x82,0x46,0xc3,0x50,0x13,0xd0,0x18,0x81,0x02,0x70,0x65,0x49,0xe8,0xdc,0x3c,0x47,0x35,0xe3,0xc4}, - {0xc2,0xb7,0x5e,0x58,0x5c,0x35,0xac,0x0f,0x61,0xb7,0x50,0xdc,0x2e,0xe3,0xf0,0x2b,0x4e,0x6a,0x1b,0x39,0x11,0xf6,0x3d,0x49,0x55,0x86,0x83,0x6f,0x19,0x47,0x3c,0x71}, - {0x08,0x8c,0xcc,0x8a,0x02,0xc8,0x5b,0x7f,0xe9,0x9a,0xf8,0x8d,0xcf,0x06,0x4d,0x2c,0x07,0x18,0xe5,0xcf,0xff,0x88,0xa0,0x0e,0xc7,0x1a,0xaf,0x2f,0x10,0xb1,0x6e,0x8d}, - {0x92,0x7f,0xed,0x4a,0xa2,0x66,0xbd,0x58,0xca,0x7a,0x61,0x4a,0x46,0xe8,0xde,0xa4,0x9f,0xa8,0xe8,0xfa,0x36,0x94,0x9e,0x67,0xce,0xd4,0x13,0x54,0x1a,0x72,0x52,0x76}, - {0xfe,0xff,0x8c,0x75,0x13,0x9a,0x42,0xb6,0xff,0x70,0x10,0x30,0x25,0x53,0xf6,0xb2,0x2c,0x3c,0x71,0x15,0xca,0x36,0x6e,0xd3,0x80,0xe9,0x2b,0xb7,0xf9,0xd9,0x22,0xd7}, - {0x7b,0x1d,0xa6,0xfb,0x6d,0x18,0x03,0x24,0x23,0x1c,0x53,0x88,0xbb,0x7b,0x78,0xb6,0xa5,0x0a,0xb7,0x6b,0x4b,0xdf,0x4b,0xc3,0xe6,0xca,0xcf,0xd1,0x98,0xdc,0x8b,0x19}, - {0xfd,0x09,0x27,0x6a,0x6f,0xf4,0xab,0x67,0x30,0xd4,0x65,0xc8,0x1a,0xff,0xac,0x18,0x5e,0xa1,0x1e,0x60,0x29,0x4b,0xd4,0x76,0x14,0xc1,0x82,0xfa,0x12,0x26,0xf5,0x1d}, - {0x50,0x2b,0x48,0xc6,0xf3,0xa3,0x7f,0x40,0x20,0x1a,0xa8,0x44,0xc6,0x92,0x9c,0xd5,0x1b,0xa2,0xa9,0x55,0x28,0xa5,0xd4,0xab,0x6c,0x48,0x35,0xca,0x8f,0x3b,0x61,0x15}, - {0x1f,0xcc,0x41,0x34,0xdd,0x97,0xd0,0x70,0x39,0xa8,0xc0,0x43,0x93,0xc8,0x2b,0x8d,0x57,0x74,0xc8,0x59,0xcd,0x30,0x0f,0x73,0xec,0xa5,0x69,0x0a,0xb6,0xe6,0x76,0xa1}, - {0x38,0x7f,0x34,0x6d,0x77,0xd8,0xa2,0xab,0x82,0xc8,0xde,0xa8,0x1f,0x6b,0xca,0x47,0xc0,0xfa,0xb1,0xac,0x09,0xb5,0x36,0xfe,0x70,0x13,0x64,0x55,0x15,0x1d,0x8b,0x1b}, - {0xd2,0x54,0x97,0x2d,0x91,0xde,0xaf,0x8e,0x4e,0x0d,0x65,0x7c,0x17,0x24,0xd8,0x92,0x7c,0x92,0xc7,0x1b,0x1d,0xd0,0xb7,0xac,0x42,0x67,0x3a,0x98,0xac,0xd9,0x49,0x2a}, - {0x1a,0x1a,0x90,0xd9,0x04,0xd8,0xdc,0x4c,0xfd,0x42,0xc1,0xfa,0x54,0x7b,0x6f,0x6c,0xc6,0xb2,0xf1,0x31,0x70,0x5d,0xb5,0x0b,0x45,0x37,0x69,0x1c,0xaa,0xa0,0xef,0xd2}, - {0x0d,0x7f,0xd6,0xb2,0x9d,0x2e,0xc1,0xcc,0xd7,0xa3,0x72,0x22,0x17,0x0f,0x18,0x23,0xc3,0xa0,0xe8,0x7b,0x79,0xb0,0x86,0x77,0xe1,0x55,0xdb,0xec,0xdc,0x41,0xc8,0x5c}, - {0x37,0x4f,0xd0,0x9e,0x5c,0xe9,0x01,0x3e,0xd2,0x58,0x22,0xb3,0xe0,0x43,0x95,0x49,0x4f,0xdd,0x32,0xec,0x51,0xe9,0x57,0xf8,0x8c,0xdb,0xb8,0x0b,0x18,0xf6,0xc3,0x40}, - {0xeb,0x81,0x1f,0x1a,0x0f,0xef,0xbb,0xbe,0xe7,0xd3,0xd4,0x32,0x42,0xd0,0x76,0x1d,0x0e,0x57,0x29,0x60,0x2f,0x54,0x0e,0x48,0xcb,0xf4,0xe4,0x44,0x40,0x28,0x07,0x7b}, - {0x1e,0x97,0x01,0x7a,0x75,0xe9,0x6e,0xcb,0xde,0x97,0xc8,0xd1,0xaa,0xb1,0x1c,0x69,0x45,0x96,0x18,0x25,0x1b,0xe8,0x26,0xd5,0xf5,0x66,0x2b,0x99,0xb1,0x77,0x8c,0x9e}, - {0xed,0x7a,0xce,0xc3,0xc9,0xc3,0x3e,0x37,0xc4,0xcb,0xf3,0xb2,0x63,0x08,0x24,0x93,0xbc,0x02,0x63,0xe9,0xce,0x57,0x79,0x5d,0xb6,0x4b,0x53,0xee,0x96,0x89,0x9a,0xe7}, - {0x69,0x9f,0x41,0x38,0x6f,0x9f,0xdc,0x8c,0xf4,0xe0,0x23,0x8c,0x51,0xe6,0x32,0x37,0x17,0xc4,0xfa,0xeb,0xd0,0xb5,0xfc,0x9f,0xb0,0xb1,0x85,0x4d,0x9c,0xf0,0x4b,0xe2}, - {0x0b,0xe0,0x9c,0x32,0xa2,0xff,0xd9,0x5c,0x28,0x2a,0x53,0xf3,0x01,0x48,0xc6,0x25,0x24,0xf0,0x3c,0xa5,0x94,0xdc,0x2e,0x68,0x38,0x3b,0x12,0x50,0x2d,0x5e,0x88,0x1b}, - {0x4f,0xdf,0x62,0x1b,0xbe,0xc8,0x8c,0x22,0x11,0x51,0xe8,0x09,0x7e,0x7a,0x4c,0xfe,0xe7,0x56,0xa7,0x16,0x94,0xab,0x19,0x05,0x06,0xf3,0xe9,0x28,0xb1,0xf4,0x32,0x49}, - {0xf9,0xb8,0x39,0x67,0x3c,0x09,0x6b,0xc3,0x88,0x2c,0x8c,0x9f,0x78,0x75,0x72,0x25,0x3f,0x02,0xc9,0x9d,0xc3,0x2e,0x05,0x36,0x80,0xef,0xd6,0xe8,0xde,0x3a,0x71,0x59}, - {0x8b,0x2d,0x51,0x20,0x7c,0xda,0xeb,0x73,0xd6,0x3a,0x70,0x0b,0xcb,0xba,0x45,0x75,0xeb,0x7a,0xf7,0x66,0xc8,0xa0,0xfb,0x90,0x01,0x7d,0xb2,0x10,0x73,0x68,0xd2,0x69}, - {0x9b,0x30,0xaf,0x41,0xc1,0x24,0xad,0x45,0x38,0x8f,0x99,0xa3,0xb0,0x6e,0xe8,0x23,0xb5,0x2c,0x5f,0x48,0x98,0xd0,0xa9,0x6b,0x94,0x7b,0xd2,0x1b,0xe9,0xe1,0x6d,0xc0}, - {0xd0,0x9a,0x01,0xbd,0x2b,0x2a,0x10,0xa4,0xdd,0xb9,0x5e,0x99,0x49,0xdd,0x48,0x5a,0xd0,0x7f,0xe0,0x9c,0x6e,0xf8,0x9f,0xd3,0x59,0x93,0xda,0x25,0x71,0xc7,0x79,0x62}, - {0xbd,0xeb,0x33,0xaa,0x5a,0x04,0x7a,0x33,0x97,0xd8,0x5f,0x7c,0xca,0xdc,0xdd,0x99,0xe3,0xf5,0xaa,0xe7,0xed,0xbd,0x4e,0x9c,0x6a,0xcc,0xc3,0xaf,0x5e,0xa9,0xe8,0xd0}, - {0xd4,0xa2,0xc3,0xd1,0xcf,0x01,0x47,0xdf,0xf0,0xf3,0x10,0x56,0xa1,0x70,0x40,0xd9,0x56,0x7f,0x83,0xd3,0x2b,0x65,0xd0,0x66,0x39,0x3a,0x00,0x18,0x3e,0xcb,0x57,0x69}, - {0x66,0xf4,0xf4,0x04,0x74,0x5e,0x55,0xd7,0x7a,0x69,0x53,0x26,0x17,0x82,0xbd,0x2f,0xbc,0xc3,0xe0,0x5d,0x33,0x6e,0x43,0x38,0x39,0xf9,0xf4,0x7d,0x2a,0x75,0x91,0xe2}, - {0x1e,0x10,0x5f,0xb5,0xca,0x98,0x1a,0xc4,0x63,0xe5,0x43,0x70,0x27,0x9d,0xed,0x78,0x09,0x50,0x1e,0x70,0x7a,0x84,0x58,0x6f,0xc7,0x99,0xd3,0x51,0x7a,0x87,0x5c,0x8a}, - {0xf0,0x5b,0xe0,0x24,0x8b,0x1d,0x3b,0x05,0x1e,0xcb,0xe6,0x9b,0x9c,0x9b,0x72,0x43,0xcb,0xdc,0x2d,0x55,0x39,0x67,0x6e,0x89,0x21,0xab,0xe9,0x44,0x52,0x78,0xeb,0xc5}, - {0xfd,0x4a,0x5d,0xc6,0x72,0x3b,0xa2,0x17,0x34,0xdc,0xe8,0xa5,0xf6,0x06,0x23,0x85,0x88,0x32,0x6b,0x61,0x55,0x34,0x10,0x01,0x25,0x1c,0x1e,0x36,0x28,0xf4,0x5b,0xf7}, - {0xdc,0xc5,0x27,0x61,0x5a,0xa6,0x4a,0x58,0xea,0xff,0xaf,0xdb,0xf5,0x81,0x20,0x98,0x0d,0x1c,0xa9,0x51,0xb8,0xc5,0x04,0x05,0xa3,0xe4,0x3a,0xdc,0xf1,0xb8,0x5e,0xd2}, - {0x68,0x3b,0x1a,0xf3,0x91,0xdc,0x70,0xb7,0xbb,0x10,0x35,0xea,0x6d,0x23,0xd0,0xa6,0xae,0x6e,0x53,0x2f,0x0c,0xbf,0x25,0xf1,0xb2,0xfb,0xfa,0xd8,0xdc,0x98,0x23,0x4e}, - {0xf5,0x93,0x3c,0xd6,0xae,0x81,0xf7,0x75,0x71,0x8d,0xf9,0x3f,0x00,0x28,0x56,0x2a,0xa9,0x98,0x9f,0xaa,0x58,0x78,0x48,0xdf,0xd4,0xcd,0x52,0x68,0x36,0x79,0xc8,0x37}, - {0x51,0x43,0x95,0x81,0x24,0xba,0x18,0x6c,0x3c,0x9f,0xdd,0x6d,0x4b,0x4f,0xcc,0x6c,0x44,0x0a,0x25,0x20,0x1b,0x94,0x6d,0x78,0x51,0xee,0xea,0x9b,0x6b,0xde,0xd2,0x4c}, - {0xa6,0x05,0x1b,0x6a,0xaf,0x5a,0xdd,0x70,0x49,0x72,0xb3,0x38,0x6c,0xc1,0xb9,0x8f,0x05,0xa3,0xa6,0xdf,0x84,0xa2,0x5e,0x1a,0x1e,0x50,0xaa,0x79,0x7b,0x78,0x9f,0x75}, - {0x30,0x3e,0x17,0xb6,0xd9,0x75,0x4a,0xb2,0x38,0x60,0x71,0xc3,0x40,0xb9,0xe8,0xb1,0x79,0x4c,0x72,0x80,0xdd,0x1f,0x38,0x2a,0xe9,0x25,0xf0,0xdc,0xc2,0x50,0xa2,0xed}, - {0xc6,0x87,0xa5,0x3a,0xc7,0x22,0xfb,0x52,0xa5,0x5c,0x78,0x30,0xad,0x81,0xa6,0xde,0x71,0x0a,0x22,0xca,0x47,0x89,0xa6,0xbc,0xfd,0x39,0x1f,0xc1,0x01,0x24,0x46,0x4f}, - {0x41,0x92,0x06,0xc4,0x4f,0xf3,0xee,0x31,0x3c,0xc0,0x34,0xc7,0x4f,0xd3,0x04,0x94,0xdd,0xdf,0xbe,0x2d,0xdb,0x38,0xbb,0x80,0x00,0xe1,0x93,0xaf,0x10,0xb8,0x70,0x0d}, - {0x27,0x0e,0x16,0xbb,0x6b,0x49,0x63,0x1f,0x9e,0xed,0xe9,0x79,0x84,0x61,0x0e,0xe8,0xba,0xfa,0x72,0x90,0x54,0xc4,0x4a,0xca,0xa6,0x09,0x98,0xad,0x60,0x06,0xad,0xd5}, - {0x5b,0x4d,0x6b,0x45,0x53,0x34,0x85,0x72,0x19,0x1d,0xec,0x2c,0x0d,0x03,0x3d,0x83,0x28,0xa5,0xe7,0x0d,0x9e,0xa2,0x33,0x53,0xa8,0xf6,0xa3,0x6e,0xbf,0x53,0xe8,0x11}, - {0xbd,0x95,0x2a,0x68,0xc0,0x1b,0x82,0xa3,0xf0,0x4a,0x34,0xd9,0x3d,0xc4,0x2a,0x3a,0x12,0xc4,0x4c,0xd6,0xc1,0x67,0xa9,0xae,0x1b,0xd0,0x7b,0x50,0x1c,0xf0,0x78,0xa2}, - {0x29,0xf4,0x06,0x14,0x50,0x8e,0x58,0x23,0x98,0xa6,0x9a,0xce,0x06,0xa2,0x80,0x20,0x8f,0x46,0xb9,0x1a,0x2a,0xbb,0x81,0xc5,0xf9,0x4c,0x44,0x85,0xa0,0x0c,0xe2,0x8c}, - {0xfc,0xfb,0xee,0xba,0x53,0xa0,0xc3,0x3a,0x93,0xac,0x29,0x53,0x1a,0xce,0x67,0xed,0x80,0x2b,0xc5,0x40,0xa2,0xec,0xe4,0x48,0x41,0xa9,0x20,0xa0,0x13,0x82,0xb8,0x81}, - {0x1b,0x3b,0xc9,0x8f,0x72,0x2d,0x16,0x84,0x0c,0xba,0xf3,0x98,0x03,0x98,0xc6,0x38,0x75,0x6f,0xb3,0x39,0x14,0x08,0xbf,0x5b,0xd0,0x1c,0xc6,0x1b,0xd3,0xe0,0xc3,0x4f}, - {0x25,0x40,0x2d,0x4d,0xd3,0x5f,0x00,0xc8,0x95,0x2b,0x3d,0xd6,0x3d,0x9c,0x57,0xa5,0x5f,0xb1,0xcc,0x59,0x65,0x80,0x9f,0x9c,0x84,0x59,0x83,0xe9,0xb7,0xc7,0x3a,0xe6}, - {0x11,0x92,0xa3,0xc1,0xfc,0x8a,0x1f,0x38,0x12,0xd3,0xec,0xf6,0x14,0x87,0xa3,0xea,0xcc,0x69,0xeb,0x03,0x98,0xcb,0x38,0x43,0xf7,0x50,0xc9,0xfd,0xa7,0xeb,0x38,0x26}, - {0xb3,0x57,0x8a,0x06,0x51,0x69,0xf8,0xe0,0xd2,0x86,0xfc,0x5f,0x65,0x9c,0x8e,0xf5,0x75,0x02,0x3f,0x70,0xce,0xac,0xee,0xe4,0xb0,0xbb,0x68,0x26,0x35,0xfb,0xae,0x14}, - {0xe0,0x85,0x78,0x90,0xb6,0x88,0xae,0xf3,0xb8,0x7c,0x36,0xfe,0x6d,0x0d,0x72,0xd1,0x47,0xc2,0xe4,0xf5,0x02,0xdd,0xcb,0xfe,0x2f,0x3e,0x44,0x3f,0xfa,0x91,0x16,0xaf}, - {0x89,0x70,0x53,0xe7,0xb5,0x27,0xc6,0xed,0x2b,0x88,0x04,0x6d,0x59,0x5c,0xa5,0x3a,0x0a,0xbb,0xe3,0x4c,0x19,0x9a,0x5f,0x2f,0x7a,0x10,0x18,0x23,0x8b,0x66,0x83,0x1b}, - {0x0d,0xac,0xa4,0x92,0xa4,0x85,0x8f,0x43,0x3e,0x39,0x5e,0x65,0x7b,0x8d,0x6b,0x83,0x0b,0xc6,0x9d,0xc0,0x77,0x15,0xce,0x16,0xd7,0x4a,0x41,0xed,0x67,0x55,0x78,0x7f}, -}; -#endif /* CMT_MERKLE_TABLE_H */ diff --git a/sys-utils/libcmt/src/merkle.c b/sys-utils/libcmt/src/merkle.c index 0e72b7ef..6e67df7d 100644 --- a/sys-utils/libcmt/src/merkle.c +++ b/sys-utils/libcmt/src/merkle.c @@ -14,27 +14,87 @@ * limitations under the License. */ -/* The Load/Store file format is as follows: - * - * | length | offset | contents | - * | ------ | ------ | ---------- | - * | 8 | 0 | leaf_count | - * | 32 | 8 | state[0] | - * | 32 | 40 | state[1] | - * | 32 | ... | state[...] | - * | 32 | 2024 | state[63] | - */ - #include "libcmt/merkle.h" + #include #include #include #include #include -extern const uint8_t cmt_merkle_table[][CMT_KECCAK_LENGTH]; +static const uint8_t pristine_hash[CMT_MERKLE_TREE_HEIGHT+1][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}, + {0x21,0xdd,0xb9,0xa3,0x56,0x81,0x5c,0x3f,0xac,0x10,0x26,0xb6,0xde,0xc5,0xdf,0x31,0x24,0xaf,0xba,0xdb,0x48,0x5c,0x9b,0xa5,0xa3,0xe3,0x39,0x8a,0x04,0xb7,0xba,0x85}, + {0xe5,0x87,0x69,0xb3,0x2a,0x1b,0xea,0xf1,0xea,0x27,0x37,0x5a,0x44,0x09,0x5a,0x0d,0x1f,0xb6,0x64,0xce,0x2d,0xd3,0x58,0xe7,0xfc,0xbf,0xb7,0x8c,0x26,0xa1,0x93,0x44}, + {0x0e,0xb0,0x1e,0xbf,0xc9,0xed,0x27,0x50,0x0c,0xd4,0xdf,0xc9,0x79,0x27,0x2d,0x1f,0x09,0x13,0xcc,0x9f,0x66,0x54,0x0d,0x7e,0x80,0x05,0x81,0x11,0x09,0xe1,0xcf,0x2d}, + {0x88,0x7c,0x22,0xbd,0x87,0x50,0xd3,0x40,0x16,0xac,0x3c,0x66,0xb5,0xff,0x10,0x2d,0xac,0xdd,0x73,0xf6,0xb0,0x14,0xe7,0x10,0xb5,0x1e,0x80,0x22,0xaf,0x9a,0x19,0x68}, + {0xff,0xd7,0x01,0x57,0xe4,0x80,0x63,0xfc,0x33,0xc9,0x7a,0x05,0x0f,0x7f,0x64,0x02,0x33,0xbf,0x64,0x6c,0xc9,0x8d,0x95,0x24,0xc6,0xb9,0x2b,0xcf,0x3a,0xb5,0x6f,0x83}, + {0x98,0x67,0xcc,0x5f,0x7f,0x19,0x6b,0x93,0xba,0xe1,0xe2,0x7e,0x63,0x20,0x74,0x24,0x45,0xd2,0x90,0xf2,0x26,0x38,0x27,0x49,0x8b,0x54,0xfe,0xc5,0x39,0xf7,0x56,0xaf}, + {0xce,0xfa,0xd4,0xe5,0x08,0xc0,0x98,0xb9,0xa7,0xe1,0xd8,0xfe,0xb1,0x99,0x55,0xfb,0x02,0xba,0x96,0x75,0x58,0x50,0x78,0x71,0x09,0x69,0xd3,0x44,0x0f,0x50,0x54,0xe0}, + {0xf9,0xdc,0x3e,0x7f,0xe0,0x16,0xe0,0x50,0xef,0xf2,0x60,0x33,0x4f,0x18,0xa5,0xd4,0xfe,0x39,0x1d,0x82,0x09,0x23,0x19,0xf5,0x96,0x4f,0x2e,0x2e,0xb7,0xc1,0xc3,0xa5}, + {0xf8,0xb1,0x3a,0x49,0xe2,0x82,0xf6,0x09,0xc3,0x17,0xa8,0x33,0xfb,0x8d,0x97,0x6d,0x11,0x51,0x7c,0x57,0x1d,0x12,0x21,0xa2,0x65,0xd2,0x5a,0xf7,0x78,0xec,0xf8,0x92}, + {0x34,0x90,0xc6,0xce,0xeb,0x45,0x0a,0xec,0xdc,0x82,0xe2,0x82,0x93,0x03,0x1d,0x10,0xc7,0xd7,0x3b,0xf8,0x5e,0x57,0xbf,0x04,0x1a,0x97,0x36,0x0a,0xa2,0xc5,0xd9,0x9c}, + {0xc1,0xdf,0x82,0xd9,0xc4,0xb8,0x74,0x13,0xea,0xe2,0xef,0x04,0x8f,0x94,0xb4,0xd3,0x55,0x4c,0xea,0x73,0xd9,0x2b,0x0f,0x7a,0xf9,0x6e,0x02,0x71,0xc6,0x91,0xe2,0xbb}, + {0x5c,0x67,0xad,0xd7,0xc6,0xca,0xf3,0x02,0x25,0x6a,0xde,0xdf,0x7a,0xb1,0x14,0xda,0x0a,0xcf,0xe8,0x70,0xd4,0x49,0xa3,0xa4,0x89,0xf7,0x81,0xd6,0x59,0xe8,0xbe,0xcc}, + {0xda,0x7b,0xce,0x9f,0x4e,0x86,0x18,0xb6,0xbd,0x2f,0x41,0x32,0xce,0x79,0x8c,0xdc,0x7a,0x60,0xe7,0xe1,0x46,0x0a,0x72,0x99,0xe3,0xc6,0x34,0x2a,0x57,0x96,0x26,0xd2}, + {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}, + {0xe1,0xd3,0xb5,0xc8,0x07,0xb2,0x81,0xe4,0x68,0x3c,0xc6,0xd6,0x31,0x5c,0xf9,0x5b,0x9a,0xde,0x86,0x41,0xde,0xfc,0xb3,0x23,0x72,0xf1,0xc1,0x26,0xe3,0x98,0xef,0x7a}, + {0x5a,0x2d,0xce,0x0a,0x8a,0x7f,0x68,0xbb,0x74,0x56,0x0f,0x8f,0x71,0x83,0x7c,0x2c,0x2e,0xbb,0xcb,0xf7,0xff,0xfb,0x42,0xae,0x18,0x96,0xf1,0x3f,0x7c,0x74,0x79,0xa0}, + {0xb4,0x6a,0x28,0xb6,0xf5,0x55,0x40,0xf8,0x94,0x44,0xf6,0x3d,0xe0,0x37,0x8e,0x3d,0x12,0x1b,0xe0,0x9e,0x06,0xcc,0x9d,0xed,0x1c,0x20,0xe6,0x58,0x76,0xd3,0x6a,0xa0}, + {0xc6,0x5e,0x96,0x45,0x64,0x47,0x86,0xb6,0x20,0xe2,0xdd,0x2a,0xd6,0x48,0xdd,0xfc,0xbf,0x4a,0x7e,0x5b,0x1a,0x3a,0x4e,0xcf,0xe7,0xf6,0x46,0x67,0xa3,0xf0,0xb7,0xe2}, + {0xf4,0x41,0x85,0x88,0xed,0x35,0xa2,0x45,0x8c,0xff,0xeb,0x39,0xb9,0x3d,0x26,0xf1,0x8d,0x2a,0xb1,0x3b,0xdc,0xe6,0xae,0xe5,0x8e,0x7b,0x99,0x35,0x9e,0xc2,0xdf,0xd9}, + {0x5a,0x9c,0x16,0xdc,0x00,0xd6,0xef,0x18,0xb7,0x93,0x3a,0x6f,0x8d,0xc6,0x5c,0xcb,0x55,0x66,0x71,0x38,0x77,0x6f,0x7d,0xea,0x10,0x10,0x70,0xdc,0x87,0x96,0xe3,0x77}, + {0x4d,0xf8,0x4f,0x40,0xae,0x0c,0x82,0x29,0xd0,0xd6,0x06,0x9e,0x5c,0x8f,0x39,0xa7,0xc2,0x99,0x67,0x7a,0x09,0xd3,0x67,0xfc,0x7b,0x05,0xe3,0xbc,0x38,0x0e,0xe6,0x52}, + {0xcd,0xc7,0x25,0x95,0xf7,0x4c,0x7b,0x10,0x43,0xd0,0xe1,0xff,0xba,0xb7,0x34,0x64,0x8c,0x83,0x8d,0xfb,0x05,0x27,0xd9,0x71,0xb6,0x02,0xbc,0x21,0x6c,0x96,0x19,0xef}, + {0x0a,0xbf,0x5a,0xc9,0x74,0xa1,0xed,0x57,0xf4,0x05,0x0a,0xa5,0x10,0xdd,0x9c,0x74,0xf5,0x08,0x27,0x7b,0x39,0xd7,0x97,0x3b,0xb2,0xdf,0xcc,0xc5,0xee,0xb0,0x61,0x8d}, + {0xb8,0xcd,0x74,0x04,0x6f,0xf3,0x37,0xf0,0xa7,0xbf,0x2c,0x8e,0x03,0xe1,0x0f,0x64,0x2c,0x18,0x86,0x79,0x8d,0x71,0x80,0x6a,0xb1,0xe8,0x88,0xd9,0xe5,0xee,0x87,0xd0}, + {0x83,0x8c,0x56,0x55,0xcb,0x21,0xc6,0xcb,0x83,0x31,0x3b,0x5a,0x63,0x11,0x75,0xdf,0xf4,0x96,0x37,0x72,0xcc,0xe9,0x10,0x81,0x88,0xb3,0x4a,0xc8,0x7c,0x81,0xc4,0x1e}, + {0x66,0x2e,0xe4,0xdd,0x2d,0xd7,0xb2,0xbc,0x70,0x79,0x61,0xb1,0xe6,0x46,0xc4,0x04,0x76,0x69,0xdc,0xb6,0x58,0x4f,0x0d,0x8d,0x77,0x0d,0xaf,0x5d,0x7e,0x7d,0xeb,0x2e}, + {0x38,0x8a,0xb2,0x0e,0x25,0x73,0xd1,0x71,0xa8,0x81,0x08,0xe7,0x9d,0x82,0x0e,0x98,0xf2,0x6c,0x0b,0x84,0xaa,0x8b,0x2f,0x4a,0xa4,0x96,0x8d,0xbb,0x81,0x8e,0xa3,0x22}, + {0x93,0x23,0x7c,0x50,0xba,0x75,0xee,0x48,0x5f,0x4c,0x22,0xad,0xf2,0xf7,0x41,0x40,0x0b,0xdf,0x8d,0x6a,0x9c,0xc7,0xdf,0x7e,0xca,0xe5,0x76,0x22,0x16,0x65,0xd7,0x35}, + {0x84,0x48,0x81,0x8b,0xb4,0xae,0x45,0x62,0x84,0x9e,0x94,0x9e,0x17,0xac,0x16,0xe0,0xbe,0x16,0x68,0x8e,0x15,0x6b,0x5c,0xf1,0x5e,0x09,0x8c,0x62,0x7c,0x00,0x56,0xa9}, + {0x27,0xae,0x5b,0xa0,0x8d,0x72,0x91,0xc9,0x6c,0x8c,0xbd,0xdc,0xc1,0x48,0xbf,0x48,0xa6,0xd6,0x8c,0x79,0x74,0xb9,0x43,0x56,0xf5,0x37,0x54,0xef,0x61,0x71,0xd7,0x57}, + {0xbf,0x55,0x8b,0xeb,0xd2,0xce,0xec,0x7f,0x3c,0x5d,0xce,0x04,0xa4,0x78,0x2f,0x88,0xc2,0xc6,0x03,0x6a,0xe7,0x8e,0xe2,0x06,0xd0,0xbc,0x52,0x89,0xd2,0x04,0x61,0xa2}, + {0xe2,0x19,0x08,0xc2,0x96,0x8c,0x06,0x99,0x04,0x0a,0x6f,0xd8,0x66,0xa5,0x77,0xa9,0x9a,0x9d,0x2e,0xc8,0x87,0x45,0xc8,0x15,0xfd,0x4a,0x47,0x2c,0x78,0x92,0x44,0xda}, + {0xae,0x82,0x4d,0x72,0xdd,0xc2,0x72,0xaa,0xb6,0x8a,0x8c,0x30,0x22,0xe3,0x6f,0x10,0x45,0x44,0x37,0xc1,0x88,0x6f,0x3f,0xf9,0x92,0x7b,0x64,0xf2,0x32,0xdf,0x41,0x4f}, + {0x27,0xe4,0x29,0xa4,0xbe,0xf3,0x08,0x3b,0xc3,0x1a,0x67,0x1d,0x04,0x6e,0xa5,0xc1,0xf5,0xb8,0xc3,0x09,0x4d,0x72,0x86,0x8d,0x9d,0xfd,0xc1,0x2c,0x73,0x34,0xac,0x5f}, + {0x74,0x3c,0xc5,0xc3,0x65,0xa9,0xa6,0xa1,0x5c,0x1f,0x24,0x0a,0xc2,0x58,0x80,0xc7,0xa9,0xd1,0xde,0x29,0x06,0x96,0xcb,0x76,0x60,0x74,0xa1,0xd8,0x3d,0x92,0x78,0x16}, + {0x4a,0xdc,0xf6,0x16,0xc3,0xbf,0xab,0xf6,0x39,0x99,0xa0,0x19,0x66,0xc9,0x98,0xb7,0xbb,0x57,0x27,0x74,0x03,0x5a,0x63,0xea,0xd4,0x9d,0xa7,0x3b,0x59,0x87,0xf3,0x47}, + {0x75,0x78,0x66,0x45,0xd0,0xc5,0xdd,0x7c,0x04,0xa2,0xf8,0xa7,0x5d,0xca,0xe0,0x85,0x21,0x36,0x52,0xf5,0xbc,0xe3,0xea,0x8b,0x9b,0x9b,0xed,0xd1,0xca,0xb3,0xc5,0xe9}, + {0xb8,0x8b,0x15,0x2c,0x9b,0x8a,0x7b,0x79,0x63,0x7d,0x35,0x91,0x18,0x48,0xb0,0xc4,0x1e,0x7c,0xc7,0xcc,0xa2,0xab,0x4f,0xe9,0xa1,0x5f,0x9c,0x38,0xbb,0x4b,0xb9,0x39}, + {0x0c,0x4e,0x2d,0x8c,0xe8,0x34,0xff,0xd7,0xa6,0xcd,0x85,0xd7,0x11,0x3d,0x45,0x21,0xab,0xb8,0x57,0x77,0x48,0x45,0xc4,0x29,0x1e,0x6f,0x6d,0x01,0x0d,0x97,0xe3,0x18}, + {0x5b,0xc7,0x99,0xd8,0x3e,0x3b,0xb3,0x15,0x01,0xb3,0xda,0x78,0x66,0x80,0xdf,0x30,0xfb,0xc1,0x8e,0xb4,0x1c,0xbc,0xe6,0x11,0xe8,0xc0,0xe9,0xc7,0x2f,0x69,0x57,0x1c}, + {0xa1,0x0d,0x3e,0xf8,0x57,0xd0,0x4d,0x9c,0x03,0xea,0xd7,0xc6,0x31,0x7d,0x79,0x7a,0x09,0x0f,0xa1,0x27,0x1a,0xd9,0xc7,0xad,0xdf,0xbc,0xb4,0x12,0xe9,0x64,0x3d,0x4f}, + {0xb3,0x3b,0x18,0x09,0xc4,0x26,0x23,0xf4,0x74,0x05,0x5f,0xa9,0x40,0x0a,0x20,0x27,0xa7,0xa8,0x85,0xc8,0xdf,0xa4,0xef,0xe2,0x06,0x66,0xb4,0xee,0x27,0xd7,0x52,0x9c}, + {0x13,0x4d,0x7f,0x28,0xd5,0x3f,0x17,0x5f,0x6b,0xf4,0xb6,0x2f,0xaa,0x21,0x10,0xd5,0xb7,0x6f,0x0f,0x77,0x0c,0x15,0xe6,0x28,0x18,0x1c,0x1f,0xcc,0x18,0xf9,0x70,0xa9}, + {0xc3,0x4d,0x24,0xb2,0xfc,0x8c,0x50,0xca,0x9c,0x07,0xa7,0x15,0x6e,0xf4,0xe5,0xff,0x4b,0xdf,0x00,0x2e,0xda,0x0b,0x11,0xc1,0xd3,0x59,0xd0,0xb5,0x9a,0x54,0x68,0x07}, + {0x04,0xdb,0xb9,0xdb,0x63,0x14,0x57,0x87,0x9b,0x27,0xe0,0xdf,0xdb,0xe5,0x01,0x58,0xfd,0x9c,0xf9,0xb4,0xcf,0x77,0x60,0x5c,0x4a,0xc4,0xc9,0x5b,0xd6,0x5f,0xc9,0xf6}, + {0xf9,0x29,0x5a,0x68,0x66,0x47,0xcb,0x99,0x90,0x90,0x81,0x9c,0xda,0x70,0x08,0x20,0xc2,0x82,0xc6,0x13,0xce,0xdc,0xd2,0x18,0x54,0x0b,0xbc,0x6f,0x37,0xb0,0x1c,0x65}, + {0x67,0xc4,0xa1,0xea,0x62,0x4f,0x09,0x2a,0x3a,0x5c,0xca,0x2d,0x6f,0x0f,0x0d,0xb2,0x31,0x97,0x2f,0xce,0x62,0x7f,0x0e,0xcc,0xa0,0xde,0xe6,0x0f,0x17,0x55,0x1c,0x5f}, + {0x8f,0xda,0xeb,0x5a,0xb5,0x60,0xb2,0xce,0xb7,0x81,0xcd,0xb3,0x39,0x36,0x1a,0x0f,0xbe,0xe1,0xb9,0xdf,0xfa,0xd5,0x91,0x15,0x13,0x8c,0x8d,0x6a,0x70,0xdd,0xa9,0xcc}, + {0xc1,0xbf,0x0b,0xbd,0xd7,0xfe,0xe1,0x57,0x64,0x84,0x5d,0xb8,0x75,0xf6,0x43,0x25,0x59,0xff,0x8d,0xbc,0x90,0x55,0x32,0x44,0x31,0xbc,0x34,0xe5,0xb9,0x3d,0x15,0xda}, + {0x30,0x73,0x17,0x84,0x9e,0xcc,0xd9,0x0c,0x0c,0x7b,0x98,0x87,0x0b,0x93,0x17,0xc1,0x5a,0x59,0x59,0xdc,0xfb,0x84,0xc7,0x6d,0xcc,0x90,0x8c,0x4f,0xe6,0xba,0x92,0x12}, + {0x63,0x39,0xbf,0x06,0xe4,0x58,0xf6,0x64,0x6d,0xf5,0xe8,0x3b,0xa7,0xc3,0xd3,0x5b,0xc2,0x63,0xb3,0x22,0x2c,0x8e,0x90,0x40,0x06,0x88,0x47,0x74,0x9c,0xa8,0xe8,0xf9}, + {0x50,0x45,0xe4,0x34,0x2a,0xeb,0x52,0x1e,0xb3,0xa5,0x58,0x7e,0xc2,0x68,0xed,0x3a,0xa6,0xfa,0xf3,0x2b,0x62,0xb0,0xbc,0x41,0xa9,0xd5,0x49,0x52,0x1f,0x40,0x6f,0xc3}, + {0x08,0x60,0x1d,0x83,0xcd,0xd3,0x4b,0x5f,0x7b,0x8d,0xf6,0x3e,0x7b,0x9a,0x16,0x51,0x9d,0x35,0x47,0x3d,0x0b,0x89,0xc3,0x17,0xbe,0xed,0x3d,0x3d,0x94,0x24,0xb2,0x53}, + {0x84,0xe3,0x5c,0x5d,0x92,0x17,0x13,0x76,0xca,0xe5,0xc8,0x63,0x00,0x82,0x2d,0x72,0x9c,0xd3,0xa8,0x47,0x95,0x83,0xbe,0xf0,0x95,0x27,0x02,0x7d,0xba,0x5f,0x11,0x26}, + {0x3c,0x5c,0xbb,0xeb,0x38,0x34,0xb7,0xa5,0xc1,0xcb,0xa9,0xaa,0x5f,0xee,0x0c,0x95,0xec,0x3f,0x17,0xa3,0x3e,0xc3,0xd8,0x04,0x7f,0xff,0x79,0x91,0x87,0xf5,0xae,0x20}, + {0x40,0xbb,0xe9,0x13,0xc2,0x26,0xc3,0x4c,0x9f,0xbe,0x43,0x89,0xdd,0x72,0x89,0x84,0x25,0x7a,0x81,0x68,0x92,0xb3,0xca,0xe3,0xe4,0x31,0x91,0xdd,0x29,0x1f,0x0e,0xb5}, + {0x14,0xaf,0x53,0x85,0xbc,0xbb,0x1e,0x47,0x38,0xbb,0xae,0x81,0x06,0x04,0x6e,0x6e,0x2f,0xca,0x42,0x87,0x5a,0xa5,0xc0,0x00,0xc5,0x82,0x58,0x77,0x42,0xbc,0xc7,0x48}, + {0x72,0xf2,0x96,0x56,0x80,0x3c,0x2f,0x4b,0xe1,0x77,0xb1,0xb8,0xdd,0x2a,0x51,0x37,0x89,0x2b,0x08,0x0b,0x02,0x21,0x00,0xfd,0xe4,0xe9,0x6d,0x93,0xef,0x8c,0x96,0xff}, + {0xd0,0x6f,0x27,0x06,0x1c,0x73,0x4d,0x78,0x25,0xb4,0x68,0x65,0xd0,0x0a,0xa9,0x00,0xe5,0xcc,0x3a,0x36,0x72,0x08,0x0e,0x52,0x71,0x71,0xe1,0x17,0x1a,0xa5,0x03,0x8a}, + {0x28,0x20,0x39,0x85,0xb5,0xf2,0xd8,0x77,0x09,0x17,0x16,0x78,0x16,0x97,0x39,0xf9,0x57,0xd2,0x74,0x5f,0x4b,0xfa,0x5c,0xc9,0x1e,0x2b,0x4b,0xd9,0xbf,0x48,0x3b,0x40}, + {0x0a,0x16,0x29,0x46,0xe5,0x61,0x58,0xba,0xc0,0x67,0x3e,0x6d,0xd3,0xbd,0xfd,0xc1,0xe4,0xa0,0xe7,0x74,0x4a,0x12,0x0f,0xdb,0x64,0x00,0x50,0xc8,0xd7,0xab,0xe1,0xc6}, + {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} +}; + +static void copy_hash(const uint8_t src[CMT_KECCAK_LENGTH], uint8_t out[CMT_KECCAK_LENGTH]) { + memcpy(out, src, CMT_KECCAK_LENGTH); +} -static void hash2(const uint8_t lhs[CMT_KECCAK_LENGTH], const uint8_t rhs[CMT_KECCAK_LENGTH], +static void concat_hash(const uint8_t lhs[CMT_KECCAK_LENGTH], const uint8_t rhs[CMT_KECCAK_LENGTH], uint8_t out[CMT_KECCAK_LENGTH]) { cmt_keccak_t c[1]; cmt_keccak_init(c); @@ -43,10 +103,20 @@ static void hash2(const uint8_t lhs[CMT_KECCAK_LENGTH], const uint8_t rhs[CMT_KE cmt_keccak_final(c, out); } +static void print_hash(uint8_t hash[CMT_KECCAK_LENGTH]) { + for (int i = 0; i < CMT_KECCAK_LENGTH; ++i) { + fprintf(stderr, "%02x", hash[i]); + } + fprintf(stderr, "\n"); +} + void cmt_merkle_init(cmt_merkle_t *me) { + return cmt_merkle_reset(me); +} + +void cmt_merkle_reset(cmt_merkle_t *me) { me->leaf_count = 0; - me->zero = cmt_merkle_table; - memcpy(me->state, me->zero, sizeof(me->state)); + memset(me->state, 0, sizeof(me->state)); } void cmt_merkle_fini(cmt_merkle_t *me) { @@ -54,77 +124,80 @@ void cmt_merkle_fini(cmt_merkle_t *me) { (void) me; } -static int read_whole_file(const char *name, size_t max, void *data, size_t *length) { +int cmt_merkle_load(cmt_merkle_t *me, const char *name) { + FILE *fin = fopen(name, "rb"); + if (!fin) { + return -errno; + } + size_t read = fread(me, 1, sizeof(*me), fin); int rc = 0; - - FILE *file = fopen(name, "rb"); - if (!file) + if (read < sizeof(*me)) { + rc = -EIO; + } + if (fclose(fin) != 0 && rc == 0) { return -errno; - - size_t size = fread(data, 1, max, file); - if (getc(file) != EOF) - rc = -ENOBUFS; - else if (length) - *length = size; - fclose(file); + } return rc; } -int cmt_merkle_load(cmt_merkle_t *me, const char *name) { - return read_whole_file(name, cmt_merkle_max_length(), me, NULL); - me->zero = cmt_merkle_table; - - return 0; -} - -static int write_whole_file(const char *name, size_t length, const void *data) { +int cmt_merkle_save(cmt_merkle_t *me, const char *name) { + FILE *fout = fopen(name, "wb"); + if (!fout) { + return -errno; + } + size_t written = fwrite(me, 1, sizeof(*me), fout); int rc = 0; - - FILE *file = fopen(name, "wb"); - if (!file) + if (written < sizeof(*me)) { + rc = -EIO; + } + if (fclose(fout) != 0 && rc == 0) { return -errno; - - if (fwrite(data, 1, length, file) != length) - rc = -errno; - fclose(file); + } return rc; } -int cmt_merkle_save(cmt_merkle_t *me, const char *name) { - return write_whole_file(name, cmt_merkle_max_length(), me); -} - -size_t cmt_merkle_max_length() { - return sizeof((cmt_merkle_t *) 0)->leaf_count + sizeof((cmt_merkle_t *) 0)->state; +uint64_t cmt_merkle_get_leaf_count(cmt_merkle_t *me) { + return me->leaf_count; } -int cmt_merkle_push_back(cmt_merkle_t *me, uint8_t hash[CMT_KECCAK_LENGTH]) { - if (me->leaf_count == UINT64_MAX) +int cmt_merkle_push_back(cmt_merkle_t *me, const uint8_t hash[CMT_KECCAK_LENGTH]) { + if (me->leaf_count == UINT64_MAX) { return -ENOBUFS; - - unsigned n = (uint64_t) ffsll(++me->leaf_count) - 1u; - for (unsigned i = 0; i < n; ++i) - hash2(me->state[i], hash, hash); - memcpy(me->state[n], hash, CMT_KECCAK_LENGTH); + } + uint8_t right[CMT_KECCAK_LENGTH]; + copy_hash(hash, right); + for (int i = 0; i < CMT_MERKLE_TREE_HEIGHT; ++i) { + uint64_t bit = ((uint64_t) 1) << i; + /* if we have a hash for a subtree of the current size in the state... */ + if (me->leaf_count & bit) { + /* ...concat it with current running right and replace running right with it. */ + concat_hash(me->state[i], right, right); + /* otherwise... */ + } else { + /* ...just copy the current subtree hash to the state and we are done. */ + copy_hash(right, me->state[i]); + break; + } + } + ++me->leaf_count; return 0; } -uint64_t cmt_merkle_get_leaf_count(cmt_merkle_t *me) { - return me->leaf_count; -} - void cmt_merkle_get_root_hash(cmt_merkle_t *me, uint8_t root[CMT_KECCAK_LENGTH]) { - /* n is bound by CMT_MERKLE_MAX_DEPTH-1u */ - unsigned n = ((uint64_t) ffsll(me->leaf_count) - 1u) & (CMT_MERKLE_MAX_DEPTH - 1u); - memcpy(root, me->state[n], CMT_KECCAK_LENGTH); - - for (unsigned i = n; i < CMT_MERKLE_MAX_DEPTH; ++i) { - bool set = me->leaf_count & (UINT64_C(1) << i); - if (set) - hash2(me->state[i], root, root); - else - hash2(root, me->zero[i], root); + copy_hash(pristine_hash[0], root); + for (int i = 0; i < CMT_MERKLE_TREE_HEIGHT; ++i) { + uint64_t bit = ((uint64_t) 1) << i; + /* if we have a hash for a subtree of the current size in the state... */ + if (me->leaf_count & bit) { + /* ...concat it with current root on the right and replace root */ + concat_hash(me->state[i], root, root); + /* otherwise... */ + } else { + /* ...concat root with pristine tree on the right and replace root */ + concat_hash(root, pristine_hash[i], root); + } } + return 0; } int cmt_merkle_push_back_data(cmt_merkle_t *me, size_t n, const void *data) { diff --git a/sys-utils/libcmt/src/rollup.c b/sys-utils/libcmt/src/rollup.c index 7d15d589..dc4fc9bb 100644 --- a/sys-utils/libcmt/src/rollup.c +++ b/sys-utils/libcmt/src/rollup.c @@ -295,3 +295,8 @@ int cmt_rollup_load_merkle(cmt_rollup_t *me, const char *path) { int cmt_rollup_save_merkle(cmt_rollup_t *me, const char *path) { return DBG(cmt_merkle_save(me->merkle, path)); } + +int cmt_rollup_reset_merkle(cmt_rollup_t *me) { + cmt_merkle_reset(me->merkle); + return 0; +} diff --git a/sys-utils/rollup/rollup.cpp b/sys-utils/rollup/rollup.cpp index 2394e89a..b8d92645 100644 --- a/sys-utils/rollup/rollup.cpp +++ b/sys-utils/rollup/rollup.cpp @@ -67,7 +67,7 @@ static void print_help(void) { emit a voucher read from stdin as a JSON object in the format {"destination":
, "value": , "payload": } where
contains a 20-byte EVM address in hex, - and contains an big-endian 32-byte unsigned integer in hex. + and contains a big-endian 32-byte unsigned integer in hex. if successful, prints to stdout a JSON object in the format {"index": } where field "index" is the index allocated for the voucher @@ -316,6 +316,7 @@ static int finish_request_and_get_next(bool accept) try { if (cmt_rollup_finish(r, &f)) return 1; if (f.next_request_type == HTIF_YIELD_REASON_ADVANCE) { + cmt_rollup_reset_merkle(r); write_advance_state(r, &f); } else if (f.next_request_type == HTIF_YIELD_REASON_INSPECT) { write_inspect_state(r, &f);