mirror of
https://github.com/OffchainLabs/prysm.git
synced 2026-01-08 23:18:15 -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:
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")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user