Skip to content

Commit

Permalink
add example for bls verify precompile contract
Browse files Browse the repository at this point in the history
  • Loading branch information
Keefe Liu committed May 6, 2024
1 parent d46af07 commit dea389f
Showing 1 changed file with 38 additions and 2 deletions.
40 changes: 38 additions & 2 deletions core/vm/contracts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,18 @@ package vm

import (
"bytes"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"os"
"testing"
"time"

"github.com/stretchr/testify/require"

"github.com/ethereum/go-ethereum/common"
"github.com/prysmaticlabs/prysm/v4/crypto/bls"
blscommon "github.com/prysmaticlabs/prysm/v4/crypto/bls/common"
"github.com/stretchr/testify/require"
)

// precompiledTest defines the input/output pairs for precompiled contract tests.
Expand Down Expand Up @@ -411,3 +413,37 @@ func TestCometBFTLightBlockValidate(t *testing.T) {
require.NoError(t, err)
require.Equal(t, expectOutputStr, hex.EncodeToString(res))
}

func TestBlsSignatureVerify(t *testing.T) {
msg := "test_bls_signature_verify_precompile_contract"
msgHash := sha256.Sum256([]byte(msg))

privateKey1, err := bls.RandKey()
require.NoError(t, err)
privateKey2, err := bls.RandKey()
require.NoError(t, err)

sig1 := privateKey1.Sign(msgHash[:])
sig2 := privateKey2.Sign(msgHash[:])
sig := bls.AggregateSignatures([]blscommon.Signature{sig1, sig2})

input := msgHash[:]
input = append(input, sig.Marshal()...)
input = append(input, privateKey1.PublicKey().Marshal()...)
input = append(input, privateKey2.PublicKey().Marshal()...)

inputStr := hex.EncodeToString(input)
t.Logf("input string: %s", inputStr)
input, err = hex.DecodeString(inputStr)
require.NoError(t, err)

contract := &blsSignatureVerify{}
res, err := contract.Run(input)
require.NoError(t, err)
require.Equal(t, big1.Bytes(), res)

input[0] += 1
res, err = contract.Run(input)
require.NoError(t, err)
require.Equal(t, big0.Bytes(), res)
}

0 comments on commit dea389f

Please sign in to comment.