Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions node-api/handlers/proof/merkle/validator_balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
ctypes "github.com/berachain/beacon-kit/consensus-types/types"
"github.com/berachain/beacon-kit/node-api/handlers/proof/types"
"github.com/berachain/beacon-kit/primitives/common"
"github.com/berachain/beacon-kit/primitives/constants"
"github.com/berachain/beacon-kit/primitives/math"
"github.com/berachain/beacon-kit/primitives/merkle"
"github.com/pkg/errors"
Expand Down Expand Up @@ -56,9 +57,8 @@ func ProveBalanceInState(
leafOffset := validatorIndex / BalancesPerLeaf

// Calculate the generalized index for the target validator's balance leaf.
// The offset multiplication is bounded by the number of validators, so
// converting to int is safe on 64-bit architectures.
gIndex := zeroBalanceGIndexState + int(leafOffset) // #nosec G115
// int conversion is safe on 64-bit architectures: (2^40-1)*8 < 2^43 < 2^63.
gIndex := zeroBalanceGIndexState + int(leafOffset) // #nosec G115 -- offset bounded by caller.
Comment thread
bar-bera marked this conversation as resolved.

balanceProof, err := stateProofTree.Prove(gIndex)
if err != nil {
Expand All @@ -82,6 +82,14 @@ func ProveBalanceInBlock(
bsm types.BeaconStateMarshallable,
allBalances []uint64,
) ([]common.Root, common.Root, common.Root, error) {
// Bound validatorIndex for subsequent cast to int.
if validatorIndex.Unwrap() >= constants.ValidatorsRegistryLimit {
return nil, common.Root{}, common.Root{}, fmt.Errorf(
"validator index %d exceeds registry limit %d",
validatorIndex, uint64(constants.ValidatorsRegistryLimit),
)
}
Comment thread
bar-bera marked this conversation as resolved.
Outdated

forkVersion := bsm.GetForkVersion()

// 1. Proof inside the state.
Expand Down
17 changes: 13 additions & 4 deletions node-api/handlers/proof/merkle/validator_credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@
package merkle

import (
"fmt"

ctypes "github.com/berachain/beacon-kit/consensus-types/types"
"github.com/berachain/beacon-kit/errors"
"github.com/berachain/beacon-kit/node-api/handlers/proof/types"
"github.com/berachain/beacon-kit/primitives/common"
"github.com/berachain/beacon-kit/primitives/constants"
"github.com/berachain/beacon-kit/primitives/math"
"github.com/berachain/beacon-kit/primitives/merkle"
)
Expand All @@ -49,10 +52,8 @@ func ProveWithdrawalCredentialsInState(
return nil, common.Root{}, err
}

// Calculate the generalized index for the target validator. The offset
// multiplication is bounded by (2^40-1)*8 < 2^43 < 2^63, so converting to
// int is safe on 64-bit architectures.
gIndex := zeroWithdrawalGIndexState + int(validatorOffset) // #nosec G115
// int conversion is safe on 64-bit architectures: (2^40-1)*8 < 2^43 < 2^63.
gIndex := zeroWithdrawalGIndexState + int(validatorOffset) // #nosec G115 -- offset bounded by caller.

withdrawalProof, err := stateProofTree.Prove(gIndex)
if err != nil {
Expand All @@ -75,6 +76,14 @@ func ProveWithdrawalCredentialsInBlock(
bbh *ctypes.BeaconBlockHeader,
bsm types.BeaconStateMarshallable,
) ([]common.Root, common.Root, error) {
// Bound validatorIndex for subsequent cast to int.
if validatorIndex.Unwrap() >= constants.ValidatorsRegistryLimit {
return nil, common.Root{}, fmt.Errorf(
"validator index %d exceeds registry limit %d",
validatorIndex, uint64(constants.ValidatorsRegistryLimit),
)
}
Comment thread
bar-bera marked this conversation as resolved.
Outdated

forkVersion := bsm.GetForkVersion()

// Calculate the validator-specific offset.
Expand Down
12 changes: 11 additions & 1 deletion node-api/handlers/proof/merkle/validator_pubkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
ctypes "github.com/berachain/beacon-kit/consensus-types/types"
"github.com/berachain/beacon-kit/node-api/handlers/proof/types"
"github.com/berachain/beacon-kit/primitives/common"
"github.com/berachain/beacon-kit/primitives/constants"
"github.com/berachain/beacon-kit/primitives/math"
"github.com/berachain/beacon-kit/primitives/merkle"
)
Expand All @@ -47,6 +48,14 @@ func ProveValidatorPubkeyInBlock(
bbh *ctypes.BeaconBlockHeader,
bsm types.BeaconStateMarshallable,
) ([]common.Root, common.Root, error) {
// Bound validatorIndex for subsequent cast to int.
if validatorIndex.Unwrap() >= constants.ValidatorsRegistryLimit {
return nil, common.Root{}, fmt.Errorf(
"validator index %d exceeds registry limit %d",
validatorIndex, uint64(constants.ValidatorsRegistryLimit),
)
}
Comment thread
bar-bera marked this conversation as resolved.
Outdated
Comment thread
bar-bera marked this conversation as resolved.
Outdated

forkVersion := bsm.GetForkVersion()

// Calculate the validator-specific offset.
Expand Down Expand Up @@ -96,7 +105,8 @@ func ProveValidatorPubkeyInState(
}

// Determine the correct gIndex based on the fork version.
gIndex := int(validatorOffset) // #nosec G115 -- max validator offset is 8 * (2^40 - 1).
// int conversion is safe on 64-bit architectures: (2^40-1)*8 < 2^43 < 2^63.
gIndex := int(validatorOffset) // #nosec G115 -- offset bounded by caller.
zeroValidatorPubkeyGIndexState, err := GetZeroValidatorPubkeyGIndexState(forkVersion)
if err != nil {
return nil, common.Root{}, err
Expand Down
Loading