evm v0.1.5 EVM.Helpers
Various helper functions with no other home.
Link to this section Summary
Functions
Defined as Eq.(224) in the Yellow Paper, this is “all but one 64th”, written as L(x)
Gets the byte at position pos
in binary
Encodes signed ints using twos compliment
Helper function to print an instruction message
Inverts a map so each key becomes a value, and vice versa
Reads a length of data from a binary, filling in all unknown values as zero
Gets the word size of a value or returns 0 if the value is 0
Wrap ints greater than the maximum allowed address size
Wrap ints greater than the max int around back to 0
Link to this section Functions
Defined as Eq.(224) in the Yellow Paper, this is “all but one 64th”, written as L(x).
Examples
iex> EVM.Helpers.all_but_one_64th(5)
5
iex> EVM.Helpers.all_but_one_64th(1000)
985
Gets the byte at position pos
in binary.
Examples
iex> EVM.Helpers.binary_get(<<1, 2, 3, 4>>, 2)
3
iex> EVM.Helpers.binary_get(<<1, 2, 3, 4>>, 4)
** (ArgumentError) argument error
Encodes signed ints using twos compliment
Examples
iex> EVM.Helpers.encode_signed(1)
1
iex> EVM.Helpers.encode_signed(-1)
EVM.max_int() - 1
Helper function to print an instruction message.
Inverts a map so each key becomes a value, and vice versa.
Examples
iex> EVM.Helpers.invert(%{a: 5, b: 10})
%{5 => :a, 10 => :b}
iex> EVM.Helpers.invert(%{dog: "cat"})
%{"cat" => :dog}
iex> EVM.Helpers.invert(%{cow: :moo})
%{moo: :cow}
iex> EVM.Helpers.invert(%{"name" => "bob"})
%{"bob" => "name"}
iex> EVM.Helpers.invert(%{})
%{}
read_zero_padded(binary, integer, integer) :: binary
Reads a length of data from a binary, filling in all unknown values as zero.
Examples
iex> EVM.Helpers.read_zero_padded(<<5, 6, 7>>, 1, 3)
<<6, 7, 0>>
iex> EVM.Helpers.read_zero_padded(<<5, 6, 7>>, 0, 2)
<<5, 6>>
iex> EVM.Helpers.read_zero_padded(<<5, 6, 7>>, 0, 3)
<<5, 6, 7>>
iex> EVM.Helpers.read_zero_padded(<<5, 6, 7>>, 4, 3)
<<0, 0, 0>>
Gets the word size of a value or returns 0 if the value is 0
Examples
iex> EVM.Helpers.word_size(<<7::256>>)
1
iex> EVM.Helpers.word_size(for val <- 1..256, into: <<>>, do: <<val>>)
8
Wrap ints greater than the maximum allowed address size.
Examples
iex> EVM.Helpers.wrap_address(1)
1
iex> EVM.Helpers.wrap_address(<<1>>)
<<1>>
iex> EVM.Helpers.wrap_address(EVM.max_address() + 1)
1