fix modexp gasFee

This commit is contained in:
andri lim 2019-05-08 23:18:06 +07:00 committed by zah
parent cd7143e9af
commit b7a1431c33
3 changed files with 36 additions and 15 deletions

View File

@ -71,7 +71,7 @@ proc rangeToPadded*[T: StUint](x: openarray[byte], first, last: int, toLen = 0):
temp[0..hi-lo] = x.toOpenArray(lo, hi) temp[0..hi-lo] = x.toOpenArray(lo, hi)
result = T.fromBytesBE( result = T.fromBytesBE(
temp, temp,
allowPadding = true allowPadding = false
) )
else: else:
result = T.fromBytesBE( result = T.fromBytesBE(
@ -79,6 +79,25 @@ proc rangeToPadded*[T: StUint](x: openarray[byte], first, last: int, toLen = 0):
allowPadding = true allowPadding = true
) )
proc rangeToPadded2*[T: StUint](x: openarray[byte], first, last: int, toLen = 0): T =
## Convert take a slice of a sequence of bytes interpret it as the big endian
## representation of an Uint256. Use padding for sequence shorter than 32 bytes
## including 0-length sequences.
const N = T.bits div 8
let lo = max(0, first)
let hi = min(min(x.high, last), (lo+N)-1)
if not(lo <= hi):
return # 0
var temp: array[N, byte]
temp[0..hi-lo] = x.toOpenArray(lo, hi)
result = T.fromBytesBE(
temp.toOpenArray(0, toLen-1),
allowPadding = true
)
# calculates the memory size required for a step # calculates the memory size required for a step
func calcMemSize*(offset, length: int): int {.inline.} = func calcMemSize*(offset, length: int): int {.inline.} =
if length.isZero: return 0 if length.isZero: return 0

View File

@ -323,17 +323,19 @@ proc selectVM(computation: BaseComputation, fork: Fork) {.gcsafe.} =
computation.constantinopleVM() computation.constantinopleVM()
proc executeOpcodes(computation: BaseComputation) = proc executeOpcodes(computation: BaseComputation) =
try:
let fork = computation.getFork let fork = computation.getFork
block:
if computation.execPrecompiles(fork): if computation.execPrecompiles(fork):
computation.nextProc() break
return
try:
computation.selectVM(fork) computation.selectVM(fork)
except: except:
let msg = getCurrentExceptionMsg() let msg = getCurrentExceptionMsg()
computation.setError(&"Opcode Dispatch Error msg={msg}, depth={computation.msg.depth}", true) computation.setError(&"Opcode Dispatch Error msg={msg}, depth={computation.msg.depth}", true)
computation.nextProc()
computation.nextProc()
if computation.isError(): if computation.isError():
if computation.tracingEnabled: computation.traceError() if computation.tracingEnabled: computation.traceError()
debug "executeOpcodes error", msg=computation.error.info debug "executeOpcodes error", msg=computation.error.info

View File

@ -150,11 +150,11 @@ proc modExpInternal(computation: BaseComputation, base_len, exp_len, mod_len: in
let adj_exp_len = block: let adj_exp_len = block:
# TODO deal with negative length # TODO deal with negative length
let first32 = rawMsg.rangeToPadded2[:Uint256](96 + base_len, 95 + base_len + exp_len, min(exp_len, 32))
if exp_len <= 32: if exp_len <= 32:
if exp.isZero(): 0 if first32.isZero(): 0
else: log2(exp) # highest-bit in exponent else: log2(first32) # highest-bit in exponent
else: else:
let first32 = rawMsg.rangeToPadded[:Uint256](96 + base_len, 95 + base_len + exp_len)
# TODO: `modexpRandomInput.json` require Uint256 arithmetic for this code below # TODO: `modexpRandomInput.json` require Uint256 arithmetic for this code below
if not first32.isZero: if not first32.isZero:
8 * (exp_len - 32) + first32.log2 8 * (exp_len - 32) + first32.log2