Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
"expect": {
"out": [],
"status": "10",
"message": "str:invalid signature",
"message": "str:ed25519 verify error",
"logs": "*",
"gas": "*",
"refund": "*"
Expand Down
24 changes: 24 additions & 0 deletions vmhost/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,30 @@ var ErrInfinityFloatOperation = errors.New("infinity operations are not allowed"
// ErrBigFloatWrongPrecision signals that the precision has a wrong value
var ErrBigFloatWrongPrecision = errors.New("precision of the big float must be 53")

// ErrBigFloatDecode signals that big float parse error
var ErrBigFloatDecode = errors.New("big float decode error")

// ErrBigFloatEncode signals that big float parse error
var ErrBigFloatEncode = errors.New("big float encode error")

// ErrSha256Hash signals a sha256 hash error
var ErrSha256Hash = errors.New("sha256 hash error")

// ErrKeccak256Hash signals a keccak256 hash error
var ErrKeccak256Hash = errors.New("keccak256 hash error")

// ErrRipemd160Hash signals a ripemd160 hash error
var ErrRipemd160Hash = errors.New("ripemd160 hash error")

// ErrBlsVerify signals a bls verify error
var ErrBlsVerify = errors.New("bls verify error")

// ErrEd25519Verify signals a ed25519 verify error
var ErrEd25519Verify = errors.New("ed25519 verify error")

// ErrSecp256k1Verify signals a secp256k1 verify error
var ErrSecp256k1Verify = errors.New("secp256k1 verify error")

// ErrAllOperandsAreEqualToZero signals that all operands are equal to 0
var ErrAllOperandsAreEqualToZero = errors.New("all operands are equal to 0")

Expand Down
3 changes: 3 additions & 0 deletions vmhost/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,7 @@ const (

// UseGasBoundedShouldFailExecutionFlag defines the flag that activates failing of execution if gas bounded check fails
UseGasBoundedShouldFailExecutionFlag core.EnableEpochFlag = "UseGasBoundedShouldFailExecutionFlag"

// MaskInternalDependenciesErrorsFlag defines the flag that activates masking of internal dependencies errors
MaskInternalDependenciesErrorsFlag core.EnableEpochFlag = "MaskInternalDependenciesErrorsFlag"
)
74 changes: 74 additions & 0 deletions vmhost/vmhooks/cryptoei.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package vmhooks

import (
"crypto/elliptic"

"github.com/multiversx/mx-chain-vm-go/crypto/signing/secp256"
"github.com/multiversx/mx-chain-vm-go/executor"
"github.com/multiversx/mx-chain-vm-go/math"
Expand Down Expand Up @@ -51,6 +52,7 @@ func (context *VMHooksImpl) Sha256(
resultOffset executor.MemPtr) int32 {

crypto := context.GetCryptoContext()
enableEpochsHandler := context.host.EnableEpochsHandler()
metering := context.GetMeteringContext()

memLoadGas := math.MulUint64(metering.GasSchedule().BaseOperationCost.DataCopyPerByte, uint64(length))
Expand All @@ -69,6 +71,12 @@ func (context *VMHooksImpl) Sha256(

result, err := crypto.Sha256(data)
if err != nil {

if enableEpochsHandler.IsFlagEnabled(vmhost.MaskInternalDependenciesErrorsFlag) {
context.FailExecution(vmhost.ErrSha256Hash)
return 1
}

context.FailExecution(err)
return 1
}
Expand All @@ -87,6 +95,7 @@ func (context *VMHooksImpl) Sha256(
func (context *VMHooksImpl) ManagedSha256(inputHandle, outputHandle int32) int32 {
managedType := context.GetManagedTypesContext()
crypto := context.GetCryptoContext()
enableEpochsHandler := context.host.EnableEpochsHandler()
metering := context.GetMeteringContext()

err := metering.UseGasBoundedAndAddTracedGas(sha256Name, metering.GasSchedule().CryptoAPICost.SHA256)
Expand All @@ -109,6 +118,11 @@ func (context *VMHooksImpl) ManagedSha256(inputHandle, outputHandle int32) int32

resultBytes, err := crypto.Sha256(inputBytes)
if err != nil {
if enableEpochsHandler.IsFlagEnabled(vmhost.MaskInternalDependenciesErrorsFlag) {
context.FailExecution(vmhost.ErrSha256Hash)
return 1
}

context.FailExecution(err)
return 1
}
Expand All @@ -122,6 +136,7 @@ func (context *VMHooksImpl) ManagedSha256(inputHandle, outputHandle int32) int32
// @autogenerate(VMHooks)
func (context *VMHooksImpl) Keccak256(dataOffset executor.MemPtr, length executor.MemLength, resultOffset executor.MemPtr) int32 {
crypto := context.GetCryptoContext()
enableEpochsHandler := context.host.EnableEpochsHandler()
metering := context.GetMeteringContext()

memLoadGas := math.MulUint64(metering.GasSchedule().BaseOperationCost.DataCopyPerByte, uint64(length))
Expand All @@ -140,6 +155,11 @@ func (context *VMHooksImpl) Keccak256(dataOffset executor.MemPtr, length executo

result, err := crypto.Keccak256(data)
if err != nil {
if enableEpochsHandler.IsFlagEnabled(vmhost.MaskInternalDependenciesErrorsFlag) {
context.FailExecution(vmhost.ErrKeccak256Hash)
return 1
}

context.FailExecution(err)
return 1
}
Expand All @@ -158,6 +178,7 @@ func (context *VMHooksImpl) Keccak256(dataOffset executor.MemPtr, length executo
func (context *VMHooksImpl) ManagedKeccak256(inputHandle, outputHandle int32) int32 {
managedType := context.GetManagedTypesContext()
crypto := context.GetCryptoContext()
enableEpochsHandler := context.host.EnableEpochsHandler()
metering := context.GetMeteringContext()

err := metering.UseGasBoundedAndAddTracedGas(keccak256Name, metering.GasSchedule().CryptoAPICost.Keccak256)
Expand All @@ -180,6 +201,11 @@ func (context *VMHooksImpl) ManagedKeccak256(inputHandle, outputHandle int32) in

resultBytes, err := crypto.Keccak256(inputBytes)
if err != nil {
if enableEpochsHandler.IsFlagEnabled(vmhost.MaskInternalDependenciesErrorsFlag) {
context.FailExecution(vmhost.ErrKeccak256Hash)
return 1
}

context.FailExecution(err)
return 1
}
Expand All @@ -193,6 +219,7 @@ func (context *VMHooksImpl) ManagedKeccak256(inputHandle, outputHandle int32) in
// @autogenerate(VMHooks)
func (context *VMHooksImpl) Ripemd160(dataOffset executor.MemPtr, length executor.MemLength, resultOffset executor.MemPtr) int32 {
crypto := context.GetCryptoContext()
enableEpochsHandler := context.host.EnableEpochsHandler()
metering := context.GetMeteringContext()

memLoadGas := math.MulUint64(metering.GasSchedule().BaseOperationCost.DataCopyPerByte, uint64(length))
Expand All @@ -211,6 +238,11 @@ func (context *VMHooksImpl) Ripemd160(dataOffset executor.MemPtr, length executo

result, err := crypto.Ripemd160(data)
if err != nil {
if enableEpochsHandler.IsFlagEnabled(vmhost.MaskInternalDependenciesErrorsFlag) {
context.FailExecution(vmhost.ErrRipemd160Hash)
return 1
}

context.FailExecution(err)
return 1
}
Expand All @@ -236,6 +268,7 @@ func ManagedRipemd160WithHost(host vmhost.VMHost, inputHandle int32, outputHandl
metering := host.Metering()
managedType := host.ManagedTypes()
crypto := host.Crypto()
enableEpochsHandler := host.EnableEpochsHandler()

err := metering.UseGasBoundedAndAddTracedGas(ripemd160Name, metering.GasSchedule().CryptoAPICost.Ripemd160)
if err != nil {
Expand All @@ -257,6 +290,11 @@ func ManagedRipemd160WithHost(host vmhost.VMHost, inputHandle int32, outputHandl

result, err := crypto.Ripemd160(inputBytes)
if err != nil {
if enableEpochsHandler.IsFlagEnabled(vmhost.MaskInternalDependenciesErrorsFlag) {
FailExecution(host, vmhost.ErrRipemd160Hash)
return 1
}

FailExecution(host, err)
return 1
}
Expand All @@ -275,6 +313,7 @@ func (context *VMHooksImpl) VerifyBLS(
sigOffset executor.MemPtr,
) int32 {
crypto := context.GetCryptoContext()
enableEpochsHandler := context.host.EnableEpochsHandler()
metering := context.GetMeteringContext()
metering.StartGasTracing(verifyBLSName)

Expand Down Expand Up @@ -312,6 +351,11 @@ func (context *VMHooksImpl) VerifyBLS(

invalidSigErr := crypto.VerifyBLS(key, message, sig)
if invalidSigErr != nil {
if enableEpochsHandler.IsFlagEnabled(vmhost.MaskInternalDependenciesErrorsFlag) {
context.FailExecution(vmhost.ErrBlsVerify)
return -1
}

context.FailExecution(invalidSigErr)
return -1
}
Expand Down Expand Up @@ -365,6 +409,7 @@ func ManagedVerifyBLSWithHost(
metering := host.Metering()
managedType := host.ManagedTypes()
crypto := host.Crypto()
enableEpochsHandler := host.EnableEpochsHandler()
err := useGasForCryptoVerify(metering, sigVerificationType)
if err != nil && runtime.UseGasBoundedShouldFailExecution() {
FailExecution(host, err)
Expand Down Expand Up @@ -424,6 +469,11 @@ func ManagedVerifyBLSWithHost(
}

if invalidSigErr != nil {
if enableEpochsHandler.IsFlagEnabled(vmhost.MaskInternalDependenciesErrorsFlag) {
FailExecution(host, vmhost.ErrBlsVerify)
return -1
}

FailExecution(host, invalidSigErr)
return -1
}
Expand All @@ -440,6 +490,7 @@ func (context *VMHooksImpl) VerifyEd25519(
sigOffset executor.MemPtr,
) int32 {
crypto := context.GetCryptoContext()
enableEpochsHandler := context.host.EnableEpochsHandler()
metering := context.GetMeteringContext()
metering.StartGasTracing(verifyEd25519Name)

Expand Down Expand Up @@ -477,6 +528,11 @@ func (context *VMHooksImpl) VerifyEd25519(

invalidSigErr := crypto.VerifyEd25519(key, message, sig)
if invalidSigErr != nil {
if enableEpochsHandler.IsFlagEnabled(vmhost.MaskInternalDependenciesErrorsFlag) {
context.FailExecution(vmhost.ErrEd25519Verify)
return -1
}

context.FailExecution(invalidSigErr)
return -1
}
Expand All @@ -500,6 +556,7 @@ func ManagedVerifyEd25519WithHost(
) int32 {
metering := host.Metering()
managedType := host.ManagedTypes()
enableEpochsHandler := host.EnableEpochsHandler()
crypto := host.Crypto()
metering.StartGasTracing(verifyEd25519Name)

Expand Down Expand Up @@ -548,6 +605,11 @@ func ManagedVerifyEd25519WithHost(

invalidSigErr := crypto.VerifyEd25519(keyBytes, msgBytes, sigBytes)
if invalidSigErr != nil {
if enableEpochsHandler.IsFlagEnabled(vmhost.MaskInternalDependenciesErrorsFlag) {
FailExecution(host, vmhost.ErrEd25519Verify)
return -1
}

FailExecution(host, invalidSigErr)
return -1
}
Expand All @@ -566,6 +628,7 @@ func (context *VMHooksImpl) VerifyCustomSecp256k1(
hashType int32,
) int32 {
crypto := context.GetCryptoContext()
enableEpochsHandler := context.host.EnableEpochsHandler()
metering := context.GetMeteringContext()
metering.StartGasTracing(verifyCustomSecp256k1Name)

Expand Down Expand Up @@ -618,6 +681,11 @@ func (context *VMHooksImpl) VerifyCustomSecp256k1(

invalidSigErr := crypto.VerifySecp256k1(key, message, sig, uint8(hashType))
if invalidSigErr != nil {
if enableEpochsHandler.IsFlagEnabled(vmhost.MaskInternalDependenciesErrorsFlag) {
context.FailExecution(vmhost.ErrSecp256k1Verify)
return -1
}

context.FailExecution(invalidSigErr)
return -1
}
Expand Down Expand Up @@ -649,6 +717,7 @@ func ManagedVerifyCustomSecp256k1WithHost(
verifyCryptoFunc string,
) int32 {
runtime := host.Runtime()
enableEpochsHandler := host.EnableEpochsHandler()
metering := host.Metering()
managedType := host.ManagedTypes()
crypto := host.Crypto()
Expand Down Expand Up @@ -704,6 +773,11 @@ func ManagedVerifyCustomSecp256k1WithHost(
}

if invalidSigErr != nil {
if enableEpochsHandler.IsFlagEnabled(vmhost.MaskInternalDependenciesErrorsFlag) {
FailExecution(host, vmhost.ErrSecp256k1Verify)
return -1
}

FailExecution(host, invalidSigErr)
return -1
}
Expand Down
10 changes: 10 additions & 0 deletions vmhost/vmhooks/manBufOps.go
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,7 @@ func (context *VMHooksImpl) MBufferFromSmallIntSigned(mBufferHandle int32, value
// @autogenerate(VMHooks)
func (context *VMHooksImpl) MBufferToBigFloat(mBufferHandle, bigFloatHandle int32) int32 {
managedType := context.GetManagedTypesContext()
enableEpochsHandler := context.host.EnableEpochsHandler()
metering := context.GetMeteringContext()
metering.StartGasTracing(mBufferToBigFloatName)

Expand Down Expand Up @@ -667,6 +668,11 @@ func (context *VMHooksImpl) MBufferToBigFloat(mBufferHandle, bigFloatHandle int3
bigFloat := new(big.Float)
err = bigFloat.GobDecode(managedBuffer)
if err != nil {
if enableEpochsHandler.IsFlagEnabled(vmhost.MaskInternalDependenciesErrorsFlag) {
context.FailExecution(vmhost.ErrBigFloatDecode)
return 1
}

context.FailExecution(err)
return 1
}
Expand All @@ -683,6 +689,7 @@ func (context *VMHooksImpl) MBufferToBigFloat(mBufferHandle, bigFloatHandle int3
// @autogenerate(VMHooks)
func (context *VMHooksImpl) MBufferFromBigFloat(mBufferHandle, bigFloatHandle int32) int32 {
managedType := context.GetManagedTypesContext()
enableEpochsHandler := context.host.EnableEpochsHandler()
metering := context.GetMeteringContext()
metering.StartGasTracing(mBufferFromBigFloatName)

Expand All @@ -701,6 +708,9 @@ func (context *VMHooksImpl) MBufferFromBigFloat(mBufferHandle, bigFloatHandle in

encodedFloat, err := value.GobEncode()
if err != nil {
if enableEpochsHandler.IsFlagEnabled(vmhost.MaskInternalDependenciesErrorsFlag) {
context.FailExecution(vmhost.ErrBigFloatEncode)
}
context.FailExecution(err)
return 1
}
Expand Down