Safe Arithmetic for U256 Integer Type

Introduction

Safe arithmetic utility functions for U256 integer type.

Module Contents

Functions

u256_safe_add

Adds together the given sequence of numbers. If the total sum of the

u256_safe_multiply

Multiplies together the given sequence of numbers. If the net product of

Module Details

u256_safe_add

u256_safe_add(*numbers: Union[ethereum.base_types.U256, ethereum.base_types.Uint], exception_type: Optional[Type[BaseException]] = None)ethereum.base_types.U256

Adds together the given sequence of numbers. If the total sum of the numbers exceeds U256.MAX_VALUE then an exception is raised. If exception_type = None then the exception raised defaults to the one raised by U256 when U256.value > U256.MAX_VALUE else exception_type is raised.

Parameters
  • numbers – The sequence of numbers that need to be added together.

  • exception_type – The exception that needs to be raised if the sum of the numbers exceeds U256.MAX_VALUE.

Returns

result – The sum of the given sequence of numbers if the total is less than U256.MAX_VALUE else an exception is raised. If exception_type = None then the exception raised defaults to the one raised by U256 when U256.value > U256.MAX_VALUE else exception_type is raised.

Return type

ethereum.base_types.U256

def u256_safe_add(
    *numbers: Union[U256, Uint],
    exception_type: Optional[Type[BaseException]] = None
) -> U256:
    try:
        return U256(sum(numbers))
    except ValueError as e:
        if exception_type:
            raise exception_type from e
        else:
            raise e

u256_safe_multiply

u256_safe_multiply(*numbers: Union[ethereum.base_types.U256, ethereum.base_types.Uint], exception_type: Optional[Type[BaseException]] = None)ethereum.base_types.U256

Multiplies together the given sequence of numbers. If the net product of the numbers exceeds U256.MAX_VALUE then an exception is raised. If exception_type = None then the exception raised defaults to the one raised by U256 when U256.value > U256.MAX_VALUE else exception_type is raised.

Parameters
  • numbers – The sequence of numbers that need to be multiplies together.

  • exception_type – The exception that needs to be raised if the sum of the numbers exceeds U256.MAX_VALUE.

Returns

result – The multiplication product of the given sequence of numbers if the net product is less than U256.MAX_VALUE else an exception is raised. If exception_type = None then the exception raised defaults to the one raised by U256 when U256.value > U256.MAX_VALUE else exception_type is raised.

Return type

ethereum.base_types.U256

def u256_safe_multiply(
    *numbers: Union[U256, Uint],
    exception_type: Optional[Type[BaseException]] = None
) -> U256:
    result = numbers[0]
    try:
        for number in numbers[1:]:
            result *= number
        return U256(result)
    except ValueError as e:
        if exception_type:
            raise exception_type from e
        else:
            raise e