-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add basic indexed merkle tree. * membership check for nullifier tree and cleanup. * test works for a simple case. * fix test. * fix. * Add comment.
- Loading branch information
Showing
18 changed files
with
827 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
cpp/src/barretenberg/stdlib/merkle_tree/nullifier_tree/nullifier_leaf.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
#pragma once | ||
#include "barretenberg/crypto/pedersen_commitment/pedersen.hpp" | ||
|
||
namespace plonk { | ||
namespace stdlib { | ||
namespace merkle_tree { | ||
|
||
using namespace barretenberg; | ||
typedef uint256_t index_t; | ||
|
||
struct nullifier_leaf { | ||
fr value; | ||
index_t nextIndex; | ||
fr nextValue; | ||
|
||
bool operator==(nullifier_leaf const&) const = default; | ||
|
||
std::ostream& operator<<(std::ostream& os) | ||
{ | ||
os << "value = " << value << "\nnextIdx = " << nextIndex << "\nnextVal = " << nextValue; | ||
return os; | ||
} | ||
|
||
void read(uint8_t const*& it) | ||
{ | ||
using serialize::read; | ||
read(it, value); | ||
read(it, nextIndex); | ||
read(it, nextValue); | ||
} | ||
|
||
inline void write(std::vector<uint8_t>& buf) | ||
{ | ||
using serialize::write; | ||
write(buf, value); | ||
write(buf, nextIndex); | ||
write(buf, nextValue); | ||
} | ||
|
||
barretenberg::fr hash() const { return stdlib::merkle_tree::hash_multiple_native({ value, nextIndex, nextValue }); } | ||
}; | ||
|
||
inline std::pair<size_t, bool> find_closest_leaf(std::vector<nullifier_leaf> const& leaves_, fr const& new_value) | ||
{ | ||
std::vector<uint256_t> diff; | ||
bool repeated = false; | ||
for (size_t i = 0; i < leaves_.size(); i++) { | ||
auto leaf_value_ = uint256_t(leaves_[i].value); | ||
auto new_value_ = uint256_t(new_value); | ||
if (leaf_value_ > new_value_) { | ||
diff.push_back(leaf_value_); | ||
} else if (leaf_value_ == new_value_) { | ||
repeated = true; | ||
return std::make_pair(i, repeated); | ||
} else { | ||
diff.push_back(new_value_ - leaf_value_); | ||
} | ||
} | ||
auto it = std::min_element(diff.begin(), diff.end()); | ||
return std::make_pair(static_cast<size_t>(it - diff.begin()), repeated); | ||
} | ||
|
||
} // namespace merkle_tree | ||
} // namespace stdlib | ||
} // namespace plonk |
Oops, something went wrong.