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 }