Skip to content

Commit

Permalink
outline algo fro edge and binary
Browse files Browse the repository at this point in the history
  • Loading branch information
rian committed May 3, 2024
1 parent 65887a9 commit 53753d1
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 46 deletions.
18 changes: 18 additions & 0 deletions core/trie/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,21 @@ func (k *Key) Truncate(length uint8) {
inUseBytes[0] = (inUseBytes[0] << unusedBitsCount) >> unusedBitsCount
}
}

func (k *Key) RemoveLastBit() {
if k.len == 0 {
return

Check warning on line 120 in core/trie/key.go

View check run for this annotation

Codecov / codecov/patch

core/trie/key.go#L120

Added line #L120 was not covered by tests
}

k.len--

unusedBytes := k.unusedBytes()
clear(unusedBytes)

// clear upper bits on the last used byte
inUseBytes := k.inUseBytes()
unusedBitsCount := 8 - (k.len % 8)
if unusedBitsCount != 8 && len(inUseBytes) > 0 {
inUseBytes[0] = (inUseBytes[0] << unusedBitsCount) >> unusedBitsCount

Check warning on line 132 in core/trie/key.go

View check run for this annotation

Codecov / codecov/patch

core/trie/key.go#L132

Added line #L132 was not covered by tests
}
}
74 changes: 28 additions & 46 deletions core/trie/proof.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package trie

import (
"errors"
"fmt"

"github.com/NethermindEth/juno/core/crypto"
"github.com/NethermindEth/juno/core/felt"
)
Expand Down Expand Up @@ -44,62 +41,47 @@ func GetProof(leaf *felt.Felt, tri *Trie) ([]ProofNode, error) {
return node.Hash(key, crypto.Pedersen), nil
}

// The spec requires Edge nodes. However Juno doesn't have edge nodes.
// if Len=250 and have left and right -> create a Binary
// if len=250 and only have one child -> create edge
// if len!=250, check if child-parent length = 1, if so ,create Binary
// if len!=250, check if child-parent length = 1, if not ,create Edge
for i, sNode := range nodesExcludingLeaf {
// Determind if binary or edge
sLeft := sNode.node.Left
sRight := sNode.node.Right
if sLeft != nil && sRight != nil { // sNode is (edge) Binary Node
leftHash, err := getHash(sNode.node.Left)
if err != nil {
return nil, err
}
rightHash, err := getHash(sNode.node.Right)
if err != nil {
return nil, err
}
proofNodes[i] = ProofNode{
Binary: &Binary{
LeftHash: leftHash,
RightHash: rightHash,
},
}
// sLeft := sNode.node.Left

Check failure on line 50 in core/trie/proof.go

View workflow job for this annotation

GitHub Actions / lint

commentedOutCode: may want to remove commented-out code (gocritic)
// sRight := sNode.node.Right
nxtNode := nodesToLeaf[i+1]

} else { // sNode is Edge Node
nxtNode := nodesToLeaf[i+1].node
nxtKey := nodesToLeaf[i+1].key
fmt.Println("sNode", sNode)
fmt.Println("sNode", sNode.node)
fmt.Println("sNode", sNode.node.Left)
fmt.Println("sNode", sNode.node.Right)
fmt.Println("nxtNode", nxtNode)

fmt.Println("nxtNode", nxtNode.Left)
fmt.Println("nxtNode", nxtNode.Right)

// Juno doesn't have a notion of an edge node, so we construct it here
edgePath, ok := findCommonKey(nxtKey, sNode.key)
if !ok {
return nil, errors.New("failed to get edge node path")
}
if nxtNode.key.len-sNode.key.len > 1 { // split node into edge + child
edgePath := NewKey(sNode.key.len, sNode.key.bitset[:])
edgePath.RemoveLastBit() // Todo: make sure we remove it from the correct side
edgePathFelt := edgePath.Felt()

var childKey felt.Felt
if sNode.key.Test(sNode.key.len - nxtKey.len - 1) { // Todo: double -check
childKey = sNode.node.Right.Felt()
} else {
childKey = sNode.node.Left.Felt()
}
childKeyFelt := sNode.key.Felt()

proofNodes[i] = ProofNode{
Edge: &Edge{
Path: &edgePathFelt,
Child: &childKey,
Child: &childKeyFelt,
// Value: value, // Todo: ??
},
}
}

leftHash, err := getHash(sNode.node.Left)
if err != nil {
return nil, err

Check warning on line 71 in core/trie/proof.go

View check run for this annotation

Codecov / codecov/patch

core/trie/proof.go#L71

Added line #L71 was not covered by tests
}
rightHash, err := getHash(sNode.node.Right)
if err != nil {
return nil, err

Check warning on line 75 in core/trie/proof.go

View check run for this annotation

Codecov / codecov/patch

core/trie/proof.go#L75

Added line #L75 was not covered by tests
}
proofNodes[i] = ProofNode{
Binary: &Binary{
LeftHash: leftHash,
RightHash: rightHash,
},
}
}

return proofNodes, nil
}

Expand Down

0 comments on commit 53753d1

Please sign in to comment.