2020-03-18 09:41:09 +00:00
|
|
|
package crypto
|
|
|
|
|
|
|
|
import (
|
2020-03-18 11:04:52 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
2020-03-18 09:41:09 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/interop"
|
2020-04-13 10:43:36 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto"
|
2020-03-18 09:41:09 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm"
|
2020-06-03 12:55:06 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
2020-03-18 09:41:09 +00:00
|
|
|
)
|
|
|
|
|
2020-06-19 09:21:37 +00:00
|
|
|
// ECDSAVerifyPrice is a gas price of a single verification.
|
|
|
|
const ECDSAVerifyPrice = 1000000
|
|
|
|
|
2020-03-18 09:41:09 +00:00
|
|
|
// ECDSAVerify checks ECDSA signature.
|
|
|
|
func ECDSAVerify(ic *interop.Context, v *vm.VM) error {
|
|
|
|
msg := getMessage(ic, v.Estack().Pop().Item())
|
|
|
|
hashToCheck := hash.Sha256(msg).BytesBE()
|
|
|
|
keyb := v.Estack().Pop().Bytes()
|
|
|
|
signature := v.Estack().Pop().Bytes()
|
|
|
|
pkey, err := keys.NewPublicKeyFromBytes(keyb)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
res := pkey.Verify(signature, hashToCheck)
|
|
|
|
v.Estack().PushVal(res)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-03-18 11:04:52 +00:00
|
|
|
// ECDSACheckMultisig checks multiple ECDSA signatures at once.
|
|
|
|
func ECDSACheckMultisig(ic *interop.Context, v *vm.VM) error {
|
|
|
|
msg := getMessage(ic, v.Estack().Pop().Item())
|
|
|
|
hashToCheck := hash.Sha256(msg).BytesBE()
|
|
|
|
pkeys, err := v.Estack().PopSigElements()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("wrong parameters: %s", err.Error())
|
|
|
|
}
|
2020-06-23 14:15:35 +00:00
|
|
|
if !v.AddGas(ECDSAVerifyPrice * int64(len(pkeys))) {
|
2020-06-19 09:21:37 +00:00
|
|
|
return errors.New("gas limit exceeded")
|
|
|
|
}
|
2020-03-18 11:04:52 +00:00
|
|
|
sigs, err := v.Estack().PopSigElements()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("wrong parameters: %s", err.Error())
|
|
|
|
}
|
|
|
|
// It's ok to have more keys than there are signatures (it would
|
|
|
|
// just mean that some keys didn't sign), but not the other way around.
|
|
|
|
if len(pkeys) < len(sigs) {
|
|
|
|
return errors.New("more signatures than there are keys")
|
|
|
|
}
|
2020-04-17 14:03:07 +00:00
|
|
|
sigok := vm.CheckMultisigPar(v, hashToCheck, pkeys, sigs)
|
2020-03-18 11:04:52 +00:00
|
|
|
v.Estack().PushVal(sigok)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-03 12:55:06 +00:00
|
|
|
func getMessage(ic *interop.Context, item stackitem.Item) []byte {
|
2020-04-13 10:43:36 +00:00
|
|
|
var msg []byte
|
|
|
|
switch val := item.(type) {
|
2020-06-03 12:55:06 +00:00
|
|
|
case *stackitem.Interop:
|
2020-04-13 10:43:36 +00:00
|
|
|
msg = val.Value().(crypto.Verifiable).GetSignedPart()
|
2020-06-03 12:55:06 +00:00
|
|
|
case stackitem.Null:
|
2020-04-13 13:18:28 +00:00
|
|
|
msg = ic.Container.GetSignedPart()
|
2020-04-13 10:43:36 +00:00
|
|
|
default:
|
|
|
|
var err error
|
|
|
|
if msg, err = val.TryBytes(); err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
2020-03-18 09:41:09 +00:00
|
|
|
}
|
|
|
|
return msg
|
|
|
|
}
|