blockchain v0.1.7 BitHelper

Helpers for common operations on the blockchain.

Link to this section Summary

Functions

Similar to :binary.decode_unsigned/1, except we decode <<>> back to 0, which is a common practice in Ethereum, since we cannot have any leading zeros

Similar to :binary.encode_unsigned/1, except we encode 0 as <<>>, the empty string. This is because the specification says that we cannot have any leading zeros, and so having <<0>> by itself is leading with a zero and prohibited

Simple wrapper for decoding hex data

Returns the keccak sha256 of a given input

Simply returns the rightmost n bits of a binary

Simply returns the rightmost n bits of a binary

Returned a binary padded to given length in bytes. Fails if binary is longer than desired length

Simple wrapper to generate hex

Link to this section Types

Link to this type keccak_hash()
keccak_hash() :: binary

Link to this section Functions

Link to this function decode_unsigned(bin)
decode_unsigned(binary) :: number

Similar to :binary.decode_unsigned/1, except we decode <<>> back to 0, which is a common practice in Ethereum, since we cannot have any leading zeros.

Examples

iex> BitHelper.decode_unsigned(<<>>)
0

iex> BitHelper.decode_unsigned(<<5>>)
5

iex> BitHelper.decode_unsigned(<<76, 75, 64>>)
5_000_000
Link to this function encode_unsigned(n)
encode_unsigned(number) :: binary

Similar to :binary.encode_unsigned/1, except we encode 0 as <<>>, the empty string. This is because the specification says that we cannot have any leading zeros, and so having <<0>> by itself is leading with a zero and prohibited.

Examples

iex> BitHelper.encode_unsigned(0)
<<>>

iex> BitHelper.encode_unsigned(5)
<<5>>

iex> BitHelper.encode_unsigned(5_000_000)
<<76, 75, 64>>
Link to this function from_hex(hex_data)
from_hex(String.t) :: binary

Simple wrapper for decoding hex data.

Examples

iex> BitHelper.from_hex("aabbcc")
<<0xaa, 0xbb, 0xcc>>
Link to this function kec(data)
kec(binary) :: keccak_hash

Returns the keccak sha256 of a given input.

Examples

iex> BitHelper.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> BitHelper.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>>
Link to this function mask(n, bits)
mask(integer, integer) :: integer

Simply returns the rightmost n bits of a binary.

Examples

iex> BitHelper.mask(0b111101111, 6)
0b101111

iex> BitHelper.mask(0b101101, 3)
0b101

iex> BitHelper.mask(0b011, 1)
1

iex> BitHelper.mask(0b011, 0)
0

iex> BitHelper.mask(0b010, 1)
0

iex> BitHelper.mask(0b010, 20)
0b010
Link to this function mask_bitstring(b, bits)
mask_bitstring(bitstring, integer) :: bitstring

Simply returns the rightmost n bits of a binary.

Examples

iex> BitHelper.mask_bitstring(<<0b111101111>>, 6)
<<0b101111::size(6)>>

iex> BitHelper.mask_bitstring(<<0b101101>>, 3)
<<0b101::size(3)>>

iex> BitHelper.mask_bitstring(<<0b011>>, 1)
<<1::size(1)>>

iex> BitHelper.mask_bitstring(<<0b011>>, 0)
<<>>

iex> BitHelper.mask_bitstring(<<0b010>>, 1)
<<0::size(1)>>

iex> BitHelper.mask_bitstring(<<0b010>>, 20)
<<0, 0, 2::size(4)>>

iex> BitHelper.mask_bitstring(<<0b010>>, 3)
<<0b010::size(3)>>

iex> BitHelper.mask_bitstring(<<>>, 3)
<<0b000::size(3)>>
Link to this function pad(binary, desired_length)
pad(binary, integer) :: binary

Returned a binary padded to given length in bytes. Fails if binary is longer than desired length.

Examples

iex> BitHelper.pad(<<9>>, 5)
<<0, 0, 0, 0, 9>>

iex> BitHelper.pad(<<9>>, 1)
<<9>>

iex> BitHelper.pad(<<9, 9>>, 1)
** (RuntimeError) Binary too long for padding
Link to this function to_hex(bin)
to_hex(binary) :: String.t

Simple wrapper to generate hex.

Examples

iex> BitHelper.to_hex(<<0xaa, 0xbb, 0xcc>>)
"aabbcc"