mirror of
https://github.com/OffchainLabs/prysm.git
synced 2026-01-09 15:37:56 -05:00
Fix cyclical dependency (#15248)
* fix cyclical dependency * fix bazel file * fix comments * fix: Break cyclical dependency by importing GetRandBlob directly from crypto/random * add changelog
This commit is contained in:
@@ -29,9 +29,8 @@ go_test(
|
|||||||
embed = [":go_default_library"],
|
embed = [":go_default_library"],
|
||||||
deps = [
|
deps = [
|
||||||
"//consensus-types/blocks:go_default_library",
|
"//consensus-types/blocks:go_default_library",
|
||||||
|
"//crypto/random:go_default_library",
|
||||||
"//testing/require:go_default_library",
|
"//testing/require:go_default_library",
|
||||||
"@com_github_consensys_gnark_crypto//ecc/bls12-381/fr:go_default_library",
|
|
||||||
"@com_github_crate_crypto_go_kzg_4844//:go_default_library",
|
"@com_github_crate_crypto_go_kzg_4844//:go_default_library",
|
||||||
"@com_github_sirupsen_logrus//:go_default_library",
|
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,16 +1,12 @@
|
|||||||
package kzg
|
package kzg
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"crypto/sha256"
|
|
||||||
"encoding/binary"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/OffchainLabs/prysm/v6/consensus-types/blocks"
|
"github.com/OffchainLabs/prysm/v6/consensus-types/blocks"
|
||||||
|
"github.com/OffchainLabs/prysm/v6/crypto/random"
|
||||||
"github.com/OffchainLabs/prysm/v6/testing/require"
|
"github.com/OffchainLabs/prysm/v6/testing/require"
|
||||||
"github.com/consensys/gnark-crypto/ecc/bls12-381/fr"
|
|
||||||
GoKZG "github.com/crate-crypto/go-kzg-4844"
|
GoKZG "github.com/crate-crypto/go-kzg-4844"
|
||||||
"github.com/sirupsen/logrus"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func GenerateCommitmentAndProof(blob GoKZG.Blob) (GoKZG.KZGCommitment, GoKZG.KZGProof, error) {
|
func GenerateCommitmentAndProof(blob GoKZG.Blob) (GoKZG.KZGCommitment, GoKZG.KZGProof, error) {
|
||||||
@@ -41,7 +37,7 @@ func TestBytesToAny(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestGenerateCommitmentAndProof(t *testing.T) {
|
func TestGenerateCommitmentAndProof(t *testing.T) {
|
||||||
blob := getRandBlob(123)
|
blob := random.GetRandBlob(123)
|
||||||
commitment, proof, err := GenerateCommitmentAndProof(blob)
|
commitment, proof, err := GenerateCommitmentAndProof(blob)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
expectedCommitment := GoKZG.KZGCommitment{180, 218, 156, 194, 59, 20, 10, 189, 186, 254, 132, 93, 7, 127, 104, 172, 238, 240, 237, 70, 83, 89, 1, 152, 99, 0, 165, 65, 143, 62, 20, 215, 230, 14, 205, 95, 28, 245, 54, 25, 160, 16, 178, 31, 232, 207, 38, 85}
|
expectedCommitment := GoKZG.KZGCommitment{180, 218, 156, 194, 59, 20, 10, 189, 186, 254, 132, 93, 7, 127, 104, 172, 238, 240, 237, 70, 83, 89, 1, 152, 99, 0, 165, 65, 143, 62, 20, 215, 230, 14, 205, 95, 28, 245, 54, 25, 160, 16, 178, 31, 232, 207, 38, 85}
|
||||||
@@ -49,36 +45,3 @@ func TestGenerateCommitmentAndProof(t *testing.T) {
|
|||||||
require.Equal(t, expectedCommitment, commitment)
|
require.Equal(t, expectedCommitment, commitment)
|
||||||
require.Equal(t, expectedProof, proof)
|
require.Equal(t, expectedProof, proof)
|
||||||
}
|
}
|
||||||
|
|
||||||
func deterministicRandomness(seed int64) [32]byte {
|
|
||||||
// Converts an int64 to a byte slice
|
|
||||||
buf := new(bytes.Buffer)
|
|
||||||
err := binary.Write(buf, binary.BigEndian, seed)
|
|
||||||
if err != nil {
|
|
||||||
logrus.WithError(err).Error("Failed to write int64 to bytes buffer")
|
|
||||||
return [32]byte{}
|
|
||||||
}
|
|
||||||
bytes := buf.Bytes()
|
|
||||||
|
|
||||||
return sha256.Sum256(bytes)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Returns a serialized random field element in big-endian
|
|
||||||
func getRandFieldElement(seed int64) [32]byte {
|
|
||||||
bytes := deterministicRandomness(seed)
|
|
||||||
var r fr.Element
|
|
||||||
r.SetBytes(bytes[:])
|
|
||||||
|
|
||||||
return GoKZG.SerializeScalar(r)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Returns a random blob using the passed seed as entropy
|
|
||||||
func getRandBlob(seed int64) GoKZG.Blob {
|
|
||||||
var blob GoKZG.Blob
|
|
||||||
bytesPerBlob := GoKZG.ScalarsPerBlob * GoKZG.SerializedScalarSize
|
|
||||||
for i := 0; i < bytesPerBlob; i += GoKZG.SerializedScalarSize {
|
|
||||||
fieldElementBytes := getRandFieldElement(seed + int64(i))
|
|
||||||
copy(blob[i:i+GoKZG.SerializedScalarSize], fieldElementBytes[:])
|
|
||||||
}
|
|
||||||
return blob
|
|
||||||
}
|
|
||||||
|
|||||||
3
changelog/gerceboss_testing-util-reuse.md
Normal file
3
changelog/gerceboss_testing-util-reuse.md
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
### Fixed
|
||||||
|
|
||||||
|
- Fix cyclical dependencies issue when using testing/util package
|
||||||
24
crypto/random/BUILD.bazel
Normal file
24
crypto/random/BUILD.bazel
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
load("@prysm//tools/go:def.bzl", "go_library", "go_test")
|
||||||
|
|
||||||
|
go_library(
|
||||||
|
name = "go_default_library",
|
||||||
|
srcs = ["random.go"],
|
||||||
|
importpath = "github.com/OffchainLabs/prysm/v6/crypto/random",
|
||||||
|
visibility = ["//visibility:public"],
|
||||||
|
deps = [
|
||||||
|
"@com_github_consensys_gnark_crypto//ecc/bls12-381/fr:go_default_library",
|
||||||
|
"@com_github_crate_crypto_go_kzg_4844//:go_default_library",
|
||||||
|
"@com_github_sirupsen_logrus//:go_default_library",
|
||||||
|
],
|
||||||
|
)
|
||||||
|
|
||||||
|
go_test(
|
||||||
|
name = "go_default_test",
|
||||||
|
srcs = ["random_test.go"],
|
||||||
|
embed = [":go_default_library"],
|
||||||
|
deps = [
|
||||||
|
"//testing/assert:go_default_library",
|
||||||
|
"//testing/require:go_default_library",
|
||||||
|
"@com_github_crate_crypto_go_kzg_4844//:go_default_library",
|
||||||
|
],
|
||||||
|
)
|
||||||
45
crypto/random/random.go
Normal file
45
crypto/random/random.go
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
package random
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"crypto/sha256"
|
||||||
|
"encoding/binary"
|
||||||
|
|
||||||
|
"github.com/consensys/gnark-crypto/ecc/bls12-381/fr"
|
||||||
|
GoKZG "github.com/crate-crypto/go-kzg-4844"
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
|
// DeterministicRandomness creates a deterministic 32 byte array from a seed
|
||||||
|
func DeterministicRandomness(seed int64) [32]byte {
|
||||||
|
// Converts an int64 to a byte slice
|
||||||
|
buf := new(bytes.Buffer)
|
||||||
|
err := binary.Write(buf, binary.BigEndian, seed)
|
||||||
|
if err != nil {
|
||||||
|
logrus.WithError(err).Error("Failed to write int64 to bytes buffer")
|
||||||
|
return [32]byte{}
|
||||||
|
}
|
||||||
|
bytes := buf.Bytes()
|
||||||
|
|
||||||
|
return sha256.Sum256(bytes)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetRandFieldElement returns a serialized random field element in big-endian
|
||||||
|
func GetRandFieldElement(seed int64) [32]byte {
|
||||||
|
bytes := DeterministicRandomness(seed)
|
||||||
|
var r fr.Element
|
||||||
|
r.SetBytes(bytes[:])
|
||||||
|
|
||||||
|
return GoKZG.SerializeScalar(r)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetRandBlob returns a random blob using the passed seed as entropy
|
||||||
|
func GetRandBlob(seed int64) GoKZG.Blob {
|
||||||
|
var blob GoKZG.Blob
|
||||||
|
bytesPerBlob := GoKZG.ScalarsPerBlob * GoKZG.SerializedScalarSize
|
||||||
|
for i := 0; i < bytesPerBlob; i += GoKZG.SerializedScalarSize {
|
||||||
|
fieldElementBytes := GetRandFieldElement(seed + int64(i))
|
||||||
|
copy(blob[i:i+GoKZG.SerializedScalarSize], fieldElementBytes[:])
|
||||||
|
}
|
||||||
|
return blob
|
||||||
|
}
|
||||||
61
crypto/random/random_test.go
Normal file
61
crypto/random/random_test.go
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
package random
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/OffchainLabs/prysm/v6/testing/assert"
|
||||||
|
"github.com/OffchainLabs/prysm/v6/testing/require"
|
||||||
|
GoKZG "github.com/crate-crypto/go-kzg-4844"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestDeterministicRandomness(t *testing.T) {
|
||||||
|
seed := int64(123)
|
||||||
|
r1 := DeterministicRandomness(seed)
|
||||||
|
r2 := DeterministicRandomness(seed)
|
||||||
|
assert.DeepEqual(t, r1, r2, "Same seed should produce same output")
|
||||||
|
|
||||||
|
// Test different seeds produce different outputs
|
||||||
|
r3 := DeterministicRandomness(seed + 1)
|
||||||
|
assert.NotEqual(t, r1, r3, "Different seeds should produce different outputs")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetRandFieldElement(t *testing.T) {
|
||||||
|
seed := int64(123)
|
||||||
|
r1 := GetRandFieldElement(seed)
|
||||||
|
r2 := GetRandFieldElement(seed)
|
||||||
|
assert.DeepEqual(t, r1, r2, "Same seed should produce same output")
|
||||||
|
|
||||||
|
// Test different seeds produce different outputs
|
||||||
|
r3 := GetRandFieldElement(seed + 1)
|
||||||
|
assert.NotEqual(t, r1, r3, "Different seeds should produce different outputs")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetRandBlob(t *testing.T) {
|
||||||
|
seed := int64(123)
|
||||||
|
r1 := GetRandBlob(seed)
|
||||||
|
r2 := GetRandBlob(seed)
|
||||||
|
assert.DeepEqual(t, r1, r2, "Same seed should produce same blob")
|
||||||
|
|
||||||
|
expectedSize := GoKZG.ScalarsPerBlob * GoKZG.SerializedScalarSize
|
||||||
|
assert.Equal(t, expectedSize, len(r1), "Blob should have correct size")
|
||||||
|
|
||||||
|
r3 := GetRandBlob(seed + 1)
|
||||||
|
assert.NotEqual(t, r1, r3, "Different seeds should produce different blobs")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetRandBlobElements(t *testing.T) {
|
||||||
|
seed := int64(123)
|
||||||
|
blob := GetRandBlob(seed)
|
||||||
|
|
||||||
|
// Check that each field element in the blob matches what we'd get from GetRandFieldElement
|
||||||
|
for i := 0; i < GoKZG.ScalarsPerBlob; i++ {
|
||||||
|
start := i * GoKZG.SerializedScalarSize
|
||||||
|
end := start + GoKZG.SerializedScalarSize
|
||||||
|
|
||||||
|
blobElement := [32]byte{}
|
||||||
|
copy(blobElement[:], blob[start:end])
|
||||||
|
|
||||||
|
expectedElement := GetRandFieldElement(seed + int64(i*GoKZG.SerializedScalarSize))
|
||||||
|
require.DeepEqual(t, expectedElement, blobElement, "Field element in blob doesn't match expected value")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -54,6 +54,7 @@ go_library(
|
|||||||
"//crypto/bls:go_default_library",
|
"//crypto/bls:go_default_library",
|
||||||
"//crypto/hash:go_default_library",
|
"//crypto/hash:go_default_library",
|
||||||
"//crypto/rand:go_default_library",
|
"//crypto/rand:go_default_library",
|
||||||
|
"//crypto/random:go_default_library",
|
||||||
"//encoding/bytesutil:go_default_library",
|
"//encoding/bytesutil:go_default_library",
|
||||||
"//encoding/ssz:go_default_library",
|
"//encoding/ssz:go_default_library",
|
||||||
"//network/forks:go_default_library",
|
"//network/forks:go_default_library",
|
||||||
@@ -65,7 +66,6 @@ go_library(
|
|||||||
"//testing/assertions:go_default_library",
|
"//testing/assertions:go_default_library",
|
||||||
"//testing/require:go_default_library",
|
"//testing/require:go_default_library",
|
||||||
"//time/slots:go_default_library",
|
"//time/slots:go_default_library",
|
||||||
"@com_github_consensys_gnark_crypto//ecc/bls12-381/fr:go_default_library",
|
|
||||||
"@com_github_crate_crypto_go_kzg_4844//:go_default_library",
|
"@com_github_crate_crypto_go_kzg_4844//:go_default_library",
|
||||||
"@com_github_ethereum_go_ethereum//common:go_default_library",
|
"@com_github_ethereum_go_ethereum//common:go_default_library",
|
||||||
"@com_github_ethereum_go_ethereum//common/hexutil:go_default_library",
|
"@com_github_ethereum_go_ethereum//common/hexutil:go_default_library",
|
||||||
|
|||||||
@@ -1,8 +1,6 @@
|
|||||||
package util
|
package util
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"crypto/sha256"
|
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"math/big"
|
"math/big"
|
||||||
"testing"
|
"testing"
|
||||||
@@ -13,17 +11,16 @@ import (
|
|||||||
"github.com/OffchainLabs/prysm/v6/consensus-types/blocks"
|
"github.com/OffchainLabs/prysm/v6/consensus-types/blocks"
|
||||||
"github.com/OffchainLabs/prysm/v6/consensus-types/primitives"
|
"github.com/OffchainLabs/prysm/v6/consensus-types/primitives"
|
||||||
"github.com/OffchainLabs/prysm/v6/crypto/bls"
|
"github.com/OffchainLabs/prysm/v6/crypto/bls"
|
||||||
|
"github.com/OffchainLabs/prysm/v6/crypto/random"
|
||||||
"github.com/OffchainLabs/prysm/v6/encoding/bytesutil"
|
"github.com/OffchainLabs/prysm/v6/encoding/bytesutil"
|
||||||
"github.com/OffchainLabs/prysm/v6/network/forks"
|
"github.com/OffchainLabs/prysm/v6/network/forks"
|
||||||
enginev1 "github.com/OffchainLabs/prysm/v6/proto/engine/v1"
|
enginev1 "github.com/OffchainLabs/prysm/v6/proto/engine/v1"
|
||||||
ethpb "github.com/OffchainLabs/prysm/v6/proto/prysm/v1alpha1"
|
ethpb "github.com/OffchainLabs/prysm/v6/proto/prysm/v1alpha1"
|
||||||
"github.com/OffchainLabs/prysm/v6/testing/require"
|
"github.com/OffchainLabs/prysm/v6/testing/require"
|
||||||
"github.com/OffchainLabs/prysm/v6/time/slots"
|
"github.com/OffchainLabs/prysm/v6/time/slots"
|
||||||
"github.com/consensys/gnark-crypto/ecc/bls12-381/fr"
|
|
||||||
GoKZG "github.com/crate-crypto/go-kzg-4844"
|
GoKZG "github.com/crate-crypto/go-kzg-4844"
|
||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common"
|
||||||
gethTypes "github.com/ethereum/go-ethereum/core/types"
|
gethTypes "github.com/ethereum/go-ethereum/core/types"
|
||||||
"github.com/sirupsen/logrus"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type DenebBlockGeneratorOption func(*denebBlockGenerator)
|
type DenebBlockGeneratorOption func(*denebBlockGenerator)
|
||||||
@@ -203,35 +200,16 @@ func ExtendBlocksPlusBlobs(t *testing.T, blks []blocks.ROBlock, size int) ([]blo
|
|||||||
return blks, blobs
|
return blks, blobs
|
||||||
}
|
}
|
||||||
|
|
||||||
func deterministicRandomness(seed int64) [32]byte {
|
func DeterministicRandomness(seed int64) [32]byte {
|
||||||
// Converts an int64 to a byte slice
|
return random.DeterministicRandomness(seed)
|
||||||
buf := new(bytes.Buffer)
|
|
||||||
err := binary.Write(buf, binary.BigEndian, seed)
|
|
||||||
if err != nil {
|
|
||||||
logrus.WithError(err).Error("Failed to write int64 to bytes buffer")
|
|
||||||
return [32]byte{}
|
|
||||||
}
|
|
||||||
bytes := buf.Bytes()
|
|
||||||
|
|
||||||
return sha256.Sum256(bytes)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns a serialized random field element in big-endian
|
// Returns a serialized random field element in big-endian
|
||||||
func GetRandFieldElement(seed int64) [32]byte {
|
func GetRandFieldElement(seed int64) [32]byte {
|
||||||
bytes := deterministicRandomness(seed)
|
return random.GetRandFieldElement(seed)
|
||||||
var r fr.Element
|
|
||||||
r.SetBytes(bytes[:])
|
|
||||||
|
|
||||||
return GoKZG.SerializeScalar(r)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns a random blob using the passed seed as entropy
|
// Returns a random blob using the passed seed as entropy
|
||||||
func GetRandBlob(seed int64) GoKZG.Blob {
|
func GetRandBlob(seed int64) GoKZG.Blob {
|
||||||
var blob GoKZG.Blob
|
return random.GetRandBlob(seed)
|
||||||
bytesPerBlob := GoKZG.ScalarsPerBlob * GoKZG.SerializedScalarSize
|
|
||||||
for i := 0; i < bytesPerBlob; i += GoKZG.SerializedScalarSize {
|
|
||||||
fieldElementBytes := GetRandFieldElement(seed + int64(i))
|
|
||||||
copy(blob[i:i+GoKZG.SerializedScalarSize], fieldElementBytes[:])
|
|
||||||
}
|
|
||||||
return blob
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user