Utility Functions For Numeric Operations

Introduction

Numeric operations specific utility functions used in this specification.

Module Contents

Functions

get_sign

Determines the sign of a number.

ceil32

Converts a unsigned integer to the next closest multiple of 32.

is_prime

Checks if number is a prime number.

le_bytes_to_uint32_sequence

Convert little endian byte stream data to a little endian Uint32

le_uint32_sequence_to_bytes

Obtain little endian byte stream from a little endian Uint32 sequence

le_uint32_sequence_to_uint

Obtain Uint from a Uint32 sequence assuming that this sequence is little

Module Details

get_sign

get_sign(value: int)int

Determines the sign of a number.

Parameters

value – The value whose sign is to be determined.

Returns

sign – The sign of the number (-1 or 0 or 1). The return value is based on math signum function.

Return type

int

def get_sign(value: int) -> int:
    if value < 0:
        return -1
    elif value == 0:
        return 0
    else:
        return 1

ceil32

ceil32(value: ethereum.base_types.Uint)ethereum.base_types.Uint

Converts a unsigned integer to the next closest multiple of 32.

Parameters

value – The value whose ceil32 is to be calculated.

Returns

ceil32 – The same value if it’s a perfect multiple of 32 else it returns the smallest multiple of 32 that is greater than value.

Return type

ethereum.base_types.U256

def ceil32(value: Uint) -> Uint:
    ceiling = Uint(32)
    remainder = value % ceiling
    if remainder == Uint(0):
        return value
    else:
        return value + ceiling - remainder

is_prime

is_prime(number: int)bool

Checks if number is a prime number.

Parameters

number – The number to check for primality.

Returns

is_number_prime – Boolean indicating if number is prime or not.

Return type

bool

def is_prime(number: int) -> bool:
    if number <= 1:
        return False

    # number ** 0.5 is faster than math.sqrt(number)
    for x in range(2, int(number**0.5) + 1):
        # Return False if number is divisible by x
        if number % x == 0:
            return False

    return True

le_bytes_to_uint32_sequence

le_bytes_to_uint32_sequence(data: bytes)Tuple[ethereum.base_types.Uint32, Ellipsis]

Convert little endian byte stream data to a little endian Uint32 sequence i.e., the first Uint32 number of the sequence is the least significant Uint32 number.

Parameters

data – The byte stream (little endian) which is to be converted to a Uint32 stream.

Returns

uint32_sequence – Sequence of Uint32 numbers obtained from the little endian byte stream.

Return type

Tuple[Uint32, …]

def le_bytes_to_uint32_sequence(data: bytes) -> Tuple[Uint32, ...]:
    sequence = []
    for i in range(0, len(data), 4):
        sequence.append(Uint32.from_le_bytes(data[i : i + 4]))

    return tuple(sequence)

le_uint32_sequence_to_bytes

le_uint32_sequence_to_bytes(sequence: Sequence[ethereum.base_types.Uint32])bytes

Obtain little endian byte stream from a little endian Uint32 sequence i.e., the first Uint32 number of the sequence is the least significant Uint32 number.

Note - In this conversion, the most significant byte (byte at the end of the little endian stream) may have leading zeroes. This function doesn’t take care of removing these leading zeroes as shown in below example.

>>> le_uint32_sequence_to_bytes([Uint32(8)])
b'\x08\x00\x00\x00'
Parameters

sequence – The Uint32 stream (little endian) which is to be converted to a little endian byte stream.

Returns

result – The byte stream obtained from the little endian Uint32 stream.

Return type

bytes

def le_uint32_sequence_to_bytes(sequence: Sequence[Uint32]) -> bytes:
    result_bytes = b""
    for item in sequence:
        result_bytes += item.to_le_bytes4()

    return result_bytes

le_uint32_sequence_to_uint

le_uint32_sequence_to_uint(sequence: Sequence[ethereum.base_types.Uint32])ethereum.base_types.Uint

Obtain Uint from a Uint32 sequence assuming that this sequence is little endian i.e., the first Uint32 number of the sequence is the least significant Uint32 number.

Parameters

sequence – The Uint32 stream (little endian) which is to be converted to a Uint.

Returns

value – The Uint number obtained from the conversion of the little endian Uint32 stream.

Return type

Uint

def le_uint32_sequence_to_uint(sequence: Sequence[Uint32]) -> Uint:
    sequence_as_bytes = le_uint32_sequence_to_bytes(sequence)
    return Uint.from_le_bytes(sequence_as_bytes)