Template Class arith_env_t#

Class Documentation#

template<class params>
class arith_env_t#

The arithmetic environment class is a wrapper around the CGBN library. It provides a context, environment, and instance for the CGBN library. It also provides some utility functions for converting between CGBN and other types.

Public Types

typedef cgbn_context_t<params::TPI, params> context_t#

The CGBN context type. This is a template type that takes the number of threads per instance and the parameters class as template parameters.

typedef cgbn_env_t<context_t, params::BITS> env_t#

The CGBN environment type. This is a template type that takes the context type as a template parameter. It provides the CGBN functions.

typedef env_t::cgbn_t bn_t#

The CGBN base type for the given number of bit in environment.

typedef env_t::cgbn_wide_t bn_wide_t#

The CGBN wide type with double the given number of bits in environment.

typedef cgbn_mem_t<params::BITS> evm_word_t#

The EVM word type. also use for store CGBN base type.

Public Functions

inline __device__ __forceinline__ arith_env_t (cgbn_monitor_t monitor, cgbn_error_report_t *report, uint32_t instance)

The constructor. This takes a monitor and error report for the CGBN library, and an instance number. The instance number is used to select the appropriate CGBN instance for the thread.

Parameters
  • monitor[in] The monitor for the CGBN library

  • report[in] The error report for the CGBN library

  • instance[in] The instance number for the CGBN library

inline __device__ __forceinline__ arith_env_t (cgbn_monitor_t monitor)

The constructor. This takes a monitor for the CGBN library. Used more in unit tests.

Parameters

monitor[in] The monitor for the CGBN library

inline __host__ __forceinline__ arith_env_t (cgbn_monitor_t monitor, uint32_t instance)

The constructor. This takes a monitor for the CGBN library, and an instance number.

Parameters
  • monitor[in] The monitor for the CGBN library

  • instance[in] The instance number for the CGBN library

inline __host__ __device__ __forceinline__ arith_env_t (const arith_env_t &env)

The clone constructor. This takes an existing arithmetic environment and clones it.

Parameters

env[in] The existing arithmetic environment

inline __host__ __device__ __forceinline__ void address_conversion (bn_t &address)
inline __host__ __device__ __forceinline__ size_t memory_from_cgbn (uint8_t *dst, bn_t &src)

Get a memory byte array from CGBN base type. The memory byte array is in Big Endian format. The memory byte array must be allocated by the caller.

Parameters
  • dst[out] The memory byte array

  • src[in] The source CGBN

Returns

The size of the byte array

inline __host__ __device__ __forceinline__ void cgbn_from_memory (bn_t &dst, uint8_t *src)

Get a CGBN type from memory byte array. The memory byte array is in Big Endian format.

Parameters
  • dst[out] The destination CGBN

  • src[in] The memory byte array

inline __host__ __device__ __forceinline__ void cgbn_from_size_t (bn_t &dst, size_t src)

Get a CGBN type from a size_t.

Parameters
  • dst[out] The destination CGBN

  • src[in] The size_t

inline __host__ __device__ __forceinline__ int32_t size_t_from_cgbn (size_t &dst, bn_t &src)

Get a size_t from a CGBN type.

Parameters
  • dst[out] The destination size_t

  • src[in] The source CGBN

Returns

1 for overflow, 0 otherwiese

inline __host__ __device__ __forceinline__ int32_t uint64_t_from_cgbn (uint64_t &dst, bn_t &src)

Get a uint64_t from a CGBN type.

Parameters
  • dst[out] The destination uint64_t

  • src[in] The source CGBN

Returns

1 for overflow, 0 otherwiese

inline __host__ void hex_string_from_cgbn_memory (char *dst_hex_string, evm_word_t &src_cgbn_mem, uint32_t count=LIMBS)

Get a hex string from the CGBn memory. The hex string is in Big Endian format. The hex string must be allocated by the caller.

Parameters
  • dst_hex_string[out] The destination hex string

  • src_cgbn_mem[in] The source CGBN memory

  • count[in] The number of limbs

inline __host__ int32_t cgbn_memory_from_hex_string (evm_word_t &dst_cgbn_memory, char *src_hex_string)

Get a CGBN memory from a hex string. The hex string is in Big Endian format. It use the GMP library to convert the hex string to a mpz_t type.

Parameters
  • dst_cgbn_memory[out] The destination CGBN memory

  • src_hex_string[in] The source hex string

Returns

1 for overflow, 0 otherwiese

inline __host__ __device__ __forceinline__ void cgbn_memory_from_size_t (evm_word_t &dst_cgbn_memory, size_t src)

Set the cgbn memory to the size_t value

Parameters
  • dst_cgbn_memory[out] The destination CGBN memory

  • src[in] The source size_t

inline __host__ __device__ __forceinline__ void print_cgbn_memory (evm_word_t &src_cgbn_memory)

Print the CGBN memory in hex string format. The hex string is in Big Endian format.

Parameters

src_cgbn_memory[in] The source CGBN memory

inline __host__ __device__ __forceinline__ int32_t has_gas (bn_t &gas_limit, bn_t &gas_used, uint32_t &error_code)

Verify if is enough gas for the operation.

Parameters
  • gas_limit[in] The gas limit

  • gas_used[in] The gas used

  • error_code[inout] The error code

Returns

1 for enough gas and no previous errors, 0 otherwise

inline __host__ __device__ __forceinline__ void max_gas_call (bn_t &gas_capped, bn_t &gas_limit, bn_t &gas_used)

Compute the max gas call.

Parameters
  • gas_capped[out] The gas capped

  • gas_limit[in] The gas limit

  • gas_used[in] The gas used

inline __host__ __device__ __forceinline__ uint8_t * get_data (data_content_t &data_content, bn_t &index, bn_t &length, size_t &available_size)

Get the data at the given index for the given length. If the index is greater than the data size, it returns NULL. If the length is greater than the data size - index, it returns the data from index to the end of the data and sets the available size to the data size - index. Otherwise, it returns the data from index to index + length and sets the available size to length.

Parameters
  • data_content[in] The data content

  • index[in] The index of the code data

  • length[in] The length of the code data

  • available_size[out] The available size of the code data

inline __host__ __device__ __forceinline__ void evm_words_gas_cost (bn_t &gas_used, bn_t &length, uint32_t gas_per_word)

Add the gas cost for the given length of bytes, but considering evm words.

Parameters
  • gas_used[inout] The gas used

  • length[in] The length of the bytes

  • gas_per_word[in] The gas per evm word

inline __host__ __device__ __forceinline__ void initcode_cost (bn_t &gas_used, bn_t &initcode_length)

Add the cost for initiliasation code. EIP-3860: https://eips.ethereum.org/EIPS/eip-3860

Parameters
  • gas_used[inout] The gas used

  • initcode_length[in] The length of the initcode

inline __host__ __device__ __forceinline__ void keccak_cost (bn_t &gas_used, bn_t &length)

Add the cost for keccak hashing.

Parameters
  • gas_used[inout] The gas used

  • length[in] The length of the data in bytes

Public Members

context_t _context#

The CGBN context

env_t _env#

The CGBN environment

uint32_t _instance#

The instance number for the CGBN

Public Static Attributes

static const uint32_t BITS = params::BITS#

The number of the bits for the CGBN

static const uint32_t BYTES = params::BITS / 8#

The number of the bytes for the CGBN

static const uint32_t LIMBS = params::BITS / 32#

The number of the limbs for the CGBN

static const uint32_t ADDRESS_BYTES = 20#

The number of the bytes for the address