Skip to content

Commit 98fca32

Browse files
authored
Merge pull request #19 from Layr-Labs/seri.choi/m1-1
feat: add leaf salting to operator table merkle tree generation
2 parents 7b20682 + d86b473 commit 98fca32

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

pkg/distribution/distribution.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,14 @@ package distribution
55

66
import (
77
"fmt"
8+
89
"github.com/ethereum/go-ethereum/common"
910
)
1011

12+
// OPERATOR_TABLE_LEAF_SALT is the salt used for encoding operator table leaves
13+
// This matches the salt used in the EigenLayer contracts to prevent second preimage attacks
14+
const OPERATOR_TABLE_LEAF_SALT = 0x8e
15+
1116
// OperatorSet represents a unique operator set in the EigenLayer ecosystem.
1217
// It consists of an ID and the address of the associated AVS (Actively Validated Service).
1318
type OperatorSet struct {
@@ -137,3 +142,20 @@ func (d *Distribution) GetOperatorSets() []OperatorSet {
137142
}
138143
return sets
139144
}
145+
146+
// EncodeOperatorTableLeaf encodes an operator table leaf for merkleization.
147+
// This function creates a consistent leaf encoding format that matches the EigenLayer
148+
// contracts' LeafCalculatorMixin implementation to prevent second preimage attacks.
149+
//
150+
// The format follows: OPERATOR_TABLE_LEAF_SALT || operatorTableBytes
151+
// The Merkle tree library will then hash this salted data with keccak256.
152+
//
153+
// Parameters:
154+
// - operatorTableBytes: The operator table data bytes
155+
//
156+
// Returns:
157+
// - []byte: The salted leaf data (salt + operatorTableBytes)
158+
func EncodeOperatorTableLeaf(operatorTableBytes []byte) []byte {
159+
saltByte := []byte{OPERATOR_TABLE_LEAF_SALT}
160+
return append(saltByte, operatorTableBytes...)
161+
}

pkg/operatorTableCalculator/operatorTableCalculator.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,15 @@ func (c *StakeTableCalculator) CalculateStakeTableRoot(
114114
zap.String("opsetAvs", opset.Avs.String()),
115115
zap.String("bytes", hexutil.Encode(tableBytes)),
116116
)
117-
opsetTableRoots[i] = tableBytes
117+
118+
encodedLeaf := distribution.EncodeOperatorTableLeaf(tableBytes)
119+
opsetTableRoots[i] = encodedLeaf
120+
121+
c.logger.Sugar().Infow("Encoded operator table leaf for opset",
122+
zap.Uint32("opsetId", opset.Id),
123+
zap.String("opsetAvs", opset.Avs.String()),
124+
zap.String("encodedLeaf", hexutil.Encode(encodedLeaf)),
125+
)
118126

119127
err = dist.SetTableData(opset, tableBytes)
120128
if err != nil {

0 commit comments

Comments
 (0)