Skip to content

Commit b8458dd

Browse files
authored
fix(m-01): add salt to merkle leaf hashing (#527)
**Motivation:** As part of an audit finding, to protect against [second preimage attacks](https://flawed.net.nz/2018/02/21/attacking-merkle-trees-with-a-second-preimage-attack/), we add a salt to the leaf similar to the RewardsCoordinator to significantly reduce the likelihood of an internal node being used to produce an unintentional proof. Layr-Labs/eigenlayer-contracts#1580 in core already did this on the cert verifier side. We now need to do this for table calc **Modifications:** - Inherit `LeafCalculatorMixin` in `BN254TableCalculatorBase` - Use `calculateOperatorInfoLeaf` **Result:** Secure code
1 parent cf32b41 commit b8458dd

File tree

3 files changed

+13
-8
lines changed

3 files changed

+13
-8
lines changed

lib/eigenlayer-contracts

Submodule eigenlayer-contracts updated 49 files

src/middlewareV2/tableCalculator/BN254TableCalculatorBase.sol

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import {IOperatorTableCalculator} from
77
import {IKeyRegistrar} from "eigenlayer-contracts/src/contracts/interfaces/IKeyRegistrar.sol";
88
import {Merkle} from "eigenlayer-contracts/src/contracts/libraries/Merkle.sol";
99
import {BN254} from "eigenlayer-contracts/src/contracts/libraries/BN254.sol";
10+
import {LeafCalculatorMixin} from
11+
"eigenlayer-contracts/src/contracts/mixins/LeafCalculatorMixin.sol";
1012
import {IBN254TableCalculator} from "../../interfaces/IBN254TableCalculator.sol";
1113

1214
/**
@@ -15,7 +17,7 @@ import {IBN254TableCalculator} from "../../interfaces/IBN254TableCalculator.sol"
1517
* @dev This contract contains all the core logic for operator table calculations,
1618
* with weight calculation left to be implemented by derived contracts
1719
*/
18-
abstract contract BN254TableCalculatorBase is IBN254TableCalculator {
20+
abstract contract BN254TableCalculatorBase is IBN254TableCalculator, LeafCalculatorMixin {
1921
using Merkle for bytes32[];
2022
using BN254 for BN254.G1Point;
2123

@@ -160,8 +162,10 @@ abstract contract BN254TableCalculatorBase is IBN254TableCalculator {
160162
totalWeights[j] += weights[i][j];
161163
}
162164
(BN254.G1Point memory g1Point,) = keyRegistrar.getBN254Key(operatorSet, operators[i]);
165+
166+
// Use `LeafCalculatorMixin` to calculate the leaf hash for the operator info
163167
operatorInfoLeaves[operatorCount] =
164-
keccak256(abi.encode(BN254OperatorInfo({pubkey: g1Point, weights: weights[i]})));
168+
calculateOperatorInfoLeaf(BN254OperatorInfo({pubkey: g1Point, weights: weights[i]}));
165169

166170
// Add the operator's G1 point to the aggregate pubkey
167171
aggregatePubkey = aggregatePubkey.plus(g1Point);

test/unit/middlewareV2/BN254TableCalculatorBaseUnit.t.sol

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import {BN254TableCalculatorBase} from
2222
import {MockEigenLayerDeployer} from "./MockDeployer.sol";
2323
import {Random} from "test/utils/Random.sol";
2424
import {Merkle} from "eigenlayer-contracts/src/contracts/libraries/Merkle.sol";
25+
import {LeafCalculatorMixin} from
26+
"eigenlayer-contracts/src/contracts/mixins/LeafCalculatorMixin.sol";
2527

2628
// Mock implementation for testing abstract contract
2729
contract BN254TableCalculatorBaseHarness is BN254TableCalculatorBase {
@@ -61,7 +63,8 @@ contract BN254TableCalculatorBaseHarness is BN254TableCalculatorBase {
6163
contract BN254TableCalculatorBaseUnitTests is
6264
MockEigenLayerDeployer,
6365
IOperatorTableCalculatorTypes,
64-
IKeyRegistrarTypes
66+
IKeyRegistrarTypes,
67+
LeafCalculatorMixin
6568
{
6669
using BN254 for BN254.G1Point;
6770
using OperatorSetLib for OperatorSet;
@@ -216,10 +219,8 @@ contract BN254TableCalculatorBaseUnitTests is
216219
BN254.G1Point memory pubkey,
217220
uint256[] memory weights
218221
) internal pure returns (bytes32) {
219-
return keccak256(
220-
abi.encode(
221-
IOperatorTableCalculatorTypes.BN254OperatorInfo({pubkey: pubkey, weights: weights})
222-
)
222+
return calculateOperatorInfoLeaf(
223+
IOperatorTableCalculatorTypes.BN254OperatorInfo({pubkey: pubkey, weights: weights})
223224
);
224225
}
225226
}

0 commit comments

Comments
 (0)