Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify commitment generation, and choose better name for util file #1063

Merged
merged 4 commits into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,28 @@ import (
"github.com/consensys/gnark-crypto/ecc"
"github.com/consensys/gnark-crypto/ecc/bn254"

"github.com/Layr-Labs/eigenda/encoding/kzg/verifier"
"github.com/Layr-Labs/eigenda/encoding/rs"
)

// GenerateBlobCommitment computes a kzg-bn254 commitment of blob data using SRS
func GenerateBlobCommitment(
kzgVerifier *verifier.Verifier,
blob []byte) (*encoding.G1Commitment, error) {
g1Srs []bn254.G1Affine,
blobBytes []byte) (*encoding.G1Commitment, error) {

inputFr, err := rs.ToFrArray(blob)
inputFr, err := rs.ToFrArray(blobBytes)
if err != nil {
return nil, fmt.Errorf("convert bytes to field elements, %w", err)
}

if len(kzgVerifier.Srs.G1) < len(inputFr) {
if len(g1Srs) < len(inputFr) {
return nil, fmt.Errorf(
"insufficient SRS in memory: have %v, need %v",
len(kzgVerifier.Srs.G1),
len(g1Srs),
len(inputFr))
}

var commitment bn254.G1Affine
_, err = commitment.MultiExp(kzgVerifier.Srs.G1[:len(inputFr)], inputFr, ecc.MultiExpConfig{})
_, err = commitment.MultiExp(g1Srs[:len(inputFr)], inputFr, ecc.MultiExpConfig{})
if err != nil {
return nil, fmt.Errorf("MultiExp: %w", err)
}
Expand All @@ -39,11 +38,11 @@ func GenerateBlobCommitment(
// GenerateAndCompareBlobCommitment generates the kzg-bn254 commitment of the blob, and compares it with a claimed
// commitment. An error is returned if there is a problem generating the commitment, or if the comparison fails.
func GenerateAndCompareBlobCommitment(
kzgVerifier *verifier.Verifier,
claimedCommitment *encoding.G1Commitment,
blobBytes []byte) error {
g1Srs []bn254.G1Affine,
blobBytes []byte,
claimedCommitment *encoding.G1Commitment) error {

computedCommitment, err := GenerateBlobCommitment(kzgVerifier, blobBytes)
computedCommitment, err := GenerateBlobCommitment(g1Srs, blobBytes)
if err != nil {
return fmt.Errorf("compute commitment: %w", err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,18 @@ package verification
import (
"github.com/Layr-Labs/eigenda/common/testutils/random"
"github.com/Layr-Labs/eigenda/encoding/kzg"
"github.com/Layr-Labs/eigenda/encoding/kzg/verifier"
"github.com/Layr-Labs/eigenda/encoding/utils/codec"
"github.com/stretchr/testify/require"
"math"
"runtime"
"testing"
)

func getKzgConfig() *kzg.KzgConfig {
return &kzg.KzgConfig{
G1Path: "../../../../inabox/resources/kzg/g1.point",
G2Path: "../../../../inabox/resources/kzg/g2.point",
G2PowerOf2Path: "../../../../inabox/resources/kzg/g2.point.powerOf2",
CacheDir: "../../../../inabox/resources/kzg/SRSTables",
SRSOrder: 3000,
SRSNumberToLoad: 2900,
NumWorker: uint64(runtime.GOMAXPROCS(0)),
LoadG2Points: false,
}
const g1Path = "../../../../inabox/resources/kzg/g1.point"

// computeSrsNumber computes the number of SRS elements that need to be loaded for a message of given byte count
func computeSrsNumber(byteCount int) uint64 {
return uint64(math.Ceil(float64(byteCount) / 32))
}

// randomlyModifyBytes picks a random byte from the input array, and increments it
Expand All @@ -35,58 +29,64 @@ func getRandomPaddedBytes(testRandom *random.TestRandom, count int) []byte {

func TestComputeAndCompareKzgCommitmentSuccess(t *testing.T) {
testRandom := random.NewTestRandom(t)
randomBytes := getRandomPaddedBytes(testRandom, 1000)
randomBytes := getRandomPaddedBytes(testRandom, 100+testRandom.Intn(1000))

srsNumberToLoad := computeSrsNumber(len(randomBytes))

kzgVerifier, err := verifier.NewVerifier(getKzgConfig(), nil)
require.NotNil(t, kzgVerifier)
g1Srs, err := kzg.ReadG1Points(g1Path, srsNumberToLoad, uint64(runtime.GOMAXPROCS(0)))
require.NotNil(t, g1Srs)
require.NoError(t, err)

commitment, err := GenerateBlobCommitment(kzgVerifier, randomBytes)
commitment, err := GenerateBlobCommitment(g1Srs, randomBytes)
require.NotNil(t, commitment)
require.NoError(t, err)

// make sure the commitment verifies correctly
err = GenerateAndCompareBlobCommitment(
kzgVerifier,
commitment,
randomBytes)
g1Srs,
randomBytes,
commitment)
require.NoError(t, err)
}

func TestComputeAndCompareKzgCommitmentFailure(t *testing.T) {
testRandom := random.NewTestRandom(t)
randomBytes := getRandomPaddedBytes(testRandom, 1000)
randomBytes := getRandomPaddedBytes(testRandom, 100+testRandom.Intn(1000))

srsNumberToLoad := computeSrsNumber(len(randomBytes))

kzgVerifier, err := verifier.NewVerifier(getKzgConfig(), nil)
require.NotNil(t, kzgVerifier)
g1Srs, err := kzg.ReadG1Points(g1Path, srsNumberToLoad, uint64(runtime.GOMAXPROCS(0)))
require.NotNil(t, g1Srs)
require.NoError(t, err)

commitment, err := GenerateBlobCommitment(kzgVerifier, randomBytes)
commitment, err := GenerateBlobCommitment(g1Srs, randomBytes)
require.NotNil(t, commitment)
require.NoError(t, err)

// randomly modify the bytes, and make sure the commitment verification fails
randomlyModifyBytes(testRandom, randomBytes)
err = GenerateAndCompareBlobCommitment(
kzgVerifier,
commitment,
randomBytes)
g1Srs,
randomBytes,
commitment)
require.NotNil(t, err)
}

func TestGenerateBlobCommitmentEquality(t *testing.T) {
testRandom := random.NewTestRandom(t)
randomBytes := getRandomPaddedBytes(testRandom, 1000)
randomBytes := getRandomPaddedBytes(testRandom, 100+testRandom.Intn(1000))

kzgVerifier, err := verifier.NewVerifier(getKzgConfig(), nil)
require.NotNil(t, kzgVerifier)
srsNumberToLoad := computeSrsNumber(len(randomBytes))

g1Srs, err := kzg.ReadG1Points(g1Path, srsNumberToLoad, uint64(runtime.GOMAXPROCS(0)))
require.NotNil(t, g1Srs)
require.NoError(t, err)

// generate two identical commitments
commitment1, err := GenerateBlobCommitment(kzgVerifier, randomBytes)
commitment1, err := GenerateBlobCommitment(g1Srs, randomBytes)
require.NotNil(t, commitment1)
require.NoError(t, err)
commitment2, err := GenerateBlobCommitment(kzgVerifier, randomBytes)
commitment2, err := GenerateBlobCommitment(g1Srs, randomBytes)
require.NotNil(t, commitment2)
require.NoError(t, err)

Expand All @@ -95,7 +95,7 @@ func TestGenerateBlobCommitmentEquality(t *testing.T) {

// randomly modify a byte
randomlyModifyBytes(testRandom, randomBytes)
commitmentA, err := GenerateBlobCommitment(kzgVerifier, randomBytes)
commitmentA, err := GenerateBlobCommitment(g1Srs, randomBytes)
require.NotNil(t, commitmentA)
require.NoError(t, err)

Expand All @@ -104,22 +104,24 @@ func TestGenerateBlobCommitmentEquality(t *testing.T) {
}

func TestGenerateBlobCommitmentTooLong(t *testing.T) {
kzgVerifier, err := verifier.NewVerifier(getKzgConfig(), nil)
require.NotNil(t, kzgVerifier)
srsNumberToLoad := uint64(500)

g1Srs, err := kzg.ReadG1Points(g1Path, srsNumberToLoad, uint64(runtime.GOMAXPROCS(0)))
require.NotNil(t, g1Srs)
require.NoError(t, err)

// this is the absolute maximum number of bytes we can handle, given how the verifier was configured
almostTooLongByteCount := 2900 * 32
almostTooLongByteCount := srsNumberToLoad * 32

// an array of exactly this size should be fine
almostTooLongBytes := make([]byte, almostTooLongByteCount)
commitment1, err := GenerateBlobCommitment(kzgVerifier, almostTooLongBytes)
commitment1, err := GenerateBlobCommitment(g1Srs, almostTooLongBytes)
require.NotNil(t, commitment1)
require.NoError(t, err)

// but 1 more byte is more than we can handle
tooLongBytes := make([]byte, almostTooLongByteCount+1)
commitment2, err := GenerateBlobCommitment(kzgVerifier, tooLongBytes)
commitment2, err := GenerateBlobCommitment(g1Srs, tooLongBytes)
require.Nil(t, commitment2)
require.NotNil(t, err)
}
Loading