Skip to content

Commit

Permalink
export type Keylist
Browse files Browse the repository at this point in the history
Signed-off-by: Guillaume Ballet <[email protected]>
  • Loading branch information
gballet committed Jan 8, 2025
1 parent 33b8a50 commit 4bc28ef
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 32 deletions.
2 changes: 1 addition & 1 deletion empty.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (Empty) Commitment() *Point {
return &id
}

func (Empty) GetProofItems(keylist, NodeResolverFn) (*ProofElements, []byte, []Stem, error) {
func (Empty) GetProofItems(Keylist, NodeResolverFn) (*ProofElements, []byte, []Stem, error) {
return nil, nil, nil, errors.New("trying to produce a commitment for an empty subtree")
}

Expand Down
2 changes: 1 addition & 1 deletion hashednode.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (HashedNode) Commitment() *Point {
panic("can not get commitment of a hash node")
}

func (HashedNode) GetProofItems(keylist, NodeResolverFn) (*ProofElements, []byte, []Stem, error) {
func (HashedNode) GetProofItems(Keylist, NodeResolverFn) (*ProofElements, []byte, []Stem, error) {
return nil, nil, nil, errors.New("can not get the full path, and there is no proof of absence")
}

Expand Down
4 changes: 2 additions & 2 deletions proof_ipa.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,8 @@ func (sd StateDiff) Equal(other StateDiff) error {
}

func GetCommitmentsForMultiproof(root VerkleNode, keys [][]byte, resolver NodeResolverFn) (*ProofElements, []byte, []Stem, error) {
sort.Sort(keylist(keys))
return root.GetProofItems(keylist(keys), resolver)
sort.Sort(Keylist(keys))
return root.GetProofItems(Keylist(keys), resolver)
}

// getProofElementsFromTree factors the logic that is used both in the proving and verification methods. It takes a pre-state
Expand Down
28 changes: 14 additions & 14 deletions proof_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ func TestProofDeserialize(t *testing.T) {
}
_ = deserialized

pe, _, _, err := root.GetProofItems(keylist{absentkey[:]}, nil)
pe, _, _, err := root.GetProofItems(Keylist{absentkey[:]}, nil)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -735,7 +735,7 @@ func TestStatelessDeserialize(t *testing.T) {
string(fourtyKeyTest): fourtyKeyTest,
string(ffx32KeyTest): fourtyKeyTest,
}
proveKeys := keylist{zeroKeyTest, fourtyKeyTest}
proveKeys := Keylist{zeroKeyTest, fourtyKeyTest}

testSerializeDeserializeProof(t, insertKVs, proveKeys)
}
Expand All @@ -748,7 +748,7 @@ func TestStatelessDeserializeMissingLeafNode(t *testing.T) {
string(oneKeyTest): fourtyKeyTest,
string(ffx32KeyTest): fourtyKeyTest,
}
proveKeys := keylist{zeroKeyTest, fourtyKeyTest}
proveKeys := Keylist{zeroKeyTest, fourtyKeyTest}

testSerializeDeserializeProof(t, insertKVs, proveKeys)
}
Expand All @@ -760,7 +760,7 @@ func TestStatelessDeserializeAbsentValueInExistingLeafNode(t *testing.T) {
string(zeroKeyTest): fourtyKeyTest,
string(ffx32KeyTest): fourtyKeyTest,
}
proveKeys := keylist{zeroKeyTest, oneKeyTest}
proveKeys := Keylist{zeroKeyTest, oneKeyTest}

testSerializeDeserializeProof(t, insertKVs, proveKeys)
}
Expand All @@ -774,7 +774,7 @@ func TestStatelessDeserializeDepth2(t *testing.T) {
string(key1): fourtyKeyTest,
string(ffx32KeyTest): fourtyKeyTest,
}
proveKeys := keylist{zeroKeyTest, key1}
proveKeys := Keylist{zeroKeyTest, key1}

testSerializeDeserializeProof(t, insertKVs, proveKeys)
}
Expand All @@ -791,7 +791,7 @@ func TestProofVerificationThreeStemsInSameExtensionStatus(t *testing.T) {
string(key3_0): fourtyKeyTest,
string(key3_1): fourtyKeyTest,
}
proveKeys := keylist{zeroKeyTest, key2_0, key3_0, key3_1, key4_0}
proveKeys := Keylist{zeroKeyTest, key2_0, key3_0, key3_1, key4_0}

testSerializeDeserializeProof(t, insertKVs, proveKeys)
}
Expand All @@ -805,7 +805,7 @@ func TestProofVerificationTwoLeavesWithDifferentValues(t *testing.T) {
string(zeroKeyTest): fourtyKeyTest,
string(key2): forkOneKeyTest,
}
proveKeys := keylist{zeroKeyTest, key2}
proveKeys := Keylist{zeroKeyTest, key2}

testSerializeDeserializeProof(t, insertKVs, proveKeys)
}
Expand All @@ -818,7 +818,7 @@ func TestProofOfAbsenceBorderCase(t *testing.T) {
insertKVs := map[string][]byte{
string(oneKeyTest): fourtyKeyTest,
}
proveKeys := keylist{oneKeyTest, key1}
proveKeys := Keylist{oneKeyTest, key1}

testSerializeDeserializeProof(t, insertKVs, proveKeys)
}
Expand All @@ -831,12 +831,12 @@ func TestProofOfAbsenceBorderCaseReversed(t *testing.T) {
insertKVs := map[string][]byte{
string(key1): fourtyKeyTest,
}
proveKeys := keylist{oneKeyTest, key1}
proveKeys := Keylist{oneKeyTest, key1}

testSerializeDeserializeProof(t, insertKVs, proveKeys)
}

func testSerializeDeserializeProof(t *testing.T, insertKVs map[string][]byte, proveKeys keylist) {
func testSerializeDeserializeProof(t *testing.T, insertKVs map[string][]byte, proveKeys Keylist) {
t.Helper()

root := New()
Expand Down Expand Up @@ -1088,7 +1088,7 @@ func TestGenerateProofWithOnlyAbsentKeys(t *testing.T) {

// Create a proof with a key with the same first byte, but different second byte (i.e: absent).
absentKey, _ := hex.DecodeString("4010000000000000000000000000000000000000000000000000000000000000")
proof, cis, zis, yis, err := MakeVerkleMultiProof(root, nil, keylist{absentKey}, nil)
proof, cis, zis, yis, err := MakeVerkleMultiProof(root, nil, Keylist{absentKey}, nil)
if err != nil {
t.Fatal(err)
}
Expand All @@ -1113,7 +1113,7 @@ func TestGenerateProofWithOnlyAbsentKeys(t *testing.T) {
}

// From the rebuilt tree, validate the proof.
pe, _, _, err := GetCommitmentsForMultiproof(droot, keylist{absentKey}, nil)
pe, _, _, err := GetCommitmentsForMultiproof(droot, Keylist{absentKey}, nil)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -1168,7 +1168,7 @@ func TestDoubleProofOfAbsence(t *testing.T) {
// in that leaf node. i.e: two proof of absence in the same leaf node with no proof of presence.
key2, _ := hex.DecodeString("0000000000000000000000000000000000000000000000000000000000000100")
key3, _ := hex.DecodeString("0000000000000000000000000000000000000000000000000000000000000200")
proof, _, _, _, _ := MakeVerkleMultiProof(root, nil, keylist{key2, key3}, nil)
proof, _, _, _, _ := MakeVerkleMultiProof(root, nil, Keylist{key2, key3}, nil)

serialized, statediff, err := SerializeProof(proof)
if err != nil {
Expand Down Expand Up @@ -1217,7 +1217,7 @@ func TestProveAbsenceInEmptyHalf(t *testing.T) {

key2, _ := hex.DecodeString("0000000000000000000000000000000000000000000000000000000000000100")
key3, _ := hex.DecodeString("0000000000000000000000000000000000000000000000000000000000000000")
proof, _, _, _, _ := MakeVerkleMultiProof(root, nil, keylist{key2, key3}, nil)
proof, _, _, _, _ := MakeVerkleMultiProof(root, nil, Keylist{key2, key3}, nil)

serialized, statediff, err := SerializeProof(proof)
if err != nil {
Expand Down
22 changes: 11 additions & 11 deletions tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,17 @@ type (
NodeResolverFn func([]byte) ([]byte, error)
)

type keylist [][]byte
type Keylist [][]byte

func (kl keylist) Len() int {
func (kl Keylist) Len() int {
return len(kl)
}

func (kl keylist) Less(i, j int) bool {
func (kl Keylist) Less(i, j int) bool {
return bytes.Compare(kl[i], kl[j]) == -1
}

func (kl keylist) Swap(i, j int) {
func (kl Keylist) Swap(i, j int) {
kl[i], kl[j] = kl[j], kl[i]
}

Expand Down Expand Up @@ -90,7 +90,7 @@ type VerkleNode interface {
// returns them breadth-first. On top of that, it returns
// one "extension status" per stem, and an alternate stem
// if the key is missing but another stem has been found.
GetProofItems(keylist, NodeResolverFn) (*ProofElements, []byte, []Stem, error)
GetProofItems(Keylist, NodeResolverFn) (*ProofElements, []byte, []Stem, error)

// Serialize encodes the node to RLP.
Serialize() ([]byte, error)
Expand Down Expand Up @@ -908,19 +908,19 @@ func commitNodesAtLevel(nodes []*InternalNode) error {
}

// groupKeys groups a set of keys based on their byte at a given depth.
func groupKeys(keys keylist, depth byte) []keylist {
func groupKeys(keys Keylist, depth byte) []Keylist {
// special case: no key
if len(keys) == 0 {
return []keylist{}
return []Keylist{}
}

// special case: only one key left
if len(keys) == 1 {
return []keylist{keys}
return []Keylist{keys}
}

// there are at least two keys left in the list at this depth
groups := make([]keylist, 0, len(keys))
groups := make([]Keylist, 0, len(keys))
firstkey, lastkey := 0, 1
for ; lastkey < len(keys); lastkey++ {
key := keys[lastkey]
Expand All @@ -938,7 +938,7 @@ func groupKeys(keys keylist, depth byte) []keylist {
return groups
}

func (n *InternalNode) GetProofItems(keys keylist, resolver NodeResolverFn) (*ProofElements, []byte, []Stem, error) {
func (n *InternalNode) GetProofItems(keys Keylist, resolver NodeResolverFn) (*ProofElements, []byte, []Stem, error) {
var (
groups = groupKeys(keys, n.depth)
pe = &ProofElements{
Expand Down Expand Up @@ -1490,7 +1490,7 @@ func leafToComms(poly []Fr, val []byte) error {
return nil
}

func (n *LeafNode) GetProofItems(keys keylist, _ NodeResolverFn) (*ProofElements, []byte, []Stem, error) { // skipcq: GO-R1005
func (n *LeafNode) GetProofItems(keys Keylist, _ NodeResolverFn) (*ProofElements, []byte, []Stem, error) { // skipcq: GO-R1005
var (
poly [NodeWidth]Fr // top-level polynomial
pe = &ProofElements{
Expand Down
4 changes: 2 additions & 2 deletions tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1025,7 +1025,7 @@ func TestEmptyCommitment(t *testing.T) {
t.Fatalf("inserting into the original failed: %v", err)
}
root.Commit()
pe, _, _, err := root.GetProofItems(keylist{ffx32KeyTest}, nil)
pe, _, _, err := root.GetProofItems(Keylist{ffx32KeyTest}, nil)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -1092,7 +1092,7 @@ func TestGetProofItemsNoPoaIfStemPresent(t *testing.T) {
key1, _ := hex.DecodeString("ffff00ffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")
key2, _ := hex.DecodeString("ffffff00ffffffffffffffffffffffffffffffffffffffffffffffffffffffff")

_, esses, poas, err := root.GetProofItems(keylist{key1, key2, ffx32KeyTest}, nil)
_, esses, poas, err := root.GetProofItems(Keylist{key1, key2, ffx32KeyTest}, nil)
if err != nil {
t.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion unknown.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (UnknownNode) Commitment() *Point {
return &id
}

func (UnknownNode) GetProofItems(keylist, NodeResolverFn) (*ProofElements, []byte, []Stem, error) {
func (UnknownNode) GetProofItems(Keylist, NodeResolverFn) (*ProofElements, []byte, []Stem, error) {
return nil, nil, nil, errors.New("can't generate proof items for unknown node")
}

Expand Down

0 comments on commit 4bc28ef

Please sign in to comment.