Packages
abi
0.1.13
1.3.0
1.2.0
1.1.0
1.0.0-bravo6
1.0.0-bravo4
1.0.0-bravo3
1.0.0-bravo2
1.0.0-bravo1
1.0.0-alpha9
1.0.0-alpha8
1.0.0-alpha7
1.0.0-alpha6
1.0.0-alpha5
1.0.0-alpha4
1.0.0-alpha3
1.0.0-alpha2
1.0.0-alpha1
0.1.21
0.1.21-alpha2
0.1.21-alpha1
0.1.20
0.1.19
0.1.18
0.1.17
0.1.16
0.1.15
0.1.13
0.1.12
0.1.11
0.1.10
0.1.9
0.1.8
0.1.7
0.1.6
0.1.5
0.1.4
0.1.3
0.1.2
0.1.1
0.1.0
Ethereum's ABI Interface
Current section
Files
Jump to
Current section
Files
lib/abi/math.ex
defmodule ABI.Math do
@moduledoc """
Helper functions for ABI's math functions.
"""
@doc """
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
"""
def mod(x, n) when x > 0, do: rem(x, n)
def mod(x, n) when x < 0, do: rem(n + x, n)
def mod(0, _n), do: 0
@doc """
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>>
"""
@spec kec(binary()) :: binary()
def kec(data) do
:keccakf1600.sha3_256(data)
end
end