ethereum_types.numeric
Numeric types (mostly integers.)
_BytesLike¶
| 26 | _BytesLike: TypeAlias = Union[bytes, bytearray, memoryview] |
|---|
_max_value ¶
Unsigned ¶
Base of integer types.
| 35 | @mypyc_attr(acyclic=True) |
|---|
class Unsigned:
__slots__¶
| 41 | __slots__ = ("_number",) |
|---|
_number¶
| 42 | _number: Final[int] |
|---|
__init__ ¶
__complex__ ¶
def __complex__(self) -> complex:
| 51 | return complex(self._number) |
|---|
__bool__ ¶
def __bool__(self) -> bool:
| 54 | return bool(self._number) |
|---|
real ¶
imag ¶
conjugate ¶
def conjugate(self) -> Self:
| 65 | return self |
|---|
__float__ ¶
def __float__(self) -> float:
| 68 | return float(self._number) |
|---|
numerator ¶
denominator ¶
__index__ ¶
def __index__(self) -> int:
| 79 | return self._number |
|---|
_in_range ¶
__abs__ ¶
def __abs__(self) -> Self:
| 86 | return type(self)(self) |
|---|
__radd__ ¶
def __radd__(self, left: Self) -> Self:
| 89 | return self.__add__(left) |
|---|
__add__ ¶
__iadd__ ¶
__sub__ ¶
__rsub__ ¶
__isub__ ¶
__mul__ ¶
__rmul__ ¶
def __rmul__(self, left: Self) -> Self:
| 138 | return self.__mul__(left) |
|---|
__imul__ ¶
__truediv__ ¶
__rtruediv__ ¶
__floordiv__ ¶
__rfloordiv__ ¶
__ifloordiv__ ¶
__mod__ ¶
__rmod__ ¶
__imod__ ¶
__divmod__ ¶
__rdivmod__ ¶
def __rdivmod__(self, left: Self) -> Union[Tuple[Self, Self], NotImplementedType]:
| 212 | class_ = type(self) |
|---|---|
| 213 | if not isinstance(left, class_): |
| 214 | # No idea why mypy is assuming this is an Any... |
| 215 | return NotImplemented # type: ignore[no-any-return] |
| 216 | |
| 217 | result = self._number.__rdivmod__(left._number) |
| 218 | return ( |
| 219 | class_(result[0]), |
| 220 | class_(result[1]), |
| 221 | ) |
__pow__ ¶
def __pow__(self, right: Self, modulo: Optional[Self]) -> Self:
| 224 | class_ = type(self) |
|---|---|
| 225 | modulo_int = None |
| 226 | if modulo is not None: |
| 227 | if not isinstance(modulo, class_): |
| 228 | return NotImplemented |
| 229 | modulo_int = modulo._number |
| 230 | |
| 231 | if not isinstance(right, class_): |
| 232 | return NotImplemented |
| 233 | |
| 234 | return class_(self._number.__pow__(right._number, modulo_int)) |
__rpow__ ¶
__ipow__ ¶
def __ipow__(self, right: Self, modulo: Optional[Self]) -> Self:
| 244 | class_ = type(self) |
|---|---|
| 245 | modulo_int = None |
| 246 | if modulo is not None: |
| 247 | if not isinstance(modulo, class_): |
| 248 | raise TypeError |
| 249 | modulo_int = modulo._number |
| 250 | |
| 251 | if not isinstance(right, class_): |
| 252 | return NotImplemented |
| 253 | |
| 254 | return class_(self._number.__pow__(right._number, modulo_int)) |
__xor__ ¶
__rxor__ ¶
__ixor__ ¶
__and__ ¶
__rand__ ¶
__or__ ¶
__ror__ ¶
__neg__ ¶
def __neg__(self) -> int:
| 306 | return -self._number |
|---|
__pos__ ¶
def __pos__(self) -> Self:
| 309 | return type(self)(self._number) |
|---|
__invert__ ¶
__floor__ ¶
def __floor__(self) -> Self:
| 316 | return type(self)(self) |
|---|
__ceil__ ¶
def __ceil__(self) -> Self:
| 319 | return type(self)(self) |
|---|
__int__ ¶
def __int__(self) -> int:
| 322 | return self._number |
|---|
__eq__ ¶
| 324 | @override |
|---|
def __eq__(self, other: object) -> bool:
| 326 | # Unlike the other comparison dunder methods (eg. `__lt__`, `__ge__`, |
|---|---|
| 327 | # etc.), `__eq__` is expected to work with any object, so mypy doesn't |
| 328 | # detect comparisons between `Uint` and `int` as errors. Instead of |
| 329 | # throwing a `TypeError` at runtime, we try to behave sanely and |
| 330 | # soundly by converting `other` to an integer if possible, then |
| 331 | # comparing. |
| 332 | if isinstance(other, Unsigned): |
| 333 | return self._number == other._number |
| 334 | elif isinstance(other, SupportsInt): |
| 335 | other_int = int(other) |
| 336 | if other != other_int: |
| 337 | # If `other` doesn't equal `int(other)`, `self` definitely |
| 338 | # doesn't equal `other` since `self` has to be an integer. |
| 339 | return False |
| 340 | return self._number == other_int |
| 341 | return NotImplemented |
__le__ ¶
__ge__ ¶
__lt__ ¶
__gt__ ¶
__round__ ¶
def __round__(self, ndigits: Optional[int]) -> Self:
| 372 | return type(self)(self) |
|---|
__trunc__ ¶
def __trunc__(self) -> Self:
| 375 | return type(self)(self) |
|---|
__rshift__ ¶
__rrshift__ ¶
__lshift__ ¶
__rlshift__ ¶
__hash__ ¶
__repr__ ¶
__str__ ¶
to_be_bytes64 ¶
Converts this unsigned integer into its big endian representation with exactly 64 bytes.
to_be_bytes32 ¶
Converts this unsigned integer into its big endian representation with exactly 32 bytes.
to_bytes1 ¶
Converts this unsigned integer into a byte sequence with exactly 1 bytes.
to_le_bytes4 ¶
Converts this unsigned integer into its little endian representation, with exactly 4 bytes.
to_be_bytes4 ¶
Converts this unsigned integer into its big endian representation, with exactly 4 bytes.
to_le_bytes8 ¶
Converts this fixed sized unsigned integer into its little endian representation, with exactly 8 bytes.
to_be_bytes8 ¶
Converts this unsigned integer into its big endian representation, with exactly 8 bytes.
to_bytes ¶
Return an array of bytes representing an integer.
to_be_bytes ¶
Converts this unsigned integer into its big endian representation, without padding.
to_le_bytes ¶
Converts this unsigned integer into its little endian representation, without padding.
to_le_bytes32 ¶
Converts this unsigned integer into its little endian representation with exactly 32 bytes.
to_le_bytes64 ¶
Converts this unsigned integer into its little endian representation with exactly 64 bytes.
bit_length ¶
Minimum number of bits required to represent this number in binary.
Uint ¶
Unsigned integer of arbitrary size.
| 516 | @mypyc_attr(acyclic=True) |
|---|
class Uint:
from_be_bytes ¶
Converts a sequence of bytes into an arbitrarily sized unsigned integer from its big endian representation.
| 522 | @classmethod |
|---|
def from_be_bytes(cls: Type[Self], buffer: _BytesLike) -> Self:
| 524 | <snip> |
|---|---|
| 528 | return cls(int.from_bytes(buffer, "big")) |
from_le_bytes ¶
Converts a sequence of bytes into an arbitrarily sized unsigned integer from its little endian representation.
| 530 | @classmethod |
|---|
def from_le_bytes(cls: Type[Self], buffer: _BytesLike) -> Self:
| 532 | <snip> |
|---|---|
| 536 | return cls(int.from_bytes(buffer, "little")) |
_in_range ¶
ulen ¶
Return the number of items in a container, as a Uint.
FixedUnsigned ¶
Superclass for fixed size unsigned integers. Not intended to be used directly, but rather to be subclassed.
| 553 | @mypyc_attr(acyclic=True) |
|---|
class FixedUnsigned:
MAX_VALUE¶
Largest value that can be represented by this integer type.
| 560 | MAX_VALUE: ClassVar[Self] |
|---|
from_be_bytes ¶
Converts a sequence of bytes into a fixed sized unsigned integer from its big endian representation.
| 565 | @classmethod |
|---|
def from_be_bytes(cls: Type[Self], buffer: _BytesLike) -> Self:
| 567 | <snip> |
|---|---|
| 571 | bits = cls.MAX_VALUE._number.bit_length() |
| 572 | byte_count = (bits + 7) // 8 |
| 573 | if len(buffer) > byte_count: |
| 574 | message = f"expected at most {byte_count} but got {len(buffer)}" |
| 575 | raise ValueError(message) |
| 576 | |
| 577 | return cls(int.from_bytes(buffer, "big")) |
from_le_bytes ¶
Converts a sequence of bytes into a fixed sized unsigned integer from its little endian representation.
| 579 | @classmethod |
|---|
def from_le_bytes(cls: Type[Self], buffer: _BytesLike) -> Self:
| 581 | <snip> |
|---|---|
| 585 | bits = cls.MAX_VALUE._number.bit_length() |
| 586 | byte_count = (bits + 7) // 8 |
| 587 | if len(buffer) > byte_count: |
| 588 | message = f"expected at most {byte_count} but got {len(buffer)}" |
| 589 | raise ValueError(message) |
| 590 | |
| 591 | return cls(int.from_bytes(buffer, "little")) |
from_signed ¶
Creates an unsigned integer representing value using two's
complement.
| 593 | @classmethod |
|---|
_in_range ¶
wrapping_add ¶
Return a new instance containing self + right (mod N).
wrapping_sub ¶
Return a new instance containing self - right (mod N).
wrapping_mul ¶
Return a new instance containing self * right (mod N).
wrapping_pow ¶
Return a new instance containing self ** right (mod modulo).
If omitted, modulo defaults to Uint(self.MAX_VALUE) + 1.
def wrapping_pow(self, right: Self, modulo: Optional[Self]) -> Self:
| 648 | <snip> |
|---|---|
| 653 | class_ = type(self) |
| 654 | modulo_int = None |
| 655 | if modulo is not None: |
| 656 | if not isinstance(modulo, class_): |
| 657 | raise TypeError |
| 658 | modulo_int = modulo._number |
| 659 | |
| 660 | if not isinstance(right, class_): |
| 661 | raise TypeError |
| 662 | |
| 663 | # This is a fast way of ensuring that the result is < (2 ** 256) |
| 664 | return class_( |
| 665 | self._number.__pow__(right._number, modulo_int) |
| 666 | & self.MAX_VALUE._number |
| 667 | ) |
__invert__ ¶
to_signed ¶
Decodes a signed integer from its two's complement representation.
def to_signed(self) -> int:
| 676 | <snip> |
|---|---|
| 679 | bits = self.MAX_VALUE._number.bit_length() |
| 680 | bits = 8 * ((bits + 7) // 8) |
| 681 | if self._number.bit_length() < bits: |
| 682 | # This means that the sign bit is 0 |
| 683 | return int(self) |
| 684 | |
| 685 | # -1 * (2's complement of value) |
| 686 | return int(self) - (self.MAX_VALUE._number + 1) |
U256 ¶
Unsigned integer, which can represent 0 to 2 ** 256 - 1, inclusive.
| 689 | @mypyc_attr(acyclic=True) |
|---|
class U256:
Largest value that can be represented by this integer type.
| 695 | MAX_VALUE: ClassVar["U256"] |
|---|
_U256 ¶
| 704 | @mypyc_attr(acyclic=True) |
|---|
class _U256:
_in_range ¶
U256.MAX_VALUE
| 711 | U256.MAX_VALUE = _U256(_max_value(256)) |
|---|
U256.MAX_VALUE
| 712 | U256.MAX_VALUE = U256(_max_value(256)) |
|---|
U8 ¶
Unsigned positive integer, which can represent 0 to 2 ** 8 - 1,
inclusive.
| 715 | @mypyc_attr(acyclic=True) |
|---|
class U8:
Largest value that can be represented by this integer type.
| 722 | MAX_VALUE: ClassVar["U8"] |
|---|
_U8 ¶
| 731 | @mypyc_attr(acyclic=True) |
|---|
class _U8:
_in_range ¶
U8.MAX_VALUE
| 738 | U8.MAX_VALUE = _U8(_max_value(8)) |
|---|
U8.MAX_VALUE
| 739 | U8.MAX_VALUE = U8(_max_value(8)) |
|---|
U16 ¶
Unsigned positive integer, which can represent 0 to 2 ** 16 - 1,
inclusive.
| 742 | @mypyc_attr(acyclic=True) |
|---|
class U16:
Largest value that can be represented by this integer type.
| 749 | MAX_VALUE: ClassVar["U16"] |
|---|
_U16 ¶
| 758 | @mypyc_attr(acyclic=True) |
|---|
class _U16:
_in_range ¶
U16.MAX_VALUE
| 765 | U16.MAX_VALUE = _U16(_max_value(16)) |
|---|
U16.MAX_VALUE
| 766 | U16.MAX_VALUE = U16(_max_value(16)) |
|---|
U32 ¶
Unsigned positive integer, which can represent 0 to 2 ** 32 - 1,
inclusive.
| 769 | @mypyc_attr(acyclic=True) |
|---|
class U32:
Largest value that can be represented by this integer type.
| 776 | MAX_VALUE: ClassVar["U32"] |
|---|
_U32 ¶
| 785 | @mypyc_attr(acyclic=True) |
|---|
class _U32:
_in_range ¶
U32.MAX_VALUE
| 792 | U32.MAX_VALUE = _U32(_max_value(32)) |
|---|
U32.MAX_VALUE
| 793 | U32.MAX_VALUE = U32(_max_value(32)) |
|---|
U64 ¶
Unsigned positive integer, which can represent 0 to 2 ** 64 - 1,
inclusive.
| 796 | @mypyc_attr(acyclic=True) |
|---|
class U64:
Largest value that can be represented by this integer type.
| 803 | MAX_VALUE: ClassVar["U64"] |
|---|
_U64 ¶
| 812 | @mypyc_attr(acyclic=True) |
|---|
class _U64:
_in_range ¶
U64.MAX_VALUE
| 819 | U64.MAX_VALUE = _U64(_max_value(64)) |
|---|
U64.MAX_VALUE
| 820 | U64.MAX_VALUE = U64(_max_value(64)) |
|---|