|
1 | 1 | package kzg
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "bytes" |
| 5 | + "crypto/sha256" |
| 6 | + "encoding/binary" |
4 | 7 | "testing"
|
5 | 8 |
|
| 9 | + "github.com/consensys/gnark-crypto/ecc/bls12-381/fr" |
6 | 10 | GoKZG "github.com/crate-crypto/go-kzg-4844"
|
7 | 11 | "github.com/prysmaticlabs/prysm/v5/consensus-types/blocks"
|
8 | 12 | "github.com/prysmaticlabs/prysm/v5/testing/require"
|
9 |
| - "github.com/prysmaticlabs/prysm/v5/testing/util" |
| 13 | + "github.com/sirupsen/logrus" |
10 | 14 | )
|
11 | 15 |
|
12 | 16 | func GenerateCommitmentAndProof(blob GoKZG.Blob) (GoKZG.KZGCommitment, GoKZG.KZGProof, error) {
|
@@ -37,11 +41,44 @@ func TestBytesToAny(t *testing.T) {
|
37 | 41 | }
|
38 | 42 |
|
39 | 43 | func TestGenerateCommitmentAndProof(t *testing.T) {
|
40 |
| - blob := util.GetRandBlob(123) |
| 44 | + blob := getRandBlob(123) |
41 | 45 | commitment, proof, err := GenerateCommitmentAndProof(blob)
|
42 | 46 | require.NoError(t, err)
|
43 | 47 | 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}
|
44 | 48 | expectedProof := GoKZG.KZGProof{128, 110, 116, 170, 56, 111, 126, 87, 229, 234, 211, 42, 110, 150, 129, 206, 73, 142, 167, 243, 90, 149, 240, 240, 236, 204, 143, 182, 229, 249, 81, 27, 153, 171, 83, 70, 144, 250, 42, 1, 188, 215, 71, 235, 30, 7, 175, 86}
|
45 | 49 | require.Equal(t, expectedCommitment, commitment)
|
46 | 50 | require.Equal(t, expectedProof, proof)
|
47 | 51 | }
|
| 52 | + |
| 53 | +func deterministicRandomness(seed int64) [32]byte { |
| 54 | + // Converts an int64 to a byte slice |
| 55 | + buf := new(bytes.Buffer) |
| 56 | + err := binary.Write(buf, binary.BigEndian, seed) |
| 57 | + if err != nil { |
| 58 | + logrus.WithError(err).Error("Failed to write int64 to bytes buffer") |
| 59 | + return [32]byte{} |
| 60 | + } |
| 61 | + bytes := buf.Bytes() |
| 62 | + |
| 63 | + return sha256.Sum256(bytes) |
| 64 | +} |
| 65 | + |
| 66 | +// Returns a serialized random field element in big-endian |
| 67 | +func getRandFieldElement(seed int64) [32]byte { |
| 68 | + bytes := deterministicRandomness(seed) |
| 69 | + var r fr.Element |
| 70 | + r.SetBytes(bytes[:]) |
| 71 | + |
| 72 | + return GoKZG.SerializeScalar(r) |
| 73 | +} |
| 74 | + |
| 75 | +// Returns a random blob using the passed seed as entropy |
| 76 | +func getRandBlob(seed int64) GoKZG.Blob { |
| 77 | + var blob GoKZG.Blob |
| 78 | + bytesPerBlob := GoKZG.ScalarsPerBlob * GoKZG.SerializedScalarSize |
| 79 | + for i := 0; i < bytesPerBlob; i += GoKZG.SerializedScalarSize { |
| 80 | + fieldElementBytes := getRandFieldElement(seed + int64(i)) |
| 81 | + copy(blob[i:i+GoKZG.SerializedScalarSize], fieldElementBytes[:]) |
| 82 | + } |
| 83 | + return blob |
| 84 | +} |
0 commit comments