Skip to content

Commit 9c00da8

Browse files
authored
fix: Check bounds before precomputing SRS table (#1846)
* Check bounds before precomputing SRS table * modify error message * Fix lint errors
1 parent 300644e commit 9c00da8

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

encoding/kzg/prover/precompute.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,16 @@ func (p *SRSTable) GetSubTables(
100100
table, ok := p.Tables[param]
101101
if !ok {
102102
log.Printf("Table with params: DimE=%v CosetSize=%v does not exist\n", dimE, cosetSize)
103+
104+
// Check if we have enough SRS points loaded for precomputation
105+
// We need polynomial degree m < len(SRS)
106+
// (Actually we only access up to index m-cosetSize, but this simpler check is safer)
107+
if m >= uint64(len(p.s1)) {
108+
return nil, fmt.Errorf("cannot precompute table: insufficient SRS points loaded (have %d, need at least %d). "+
109+
"Consider increasing loaded SRS points or using precomputed tables",
110+
len(p.s1), m+1)
111+
}
112+
103113
log.Printf("Generating the table. May take a while\n")
104114
log.Printf("... ...\n")
105115
filename := fmt.Sprintf("dimE%v.coset%v", dimE, cosetSize)

encoding/kzg/prover/precompute_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/Layr-Labs/eigenda/encoding"
1010
"github.com/Layr-Labs/eigenda/encoding/kzg"
1111
"github.com/Layr-Labs/eigenda/encoding/kzg/prover"
12+
"github.com/consensys/gnark-crypto/ecc/bn254"
1213
)
1314

1415
func TestNewSRSTable_PreComputeWorks(t *testing.T) {
@@ -43,3 +44,41 @@ func TestNewSRSTable_PreComputeWorks(t *testing.T) {
4344
// Result of non precomputed GetSubTables should equal precomputed GetSubTables
4445
assert.Equal(t, fftPoints1, fftPoints2)
4546
}
47+
48+
// This test reproduces the scenario where SRS_LOAD=2097152 and computing a subtable
49+
// with the parameters (DimE=4, CosetSize=2097152) would cause a panic.
50+
// The issue: m = numChunks*chunkLen - 1 = 4*2097152 - 1 = 8388607
51+
// When j=0, k starts at m - cosetSize = 8388607 - 2097152 = 6291455
52+
// Since 6291455 >= 2097152 (the length of our SRS), we get:
53+
// panic: runtime error: index out of range [6291455] with length 2097152
54+
func TestSRSTable_InsufficientSRSPoints_NoPanic(t *testing.T) {
55+
// Create a limited SRS with only 2097152 points
56+
limitedSRSSize := uint64(2097152)
57+
limitedSRS := make([]bn254.G1Affine, limitedSRSSize)
58+
59+
// Initialize with some dummy points (doesn't matter what they are for this test)
60+
var generator bn254.G1Affine
61+
_, err := generator.X.SetString("1")
62+
require.NoError(t, err)
63+
_, err = generator.Y.SetString("2")
64+
require.NoError(t, err)
65+
for i := range limitedSRS {
66+
limitedSRS[i] = generator
67+
}
68+
69+
// Create SRSTable with limited SRS points
70+
tempDir := t.TempDir()
71+
srsTable, err := prover.NewSRSTable(tempDir, limitedSRS, 1)
72+
require.NoError(t, err)
73+
74+
// Try to create subtables with the following parameters
75+
numChunks := uint64(4)
76+
chunkLen := uint64(2097152)
77+
78+
// This should return an error instead of panicking
79+
fftPoints, err := srsTable.GetSubTables(numChunks, chunkLen)
80+
81+
assert.Error(t, err)
82+
assert.Nil(t, fftPoints)
83+
assert.Contains(t, err.Error(), "insufficient SRS points")
84+
}

0 commit comments

Comments
 (0)