Caustic v0.1.17 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
Checks whether an integer n > 1
is a composite number. 1 is a unit,
neither a prime nor composite. Will return false on n <= 1
Checks whether a
divides b
.
See https://math.stackexchange.com/questions/666103/why-would-some-elementary-number-theory-notes-exclude-00 and
https://math.stackexchange.com/questions/2174535/does-zero-divide-zero
Find the prime factors of an integer
Find the greatest common divisor of two integers
Find the greatest common divisor d
of two integers a
and b
, while also finding
the coefficients x
and y
such that ax + by = d
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
Solves the linear congruence ax = b (mod m)
Splits a list into its first n
elements and the rest
Calculate the md5 hash
Finds the least residue of a number modulo m
Find the modular inverse
Gets all the positive divisors of a number
Counts how many positive divisors an integer has. d(n)
Sums the positive divisors of an integer. σ(n)
Calculates integer exponentiation. Exponent can be negative
Fast exponentiation modulo m. Calculates x^y mod m
Checks whether an integer is a prime using Sieve of Eratosthenes algorithm. Don’t use on very large numbers
Find all primes p ≤ n
using Sieve of Eratosthenes method
Finds the index of an ASCII character inside a string. Not unicode friendly!
Finds all subsets of a set (represented by a keyword list)
Finds all subsets with cardinality n
of a set (represented by a keyword list)
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
If an integer can be written as the sum of the squares of two positive integers, return those two integers {a, b} where a <= b
Link to this section Functions
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"
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}
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
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)
"/"
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"
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]
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
Checks whether an integer n > 1
is a composite number. 1 is a unit,
neither a prime nor composite. Will return false on n <= 1
.
Examples
iex> Caustic.Utils.composite?(4)
true
iex> Caustic.Utils.composite?(2)
false
iex> Caustic.Utils.composite?(3)
false
iex> Caustic.Utils.composite?(1)
false
Checks whether a
divides b
.
See https://math.stackexchange.com/questions/666103/why-would-some-elementary-number-theory-notes-exclude-00 and
https://math.stackexchange.com/questions/2174535/does-zero-divide-zero
Examples
iex> Caustic.Utils.divides(2, 8)
true
iex> Caustic.Utils.divides(2, 3)
false
iex> Caustic.Utils.divides(2, 0)
true
iex> Caustic.Utils.divides(0, 0)
true
Find the prime factors of an integer.
Examples
iex> Caustic.Utils.factorize(72)
[2, 2, 2, 3, 3]
iex> Caustic.Utils.factorize(480)
[2, 2, 2, 2, 2, 3, 5]
iex> Caustic.Utils.factorize(357171293798123)
[7, 181, 1459, 193216691]
iex> Caustic.Utils.factorize(100000001)
[17, 5882353]
iex> Caustic.Utils.factorize(9223372036854775807) # largest 64-bit integer
[7, 7, 73, 127, 337, 92737, 649657]
iex> Caustic.Utils.factorize(18446744073709551615) # largest 64-bit unsigned integer
[3, 5, 17, 257, 641, 65537, 6700417]
iex> Caustic.Utils.factorize(18446744073709551615 * 3571 * 5901331)
[3, 5, 17, 257, 641, 3571, 65537, 5901331, 6700417]
iex> Caustic.Utils.factorize(1)
[]
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(1, 0)
1
iex> Caustic.Utils.gcd(-1, 0)
1
iex> Caustic.Utils.gcd(270, 192)
6
iex> Caustic.Utils.gcd(-270, 192)
6
iex> Caustic.Utils.gcd(270, -192)
6
iex> Caustic.Utils.gcd(-270, -192)
6
Find the greatest common divisor d
of two integers a
and b
, while also finding
the coefficients x
and y
such that ax + by = d
.
Examples
iex> Caustic.Utils.gcd_with_coefficients(3, 0)
{3, 1, 0}
iex> Caustic.Utils.gcd_with_coefficients(6, 3)
{3, 0, 1}
iex> Caustic.Utils.gcd_with_coefficients(270, 192)
{6, 5, -7}
iex> Caustic.Utils.gcd_with_coefficients(-270, 192)
{6, -5, -7}
iex> Caustic.Utils.gcd_with_coefficients(270, -192)
{6, 5, 7}
iex> Caustic.Utils.gcd_with_coefficients(-270, -192)
{6, -5, 7}
iex> Caustic.Utils.gcd_with_coefficients(314, 159)
{1, -40, 79}
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"
Solves the linear congruence ax = b (mod m).
Examples
iex> Caustic.Utils.linear_congruence_solve(1, 3, 4)
[3]
iex> Caustic.Utils.linear_congruence_solve(2, 1, 4)
[]
iex> Caustic.Utils.linear_congruence_solve(2, 6, 4)
[1, 3]
iex> Caustic.Utils.linear_congruence_solve(0, 0, 3)
[0, 1, 2]
iex> Caustic.Utils.linear_congruence_solve(0, 1, 3)
[]
iex> Caustic.Utils.linear_congruence_solve(0, 2, 3)
[]
iex> Caustic.Utils.linear_congruence_solve(0, 3, 3)
[0, 1, 2]
Splits a list into its first n
elements and the rest.
Examples
iex> Caustic.Utils.list_split([1, 2, 3, 4, 5], 2)
{[1, 2], [3, 4, 5]}
iex> Caustic.Utils.list_split([1, 2, 3], 5)
{[1, 2, 3], []}
Calculate the md5 hash.
Finds the least residue of a number modulo m
.
Examples
iex> Caustic.Utils.mod(0, 3)
0
iex> Caustic.Utils.mod(-27, 13)
12
iex> Caustic.Utils.mod(-3, 3)
0
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
Gets all the positive divisors of a number.
Examples
iex> Caustic.Utils.positive_divisors(1)
[1]
iex> Caustic.Utils.positive_divisors(3)
[1, 3]
iex> Caustic.Utils.positive_divisors(6)
[1, 2, 3, 6]
iex> Caustic.Utils.positive_divisors(-4)
[1, 2, 4]
Counts how many positive divisors an integer has. d(n)
.
Examples
iex> Caustic.Utils.positive_divisors_count(1)
1
iex> Caustic.Utils.positive_divisors_count(3)
2
iex> Caustic.Utils.positive_divisors_count(6)
4
Sums the positive divisors of an integer. σ(n)
.
Examples
iex> Caustic.Utils.positive_divisors_sum(1)
1
iex> Caustic.Utils.positive_divisors_sum(3)
4
iex> Caustic.Utils.positive_divisors_sum(6)
12
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
Checks whether an integer is a prime using Sieve of Eratosthenes algorithm. Don’t use on very large numbers.
Find all primes p ≤ n
using Sieve of Eratosthenes method.
Examples
iex> Caustic.Utils.prime_sieve_up_to(10)
[2, 3, 5, 7]
iex> Caustic.Utils.prime_sieve_up_to(30)
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
iex> Caustic.Utils.prime_sieve_up_to(100)
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
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
Finds all subsets of a set (represented by a keyword list).
Examples
iex> Caustic.Utils.subsets([:a])
[[], [:a]]
iex> Caustic.Utils.subsets([:a, :b, :c])
[[], [:a], [:b], [:c], [:a, :b], [:a, :c], [:b, :c], [:a, :b, :c]]
Finds all subsets with cardinality n
of a set (represented by a keyword list).
Examples
iex> members = ["nakai", "kusanagi", "mori", "kimura", "katori", "inagaki"]
iex> Caustic.Utils.subsets(members, 3)
[
["nakai", "kusanagi", "mori"],
["nakai", "kusanagi", "kimura"],
["nakai", "kusanagi", "katori"],
["nakai", "kusanagi", "inagaki"],
["nakai", "mori", "kimura"],
["nakai", "mori", "katori"],
["nakai", "mori", "inagaki"],
["nakai", "kimura", "katori"],
["nakai", "kimura", "inagaki"],
["nakai", "katori", "inagaki"],
["kusanagi", "mori", "kimura"],
["kusanagi", "mori", "katori"],
["kusanagi", "mori", "inagaki"],
["kusanagi", "kimura", "katori"],
["kusanagi", "kimura", "inagaki"],
["kusanagi", "katori", "inagaki"],
["mori", "kimura", "katori"],
["mori", "kimura", "inagaki"],
["mori", "katori", "inagaki"],
["kimura", "katori", "inagaki"]
]
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
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"
If an integer can be written as the sum of the squares of two positive integers, return those two integers {a, b} where a <= b.
Examples
iex> Caustic.Utils.to_sum_of_two_squares(17)
{1, 4}
iex> Caustic.Utils.to_sum_of_two_squares(1)
nil