From dea389f022da7231b6fb38080391bf567e6126d2 Mon Sep 17 00:00:00 2001 From: Keefe Liu Date: Mon, 6 May 2024 17:11:05 +0800 Subject: [PATCH] add example for bls verify precompile contract --- core/vm/contracts_test.go | 40 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/core/vm/contracts_test.go b/core/vm/contracts_test.go index 2557271736..9fd4c08710 100644 --- a/core/vm/contracts_test.go +++ b/core/vm/contracts_test.go @@ -18,6 +18,7 @@ package vm import ( "bytes" + "crypto/sha256" "encoding/hex" "encoding/json" "fmt" @@ -25,9 +26,10 @@ import ( "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. @@ -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) +}