ABI.Math (hieroglyph v1.4.0)

Copy Markdown View Source

Helper functions for ABI's math functions.

API Functions

FunctionArityDescriptionParam Kinds
unpad3Inverse of pad/4. Reads size_in_bytes of content out of data, skipping 32-byte-slot padding on the matching side.data: value, size_in_bytes: value, padding_direction: value
pad4Pad a binary up to the next 32-byte ABI word boundary, with side and fill byte chosen by argument.bin: value, size_in_bytes: value, direction: value, opts: value
kec1Compute the keccak-256 hash of a binary input. The hash function used throughout Ethereum and the ABI spec for selector and event-topic derivation.data: value
mod2Compute integer modulo with sign-aware behavior, returning a non-negative result for negative dividends.x: value, n: value

Summary

Functions

Returns the keccak sha256 of a given input.

Simple function to compute modulo function to work on integers of any sign.

Pads a binary up to the next 32-byte ABI word boundary.

Inverse of pad/4. Reads size_in_bytes of content out of data, skipping 32-byte-slot padding on the side matching padding_direction (:left for left-padded types like address, :right for right-padded types like bytes<M> and string). Returns {value, rest}, where rest is whatever follows the padded word in data.

Functions

kec(data)

@spec kec(binary()) :: binary()

Returns the keccak sha256 of a given input.

Examples

iex> ABI.Math.kec("hello world")
<<71, 23, 50, 133, 168, 215, 52, 30, 94, 151, 47, 198, 119, 40, 99,
  132, 248, 2, 248, 239, 66, 165, 236, 95, 3, 187, 250, 37, 76, 176,
  31, 173>>

iex> ABI.Math.kec(<<0x01, 0x02, 0x03>>)
<<241, 136, 94, 218, 84, 183, 160, 83, 49, 140, 212, 30, 32, 147, 34,
  13, 171, 21, 214, 83, 129, 177, 21, 122, 54, 51, 168, 59, 253, 92,
  146, 57>>

mod(x, n)

@spec mod(integer(), pos_integer()) :: non_neg_integer()

Simple function to compute modulo function to work on integers of any sign.

Examples

iex> ABI.Math.mod(5, 2)
1

iex> ABI.Math.mod(-5, 1337)
1332

iex> ABI.Math.mod(1337 + 5, 1337)
5

iex> ABI.Math.mod(0, 1337)
0

iex> ABI.Math.mod(-7, 5)
3

iex> ABI.Math.mod(-1338, 1337)
1336

pad(bin, size_in_bytes, direction, opts \\ [])

@spec pad(binary(), non_neg_integer(), :left | :right, keyword()) :: binary()

Pads a binary up to the next 32-byte ABI word boundary.

size_in_bytes is the logical field width used to compute the word boundary — not necessarily byte_size(bin). The output is always rounded up from size_in_bytes to a 32-byte multiple. bin may be shorter than size_in_bytes (callers like encode_int/encode_uint pass a target slot size and let padding absorb the difference); encode_bytes passes byte_size(bin) directly.

direction is :left for left-aligned types like uint/int/address, :right for right-padded types like bytes<M> and string. Pass fill_byte: <<0xFF>> for signed sign-extension; defaults to <<0x00>>.

Examples

iex> ABI.Math.pad(<<1, 2, 3>>, 3, :left)
<<0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3>>

iex> ABI.Math.pad(<<1, 2, 3>>, 3, :right)
<<1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0>>

unpad(data, size_in_bytes, padding_direction)

@spec unpad(binary(), non_neg_integer(), :left | :right) :: {binary(), binary()}

Inverse of pad/4. Reads size_in_bytes of content out of data, skipping 32-byte-slot padding on the side matching padding_direction (:left for left-padded types like address, :right for right-padded types like bytes<M> and string). Returns {value, rest}, where rest is whatever follows the padded word in data.

Examples

iex> ABI.Math.unpad(
...>   <<0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
...>     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3>>,
...>   3,
...>   :left
...> )
{<<1, 2, 3>>, <<>>}

iex> ABI.Math.unpad(
...>   <<1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
...>     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
...>     9, 9>>,
...>   3,
...>   :right
...> )
{<<1, 2, 3>>, <<9, 9>>}