Skip to content

Quantus-Network/hash-comparison

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Hash Function Performance Comparison

A Rust project for benchmarking the performance of SHAKE, SHA2, and SHA3 hash functions across randomly generated inputs of various sizes.

Overview

This project provides comprehensive benchmarks for:

SHA2 Family

  • SHA2-224
  • SHA2-256
  • SHA2-384
  • SHA2-512

SHA3 Family

  • SHA3-224
  • SHA3-256
  • SHA3-384
  • SHA3-512

SHAKE Functions (Extendable Output Functions)

  • SHAKE128
  • SHAKE256

Features

  • Criterion-based benchmarks: Uses the Criterion library for statistical analysis and accurate performance measurements
  • Multiple input sizes: Tests performance across different data sizes (1KB, 4KB, 16KB, 64KB, 256KB)
  • Throughput analysis: Measures both execution time and throughput (MB/s)
  • HTML reports: Generates detailed HTML reports with graphs and statistics
  • Random data generation: Uses cryptographically secure random data for testing

Requirements

  • Rust 1.70 or later
  • Cargo

Usage

Running Benchmarks

To run all benchmarks:

cargo bench

To run benchmarks for specific hash functions:

# Run only SHA2 benchmarks
cargo bench "SHA2"

# Run only SHA3 benchmarks
cargo bench "SHA3"

# Run only SHAKE benchmarks
cargo bench "SHAKE"

# Run the comparison benchmark (all algorithms with 16KB data)
cargo bench "Hash Comparison"

Running the Demo Application

cargo run

This will run a quick demonstration showing the hash functions in action with sample data.

Running Tests

cargo test

Benchmark Results

After running benchmarks, you can find detailed HTML reports in the target/criterion/ directory. Open target/criterion/report/index.html in your browser to view interactive performance charts.

The benchmarks measure:

  • Execution time: How long each hash function takes
  • Throughput: Data processed per second (MB/s)
  • Statistical analysis: Mean, standard deviation, and confidence intervals

Understanding the Results

Expected Performance Characteristics

Generally, you can expect:

  1. SHA2 vs SHA3: SHA2 is typically faster than SHA3 due to hardware optimizations and simpler operations
  2. Hash size impact: Larger hash sizes (512-bit) may be slower than smaller ones (256-bit) for the same family
  3. SHAKE functions: Performance depends on the requested output length
  4. Input size scaling: Throughput often increases with larger input sizes due to amortized overhead

Sample Output

Hash Comparison/SHA2-256    time:   [45.2 µs 45.8 µs 46.4 µs]
                           thrpt:  [345.2 MB/s 350.1 MB/s 354.7 MB/s]

Hash Comparison/SHA3-256    time:   [89.1 µs 90.2 µs 91.4 µs]
                           thrpt:  [175.4 MB/s 177.8 MB/s 179.9 MB/s]

Dependencies

  • sha2: SHA2 family implementation
  • sha3: SHA3 and SHAKE implementation
  • criterion: Benchmarking framework
  • rand: Random data generation
  • clap: Command line parsing (for future CLI features)

Project Structure

hash-compare/
├── src/
│   └── main.rs          # Library functions and demo app
├── benches/
│   └── hash_benchmark.rs # Criterion benchmarks
├── Cargo.toml           # Dependencies and configuration
└── README.md            # This file

Extending the Project

Adding New Hash Functions

  1. Add the dependency to Cargo.toml
  2. Import the hash function in src/main.rs
  3. Create wrapper functions following the existing pattern
  4. Add benchmark functions in benches/hash_benchmark.rs

Customizing Benchmark Parameters

You can modify the input sizes in benches/hash_benchmark.rs:

for size in [1024, 4096, 16384, 65536, 262144].iter() {
    // Add or modify sizes here
}

Performance Tips

  • Run benchmarks in release mode (automatically done with cargo bench)
  • Close other applications to reduce system noise
  • Run on consistent hardware for reproducible results
  • Consider CPU frequency scaling and thermal throttling

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Add your improvements
  4. Run tests and benchmarks
  5. Submit a pull request

License

This project is open source. Feel free to use, modify, and distribute.

About

Benchmarks comparing different hash functions

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages