@@ -2,6 +2,7 @@ package vmhooks
22
33import (
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(
8794func (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)
123135func (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
158175func (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)
194216func (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 }
0 commit comments