Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Implement BitArray and replace trie.Key #2322

Open
wants to merge 63 commits into
base: main
Choose a base branch
from
Open

Conversation

weiihann
Copy link
Contributor

@weiihann weiihann commented Dec 15, 2024

This PR implements a new data structure BitArray to replace trie.Key. The reasons being:

  1. Misleading representation
    trie.Key doesn't actually just represent a Trie key in our use case. But rather, it’s a special data type that we use to just represent a fixed size of bit array with the number of bits used specified.

  2. Inefficient operations
    The current implementation of the bitwise operations is scattered all around the place. We implement ourselves for some, and for others, we rely on conversion. BitArray implements everything from scratch without unnecessary conversion.

Regarding BitArray:

  • It uses a little-endian format to perform bitwise operations more efficiently. All available operations are multiple times faster than trie.Key
  • It is alloc-free
  • It supports all use cases for trie and proofs (e.g. derive path from two bit arrays, check if path matches, etc)
  • Adds built-in support for conversion (e.g. felt to bit array, bytes to bit array, etc)

Credits: some implementation details are inspired by https://github.com/holiman/uint256

Copy link

codecov bot commented Dec 15, 2024

Codecov Report

Attention: Patch coverage is 76.62539% with 151 lines in your changes missing coverage. Please review.

Project coverage is 74.68%. Comparing base (0c9ee5a) to head (4593de5).
Report is 5 commits behind head on main.

Files with missing lines Patch % Lines
core/trie/bitarray.go 73.51% 134 Missing and 4 partials ⚠️
core/trie/proof.go 88.09% 3 Missing and 2 partials ⚠️
core/trie/trie.go 90.90% 2 Missing and 2 partials ⚠️
core/trie/node.go 83.33% 1 Missing and 2 partials ⚠️
migration/migration.go 85.71% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2322      +/-   ##
==========================================
- Coverage   74.71%   74.68%   -0.04%     
==========================================
  Files         110      111       +1     
  Lines       12105    12524     +419     
==========================================
+ Hits         9044     9353     +309     
- Misses       2364     2478     +114     
+ Partials      697      693       -4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@rodrigo-pino rodrigo-pino left a comment

Choose a reason for hiding this comment

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

Just some comments. Let. me know what you think. Thanks for detailing the implementation!

core/trie/bitarray.go Show resolved Hide resolved
core/trie/bitarray.go Outdated Show resolved Hide resolved
core/trie/bitarray.go Outdated Show resolved Hide resolved
core/trie/bitarray.go Show resolved Hide resolved
core/trie/bitarray.go Outdated Show resolved Hide resolved
core/trie/bitarray.go Outdated Show resolved Hide resolved
@weiihann weiihann marked this pull request as ready for review December 18, 2024 02:35
core/state.go Outdated Show resolved Hide resolved
core/trie/bitarray.go Show resolved Hide resolved
core/trie/bitarray.go Outdated Show resolved Hide resolved
core/trie/bitarray.go Outdated Show resolved Hide resolved
core/trie/bitarray.go Outdated Show resolved Hide resolved
core/trie/bitarray.go Outdated Show resolved Hide resolved
core/trie/bitarray.go Show resolved Hide resolved
core/trie/proof.go Show resolved Hide resolved
core/trie/bitarray.go Outdated Show resolved Hide resolved
@weiihann weiihann force-pushed the weiihann/bitarray branch 3 times, most recently from c7fd009 to 2692494 Compare December 24, 2024 03:02
@weiihann weiihann force-pushed the weiihann/bitarray branch 3 times, most recently from 7ba050a to 7f479c1 Compare December 30, 2024 10:56
core/trie/bitarray.go Outdated Show resolved Hide resolved
core/trie/bitarray.go Outdated Show resolved Hide resolved
core/trie/bitarray.go Outdated Show resolved Hide resolved
core/trie/bitarray.go Outdated Show resolved Hide resolved
core/trie/bitarray.go Outdated Show resolved Hide resolved
@weiihann weiihann force-pushed the weiihann/bitarray branch 4 times, most recently from 6e68d5f to b67ff0a Compare January 17, 2025 08:07
core/trie/bitarray.go Outdated Show resolved Hide resolved
core/trie/bitarray.go Outdated Show resolved Hide resolved
core/trie/bitarray.go Outdated Show resolved Hide resolved
Comment on lines 473 to 593
func (b *BitArray) SetBytes(length uint8, data []byte) *BitArray {
b.setBytes32(data)
b.len = length
b.truncateToLength()
return b
}
Copy link
Contributor

Choose a reason for hiding this comment

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

This panics if data isn't at least 32 bytes

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yup, sounds right, added the new changes

Copy link
Contributor

Choose a reason for hiding this comment

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

That new SetBytes function is wild lol :) Guess it's more performant than handling in a loop or something?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah hahahah

Copy link
Contributor

@rianhughes rianhughes left a comment

Choose a reason for hiding this comment

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

Very nice work!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants