Files
prysm/runtime/interop/generate_keys.go
Preston Van Loon 2fd6bd8150 Add golang.org/x/tools modernize static analyzer and fix violations (#15946)
* Ran gopls modernize to fix everything

go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -fix -test ./...

* Override rules_go provided dependency for golang.org/x/tools to v0.38.0.

To update this, checked out rules_go, then ran `bazel run //go/tools/releaser -- upgrade-dep -mirror=false org_golang_x_tools` and copied the patches.

* Fix buildtag violations and ignore buildtag violations in external

* Introduce modernize analyzer package.

* Add modernize "any" analyzer.

* Fix violations of any analyzer

* Add modernize "appendclipped" analyzer.

* Fix violations of appendclipped

* Add modernize "bloop" analyzer.

* Add modernize "fmtappendf" analyzer.

* Add modernize "forvar" analyzer.

* Add modernize "mapsloop" analyzer.

* Add modernize "minmax" analyzer.

* Fix violations of minmax analyzer

* Add modernize "omitzero" analyzer.

* Add modernize "rangeint" analyzer.

* Fix violations of rangeint.

* Add modernize "reflecttypefor" analyzer.

* Fix violations of reflecttypefor analyzer.

* Add modernize "slicescontains" analyzer.

* Add modernize "slicessort" analyzer.

* Add modernize "slicesdelete" analyzer. This is disabled by default for now. See https://go.dev/issue/73686.

* Add modernize "stringscutprefix" analyzer.

* Add modernize "stringsbuilder" analyzer.

* Fix violations of stringsbuilder analyzer.

* Add modernize "stringsseq" analyzer.

* Add modernize "testingcontext" analyzer.

* Add modernize "waitgroup" analyzer.

* Changelog fragment

* gofmt

* gazelle

* Add modernize "newexpr" analyzer.

* Disable newexpr until go1.26

* Add more details in WORKSPACE on how to update the override

* @nalepae feedback on min()

* gofmt

* Fix violations of forvar
2025-11-14 01:27:22 +00:00

79 lines
2.8 KiB
Go

package interop
import (
"encoding/binary"
"math/big"
"sync"
"github.com/OffchainLabs/prysm/v7/async"
"github.com/OffchainLabs/prysm/v7/crypto/bls"
"github.com/OffchainLabs/prysm/v7/crypto/hash"
"github.com/OffchainLabs/prysm/v7/encoding/bytesutil"
"github.com/pkg/errors"
)
const (
blsWithdrawalPrefixByte = byte(0)
)
// DeterministicallyGenerateKeys creates BLS private keys using a fixed curve order according to
// the algorithm specified in the Ethereum beacon chain specification interop mock start section found here:
// https://github.com/ethereum/eth2.0-pm/blob/a085c9870f3956d6228ed2a40cd37f0c6580ecd7/interop/mocked_start/README.md
func DeterministicallyGenerateKeys(startIndex, numKeys uint64) ([]bls.SecretKey, []bls.PublicKey, error) {
privKeys := make([]bls.SecretKey, numKeys)
pubKeys := make([]bls.PublicKey, numKeys)
type keys struct {
secrets []bls.SecretKey
publics []bls.PublicKey
}
// lint:ignore uintcast -- this is safe because we can reasonably expect that the number of keys is less than max int64.
results, err := async.Scatter(int(numKeys), func(offset int, entries int, _ *sync.RWMutex) (any, error) {
secs, pubs, err := deterministicallyGenerateKeys(uint64(offset)+startIndex, uint64(entries))
return &keys{secrets: secs, publics: pubs}, err
})
if err != nil {
return nil, nil, errors.Wrap(err, "failed to generate keys")
}
for _, result := range results {
if keysExtent, ok := result.Extent.(*keys); ok {
copy(privKeys[result.Offset:], keysExtent.secrets)
copy(pubKeys[result.Offset:], keysExtent.publics)
} else {
return nil, nil, errors.New("extent not of expected type")
}
}
return privKeys, pubKeys, nil
}
func deterministicallyGenerateKeys(startIndex, numKeys uint64) ([]bls.SecretKey, []bls.PublicKey, error) {
privKeys := make([]bls.SecretKey, numKeys)
pubKeys := make([]bls.PublicKey, numKeys)
for i := startIndex; i < startIndex+numKeys; i++ {
enc := make([]byte, 32)
binary.LittleEndian.PutUint32(enc, uint32(i))
h := hash.Hash(enc)
// Reverse byte order to big endian for use with big ints.
num := bytesutil.LittleEndianBytesToBigInt(h[:])
order := new(big.Int)
var ok bool
order, ok = order.SetString(bls.CurveOrder, 10)
if !ok {
return nil, nil, errors.New("could not set bls curve order as big int")
}
num = num.Mod(num, order)
numBytes := num.Bytes()
// pad key at the start with zero bytes to make it into a 32 byte key
if len(numBytes) < 32 {
emptyBytes := make([]byte, 32-len(numBytes))
numBytes = append(emptyBytes, numBytes...)
}
priv, err := bls.SecretKeyFromBytes(numBytes)
if err != nil {
return nil, nil, errors.Wrapf(err, "could not create bls secret key at index %d from raw bytes", i)
}
privKeys[i-startIndex] = priv
pubKeys[i-startIndex] = priv.PublicKey()
}
return privKeys, pubKeys, nil
}