Implement 5.1 Full Audit Naming Suggestions (#5215)
Co-authored-by: Hadrien Croubois <hadrien.croubois@gmail.com> Co-authored-by: cairo <cairoeth@protonmail.com>
This commit is contained in:
@ -49,7 +49,7 @@ library MerkleTree {
|
||||
|
||||
/**
|
||||
* @dev Initialize a {Bytes32PushTree} using {Hashes-commutativeKeccak256} to hash internal nodes.
|
||||
* The capacity of the tree (i.e. number of leaves) is set to `2**levels`.
|
||||
* The capacity of the tree (i.e. number of leaves) is set to `2**treeDepth`.
|
||||
*
|
||||
* Calling this function on MerkleTree that was already setup and used will reset it to a blank state.
|
||||
*
|
||||
@ -59,8 +59,8 @@ library MerkleTree {
|
||||
* IMPORTANT: The zero value should be carefully chosen since it will be stored in the tree representing
|
||||
* empty leaves. It should be a value that is not expected to be part of the tree.
|
||||
*/
|
||||
function setup(Bytes32PushTree storage self, uint8 levels, bytes32 zero) internal returns (bytes32 initialRoot) {
|
||||
return setup(self, levels, zero, Hashes.commutativeKeccak256);
|
||||
function setup(Bytes32PushTree storage self, uint8 treeDepth, bytes32 zero) internal returns (bytes32 initialRoot) {
|
||||
return setup(self, treeDepth, zero, Hashes.commutativeKeccak256);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -77,17 +77,17 @@ library MerkleTree {
|
||||
*/
|
||||
function setup(
|
||||
Bytes32PushTree storage self,
|
||||
uint8 levels,
|
||||
uint8 treeDepth,
|
||||
bytes32 zero,
|
||||
function(bytes32, bytes32) view returns (bytes32) fnHash
|
||||
) internal returns (bytes32 initialRoot) {
|
||||
// Store depth in the dynamic array
|
||||
Arrays.unsafeSetLength(self._sides, levels);
|
||||
Arrays.unsafeSetLength(self._zeros, levels);
|
||||
Arrays.unsafeSetLength(self._sides, treeDepth);
|
||||
Arrays.unsafeSetLength(self._zeros, treeDepth);
|
||||
|
||||
// Build each root of zero-filled subtrees
|
||||
bytes32 currentZero = zero;
|
||||
for (uint32 i = 0; i < levels; ++i) {
|
||||
for (uint32 i = 0; i < treeDepth; ++i) {
|
||||
Arrays.unsafeAccess(self._zeros, i).value = currentZero;
|
||||
currentZero = fnHash(currentZero, currentZero);
|
||||
}
|
||||
@ -129,20 +129,20 @@ library MerkleTree {
|
||||
function(bytes32, bytes32) view returns (bytes32) fnHash
|
||||
) internal returns (uint256 index, bytes32 newRoot) {
|
||||
// Cache read
|
||||
uint256 levels = self._zeros.length;
|
||||
uint256 treeDepth = depth(self);
|
||||
|
||||
// Get leaf index
|
||||
index = self._nextLeafIndex++;
|
||||
|
||||
// Check if tree is full.
|
||||
if (index >= 1 << levels) {
|
||||
if (index >= 1 << treeDepth) {
|
||||
Panic.panic(Panic.RESOURCE_ERROR);
|
||||
}
|
||||
|
||||
// Rebuild branch from leaf to root
|
||||
uint256 currentIndex = index;
|
||||
bytes32 currentLevelHash = leaf;
|
||||
for (uint32 i = 0; i < levels; i++) {
|
||||
for (uint32 i = 0; i < treeDepth; i++) {
|
||||
// Reaching the parent node, is currentLevelHash the left child?
|
||||
bool isLeft = currentIndex % 2 == 0;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user