Skip to content

Commit 0e4d012

Browse files
authored
Merge pull request #897 from multiversx/mask-internal-errors
mask internal libs errors
2 parents 2980159 + ef40054 commit 0e4d012

File tree

5 files changed

+99
-1
lines changed

5 files changed

+99
-1
lines changed

test/features/basic-features/scenarios/crypto_verify_ed25519.scen.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
"expect": {
5656
"out": [],
5757
"status": "10",
58-
"message": "str:invalid signature",
58+
"message": "str:ed25519 verify error",
5959
"logs": "*",
6060
"gas": "*",
6161
"refund": "*"

vmhost/errors.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,30 @@ var ErrInfinityFloatOperation = errors.New("infinity operations are not allowed"
251251
// ErrBigFloatWrongPrecision signals that the precision has a wrong value
252252
var ErrBigFloatWrongPrecision = errors.New("precision of the big float must be 53")
253253

254+
// ErrBigFloatDecode signals that big float parse error
255+
var ErrBigFloatDecode = errors.New("big float decode error")
256+
257+
// ErrBigFloatEncode signals that big float parse error
258+
var ErrBigFloatEncode = errors.New("big float encode error")
259+
260+
// ErrSha256Hash signals a sha256 hash error
261+
var ErrSha256Hash = errors.New("sha256 hash error")
262+
263+
// ErrKeccak256Hash signals a keccak256 hash error
264+
var ErrKeccak256Hash = errors.New("keccak256 hash error")
265+
266+
// ErrRipemd160Hash signals a ripemd160 hash error
267+
var ErrRipemd160Hash = errors.New("ripemd160 hash error")
268+
269+
// ErrBlsVerify signals a bls verify error
270+
var ErrBlsVerify = errors.New("bls verify error")
271+
272+
// ErrEd25519Verify signals a ed25519 verify error
273+
var ErrEd25519Verify = errors.New("ed25519 verify error")
274+
275+
// ErrSecp256k1Verify signals a secp256k1 verify error
276+
var ErrSecp256k1Verify = errors.New("secp256k1 verify error")
277+
254278
// ErrAllOperandsAreEqualToZero signals that all operands are equal to 0
255279
var ErrAllOperandsAreEqualToZero = errors.New("all operands are equal to 0")
256280

vmhost/flags.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,7 @@ const (
1111

1212
// UseGasBoundedShouldFailExecutionFlag defines the flag that activates failing of execution if gas bounded check fails
1313
UseGasBoundedShouldFailExecutionFlag core.EnableEpochFlag = "UseGasBoundedShouldFailExecutionFlag"
14+
15+
// MaskInternalDependenciesErrorsFlag defines the flag that activates masking of internal dependencies errors
16+
MaskInternalDependenciesErrorsFlag core.EnableEpochFlag = "MaskInternalDependenciesErrorsFlag"
1417
)

vmhost/vmhooks/cryptoei.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package vmhooks
22

33
import (
44
"crypto/elliptic"
5+
56
"github.com/multiversx/mx-chain-vm-go/crypto/signing/secp256"
67
"github.com/multiversx/mx-chain-vm-go/executor"
78
"github.com/multiversx/mx-chain-vm-go/math"
@@ -51,6 +52,7 @@ func (context *VMHooksImpl) Sha256(
5152
resultOffset executor.MemPtr) int32 {
5253

5354
crypto := context.GetCryptoContext()
55+
enableEpochsHandler := context.host.EnableEpochsHandler()
5456
metering := context.GetMeteringContext()
5557

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

7072
result, err := crypto.Sha256(data)
7173
if err != nil {
74+
75+
if enableEpochsHandler.IsFlagEnabled(vmhost.MaskInternalDependenciesErrorsFlag) {
76+
err = vmhost.ErrSha256Hash
77+
}
78+
7279
context.FailExecution(err)
7380
return 1
7481
}
@@ -87,6 +94,7 @@ func (context *VMHooksImpl) Sha256(
8794
func (context *VMHooksImpl) ManagedSha256(inputHandle, outputHandle int32) int32 {
8895
managedType := context.GetManagedTypesContext()
8996
crypto := context.GetCryptoContext()
97+
enableEpochsHandler := context.host.EnableEpochsHandler()
9098
metering := context.GetMeteringContext()
9199

92100
err := metering.UseGasBoundedAndAddTracedGas(sha256Name, metering.GasSchedule().CryptoAPICost.SHA256)
@@ -109,6 +117,10 @@ func (context *VMHooksImpl) ManagedSha256(inputHandle, outputHandle int32) int32
109117

110118
resultBytes, err := crypto.Sha256(inputBytes)
111119
if err != nil {
120+
if enableEpochsHandler.IsFlagEnabled(vmhost.MaskInternalDependenciesErrorsFlag) {
121+
err = vmhost.ErrSha256Hash
122+
}
123+
112124
context.FailExecution(err)
113125
return 1
114126
}
@@ -122,6 +134,7 @@ func (context *VMHooksImpl) ManagedSha256(inputHandle, outputHandle int32) int32
122134
// @autogenerate(VMHooks)
123135
func (context *VMHooksImpl) Keccak256(dataOffset executor.MemPtr, length executor.MemLength, resultOffset executor.MemPtr) int32 {
124136
crypto := context.GetCryptoContext()
137+
enableEpochsHandler := context.host.EnableEpochsHandler()
125138
metering := context.GetMeteringContext()
126139

127140
memLoadGas := math.MulUint64(metering.GasSchedule().BaseOperationCost.DataCopyPerByte, uint64(length))
@@ -140,6 +153,10 @@ func (context *VMHooksImpl) Keccak256(dataOffset executor.MemPtr, length executo
140153

141154
result, err := crypto.Keccak256(data)
142155
if err != nil {
156+
if enableEpochsHandler.IsFlagEnabled(vmhost.MaskInternalDependenciesErrorsFlag) {
157+
err = vmhost.ErrKeccak256Hash
158+
}
159+
143160
context.FailExecution(err)
144161
return 1
145162
}
@@ -158,6 +175,7 @@ func (context *VMHooksImpl) Keccak256(dataOffset executor.MemPtr, length executo
158175
func (context *VMHooksImpl) ManagedKeccak256(inputHandle, outputHandle int32) int32 {
159176
managedType := context.GetManagedTypesContext()
160177
crypto := context.GetCryptoContext()
178+
enableEpochsHandler := context.host.EnableEpochsHandler()
161179
metering := context.GetMeteringContext()
162180

163181
err := metering.UseGasBoundedAndAddTracedGas(keccak256Name, metering.GasSchedule().CryptoAPICost.Keccak256)
@@ -180,6 +198,10 @@ func (context *VMHooksImpl) ManagedKeccak256(inputHandle, outputHandle int32) in
180198

181199
resultBytes, err := crypto.Keccak256(inputBytes)
182200
if err != nil {
201+
if enableEpochsHandler.IsFlagEnabled(vmhost.MaskInternalDependenciesErrorsFlag) {
202+
err = vmhost.ErrKeccak256Hash
203+
}
204+
183205
context.FailExecution(err)
184206
return 1
185207
}
@@ -193,6 +215,7 @@ func (context *VMHooksImpl) ManagedKeccak256(inputHandle, outputHandle int32) in
193215
// @autogenerate(VMHooks)
194216
func (context *VMHooksImpl) Ripemd160(dataOffset executor.MemPtr, length executor.MemLength, resultOffset executor.MemPtr) int32 {
195217
crypto := context.GetCryptoContext()
218+
enableEpochsHandler := context.host.EnableEpochsHandler()
196219
metering := context.GetMeteringContext()
197220

198221
memLoadGas := math.MulUint64(metering.GasSchedule().BaseOperationCost.DataCopyPerByte, uint64(length))
@@ -211,6 +234,10 @@ func (context *VMHooksImpl) Ripemd160(dataOffset executor.MemPtr, length executo
211234

212235
result, err := crypto.Ripemd160(data)
213236
if err != nil {
237+
if enableEpochsHandler.IsFlagEnabled(vmhost.MaskInternalDependenciesErrorsFlag) {
238+
err = vmhost.ErrRipemd160Hash
239+
}
240+
214241
context.FailExecution(err)
215242
return 1
216243
}
@@ -236,6 +263,7 @@ func ManagedRipemd160WithHost(host vmhost.VMHost, inputHandle int32, outputHandl
236263
metering := host.Metering()
237264
managedType := host.ManagedTypes()
238265
crypto := host.Crypto()
266+
enableEpochsHandler := host.EnableEpochsHandler()
239267

240268
err := metering.UseGasBoundedAndAddTracedGas(ripemd160Name, metering.GasSchedule().CryptoAPICost.Ripemd160)
241269
if err != nil {
@@ -257,6 +285,10 @@ func ManagedRipemd160WithHost(host vmhost.VMHost, inputHandle int32, outputHandl
257285

258286
result, err := crypto.Ripemd160(inputBytes)
259287
if err != nil {
288+
if enableEpochsHandler.IsFlagEnabled(vmhost.MaskInternalDependenciesErrorsFlag) {
289+
err = vmhost.ErrRipemd160Hash
290+
}
291+
260292
FailExecution(host, err)
261293
return 1
262294
}
@@ -275,6 +307,7 @@ func (context *VMHooksImpl) VerifyBLS(
275307
sigOffset executor.MemPtr,
276308
) int32 {
277309
crypto := context.GetCryptoContext()
310+
enableEpochsHandler := context.host.EnableEpochsHandler()
278311
metering := context.GetMeteringContext()
279312
metering.StartGasTracing(verifyBLSName)
280313

@@ -312,6 +345,10 @@ func (context *VMHooksImpl) VerifyBLS(
312345

313346
invalidSigErr := crypto.VerifyBLS(key, message, sig)
314347
if invalidSigErr != nil {
348+
if enableEpochsHandler.IsFlagEnabled(vmhost.MaskInternalDependenciesErrorsFlag) {
349+
invalidSigErr = vmhost.ErrBlsVerify
350+
}
351+
315352
context.FailExecution(invalidSigErr)
316353
return -1
317354
}
@@ -365,6 +402,7 @@ func ManagedVerifyBLSWithHost(
365402
metering := host.Metering()
366403
managedType := host.ManagedTypes()
367404
crypto := host.Crypto()
405+
enableEpochsHandler := host.EnableEpochsHandler()
368406
err := useGasForCryptoVerify(metering, sigVerificationType)
369407
if err != nil && runtime.UseGasBoundedShouldFailExecution() {
370408
FailExecution(host, err)
@@ -424,6 +462,10 @@ func ManagedVerifyBLSWithHost(
424462
}
425463

426464
if invalidSigErr != nil {
465+
if enableEpochsHandler.IsFlagEnabled(vmhost.MaskInternalDependenciesErrorsFlag) {
466+
invalidSigErr = vmhost.ErrBlsVerify
467+
}
468+
427469
FailExecution(host, invalidSigErr)
428470
return -1
429471
}
@@ -440,6 +482,7 @@ func (context *VMHooksImpl) VerifyEd25519(
440482
sigOffset executor.MemPtr,
441483
) int32 {
442484
crypto := context.GetCryptoContext()
485+
enableEpochsHandler := context.host.EnableEpochsHandler()
443486
metering := context.GetMeteringContext()
444487
metering.StartGasTracing(verifyEd25519Name)
445488

@@ -477,6 +520,10 @@ func (context *VMHooksImpl) VerifyEd25519(
477520

478521
invalidSigErr := crypto.VerifyEd25519(key, message, sig)
479522
if invalidSigErr != nil {
523+
if enableEpochsHandler.IsFlagEnabled(vmhost.MaskInternalDependenciesErrorsFlag) {
524+
invalidSigErr = vmhost.ErrEd25519Verify
525+
}
526+
480527
context.FailExecution(invalidSigErr)
481528
return -1
482529
}
@@ -500,6 +547,7 @@ func ManagedVerifyEd25519WithHost(
500547
) int32 {
501548
metering := host.Metering()
502549
managedType := host.ManagedTypes()
550+
enableEpochsHandler := host.EnableEpochsHandler()
503551
crypto := host.Crypto()
504552
metering.StartGasTracing(verifyEd25519Name)
505553

@@ -548,6 +596,10 @@ func ManagedVerifyEd25519WithHost(
548596

549597
invalidSigErr := crypto.VerifyEd25519(keyBytes, msgBytes, sigBytes)
550598
if invalidSigErr != nil {
599+
if enableEpochsHandler.IsFlagEnabled(vmhost.MaskInternalDependenciesErrorsFlag) {
600+
invalidSigErr = vmhost.ErrEd25519Verify
601+
}
602+
551603
FailExecution(host, invalidSigErr)
552604
return -1
553605
}
@@ -566,6 +618,7 @@ func (context *VMHooksImpl) VerifyCustomSecp256k1(
566618
hashType int32,
567619
) int32 {
568620
crypto := context.GetCryptoContext()
621+
enableEpochsHandler := context.host.EnableEpochsHandler()
569622
metering := context.GetMeteringContext()
570623
metering.StartGasTracing(verifyCustomSecp256k1Name)
571624

@@ -618,6 +671,10 @@ func (context *VMHooksImpl) VerifyCustomSecp256k1(
618671

619672
invalidSigErr := crypto.VerifySecp256k1(key, message, sig, uint8(hashType))
620673
if invalidSigErr != nil {
674+
if enableEpochsHandler.IsFlagEnabled(vmhost.MaskInternalDependenciesErrorsFlag) {
675+
invalidSigErr = vmhost.ErrSecp256k1Verify
676+
}
677+
621678
context.FailExecution(invalidSigErr)
622679
return -1
623680
}
@@ -649,6 +706,7 @@ func ManagedVerifyCustomSecp256k1WithHost(
649706
verifyCryptoFunc string,
650707
) int32 {
651708
runtime := host.Runtime()
709+
enableEpochsHandler := host.EnableEpochsHandler()
652710
metering := host.Metering()
653711
managedType := host.ManagedTypes()
654712
crypto := host.Crypto()
@@ -704,6 +762,10 @@ func ManagedVerifyCustomSecp256k1WithHost(
704762
}
705763

706764
if invalidSigErr != nil {
765+
if enableEpochsHandler.IsFlagEnabled(vmhost.MaskInternalDependenciesErrorsFlag) {
766+
invalidSigErr = vmhost.ErrSecp256k1Verify
767+
}
768+
707769
FailExecution(host, invalidSigErr)
708770
return -1
709771
}

vmhost/vmhooks/manBufOps.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,7 @@ func (context *VMHooksImpl) MBufferFromSmallIntSigned(mBufferHandle int32, value
631631
// @autogenerate(VMHooks)
632632
func (context *VMHooksImpl) MBufferToBigFloat(mBufferHandle, bigFloatHandle int32) int32 {
633633
managedType := context.GetManagedTypesContext()
634+
enableEpochsHandler := context.host.EnableEpochsHandler()
634635
metering := context.GetMeteringContext()
635636
metering.StartGasTracing(mBufferToBigFloatName)
636637

@@ -667,6 +668,10 @@ func (context *VMHooksImpl) MBufferToBigFloat(mBufferHandle, bigFloatHandle int3
667668
bigFloat := new(big.Float)
668669
err = bigFloat.GobDecode(managedBuffer)
669670
if err != nil {
671+
if enableEpochsHandler.IsFlagEnabled(vmhost.MaskInternalDependenciesErrorsFlag) {
672+
err = vmhost.ErrBigFloatDecode
673+
}
674+
670675
context.FailExecution(err)
671676
return 1
672677
}
@@ -683,6 +688,7 @@ func (context *VMHooksImpl) MBufferToBigFloat(mBufferHandle, bigFloatHandle int3
683688
// @autogenerate(VMHooks)
684689
func (context *VMHooksImpl) MBufferFromBigFloat(mBufferHandle, bigFloatHandle int32) int32 {
685690
managedType := context.GetManagedTypesContext()
691+
enableEpochsHandler := context.host.EnableEpochsHandler()
686692
metering := context.GetMeteringContext()
687693
metering.StartGasTracing(mBufferFromBigFloatName)
688694

@@ -701,6 +707,9 @@ func (context *VMHooksImpl) MBufferFromBigFloat(mBufferHandle, bigFloatHandle in
701707

702708
encodedFloat, err := value.GobEncode()
703709
if err != nil {
710+
if enableEpochsHandler.IsFlagEnabled(vmhost.MaskInternalDependenciesErrorsFlag) {
711+
err = vmhost.ErrBigFloatEncode
712+
}
704713
context.FailExecution(err)
705714
return 1
706715
}

0 commit comments

Comments
 (0)