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

Declare hash func in core/crypto #2352

Merged
merged 1 commit into from
Dec 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions core/crypto/hash.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package crypto

import "github.com/NethermindEth/juno/core/felt"

type HashFn func(*felt.Felt, *felt.Felt) *felt.Felt
9 changes: 5 additions & 4 deletions core/trie/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"errors"
"fmt"

"github.com/NethermindEth/juno/core/crypto"
"github.com/NethermindEth/juno/core/felt"
)

Expand All @@ -19,7 +20,7 @@
}

// Hash calculates the hash of a [Node]
func (n *Node) Hash(path *Key, hashFunc hashFunc) *felt.Felt {
func (n *Node) Hash(path *Key, hashFn crypto.HashFn) *felt.Felt {
if path.Len() == 0 {
// we have to deference the Value, since the Node can released back
// to the NodePool and be reused anytime
Expand All @@ -28,15 +29,15 @@
}

pathFelt := path.Felt()
hash := hashFunc(n.Value, &pathFelt)
hash := hashFn(n.Value, &pathFelt)
pathFelt.SetUint64(uint64(path.Len()))
return hash.Add(hash, &pathFelt)
}

// Hash calculates the hash of a [Node]
func (n *Node) HashFromParent(parentKey, nodeKey *Key, hashFunc hashFunc) *felt.Felt {
func (n *Node) HashFromParent(parentKey, nodeKey *Key, hashFn crypto.HashFn) *felt.Felt {

Check warning on line 38 in core/trie/node.go

View check run for this annotation

Codecov / codecov/patch

core/trie/node.go#L38

Added line #L38 was not covered by tests
path := path(nodeKey, parentKey)
return n.Hash(&path, hashFunc)
return n.Hash(&path, hashFn)

Check warning on line 40 in core/trie/node.go

View check run for this annotation

Codecov / codecov/patch

core/trie/node.go#L40

Added line #L40 was not covered by tests
}

func (n *Node) WriteTo(buf *bytes.Buffer) (int64, error) {
Expand Down
8 changes: 4 additions & 4 deletions core/trie/proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func NewProofNodeSet() *ProofNodeSet {
}

type ProofNode interface {
Hash(hash hashFunc) *felt.Felt
Hash(hash crypto.HashFn) *felt.Felt
Len() uint8
String() string
}
Expand All @@ -26,7 +26,7 @@ type Binary struct {
RightHash *felt.Felt
}

func (b *Binary) Hash(hash hashFunc) *felt.Felt {
func (b *Binary) Hash(hash crypto.HashFn) *felt.Felt {
return hash(b.LeftHash, b.RightHash)
}

Expand All @@ -43,7 +43,7 @@ type Edge struct {
Path *Key // path from parent to child
}

func (e *Edge) Hash(hash hashFunc) *felt.Felt {
func (e *Edge) Hash(hash crypto.HashFn) *felt.Felt {
length := make([]byte, len(e.Path.bitset))
length[len(e.Path.bitset)-1] = e.Path.len
pathFelt := e.Path.Felt()
Expand Down Expand Up @@ -137,7 +137,7 @@ func (t *Trie) GetRangeProof(leftKey, rightKey *felt.Felt, proofSet *ProofNodeSe
// - Any node's computed hash doesn't match its expected hash
// - The path bits don't match the key bits
// - The proof ends before processing all key bits
func VerifyProof(root, keyFelt *felt.Felt, proof *ProofNodeSet, hash hashFunc) (*felt.Felt, error) {
func VerifyProof(root, keyFelt *felt.Felt, proof *ProofNodeSet, hash crypto.HashFn) (*felt.Felt, error) {
key := FeltToKey(globalTrieHeight, keyFelt)
expectedHash := root
keyLen := key.Len()
Expand Down
6 changes: 2 additions & 4 deletions core/trie/trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ import (

const globalTrieHeight = 251 // TODO(weiihann): this is declared in core also, should be moved to a common place

type hashFunc func(*felt.Felt, *felt.Felt) *felt.Felt

// Trie is a dense Merkle Patricia Trie (i.e., all internal nodes have two children).
//
// This implementation allows for a "flat" storage by keying nodes on their path rather than
Expand All @@ -40,7 +38,7 @@ type Trie struct {
rootKey *Key
maxKey *felt.Felt
storage *Storage
hash hashFunc
hash crypto.HashFn

dirtyNodes []*Key
rootKeyIsDirty bool
Expand All @@ -56,7 +54,7 @@ func NewTriePoseidon(storage *Storage, height uint8) (*Trie, error) {
return newTrie(storage, height, crypto.Poseidon)
}

func newTrie(storage *Storage, height uint8, hash hashFunc) (*Trie, error) {
func newTrie(storage *Storage, height uint8, hash crypto.HashFn) (*Trie, error) {
if height > felt.Bits {
return nil, fmt.Errorf("max trie height is %d, got: %d", felt.Bits, height)
}
Expand Down
Loading