Code Monkey home page Code Monkey logo

Comments (7)

aszepieniec avatar aszepieniec commented on June 3, 2024 1

Yes, please! We would welcome a PR.

from twenty-first.

jan-ferdinand avatar jan-ferdinand commented on June 3, 2024 1

I'm fully in support of using an architecture independent type. 👍

from twenty-first.

elielnfinic avatar elielnfinic commented on June 3, 2024

Can I work on this?

from twenty-first.

elielnfinic avatar elielnfinic commented on June 3, 2024

Alright, will work on this.

from twenty-first.

elielnfinic avatar elielnfinic commented on June 3, 2024

Hey @aszepieniec, moving to 1 << 64 is a great choice as it future-proofs our design by accommodating a larger number of hashes.

I suggest making the variable type independent of the CPU/OS architecture. Therefore, instead of using the usize data type, I recommend using u64 or u128. This approach ensures consistent behavior, whether we're on a 16-bit device or a 256-bit behemoth (LOL).

Additionally, employing 1 << 64 allows us to potentially store up to 2^65 - 1 hashes. If each hash is a 32-byte SHA-256, we would require approximately 1 exabyte of storage.

Does this sound good to you?

from twenty-first.

Sword-Smith avatar Sword-Smith commented on June 3, 2024

So I've been talking about this with @jan-ferdinand and @aszepieniec, and we've come to the conclusion that nothing needs to be changed in the merkle_tree module.

I'm sorry that I haven't caught this earlier, but I don't see a reason to change from usize indices to u64 indices. I should have killed this issue before someone started working on it, @elielnfinic.

The reason this issue was opened is that @aszepieniec saw that Merkle Trees were being used in neptune-core where we also have Merkle Mountain Ranges that use u64 for indexing into the leaves. Notice that Merkle Mountain Ranges (MMR) is a entirely different data structure from Merkle trees, as the two implementations do not share any code (other than for a few (paranoid) tests).

The reason we want to use usize for the public-facing interface is that MerkleTree uses a Vec<Digest> to store all nodes in the tree. If the public interface was changed type conversion would just happen inside of the merkle_tree module instead of being handled by the caller. Since the Vec<T> type from the standard library always uses usize for lookups. In other words, you would just be sweeping the restriction under the rug and not actually fixing anything if you changed the public-facing interface of merkle_tree.

Notice that Vec<Digest> is used to store all nodes:

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MerkleTree<H>
where
    H: AlgebraicHasher,
{
    nodes: Vec<Digest>,
    _hasher: PhantomData<H>,
}

On the question of the size-limits:
The limits that are in place are actually good for cross-platform support, as they ensure that no one makes a MerkleTree that cannot be indexed on a 32-bit architecture.

const MAX_NUM_NODES: usize = 1 << 32;
const MAX_NUM_LEAVES: usize = MAX_NUM_NODES / 2;
pub const MAX_TREE_HEIGHT: usize = MAX_NUM_LEAVES.ilog2() as usize;

Removing these limits would exclude 32-bit architectures from handling all instances of a MerkleTree that could be generated on a 64-bit machine. So the current limits just ensure that 64-bit machines don't exclude 32-bit machines.

TL;DR: Let's not change anything, and sorry for not killing this issue before someone started working on it.

The only thing I see that could be improved is a comment on why these limits are there, so we don't go down this rabbit hole again.

from twenty-first.

elielnfinic avatar elielnfinic commented on June 3, 2024

Alright @Sword-Smith, thank you for clarification.

from twenty-first.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.