Skip to content

Commit

Permalink
Declare hash func in core/crypto (#2352)
Browse files Browse the repository at this point in the history
  • Loading branch information
weiihann authored Dec 26, 2024
1 parent 4ba933f commit 4b9d3c2
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 12 deletions.
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 @@ import (
"errors"
"fmt"

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

Expand All @@ -19,7 +20,7 @@ type Node struct {
}

// 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 @@ func (n *Node) Hash(path *Key, hashFunc hashFunc) *felt.Felt {
}

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 {
path := path(nodeKey, parentKey)
return n.Hash(&path, hashFunc)
return n.Hash(&path, hashFn)
}

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

0 comments on commit 4b9d3c2

Please sign in to comment.