From 077fbe6b731d85048e98edb15711caba9e957957 Mon Sep 17 00:00:00 2001 From: andri lim Date: Thu, 9 May 2019 08:56:10 +0700 Subject: [PATCH] modexp cleanup --- GeneralStateTests.md | 6 +++--- nimbus/vm/precompiles.nim | 14 ++++++++------ tests/test_generalstate_failing.nim | 1 - 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/GeneralStateTests.md b/GeneralStateTests.md index b58af3d4e..caa48fcf1 100644 --- a/GeneralStateTests.md +++ b/GeneralStateTests.md @@ -394,7 +394,7 @@ OK: 3/3 Fail: 0/3 Skip: 0/3 + call_then_create2_successful_then_returndatasize.json OK + create2InitCodes.json OK + create2SmartInitCode.json OK - create2callPrecompiles.json Skip ++ create2callPrecompiles.json OK + create2checkFieldsInInitcode.json OK + create2collisionBalance.json OK + create2collisionCode.json OK @@ -413,7 +413,7 @@ OK: 3/3 Fail: 0/3 Skip: 0/3 + returndatacopy_following_successful_create.json OK + returndatasize_following_successful_create.json OK ``` -OK: 41/44 Fail: 0/44 Skip: 3/44 +OK: 42/44 Fail: 0/44 Skip: 2/44 ## stCreateTest ```diff + CREATE_AcreateB_BSuicide_BStore.json OK @@ -2645,4 +2645,4 @@ OK: 133/133 Fail: 0/133 Skip: 0/133 OK: 130/130 Fail: 0/130 Skip: 0/130 ---TOTAL--- -OK: 2342/2447 Fail: 0/2447 Skip: 105/2447 +OK: 2343/2447 Fail: 0/2447 Skip: 104/2447 diff --git a/nimbus/vm/precompiles.nim b/nimbus/vm/precompiles.nim index 1508e6a7b..439ac11f5 100644 --- a/nimbus/vm/precompiles.nim +++ b/nimbus/vm/precompiles.nim @@ -133,10 +133,6 @@ proc identity*(computation: BaseComputation) = proc modExpInternal(computation: BaseComputation, base_len, exp_len, mod_len: int, T: type StUint) = template rawMsg: untyped {.dirty.} = computation.msg.data - let - base = rawMsg.rangeToPadded[:T](96, 95 + base_len, base_len) - exp = rawMsg.rangeToPadded[:T](96 + base_len, 95 + base_len + exp_len, exp_len) - modulo = rawMsg.rangeToPadded[:T](96 + base_len + exp_len, 95 + base_len + exp_len + mod_len, mod_len) block: # Gas cost func gasModExp_f(x: Natural): int = @@ -170,6 +166,11 @@ proc modExpInternal(computation: BaseComputation, base_len, exp_len, mod_len: in computation.gasMeter.consumeGas(gasFee, reason="ModExp Precompile") block: # Processing + let + base = rawMsg.rangeToPadded[:T](96, 95 + base_len, base_len) + exp = rawMsg.rangeToPadded[:T](96 + base_len, 95 + base_len + exp_len, exp_len) + modulo = rawMsg.rangeToPadded[:T](96 + base_len + exp_len, 95 + base_len + exp_len + mod_len, mod_len) + # TODO: specs mentions that we should return in "M" format # i.e. if Base and exp are uint512 and Modulo an uint256 # we should return a 256-bit big-endian byte array @@ -187,11 +188,11 @@ proc modExpInternal(computation: BaseComputation, base_len, exp_len, mod_len: in # If m == 0: EVM returns 0. # If m == 1: we can shortcut that to 0 as well zero() - elif exp.isZero(): + elif exp.isZero(): # If 0^0: EVM returns 1 # For all x != 0, x^0 == 1 as well one() - else: + else: powmod(base, exp, modulo).toByteArrayBE # maximum output len is the same as mod_len @@ -209,6 +210,7 @@ proc modExp*(computation: BaseComputation) = # Parsing the data template rawMsg: untyped {.dirty.} = computation.msg.data + let # lengths Base, Exponent, Modulus base_len = rawMsg.rangeToPadded[:Uint256](0, 31).safeInt exp_len = rawMsg.rangeToPadded[:Uint256](32, 63).safeInt diff --git a/tests/test_generalstate_failing.nim b/tests/test_generalstate_failing.nim index bbd00c0dc..b6991caf5 100644 --- a/tests/test_generalstate_failing.nim +++ b/tests/test_generalstate_failing.nim @@ -14,7 +14,6 @@ func allowedFailingGeneralStateTest*(folder, name: string): bool = let allowedFailingGeneralStateTests = @[ "modexp.json", - "create2callPrecompiles.json", "create2noCash.json", ] result = name in allowedFailingGeneralStateTests