Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Hash the SMT leaf key to prevent tree structure manipulation #496

Merged
merged 2 commits into from
Jun 12, 2023

Conversation

xgreenx
Copy link
Contributor

@xgreenx xgreenx commented Jun 12, 2023

We use SMT in two places for contract balances and contract state. While it is not a huge problem for balances SMT root(because AssetId is randomly derived from sha256), it is a massive problem for contract state root. Each leaf key is specified by the user/developer for the storage key-value pair. The SMT is a vast data structure that uses some optimization that helps to improve its performance and occupied storage.

Based on the knowledge of how our SMT works inside, malicious users can manipulate the structure and make it work in a non-optimal way.
We've already faced that in the beta3 testnet.

It is a snapshot of the state of the contract from the beta3 testnet. It has only 30k leafs but because those leafs are close to each other it produces 1.3m of nodes in the SMT.

But if we hash each leaf key it reduces the number of the nodes from 1.3m to only 70k. Because of the randomness leafs are distributed in a better way preventing a huge number of empty side nodes.

This PR proposes to hash each leaf key of any SMT to prevent any kind of manipulation.

Copy link
Member

@Voxelot Voxelot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, ensuring a random uniform distribution of keys will prevent inefficiencies in the tree due to how structs are serialized into sequential slots by sway.

@xgreenx xgreenx merged commit 927f194 into master Jun 12, 2023
@xgreenx xgreenx deleted the feature/smt-key-hashing branch June 12, 2023 22:30
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants