View Source Tezex.Zarith (tezex v3.0.1)
Provides encoding and decoding functions for Zarith numbers.
A Zarith number is an integer encoded as a variable length sequence of bytes.
Each byte has a running unary size bit:
- The most significant bit of each byte tells if this is the last byte in the sequence (0) or if there is more to read (1)
- The second most significant bit of the first byte is reserved for the sign (positive if zero)
Size and sign bits ignored, data is then the binary representation of the absolute value of the number in little endian order.
Summary
Types
Represents a decoded Zarith number as an Elixir integer.
Represents a Zarith-encoded integer as a hexadecimal string.
Functions
Consumes a Zarith-encoded integer.
Decodes a Zarith-encoded integer.
Encodes an integer to a Zarith-encoded hexadecimal string.
Types
Functions
@spec consume(zarith_hex()) :: {%{int: String.t()}, pos_integer()}
Consumes a Zarith-encoded integer.
Takes a hexadecimal string and returns the decoded integer in base 10 along with how many characters of the input string were used to decode the integer.
Parameters
binary_input
- A hexadecimal string representing the Zarith-encoded integer.
Returns
A tuple containing:
- A map with the key
:int
and the decoded integer as a string value. - The number of characters consumed from the input string.
Examples
iex> Tezex.Zarith.consume("a1d22c")
{%{int: "365729"}, 6}
iex> Tezex.Zarith.consume("e1d22c")
{%{int: "-365729"}, 6}
@spec decode(zarith_hex()) :: decoded_zarith()
Decodes a Zarith-encoded integer.
Takes a hexadecimal string representation of a Zarith-encoded number and returns the integer in base 10.
Parameters
binary_input
- A hexadecimal string representing the Zarith-encoded integer.
Returns
The decoded integer.
Examples
iex> Tezex.Zarith.decode("a1d22c")
365_729
iex> Tezex.Zarith.decode("e1d22c")
-365_729
@spec encode(decoded_zarith()) :: zarith_hex()
Encodes an integer to a Zarith-encoded hexadecimal string.
Takes an integer in base 10 and returns the Zarith-encoded hexadecimal string.
Implementation based on anchorageoss/tezosprotocol (MIT License - Copyright (c) 2019 Anchor Labs, Inc.)
Parameters
integer
- The integer to be encoded.
Returns
A hexadecimal string representing the Zarith-encoded integer.
Examples
iex> Tezex.Zarith.encode(365_729)
"a1d22c"
iex> Tezex.Zarith.encode(-365_729)
"e1d22c"