gleeth/crypto/random

Types

Error types for random number generation operations

pub type RandomError {
  SystemRandomNotAvailable(String)
  InsufficientEntropy(String)
  InvalidLength(String)
  CryptographicError(String)
  GenerationFailed(String)
}

Constructors

  • SystemRandomNotAvailable(String)
  • InsufficientEntropy(String)
  • InvalidLength(String)
  • CryptographicError(String)
  • GenerationFailed(String)

Result of randomness quality testing

pub type RandomnessTestResult {
  RandomnessTestResult(
    sample_count: Int,
    byte_length: Int,
    all_different: Bool,
    no_all_zeros: Bool,
    no_all_ones: Bool,
    average_bit_density: Float,
  )
}

Constructors

  • RandomnessTestResult(
      sample_count: Int,
      byte_length: Int,
      all_different: Bool,
      no_all_zeros: Bool,
      no_all_ones: Bool,
      average_bit_density: Float,
    )

Values

pub const default_max_retries: Int

Maximum retry attempts for private key generation

pub fn error_to_string(error: RandomError) -> String

Convert RandomError to string for display

pub fn generate_private_key() -> Result(
  secp256k1.PrivateKey,
  RandomError,
)

Generate a cryptographically secure secp256k1 private key

This function generates a random 32-byte private key that is guaranteed to be:

  • Cryptographically secure (using system CSPRNG)
  • Within the valid secp256k1 curve order
  • Non-zero (not the invalid all-zeros key)

The function will retry generation if the random bytes don’t form a valid private key (extremely rare but theoretically possible).

Returns

  • Ok(PrivateKey) - A valid secp256k1 private key
  • Error(RandomError) - If generation fails after maximum retries

Example

case generate_private_key() {
  Ok(private_key) -> {
    // Use the private key to create a wallet
    wallet.from_private_key_bytes(secp256k1.private_key_to_bytes(private_key))
  }
  Error(err) -> // Handle generation error
}
pub fn generate_private_key_with_retries(
  max_retries: Int,
) -> Result(secp256k1.PrivateKey, RandomError)

Generate a private key with specified retry attempts This is useful for testing or when you want control over retry behavior

pub fn generate_secure_bytes(
  length: Int,
) -> Result(BitArray, RandomError)
pub fn is_retryable_error(error: RandomError) -> Bool

Check if an error indicates a temporary failure that might succeed on retry

pub const min_test_samples: Int

Minimum entropy test sample size

pub const private_key_length: Int

Standard private key length for secp256k1 (32 bytes)

pub fn test_random_availability() -> Result(Nil, RandomError)

Check if the random system is available and working

pub fn test_randomness_quality(
  sample_count: Int,
  byte_length: Int,
) -> Result(RandomnessTestResult, RandomError)

Test the quality of system randomness by generating multiple samples and checking for basic statistical properties. This is useful for development and debugging.

Note: This is not a comprehensive randomness test - for production systems, use proper statistical test suites like NIST SP 800-22.

Search Document