Skip to content

Commit 86a2fa8

Browse files
Verify key length in range proofs (#176)
## Why this should be merged This enforces an assumption that is currently made by `VerifyRangeProof`. If a key is a prefix of another key, the current code can panic. ## How this works Enforces all keys have the same length. ## How this was tested Added a unit test.
1 parent d7bb4f6 commit 86a2fa8

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

trie/proof.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,10 @@ func VerifyRangeProof(rootHash common.Hash, firstKey []byte, keys [][]byte, valu
487487
}
488488
// Ensure the received batch is monotonic increasing and contains no deletions
489489
for i := 0; i < len(keys)-1; i++ {
490+
// See: https://github.com/ava-labs/coreth/issues/907
491+
if len(keys[i]) != len(keys[i+1]) {
492+
return false, errKeysHaveDifferentLengths
493+
}
490494
if bytes.Compare(keys[i], keys[i+1]) >= 0 {
491495
return false, errors.New("range is not monotonically increasing")
492496
}

trie/proof.libevm.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2025 the libevm authors.
2+
//
3+
// The libevm additions to go-ethereum are free software: you can redistribute
4+
// them and/or modify them under the terms of the GNU Lesser General Public License
5+
// as published by the Free Software Foundation, either version 3 of the License,
6+
// or (at your option) any later version.
7+
//
8+
// The libevm additions are distributed in the hope that they will be useful,
9+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
11+
// General Public License for more details.
12+
//
13+
// You should have received a copy of the GNU Lesser General Public License
14+
// along with the go-ethereum library. If not, see
15+
// <http://www.gnu.org/licenses/>.
16+
17+
package trie
18+
19+
import (
20+
"errors"
21+
)
22+
23+
var errKeysHaveDifferentLengths = errors.New("keys have different lengths")

trie/proof.libevm_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright 2025 the libevm authors.
2+
//
3+
// The libevm additions to go-ethereum are free software: you can redistribute
4+
// them and/or modify them under the terms of the GNU Lesser General Public License
5+
// as published by the Free Software Foundation, either version 3 of the License,
6+
// or (at your option) any later version.
7+
//
8+
// The libevm additions are distributed in the hope that they will be useful,
9+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
11+
// General Public License for more details.
12+
//
13+
// You should have received a copy of the GNU Lesser General Public License
14+
// along with the go-ethereum library. If not, see
15+
// <http://www.gnu.org/licenses/>.
16+
17+
package trie
18+
19+
import (
20+
"testing"
21+
22+
"github.com/stretchr/testify/require"
23+
24+
"github.com/ava-labs/libevm/common"
25+
)
26+
27+
func TestRangeProofKeysWithDifferentLengths(t *testing.T) {
28+
var (
29+
root = common.HexToHash("0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef")
30+
start = common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000000")
31+
keys = [][]byte{
32+
common.Hex2Bytes("1000000000000000000000000000000"),
33+
common.Hex2Bytes("1000000000000000000000000000000000000000000000000000000000000000"),
34+
}
35+
values = [][]byte{
36+
common.Hex2Bytes("02"),
37+
common.Hex2Bytes("03"),
38+
}
39+
)
40+
_, err := VerifyRangeProof(
41+
root,
42+
start,
43+
keys,
44+
values,
45+
nil, // force it to use stacktrie
46+
)
47+
require.ErrorIs(t, err, errKeysHaveDifferentLengths)
48+
}

0 commit comments

Comments
 (0)