Caustic v0.1.12 Caustic.Utils View Source

A collection of useful methods.

Link to this section Summary

Functions

Decode a base58-encoded string into its hexadecimal string representation

Encodes an integer into its base58 representation. If given a string, by default it will interpret the string as a hex

Same as base58_decode but outputs an integer instead of hex string

Returns checksum, payload, and version

Encodes a binary to its base58check representation

Decodes a MIME Base64 encoded string

Gets the character code of a MIME base64 digit

Encodes a string into its MIME Base64 representation

Encodes an integer codepoint 0 <= n <= 63 to its MIME Base64 character

Converts a Bitcoin 256-bit private key to the Wallet Import Format. Defaults to outputting compressed format

Converts a bitstring (including binary) into array of 0s and 1s. For simple binary you can also use :binary.decode_unsigned

Interprets a bitstring (including binary) as an unsigned integer. You can use :binary.decode_unsigned/1 if it’s a normal binary

Find the greatest common divisor of two integers

Guess the base of an integer string using its prefix. Defaults to 10, and doesn’t check for validity of the digits

Calculate the document hash, used for ECDSA

Removes hexadecimal prefix from a string

Examples

Find the modular inverse

Calculates integer exponentiation. Exponent can be negative

Fast exponentiation modulo m. Calculates x^y mod m

Check whether an integer is a prime using Sieve of Eratosthenes algorithm. Don’t use on very large numbers

Finds the index of an ASCII character inside a string. Not unicode friendly!

Find the digits of a nonnegative integer n in a particular base

Parse a string which can be in any base to integer, autodetecting the base using the prefix

Parse a string which can be in any base to integer, specifying the base

Convert an integer into its string representation in any base

Link to this section Functions

Link to this function base58_decode(str, opts \\ []) View Source

Decode a base58-encoded string into its hexadecimal string representation.

Examples

iex> Caustic.Utils.base58_decode("5J3mBbAH58CpQ3Y5RNJpUKPE62SQ5tfcvU2JpbnkeyhfsYB1Jcn")
"0x801e99423a4ed27608a15a2616a2b0e9e52ced330ac530edcc32c8ffc6a526aeddc47e83ff"
iex> Caustic.Utils.base58_decode("5J3mBbAH58CpQ3Y5RNJpUKPE62SQ5tfcvU2JpbnkeyhfsYB1Jcn", prefix: false)
"801e99423a4ed27608a15a2616a2b0e9e52ced330ac530edcc32c8ffc6a526aeddc47e83ff"
Link to this function base58_encode(str, opts \\ []) View Source

Encodes an integer into its base58 representation. If given a string, by default it will interpret the string as a hex.

If given hex and it has leading zeros, then each byte of zeros will be encoded as 1.

Examples

iex> Caustic.Utils.base58_encode("801e99423a4ed27608a15a2616a2b0e9e52ced330ac530edcc32c8ffc6a526aeddc47e83ff")
"5J3mBbAH58CpQ3Y5RNJpUKPE62SQ5tfcvU2JpbnkeyhfsYB1Jcn"
iex> Caustic.Utils.base58_encode(<<57>>, convert_from_hex: false)
"z"
iex> Caustic.Utils.base58_encode(63716817338599314535577169638518475271320430400871647684951348108655027767484127754748927)
"5J3mBbAH58CpQ3Y5RNJpUKPE62SQ5tfcvU2JpbnkeyhfsYB1Jcn"
iex> Caustic.Utils.base58_encode("0x000001")
"112"

Same as base58_decode but outputs an integer instead of hex string.

Returns checksum, payload, and version

Examples

iex> Caustic.Utils.base58check_decode "5J3mBbAH58CpQ3Y5RNJpUKPE62SQ5tfcvU2JpbnkeyhfsYB1Jcn"
{:ok, <<196, 126, 131, 255>>, "1e99423a4ed27608a15a2616a2b0e9e52ced330ac530edcc32c8ffc6a526aedd", :private_key_wif}
iex> Caustic.Utils.base58check_decode "1J7mdg5rbQyUHENYdx39WVWK7fsLpEoXZy"
{:ok, <<55, 254, 252, 208>>, "bbc1e42a39d05a4cc61752d6963b7f69d09bb27b", :address}
Link to this function base58check_encode(payload, version, opts \\ []) View Source

Encodes a binary to its base58check representation.

Possible values for version: :address, :address_p2sh, :address_testnet, private_key_wif, private_key_bip38_encrypted, public_key_bit32_extended

Can also use custom binary version.

Examples

iex> Caustic.Utils.base58check_encode("0x1e99423a4ed27608a15a2616a2b0e9e52ced330ac530edcc32c8ffc6a526aedd", :private_key_wif)
"5J3mBbAH58CpQ3Y5RNJpUKPE62SQ5tfcvU2JpbnkeyhfsYB1Jcn"
iex> Caustic.Utils.base58check_encode(<<Caustic.Utils.to_integer("0x1e99423a4ed27608a15a2616a2b0e9e52ced330ac530edcc32c8ffc6a526aedd")::size(256)>>, :private_key_wif, convert_from_hex: false)
"5J3mBbAH58CpQ3Y5RNJpUKPE62SQ5tfcvU2JpbnkeyhfsYB1Jcn"
iex> Caustic.Utils.base58check_encode(<<Caustic.Utils.to_integer("0x1e99423a4ed27608a15a2616a2b0e9e52ced330ac530edcc32c8ffc6a526aedd")::size(256), 0x01>>, :private_key_wif, convert_from_hex: false)
"KxFC1jmwwCoACiCAWZ3eXa96mBM6tb3TYzGmf6YwgdGWZgawvrtJ"
iex> Caustic.Utils.base58check_encode(<<Caustic.Utils.to_integer("1E99423A4ED27608A15A2616A2B0E9E52CED330AC530EDCC32C8FFC6A526AEDD", 16)::size(256), 0x01>>, :private_key_wif, convert_from_hex: false)
"KxFC1jmwwCoACiCAWZ3eXa96mBM6tb3TYzGmf6YwgdGWZgawvrtJ"
iex> Caustic.Utils.base58check_encode("f5f2d624cfb5c3f66d06123d0829d1c9cebf770e", :address)
"1PRTTaJesdNovgne6Ehcdu1fpEdX7913CK"
iex> Caustic.Utils.base58check_encode("000000cb23faea20aa20f02a02955ffd1d785518", :address)
"1111DVWAb9XQh88gakJRcK14e1i1onvAL" # private key is 5KjhZsxt61XSPunjrPm8XUEAH1YN6zXm6pqT5D1hZ9mLoEAqKTp

Decodes a MIME Base64 encoded string.

https://en.wikipedia.org/wiki/Base64 (see Variants summary table)

Examples

iex> Caustic.Utils.base64_decode("TWFu")
"Man"
iex> Caustic.Utils.base64_decode("TWE=")
"Ma"
iex> Caustic.Utils.base64_decode("TQ==")
"M"
iex> Caustic.Utils.base64_decode("TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz\r\nIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLi4u")
"Man is distinguished, not only by his reason, but by this singular passion from other animals..."

Gets the character code of a MIME base64 digit.

Examples

iex> Caustic.Utils.base64_decode_char("A")
0
iex> Caustic.Utils.base64_decode_char("F")
5
iex> Caustic.Utils.base64_decode_char("a")
26
iex> Caustic.Utils.base64_decode_char("f")
31
iex> Caustic.Utils.base64_decode_char("0")
52
iex> Caustic.Utils.base64_decode_char("5")
57
iex> Caustic.Utils.base64_decode_char("+")
62
iex> Caustic.Utils.base64_decode_char("/")
63
Link to this function base64_encode(data, opts \\ []) View Source

Encodes a string into its MIME Base64 representation.

https://en.wikipedia.org/wiki/Base64 (see Variants summary table)

Examples

iex> Caustic.Utils.base64_encode("Man")
"TWFu"
iex> Caustic.Utils.base64_encode("Ma")
"TWE="
iex> Caustic.Utils.base64_encode("M")
"TQ=="
iex> Caustic.Utils.base64_encode("Man is distinguished, not only by his reason, but by this singular passion from other animals...")
"TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz\r\nIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLi4u"
iex> Caustic.Utils.base64_encode("Man is distinguished, not only by his reason, but by this singular passion from other animals...", new_line: false)
"TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLi4u"

Encodes an integer codepoint 0 <= n <= 63 to its MIME Base64 character.

Examples

iex> Caustic.Utils.base64_encode_char(0)
"A"
iex> Caustic.Utils.base64_encode_char(5)
"F"
iex> Caustic.Utils.base64_encode_char(26)
"a"
iex> Caustic.Utils.base64_encode_char(31)
"f"
iex> Caustic.Utils.base64_encode_char(52)
"0"
iex> Caustic.Utils.base64_encode_char(57)
"5"
iex> Caustic.Utils.base64_encode_char(62)
"+"
iex> Caustic.Utils.base64_encode_char(63)
"/"
Link to this function bitcoin_private_key_to_wif(hex_str, opts \\ []) View Source

Converts a Bitcoin 256-bit private key to the Wallet Import Format. Defaults to outputting compressed format.

Examples

iex> Caustic.Utils.bitcoin_private_key_to_wif("1e99423a4ed27608a15a2616a2b0e9e52ced330ac530edcc32c8ffc6a526aedd")
"KxFC1jmwwCoACiCAWZ3eXa96mBM6tb3TYzGmf6YwgdGWZgawvrtJ"
iex> Caustic.Utils.bitcoin_private_key_to_wif("1e99423a4ed27608a15a2616a2b0e9e52ced330ac530edcc32c8ffc6a526aedd", compressed: false)
"5J3mBbAH58CpQ3Y5RNJpUKPE62SQ5tfcvU2JpbnkeyhfsYB1Jcn"
Link to this function bitstring_to_array(data) View Source

Converts a bitstring (including binary) into array of 0s and 1s. For simple binary you can also use :binary.decode_unsigned

Examples

iex> Caustic.Utils.bitstring_to_array "Hey"
[0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 1]
iex> Caustic.Utils.bitstring_to_array << 1 :: size(1), 0 :: size(1), 1 :: size(1) >>
[1, 0, 1]
Link to this function bitstring_to_integer(data) View Source

Interprets a bitstring (including binary) as an unsigned integer. You can use :binary.decode_unsigned/1 if it’s a normal binary.

Examples

iex> Caustic.Utils.bitstring_to_integer(<<255, 255>>)
65535

Find the greatest common divisor of two integers.

Proof: https://www.khanacademy.org/computing/computer-science/cryptography/modarithmetic/a/the-euclidean-algorithm

Examples

iex> Caustic.Utils.gcd(270, 192)
6

Guess the base of an integer string using its prefix. Defaults to 10, and doesn’t check for validity of the digits.

Examples

iex> Caustic.Utils.get_base("0xabf")
16
iex> Caustic.Utils.get_base("0b101")
2
iex> Caustic.Utils.get_base("321")
10

Calculate the document hash, used for ECDSA.

Removes hexadecimal prefix from a string.

Examples

iex> Caustic.Utils.hex_remove_prefix("0xff")
"ff"
iex> Caustic.Utils.hex_remove_prefix("0XFF")
"FF"
iex> Caustic.Utils.hex_remove_prefix("ff")
"ff"

Examples

iex> Caustic.Utils.mod(0, 3)
0
iex> Caustic.Utils.mod(-27, 13)
12

Find the modular inverse.

Using Euclidean Algorithm: https://www.math.utah.edu/~fguevara/ACCESS2013/Euclid.pdf

Examples

iex> Caustic.Utils.mod_inverse(1, 101)
1
iex> Caustic.Utils.mod_inverse(2, 3)
2
iex> Caustic.Utils.mod_inverse(50, 71)
27
iex> Caustic.Utils.mod_inverse(25, 50)
nil
iex> Caustic.Utils.mod_inverse(8, 11)
7
iex> Caustic.Utils.mod_inverse(345, 76408)
48281
iex> Caustic.Utils.mod_inverse(71, 50)
31

# Bitcoin elliptic curve
iex> Caustic.Utils.mod_inverse(345, 115792089237316195423570985008687907853269984665640564039457584007908834671663)
53029420578249156164997726467746925915410601672960026429664632676085785153979

Calculates integer exponentiation. Exponent can be negative.

Examples

iex> Caustic.Utils.pow(3, 9)
19683
iex> Caustic.Utils.pow(2, 8)
256
iex> Caustic.Utils.pow(2, 256)
115792089237316195423570985008687907853269984665640564039457584007913129639936
iex> Caustic.Utils.pow(2, -2)
0.25

Fast exponentiation modulo m. Calculates x^y mod m.

With x = 5, y = 12345, m = 17, and repeated 1000 times, it is faster by naive method by a factor of 150 on a particular benchmark machine.

https://www.khanacademy.org/computing/computer-science/cryptography/modarithmetic/a/modular-exponentiation https://www.khanacademy.org/computing/computer-science/cryptography/modarithmetic/a/fast-modular-exponentiation

Examples

iex> Caustic.Utils.pow_mod(5, 0, 19)
1
iex> Caustic.Utils.pow_mod(5, 1, 19)
5
iex> Caustic.Utils.pow_mod(5, 117, 19)
1
iex> Caustic.Utils.pow_mod(7, 256, 13)
9
iex> Caustic.Utils.pow_mod(2, 90, 13)
12
iex> Caustic.Utils.pow_mod(50, -1, 71)
27
iex> Caustic.Utils.pow_mod(7, -3, 13)
8

Check whether an integer is a prime using Sieve of Eratosthenes algorithm. Don’t use on very large numbers.

Finds the index of an ASCII character inside a string. Not unicode friendly!

Examples

iex> Caustic.Utils.string_index_of("Hello", "H")
0
iex> Caustic.Utils.string_index_of("Hello", "h")
nil
iex> Caustic.Utils.string_index_of("Hello", "l")
2

Find the digits of a nonnegative integer n in a particular base.

Examples

iex> Caustic.Utils.to_digits(321, 10)
[3, 2, 1]
iex> Caustic.Utils.to_digits(5, 2)
[1, 0, 1]
iex> Caustic.Utils.to_digits(255, 16)
[15, 15]
iex> Caustic.Utils.to_digits(0, 8)
[0]

Parse a string which can be in any base to integer, autodetecting the base using the prefix.

Examples

iex> Caustic.Utils.to_integer("0xff")
255
iex> Caustic.Utils.to_integer("0b101")
5
iex> Caustic.Utils.to_integer("321")
321

Parse a string which can be in any base to integer, specifying the base.

Examples

iex> Caustic.Utils.to_integer("ff", 16)
255
iex> Caustic.Utils.to_integer("101", 2)
5
iex> Caustic.Utils.to_integer("755", 8)
493
iex> Caustic.Utils.to_integer("321", 10)
321
Link to this function to_string(n, base, opts \\ []) View Source

Convert an integer into its string representation in any base.

Examples

iex> Caustic.Utils.to_string(255, 16)
"0xff"
iex> Caustic.Utils.to_string(255, 16, prefix: false)
"ff"
iex> Caustic.Utils.to_string(5, 2)
"0b101"