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

chore: Add code to implement pedersenHash/GetTreeKeyHash in javascript #22

Merged
merged 23 commits into from
Feb 14, 2024

Conversation

kevaundray
Copy link
Collaborator

@kevaundray kevaundray commented Feb 11, 2024

This is a first pass at adding the code needed for pedersenHash/GetTreeKeyHash.

This would supercede #16

@kevaundray kevaundray marked this pull request as ready for review February 11, 2024 20:43
@kevaundray kevaundray force-pushed the kw/add-get-tree-key-hash branch from f6b4a43 to e91df9c Compare February 11, 2024 20:54
@kevaundray kevaundray force-pushed the kw/add-get-tree-key-hash branch from e91df9c to ef6d688 Compare February 11, 2024 21:04
@kevaundray
Copy link
Collaborator Author

I'm not sure where this is called in the down stream package, though note that you likely don't want to implement calls like get_tree_key_for_version/balance/nonce as its done in the specs because those call get_tree_key independently whereas the only difference between their outputs is the last byte being swapped.

Copy link
Member

@holgerd77 holgerd77 left a comment

Choose a reason for hiding this comment

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

Thanks, two small first comments. 🙂

@gabrocheleau
Copy link
Contributor

I will go ahead and merge this in.

In a subsequent PR I will clean the API up when possible, and add some documentation.

@gabrocheleau gabrocheleau merged commit bc6b8e3 into master Feb 14, 2024
3 checks passed
@holgerd77 holgerd77 deleted the kw/add-get-tree-key-hash branch February 14, 2024 15:36
// ext_input = inp + b"\0" * (255 * 16 - len(inp))
// ints = [2 + 256 * len(inp)] + \
// int.from_bytes(ext_input[16 * i:16 * (i + 1)]) for i in range(255)]
// return compute_commitment_root(ints).serialize()
Copy link
Member

Choose a reason for hiding this comment

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

Hi Kev,
some additional comments here: we also need getTreeKeyHash() for optimization to not have this called over and over again with the same parameters from getTreeKey(), I will just expose this method in a quick follow-up release (so note is purely informational, nothing to do on your side).

Generally I am not so sure about the relation of Context.getTreeKey() and this standalone getTreeKey() coming from this file (and both exposed on the API level), can you clarify?

All the best
Holge


const subIndex = 0

const keyRust = context.getTreeKey(address, treeIndex, subIndex)
Copy link
Member

Choose a reason for hiding this comment

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

This Context thing needs to be "statefull" I assume?

Or could the methods on this also be static (at least partially)?

It would be a bit more convenient if it would not be necessary to initialize an initial context with new Context() and manage this object (what does this object do?) and just call Context.getTreeKey() (so, the captial Context, and not context).

In case all methods from Context could also be made static it would be even better if Context goes away completely and only the methods would be exposed directly.

If Context need to stay I would prefer another name, something more expressive and less generic (FFI ?).

const treeIndex = new Uint8Array([
33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55,
56, 57, 58, 59, 60, 61, 62, 63, 64,
])
Copy link
Member

Choose a reason for hiding this comment

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

I am confused, taking this from the EIP https://eips.ethereum.org/EIPS/eip-6800 ...

def get_tree_key(address: Address32, tree_index: int, sub_index: int):
    # Asssumes VERKLE_NODE_WIDTH = 256
    return (
        pedersen_hash(address + tree_index.to_bytes(32, 'little'))[:31] +
        bytes([sub_index])
    )
    
def get_tree_key_for_version(address: Address32):
    return get_tree_key(address, 0, VERSION_LEAF_KEY)

... tree_index is simply 0, while above it is such a complex byte value. 🤔 How is this coming together?

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.

3 participants