Skip to content

Commit

Permalink
Tests for kzg package (#1462)
Browse files Browse the repository at this point in the history
  • Loading branch information
nidhi-singh02 authored Jun 13, 2024
1 parent f72caae commit fc3467a
Show file tree
Hide file tree
Showing 5 changed files with 180 additions and 0 deletions.
4 changes: 4 additions & 0 deletions mod/da/pkg/kzg/ckzg/ckzg_cgo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,7 @@ func TestVerifyBlobKZGInvalidProof(t *testing.T) {
})
}
}

func TestGetImplementation(t *testing.T) {
require.Equal(t, "ethereum/c-kzg-4844", globalVerifier.GetImplementation())
}
5 changes: 5 additions & 0 deletions mod/da/pkg/kzg/ckzg/ckzg_no_cgo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,8 @@ func TestVerifyBlobKZGInvalidProof(t *testing.T) {
})
}
}

func TestGetImplementation(t *testing.T) {
require.NotNil(t, globalVerifier)
require.Equal(t, "ethereum/c-kzg-4844", globalVerifier.GetImplementation())
}
42 changes: 42 additions & 0 deletions mod/da/pkg/kzg/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// SPDX-License-Identifier: BUSL-1.1
//
// Copyright (C) 2024, Berachain Foundation. All rights reserved.
// Use of this software is governed by the Business Source License included
// in the LICENSE file of this repository and at www.mariadb.com/bsl11.
//
// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY
// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER
// VERSIONS OF THE LICENSED WORK.
//
// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF
// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF
// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE).
//
// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON
// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS,
// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND
// TITLE.

package kzg_test

import (
"testing"

"github.com/berachain/beacon-kit/mod/da/pkg/kzg"
"github.com/stretchr/testify/require"
)

func TestDefaultConfig(t *testing.T) {
cfg := kzg.DefaultConfig()
require.Equal(
t,
"./testing/files/kzg-trusted-setup.json",
cfg.TrustedSetupPath,
)
require.Equal(
t,
"crate-crypto/go-kzg-4844",
cfg.Implementation,
)
}
8 changes: 8 additions & 0 deletions mod/da/pkg/kzg/gokzg/gokzg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,14 @@ func TestVerifyBlobProofBatch(t *testing.T) {
require.NoError(t, err)
}

func TestGetImplementation(t *testing.T) {
verifier, err := setupVerifier()
require.NoError(t, err)

impl := verifier.GetImplementation()
require.Equal(t, gokzg.Implementation, impl)
}

// setupVerifier reads the trusted setup file and creates a new GoKZGVerifier.
func setupVerifier() (*gokzg.Verifier, error) {
fs := afero.NewOsFs()
Expand Down
121 changes: 121 additions & 0 deletions mod/da/pkg/kzg/proof_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
// SPDX-License-Identifier: BUSL-1.1
//
// Copyright (C) 2024, Berachain Foundation. All rights reserved.
// Use of this software is governed by the Business Source License included
// in the LICENSE file of this repository and at www.mariadb.com/bsl11.
//
// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY
// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER
// VERSIONS OF THE LICENSED WORK.
//
// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF
// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF
// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE).
//
// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON
// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS,
// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND
// TITLE.

package kzg_test

import (
"encoding/json"
"os"
"path/filepath"
"testing"

"github.com/berachain/beacon-kit/mod/da/pkg/kzg"
"github.com/berachain/beacon-kit/mod/da/pkg/kzg/ckzg"
"github.com/berachain/beacon-kit/mod/da/pkg/kzg/gokzg"
"github.com/berachain/beacon-kit/mod/da/pkg/types"
"github.com/berachain/beacon-kit/mod/primitives/pkg/eip4844"
gokzg4844 "github.com/crate-crypto/go-kzg-4844"
"github.com/spf13/afero"
"github.com/stretchr/testify/require"
)

var baseDir = "../../../../testing/files/"

func TestNewBlobProofVerifier_KzgImpl(t *testing.T) {
ts, err := loadTrustedSetupFromFile()
require.NoError(t, err)

verifier, err := kzg.NewBlobProofVerifier(gokzg.Implementation, ts)
require.NoError(t, err)
require.NotNil(t, verifier)
require.Equal(t, gokzg.Implementation, verifier.GetImplementation())
}

func TestNewBlobProofVerifier_CkzgImpl(t *testing.T) {
ts, err := loadTrustedSetupFromFile()
require.NoError(t, err)

verifier, err := kzg.NewBlobProofVerifier(ckzg.Implementation, ts)
require.NoError(t, err)
require.NotNil(t, verifier)
require.Equal(t, ckzg.Implementation, verifier.GetImplementation())
}

func TestNewBlobProofVerifier_InvalidImpl(t *testing.T) {
ts, err := loadTrustedSetupFromFile()
require.NoError(t, err)

invalidImpl := "invalid-implementation"
_, err = kzg.NewBlobProofVerifier(invalidImpl, ts)
require.ErrorIs(t, err, kzg.ErrUnsupportedKzgImplementation)
}

// loadTrustedSetupFromFile is the helper function.
func loadTrustedSetupFromFile() (*gokzg4844.JSONTrustedSetup, error) {
fileName := "kzg-trusted-setup.json"
fullPath := filepath.Join(baseDir, fileName)
data, err := os.ReadFile(fullPath)
if err != nil {
return nil, err
}

var ts gokzg4844.JSONTrustedSetup
err = json.Unmarshal(data, &ts)
if err != nil {
return nil, err
}

return &ts, nil
}

func TestArgsFromSidecars(t *testing.T) {
fs := afero.NewOsFs()
fullPath := filepath.Join(baseDir, "test_data.json")
file, err := afero.ReadFile(fs, fullPath)
require.NoError(t, err)

type Data struct {
Input struct {
Blob string `json:"blob"`
Commitment string `json:"commitment"`
Proof string `json:"proof"`
} `json:"input"`
}
var data Data

err = json.Unmarshal(file, &data)
require.NoError(t, err)

scs := &types.BlobSidecars{
Sidecars: []*types.BlobSidecar{
{
Blob: eip4844.Blob{data.Input.Blob[0]},
KzgProof: eip4844.KZGProof{data.Input.Proof[0]},
KzgCommitment: eip4844.KZGCommitment{data.Input.Commitment[0]},
},
},
}

args := kzg.ArgsFromSidecars(scs)

require.Len(t, args.Blobs, 1)
require.Len(t, args.Proofs, 1)
require.Len(t, args.Commitments, 1)
}

0 comments on commit fc3467a

Please sign in to comment.