Skip to content

Commit

Permalink
Add Sparse Merkle Tree documentation (#26)
Browse files Browse the repository at this point in the history
* Added Sparse Merkle Tree documentation

* cosmetic fixes

* adjusted diamond

* Typo

Co-authored-by: Artem Chystiakov <[email protected]>

* Typo

Co-authored-by: Artem Chystiakov <[email protected]>

* Updated image

* Considered the practical stack limit.

---------

Co-authored-by: dovgopoly <[email protected]>
Co-authored-by: dovgopoly <[email protected]>
Co-authored-by: Artem Chystiakov <[email protected]>
  • Loading branch information
4 people authored Mar 5, 2024
1 parent a6cccd7 commit c3682db
Show file tree
Hide file tree
Showing 5 changed files with 617 additions and 13 deletions.
8 changes: 8 additions & 0 deletions docs/getting-started/guides/diamond/diamond.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,14 @@ If you want your Diamond to behave like an `ERC20`, `ERC721`, or other popular s
<td><a href="https://github.com/dl-solarity/solidity-lib/blob/master/contracts/diamond/introspection/DiamondERC165.sol">DiamondERC165</a></td>
<td>OpenZeppelin ERC165-based Facet</td>
</tr>
<tr>
<td><a href="https://github.com/dl-solarity/solidity-lib/tree/master/contracts/diamond/access/ownable">DiamondOwnable</a></td>
<td>OpenZeppelin Ownable-based Facet</td>
</tr>
<tr>
<td><a href="https://github.com/dl-solarity/solidity-lib/tree/master/contracts/diamond/access/access-control">DiamondAccessControl</a></td>
<td>OpenZeppelin AccessControl-based Facet</td>
</tr>
<tr>
<td><a href="https://github.com/dl-solarity/solidity-lib/blob/master/contracts/diamond/utils/InitializableStorage.sol">InitializableStorage</a></td>
<td>OpenZeppelin Initializable-based Storage</td>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Introduction

The Incremental Merkle Tree library library provides a cost-effective and straightforward method for maintaining the Merkle Tree data structure on-chain, including the capability to retrieve its root at the contract level. It also offers the flexibility to set a custom hash function, such as the Poseidon hash function, to make it ZKP-friendly. It has been implemented following the analysis of the [Deposit Contract Verification](https://github.com/runtimeverification/deposit-contract-verification/blob/master/deposit-contract-verification.pdf).
The Incremental Merkle Tree library provides a cost-effective and straightforward method for maintaining the Merkle Tree data structure on-chain, including the capability to retrieve its root at the contract level. It also offers the flexibility to set a custom hash function, such as the Poseidon hash function, to make it ZKP-friendly. It has been implemented following the analysis of the [Deposit Contract Verification](https://github.com/runtimeverification/deposit-contract-verification/blob/master/deposit-contract-verification.pdf).

## Implementation

Expand Down Expand Up @@ -76,7 +76,7 @@ function setHeight(AddressIMT storage tree, uint256 height_) internal;

This function sets the height of the Merkle Tree. It **reverts** if the provided height is less than or equal to the current Merkle Tree height. After the Tree Height is set, it will no longer grow automatically, and its height will have to be adjusted manually.

**Time complexity**
#### Time complexity

Constant.

Expand Down Expand Up @@ -114,7 +114,7 @@ This function adds a new element to the tree. In the case where the tree has (`2

However, if the tree height was previously set manually, the operation will revert.

**Time complexity**
#### Time complexity

`O(log(n))`, where `n` is the number of elements in the tree.

Expand Down Expand Up @@ -163,7 +163,7 @@ function setHashers(

This function sets custom hash functions to be used for Merkle Tree construction. The function will **revert** if the tree already contains at least one leaf.

#### **Time complexity**
#### Time complexity

Constant.

Expand Down Expand Up @@ -206,7 +206,7 @@ function root(AddressIMT storage tree) internal view returns (bytes32);

This function calculates and returns the root of the Merkle Tree based on the elements that were added to the tree previously and the tree's height

#### **Time complexity**
#### Time complexity

`O(log(n) + h)`, where `n` is the number of elements in the tree and `h` is the height of the tree.

Expand Down Expand Up @@ -238,7 +238,7 @@ function height(AddressIMT storage tree) internal view returns (uint256)

This function returns the tree height, which corresponds directly to the length of the `branches` array.

#### **Time complexity**
#### Time complexity

Constant.

Expand Down Expand Up @@ -276,7 +276,7 @@ function length(AddressIMT storage tree) internal view returns (uint256);

This function returns the number of leaves that have been added to the Merkle Tree.

#### **Time complexity**
#### Time complexity

Constant.

Expand Down Expand Up @@ -314,7 +314,7 @@ function isCustomHasherSet(AddressIMT storage tree) internal view returns (bool)

This function returns true, if the custom hash function was set, and false otherwise.

#### **Time complexity**
#### Time complexity

Constant.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ function push(AddressVector memory vector, address[] memory values_) internal pu

This function appends new elements to the `vector`. If there is no space left, it reallocates memory, doubling capacity.

**Time complexity**
#### Time complexity

Amortized `O(1)` when a single `value_` is passed. Otherwise, it is amortized `O(n)`, where `n` is the `values_.length`.

Expand Down Expand Up @@ -175,7 +175,7 @@ function pop(AddressVector memory vector) internal pure;

This function removes the last element from the `vector`. It neither changes the capacity nor reallocates memory. It will revert if called on an empty `vector`.

**Time complexity**
#### Time complexity

Constant.

Expand Down Expand Up @@ -273,7 +273,7 @@ function at(

This function returns an element by its 0-based `index_`. It is a read-only function. It will revert if the `index_` is out of bounds.

**Time complexity**
#### Time complexity

Constant.

Expand Down Expand Up @@ -309,7 +309,7 @@ function length(AddressVector memory vector) internal pure returns (uint256);

This function returns the length of the `vector`. It's a read-only function.

**Time complexity**
#### Time complexity

Constant.

Expand Down Expand Up @@ -354,7 +354,7 @@ function toArray(

This function returns a raw array `vector` based on. It's a read-only function.

**Time complexity**
#### Time complexity

Constant, as it's a pointer copy, not a deep copy.

Expand Down
Loading

0 comments on commit c3682db

Please sign in to comment.