2018-04-06 14:52:10 +00:00
|
|
|
# Nimbus
|
|
|
|
# Copyright (c) 2018 Status Research & Development GmbH
|
|
|
|
# Licensed under either of
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
2018-06-15 09:11:25 +00:00
|
|
|
import
|
|
|
|
stint, strformat, strutils, sequtils, endians, macros, rlp,
|
|
|
|
../../constants, ../../utils/padding
|
2018-01-16 17:05:20 +00:00
|
|
|
|
2018-03-13 14:30:38 +00:00
|
|
|
# some methods based on py-evm utils/numeric
|
|
|
|
|
2018-05-30 16:11:15 +00:00
|
|
|
proc bigEndianToInt*(value: openarray[byte]): UInt256 =
|
|
|
|
if value.len == 32:
|
|
|
|
readUintBE[256](value)
|
|
|
|
else:
|
|
|
|
readUintBE[256](padLeft(@value, 32, 0.byte))
|
2018-01-16 17:05:20 +00:00
|
|
|
|
2018-05-25 10:25:19 +00:00
|
|
|
proc log256*(value: UInt256): Natural =
|
|
|
|
(255 - value.countLeadingZeroBits) div 8 # Compilers optimize to `shr 3`
|
2018-05-16 08:41:34 +00:00
|
|
|
|
2018-05-17 08:08:28 +00:00
|
|
|
proc unsignedToPseudoSigned*(value: UInt256): UInt256 =
|
|
|
|
result = value
|
|
|
|
if value > INT_256_MAX_AS_UINT256:
|
|
|
|
result -= INT_256_MAX_AS_UINT256
|
|
|
|
|
|
|
|
proc pseudoSignedToUnsigned*(value: UInt256): UInt256 =
|
|
|
|
result = value
|
|
|
|
if value > INT_256_MAX_AS_UINT256:
|
|
|
|
result += INT_256_MAX_AS_UINT256
|
2018-01-16 17:05:20 +00:00
|
|
|
|
2018-06-12 15:33:47 +00:00
|
|
|
func ceil32*(value: Natural): Natural {.inline.}=
|
|
|
|
# Round input to the nearest bigger multiple of 32
|
|
|
|
|
|
|
|
result = value
|
2018-01-16 18:42:38 +00:00
|
|
|
|
2018-06-12 15:33:47 +00:00
|
|
|
let remainder = result and 31 # equivalent to modulo 32
|
|
|
|
if remainder != 0:
|
|
|
|
return value + 32 - remainder
|
2018-01-16 17:05:20 +00:00
|
|
|
|
2018-06-12 15:33:47 +00:00
|
|
|
func wordCount*(length: Natural): Natural {.inline.}=
|
|
|
|
# Returns the number of EVM words corresponding to a specific size.
|
|
|
|
# EVM words is rounded up
|
|
|
|
length.ceil32 shr 5 # equivalent to `div 32` (32 = 2^5)
|