Skip to content

Commit

Permalink
chore: Replace dup code with get_merkle_tree_hash
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcos Pernambuco Motta committed Nov 7, 2023
1 parent 070435f commit 7b3a9dd
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 60 deletions.
33 changes: 33 additions & 0 deletions src/i-hasher.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <array>
#include <cstddef>
#include <cstdint>
#include <stdexcept>

#include "meta.h"

Expand Down Expand Up @@ -100,6 +101,38 @@ inline static typename H::hash_type get_concat_hash(H &h, const typename H::hash
return result;
}

/// \brief Computes a merkle tree hash of a data buffer
/// \tparam H Hasher class
/// \param h Hasher object
/// \param data Data to be hashed
/// \param data_length Length of data
/// \param word_length Length of each word
/// \param result Receives the resulting merkle tree hash
template <typename H>
inline static void get_merkle_tree_hash(H &h, const unsigned char *data, uint64_t data_length, uint64_t word_length,
typename H::hash_type &result) {
if (data_length > word_length) {
if (data_length & 1) {
throw std::invalid_argument("data_length must be a power of 2 multiple of word_length");
}
data_length = data_length / 2;
typename H::hash_type left;
get_merkle_tree_hash(h, data, data_length, word_length, left);
get_merkle_tree_hash(h, data + data_length, data_length, word_length, result);
h.begin();
h.add_data(left.data(), left.size());
h.add_data(result.data(), result.size());
h.end(result);
} else {
if (data_length != word_length) {
throw std::invalid_argument("data_length must be a power of 2 multiple of word_length");
}
h.begin();
h.add_data(data, data_length);
h.end(result);
}
}

} // namespace cartesi

#endif
21 changes: 1 addition & 20 deletions src/uarch-record-step-state-access.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,28 +65,9 @@ class uarch_record_step_state_access : public i_uarch_step_state_access<uarch_re
}
}

static void get_hash(hasher_type &hasher, const unsigned char *data, size_t len, hash_type &hash) {
if (len <= 8) {
assert(len == 8);
hasher.begin();
hasher.add_data(data, len);
hasher.end(hash);
} else {
assert((len & 1) == 0);
len = len / 2;
hash_type left;
get_hash(hasher, data, len, left);
get_hash(hasher, data + len, len, hash);
hasher.begin();
hasher.add_data(left.data(), left.size());
hasher.add_data(hash.data(), hash.size());
hasher.end(hash);
}
}

static void get_hash(const access_data &data, hash_type &hash) {
hasher_type hasher;
get_hash(hasher, data.data(), data.size(), hash);
get_merkle_tree_hash(hasher, data.data(), data.size(), sizeof(uint64_t), hash);
}

public:
Expand Down
25 changes: 6 additions & 19 deletions src/uarch-replay-reset-state-access.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ class uarch_replay_reset_state_access : public i_uarch_reset_state_access<uarch_
if (access.get_read().has_value()) {
// if read data is available then its hash and the logged read hash must match
hash_type computed_hash;
get_hash(hasher, access.get_read().value().data(), access.get_read().value().size(), computed_hash);
get_hash(hasher, access.get_read().value(), computed_hash);

if (computed_hash != access.get_read_hash()) {
throw std::invalid_argument{"hash of read data and read hash at access " +
std::to_string(access_to_report()) + " does not match read hash"};
Expand All @@ -169,7 +170,7 @@ class uarch_replay_reset_state_access : public i_uarch_reset_state_access<uarch_
if (access.get_written().has_value()) {
// if written data is available then its hash and the logged written hash must match
hash_type computed_hash;
get_hash(hasher, access.get_written().value().data(), access.get_written().value().size(), computed_hash);
get_hash(hasher, access.get_written().value(), computed_hash);
if (computed_hash != access.get_written_hash().value()) {
throw std::invalid_argument{
"written hash and written data mismatch at access " + std::to_string(access_to_report())};
Expand Down Expand Up @@ -209,23 +210,9 @@ class uarch_replay_reset_state_access : public i_uarch_reset_state_access<uarch_
m_next_access++;
}

static void get_hash(hasher_type &hasher, const unsigned char *data, size_t len, hash_type &hash) {
if (len <= 8) {
assert(len == 8);
hasher.begin();
hasher.add_data(data, len);
hasher.end(hash);
} else {
assert((len & 1) == 0);
len = len / 2;
hash_type left;
get_hash(hasher, data, len, left);
get_hash(hasher, data + len, len, hash);
hasher.begin();
hasher.add_data(left.data(), left.size());
hasher.add_data(hash.data(), hash.size());
hasher.end(hash);
}
static void get_hash(machine_merkle_tree::hasher_type &hasher, const access_data &data,
machine_merkle_tree::hash_type &hash) {
get_merkle_tree_hash(hasher, data.data(), data.size(), sizeof(uint64_t), hash);
}
};

Expand Down
22 changes: 1 addition & 21 deletions src/uarch-replay-step-state-access.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,29 +112,9 @@ class uarch_replay_step_state_access : public i_uarch_step_state_access<uarch_re
}
}

static void get_hash(machine_merkle_tree::hasher_type &hasher, const unsigned char *data, size_t len,
machine_merkle_tree::hash_type &hash) {
if (len <= 8) {
assert(len == 8);
hasher.begin();
hasher.add_data(data, len);
hasher.end(hash);
} else {
assert((len & 1) == 0);
len = len / 2;
machine_merkle_tree::hash_type left;
get_hash(hasher, data, len, left);
get_hash(hasher, data + len, len, hash);
hasher.begin();
hasher.add_data(left.data(), left.size());
hasher.add_data(hash.data(), hash.size());
hasher.end(hash);
}
}

static void get_hash(machine_merkle_tree::hasher_type &hasher, const access_data &data,
machine_merkle_tree::hash_type &hash) {
get_hash(hasher, data.data(), data.size(), hash);
get_merkle_tree_hash(hasher, data.data(), data.size(), sizeof(uint64_t), hash);
}

/// \brief Checks a logged word read and advances log.
Expand Down

0 comments on commit 7b3a9dd

Please sign in to comment.