Skip to content

Commit

Permalink
Simplify commitment generation, and choose better name for util file
Browse files Browse the repository at this point in the history
Signed-off-by: litt3 <[email protected]>
  • Loading branch information
litt3 committed Jan 2, 2025
1 parent 290bc67 commit 6bbd2f4
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 40 deletions.
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,
g1Srs []bn254.G1Affine,
blob []byte) (*encoding.G1Commitment, error) {

inputFr, err := rs.ToFrArray(blob)
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,
g1Srs []bn254.G1Affine,
claimedCommitment *encoding.G1Commitment,
blobBytes []byte) 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)
s1, err := kzg.ReadG1Points(g1Path, srsNumberToLoad, uint64(runtime.GOMAXPROCS(0)))
require.NotNil(t, s1)
require.NoError(t, err)

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

// make sure the commitment verifies correctly
err = GenerateAndCompareBlobCommitment(
kzgVerifier,
s1,
commitment,
randomBytes)
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)
s1, err := kzg.ReadG1Points(g1Path, srsNumberToLoad, uint64(runtime.GOMAXPROCS(0)))
require.NotNil(t, s1)
require.NoError(t, err)

commitment, err := GenerateBlobCommitment(kzgVerifier, randomBytes)
commitment, err := GenerateBlobCommitment(s1, 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,
s1,
commitment,
randomBytes)
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))

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

// generate two identical commitments
commitment1, err := GenerateBlobCommitment(kzgVerifier, randomBytes)
commitment1, err := GenerateBlobCommitment(s1, randomBytes)
require.NotNil(t, commitment1)
require.NoError(t, err)
commitment2, err := GenerateBlobCommitment(kzgVerifier, randomBytes)
commitment2, err := GenerateBlobCommitment(s1, 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(s1, 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)

s1, err := kzg.ReadG1Points(g1Path, srsNumberToLoad, uint64(runtime.GOMAXPROCS(0)))
require.NotNil(t, s1)
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(s1, 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(s1, tooLongBytes)
require.Nil(t, commitment2)
require.NotNil(t, err)
}

0 comments on commit 6bbd2f4

Please sign in to comment.