github.com/nspcc-dev/neo-go@v0.105.2-0.20240517133400-6be757af3eba/pkg/core/interop/crypto/ecdsa.go (about)

     1  package crypto
     2  
     3  import (
     4  	"crypto/elliptic"
     5  	"errors"
     6  	"fmt"
     7  
     8  	"github.com/nspcc-dev/neo-go/pkg/core/fee"
     9  	"github.com/nspcc-dev/neo-go/pkg/core/interop"
    10  	"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
    11  	"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
    12  	"github.com/nspcc-dev/neo-go/pkg/vm"
    13  	"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
    14  )
    15  
    16  // ECDSASecp256r1CheckMultisig checks multiple ECDSA signatures at once using
    17  // Secp256r1 elliptic curve.
    18  func ECDSASecp256r1CheckMultisig(ic *interop.Context) error {
    19  	pkeys, err := ic.VM.Estack().PopSigElements()
    20  	if err != nil {
    21  		return fmt.Errorf("wrong key parameters: %w", err)
    22  	}
    23  	if !ic.VM.AddGas(ic.BaseExecFee() * fee.ECDSAVerifyPrice * int64(len(pkeys))) {
    24  		return errors.New("gas limit exceeded")
    25  	}
    26  	sigs, err := ic.VM.Estack().PopSigElements()
    27  	if err != nil {
    28  		return fmt.Errorf("wrong signature parameters: %w", err)
    29  	}
    30  	// It's ok to have more keys than there are signatures (it would
    31  	// just mean that some keys didn't sign), but not the other way around.
    32  	if len(pkeys) < len(sigs) {
    33  		return errors.New("more signatures than there are keys")
    34  	}
    35  	sigok := vm.CheckMultisigPar(ic.VM, elliptic.P256(), hash.NetSha256(ic.Network, ic.Container).BytesBE(), pkeys, sigs)
    36  	ic.VM.Estack().PushItem(stackitem.Bool(sigok))
    37  	return nil
    38  }
    39  
    40  // ECDSASecp256r1CheckSig checks ECDSA signature using Secp256r1 elliptic curve.
    41  func ECDSASecp256r1CheckSig(ic *interop.Context) error {
    42  	keyb := ic.VM.Estack().Pop().Bytes()
    43  	signature := ic.VM.Estack().Pop().Bytes()
    44  	pkey, err := keys.NewPublicKeyFromBytes(keyb, elliptic.P256())
    45  	if err != nil {
    46  		return err
    47  	}
    48  	res := pkey.VerifyHashable(signature, ic.Network, ic.Container)
    49  	ic.VM.Estack().PushItem(stackitem.Bool(res))
    50  	return nil
    51  }