Skip to content

feat: customizable LeanIMT implementation #62

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

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

Conversation

brech1
Copy link
Member

@brech1 brech1 commented Mar 17, 2025

Description

This PR introduces an implementation of the Lean Incremental Merkle Tree:

  • Created a generic HashedLeanIMT struct that supports arbitrary hash functions through a trait
  • Implemented the LeanIMTHasher trait to allow users to provide their own hashing functions
  • Implemented serialization/deserialization for all structs
  • Added support for dynamic dispatch of the hash function

Related Issue(s)

Checklist

  • I have read and understand the contributor guidelines and code of conduct.
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • My changes generate no new warnings
  • I have run cargo fmt without getting any errors
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

@brech1 brech1 requested review from cedoor and sripwoud as code owners March 17, 2025 21:08
@vplasencia vplasencia self-requested a review March 17, 2025 21:57
@brech1 brech1 force-pushed the feat/generic-tree branch from 04026bf to f94ba61 Compare March 18, 2025 18:05
@vplasencia vplasencia requested a review from sinui0 March 18, 2025 18:22
Copy link

@sinui0 sinui0 left a comment

Choose a reason for hiding this comment

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

Looks like a good base to start with!

There are some changes and features we will need but let's tackle them incrementally.

#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
pub struct LeanIMT {
/// Nodes
nodes: Vec<Vec<Vec<u8>>>,
Copy link

Choose a reason for hiding this comment

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

This will have poor cache efficiency as each hash will be at its own heap address. I recommend flattening at least the hashes so its Vec<Vec<u8>>

Copy link
Member Author

Choose a reason for hiding this comment

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

What do you think about this?

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct LeanIMT<const LEAF_SIZE: usize> {
    #[serde(bound(serialize = "[u8; LEAF_SIZE]: Serialize"))]
    #[serde(bound(deserialize = "[u8; LEAF_SIZE]: Deserialize<'de>"))]
    nodes: Vec<Vec<[u8; LEAF_SIZE]>>,
}

This will fail to compile if LEAF_SIZE is greater than 32 and you're trying to do serialization/deserialization. Otherwise works just fine. It's an upper limit on LEAF_SIZE if serde is used. Imo makes sense since this is a common, safe size for a hash output.

By doing this all hashes will be stored in the vec and not at random addresses.

Copy link

@sinui0 sinui0 Apr 9, 2025

Choose a reason for hiding this comment

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

The issue with this is that the hash size has to be known at compile time. I think its not too hard to just use dynamic indexing at runtime depending on the provided hasher.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, you're right, we're forcing a specified output of hash function. I think we could make this temporary tradeoff in favor of performance, and then work on an update for a flattened flexible node structure, so we can have this and semaphore-rs going for the moment. I can mention this problem in the issue.

Copy link
Member Author

Choose a reason for hiding this comment

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

Happy to know your thoughts @vplasencia

@brech1 brech1 force-pushed the feat/generic-tree branch from 331d688 to 3a133a7 Compare April 10, 2025 17:24
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.

lean-imt TLSNotary requirements Create a crate for the LeanIMT
2 participants