Powex (powex v0.1.1)
Proof of Work implementation using Rust NIF for high-performance mining.
This module provides functions to compute and validate Proof of Work using SHA-256 hashing. The implementation uses Rust for performance-critical operations.
Summary
Functions
Computes a Proof of Work nonce for the given data and difficulty.
Computes a Proof of Work nonce using parallel processing for improved performance.
Gets the hash for given data and nonce combination.
Validates if a nonce produces a valid Proof of Work for the given data and difficulty.
Functions
@spec compute(binary(), non_neg_integer()) :: {:ok, non_neg_integer()} | {:error, String.t()}
Computes a Proof of Work nonce for the given data and difficulty.
Parameters
data
: The input data (string or binary) to hashdifficulty
: Number of leading zeros required in the hash (integer)
Returns
{:ok, nonce}
when a valid nonce is found{:error, reason}
if computation fails
Examples
iex> {:ok, nonce} = Powex.compute("hello world", 4)
iex> is_integer(nonce)
true
iex> Powex.compute("", 0)
{:ok, 0}
@spec compute_parallel(binary(), non_neg_integer(), pos_integer()) :: {:ok, non_neg_integer()} | {:error, String.t()}
Computes a Proof of Work nonce using parallel processing for improved performance.
Parameters
data
: The input data (string or binary) to hashdifficulty
: Number of leading zeros required in the hash (integer)threads
: Number of threads to use for parallel computation (default: number of CPU cores)
Returns
{:ok, nonce}
when a valid nonce is found{:error, reason}
if computation fails
Examples
iex> {:ok, nonce} = Powex.compute_parallel("hello world", 4, 4)
iex> is_integer(nonce)
true
@spec get_hash(binary(), non_neg_integer()) :: {:ok, String.t()} | {:error, String.t()}
Gets the hash for given data and nonce combination.
Parameters
data
: The input data (string or binary)nonce
: The nonce value (integer)
Returns
{:ok, hash}
where hash is the SHA-256 hash as a hex string{:error, reason}
if hashing fails
Examples
iex> {:ok, hash} = Powex.get_hash("test", 123)
iex> String.length(hash)
64
@spec valid?(binary(), non_neg_integer(), non_neg_integer()) :: boolean()
Validates if a nonce produces a valid Proof of Work for the given data and difficulty.
Parameters
data
: The input data (string or binary) that was hashednonce
: The nonce value to validate (integer)difficulty
: Number of leading zeros required in the hash (integer)
Returns
true
if the nonce is valid for the given difficultyfalse
if the nonce is invalid
Examples
iex> {:ok, nonce} = Powex.compute("test data", 3)
iex> Powex.valid?("test data", nonce, 3)
true
iex> Powex.valid?("test data", 12345, 3)
false