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

Snap/merge range and proof #1913

Closed
wants to merge 39 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
5cd826f
updating verify proof range to handle empty proof keys
Jun 7, 2024
3ecb395
test non set proof key - wip
Jun 7, 2024
d2fe491
wip - proof to Path doesn't work
Jun 7, 2024
2cd3157
store the hashes of children in ProofToPath
Jun 8, 2024
3603e28
ProoftoPath update to handle unset proof key
Jun 8, 2024
d8f5811
test - wip
Jun 8, 2024
40a3d04
test - wip
Jun 9, 2024
fb6c899
test passes! :D
Jun 10, 2024
07e99f5
lint
Jun 10, 2024
c0b933a
tidy
Jun 10, 2024
529aef2
fixing tests that broke wip
Jun 10, 2024
ca360e9
build4keyTrie passes again!
Jun 11, 2024
fdf6af2
fixed more tests
Jun 11, 2024
9f9f2fe
fix test wip
Jun 11, 2024
bad7267
fix more tests
Jun 11, 2024
92a98df
fix test -wip
Jun 11, 2024
727801b
fix tests wip
Jun 11, 2024
b4ae2b6
wip
Jun 11, 2024
a123730
all tests passgit add .
Jun 12, 2024
d249186
lint
Jun 12, 2024
21f9453
tidy
Jun 12, 2024
2d7c6e8
tidy logic
Jun 12, 2024
46c7b8a
tidy logic
Jun 12, 2024
0bb2edc
tidy logic
Jun 12, 2024
ce20e08
comment: update getLeftRightHash
Jun 12, 2024
ec2f917
update ProofToPath comment
Jun 12, 2024
dd21cc0
bubble up getLeftRightHash err
Jun 12, 2024
571eb6e
Trie iterate
asdacap Jun 14, 2024
b614c2e
Address comment
asdacap Jun 14, 2024
89cf035
Merge branch 'feature/trie-iterate' into snap/merge-range-and-proof
asdacap Jun 19, 2024
20e2aa0
Snap support
asdacap Jun 20, 2024
faa0884
Finished is needed internally
asdacap Jun 20, 2024
59440ac
Missed a printf
asdacap Jun 20, 2024
35b46d0
Format
asdacap Jun 20, 2024
cb26dd7
Fix lint
asdacap Jun 20, 2024
1454ad5
Update core/trie/key.go
asdacap Jun 25, 2024
d4b4232
Update core/trie/snap_support.go
asdacap Jun 25, 2024
4f4940f
address keys
asdacap Jun 27, 2024
909914d
Fix lint
asdacap Jun 27, 2024
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
57 changes: 57 additions & 0 deletions core/trie/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,60 @@ func (k *Key) RemoveLastBit() {
inUseBytes[0] = (inUseBytes[0] << unusedBitsCount) >> unusedBitsCount
}
}

// CmpAligned is Cmp as if the value is bigendian bytes of key of the same length
func (k Key) CmpAligned(other *Key) int {
// No its not aligned, so need to convert to bigint then left shift it so that the MSB is of the same index
height := k.len
if other.len > height {
height = other.len
}

b1i := k.alignedBitInt(height)
b2i := other.alignedBitInt(height)
return b1i.Cmp(b2i)
}

func (k Key) alignedBitInt(height uint8) *big.Int {
bigint := &big.Int{}
bigint = bigint.SetBytes(k.bitset[:])
if k.len < height {
bigint = bigint.Lsh(bigint, uint(height-k.len))
}

return bigint
}

func (k *Key) AppendBitMut(flag bool) {
bit := k.len
byteIdx := bit / 8
byteAtIdx := k.bitset[len(k.bitset)-int(byteIdx)-1]
bitIdx := bit % 8

mask := uint8(1 << bitIdx)
if flag {
byteAtIdx |= mask // set bit
} else {
byteAtIdx &= ^mask // clear bit
}

k.len++
k.bitset[len(k.bitset)-int(byteIdx)-1] = byteAtIdx
}

func (k Key) Append(otherKey *Key) Key {
result := NewKey(otherKey.len, otherKey.bitset[:])

// I'm sure someone will make this faster
for i := uint8(0); i < k.len; i++ {
result.AppendBitMut(k.Test(i))
}

return result
}

func (k Key) AppendBit(flag bool) Key {
result := NewKey(0, []byte{})
result.AppendBitMut(flag)
return k.Append(&result)
}
118 changes: 118 additions & 0 deletions core/trie/key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package trie_test

import (
"bytes"
"fmt"
"testing"

"github.com/NethermindEth/juno/core/felt"
Expand Down Expand Up @@ -153,3 +154,120 @@ func TestTruncate(t *testing.T) {
})
}
}

func Test_cmp(t *testing.T) {
tests := []struct {
n1 int
n2 int
isHigher bool
}{
{
n1: 10,
n2: 0,
isHigher: true,
},
{
n1: 5,
n2: 0,
isHigher: true,
},
{
n1: 5,
n2: 4,
isHigher: true,
},
{
n1: 5,
n2: 5,
isHigher: false,
},
{
n1: 4,
n2: 5,
isHigher: false,
},
{
n1: 0,
n2: 5,
isHigher: false,
},
{
n1: 300,
n2: 1,
isHigher: true,
},
{
n1: 1,
n2: 300,
isHigher: false,
},
}

for _, test := range tests {
t.Run(fmt.Sprintf("%d %d %v", test.n1, test.n2, test.isHigher), func(t *testing.T) {
k1 := numToKey(test.n1)
k2 := numToKey(test.n2)

assert.Equal(t,
k1.CmpAligned(&k2) > 0,
test.isHigher)
})
}
}

func numToKey(num int) trie.Key {
return trie.NewKey(8, []byte{byte(num)})
}

func TestKeyAppend(t *testing.T) {
tests := map[string]struct {
Key1 trie.Key
Key2 trie.Key
ExpectedKey trie.Key
}{
"no append": {
Key1: trie.NewKey(1, []byte{0x01}),
Key2: trie.NewKey(0, []byte{0x00}),
ExpectedKey: trie.NewKey(1, []byte{0x01}),
},
"from zero append": {
Key1: trie.NewKey(0, []byte{0x00}),
Key2: trie.NewKey(1, []byte{0x01}),
ExpectedKey: trie.NewKey(1, []byte{0x01}),
},
"append shift": {
Key1: trie.NewKey(1, []byte{0x01}),
Key2: trie.NewKey(7, []byte{0x00}),
ExpectedKey: trie.NewKey(8, []byte{0x80}),
},
"append to a new byte": {
Key1: trie.NewKey(8, []byte{0xff}),
Key2: trie.NewKey(1, []byte{0x01}),
ExpectedKey: trie.NewKey(9, []byte{0x01, 0xff}),
},
"append multi byte": {
Key1: trie.NewKey(11, []byte{0x00, 0xff}), // 000 1111 1111
Key2: trie.NewKey(12, []byte{0x00, 0xff}), // 0000 1111 1111
ExpectedKey: trie.NewKey(23, []byte{0x0f, 0xf0, 0xff}), // 000 1111 1111 0000 1111 1111
},
}

for desc, test := range tests {
t.Run(desc, func(t *testing.T) {
appended := test.Key1.Append(&test.Key2)
assert.Equal(t, test.ExpectedKey, appended)
})
}
}

func TestKeyAppendBit(t *testing.T) {
k1 := trie.NewKey(1, []byte{0x01})
k2 := k1.AppendBit(true)
expected := trie.NewKey(2, []byte{0x03})
assert.Equal(t, k2, expected)

k1 = trie.NewKey(1, []byte{0x00})
k2 = k1.AppendBit(true)
expected = trie.NewKey(2, []byte{0x01})
assert.Equal(t, k2, expected)
}
67 changes: 59 additions & 8 deletions core/trie/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@

// A Node represents a node in the [Trie]
type Node struct {
Value *felt.Felt
Left *Key
Right *Key
Value *felt.Felt
Left *Key
Right *Key
LeftHash *felt.Felt
RightHash *felt.Felt
}

// Hash calculates the hash of a [Node]
Expand All @@ -30,6 +32,12 @@
return hash.Add(hash, &pathFelt)
}

// Hash calculates the hash of a [Node]
func (n *Node) HashFromParent(parnetKey, nodeKey *Key, hashFunc hashFunc) *felt.Felt {
path := path(nodeKey, parnetKey)
return n.Hash(&path, hashFunc)
}

func (n *Node) WriteTo(buf *bytes.Buffer) (int64, error) {
if n.Value == nil {
return 0, errors.New("cannot marshal node with nil value")
Expand All @@ -45,18 +53,38 @@
}

if n.Left != nil {
wrote, err := n.Left.WriteTo(buf)
wrote, errInner := n.Left.WriteTo(buf)
totalBytes += wrote
if err != nil {
return totalBytes, err
return totalBytes, errInner

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

View check run for this annotation

Codecov / codecov/patch

core/trie/node.go#L59

Added line #L59 was not covered by tests
}
wrote, err = n.Right.WriteTo(buf) // n.Right is non-nil by design
wrote, errInner = n.Right.WriteTo(buf) // n.Right is non-nil by design
totalBytes += wrote
if err != nil {
return totalBytes, err
return totalBytes, errInner

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

View check run for this annotation

Codecov / codecov/patch

core/trie/node.go#L64

Added line #L64 was not covered by tests
}
}

if n.LeftHash == nil && n.RightHash == nil {
return totalBytes, nil
} else if (n.LeftHash != nil && n.RightHash == nil) || (n.LeftHash == nil && n.RightHash != nil) {
return totalBytes, errors.New("cannot store only one lefthash or righthash")

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

View check run for this annotation

Codecov / codecov/patch

core/trie/node.go#L71

Added line #L71 was not covered by tests
}

leftHashB := n.LeftHash.Bytes()
wrote, err = buf.Write(leftHashB[:])
totalBytes += int64(wrote)
if err != nil {
return totalBytes, err

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

View check run for this annotation

Codecov / codecov/patch

core/trie/node.go#L78

Added line #L78 was not covered by tests
}

rightHashB := n.RightHash.Bytes()
wrote, err = buf.Write(rightHashB[:])
totalBytes += int64(wrote)
if err != nil {
return totalBytes, err

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

View check run for this annotation

Codecov / codecov/patch

core/trie/node.go#L85

Added line #L85 was not covered by tests
}

return totalBytes, nil
}

Expand All @@ -74,6 +102,8 @@
if len(data) == 0 {
n.Left = nil
n.Right = nil
n.LeftHash = nil
n.RightHash = nil
return nil
}

Expand All @@ -85,5 +115,26 @@
if err := n.Left.UnmarshalBinary(data); err != nil {
return err
}
return n.Right.UnmarshalBinary(data[n.Left.EncodedLen():])
data = data[n.Left.EncodedLen():]
if err := n.Right.UnmarshalBinary(data); err != nil {
return err

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

View check run for this annotation

Codecov / codecov/patch

core/trie/node.go#L120

Added line #L120 was not covered by tests
}
data = data[n.Right.EncodedLen():]

if n.LeftHash == nil {
n.LeftHash = new(felt.Felt)
}
if n.RightHash == nil {
n.RightHash = new(felt.Felt)
}
if len(data) == 0 {
return nil
}
if len(data) != 2*felt.Bytes {
return errors.New("the node does not contain both left and right hash")

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

View check run for this annotation

Codecov / codecov/patch

core/trie/node.go#L134

Added line #L134 was not covered by tests
}
n.LeftHash.SetBytes(data[:felt.Bytes])
data = data[felt.Bytes:]
n.RightHash.SetBytes(data[:felt.Bytes])
return nil
}
Loading
Loading