|
| 1 | +# SpectreX |
| 2 | + |
| 3 | +[](https://crates.io/crates/spectrex) |
| 4 | +[](https://github.com/spectre-project/rusty-spectrex/blob/main/LICENSE) |
| 5 | + |
| 6 | +SpectreX is a versatile CPU mining algorithm library used by the |
| 7 | +[Spectre On Rust](https://github.com/spectre-project/rusty-spectre) |
| 8 | +full-node daemon. |
| 9 | + |
| 10 | +## Overview |
| 11 | + |
| 12 | +SpectreX features the [AstroBWTv3](https://github.com/deroproject/derohe/tree/main/astrobwt/astrobwtv3) |
| 13 | +algorithm, a proof-of-work (PoW) system based on the Burrows-Wheeler |
| 14 | +transform (BWT). This version of AstroBWTv3 is completely written in |
| 15 | +Rust, without any external C dependencies, relying solely on various |
| 16 | +Rust crates. |
| 17 | + |
| 18 | +## Hashing Function |
| 19 | + |
| 20 | +The proof-of-work calculation involves a series of sequential hashing |
| 21 | +functions to form the final hash: |
| 22 | + |
| 23 | +* Step 1: Calculate sha256 of the input data. |
| 24 | +* Step 2: Expand data using Salsa20. |
| 25 | +* Step 3: Encrypt data with RC4 based on the output from step 2. |
| 26 | +* Step 4: Compute an initial FNV-1a hash of the result from step 3. |
| 27 | +* Step 5: Apply a branchy loop using RC4 stream encryption on the data from step 4. |
| 28 | +* Step 6: Build and sort a Suffix Array with the result from step 5. |
| 29 | +* Step 7: Calculate the final sha256 of the data from step 6. |
| 30 | + |
| 31 | +## Improvements |
| 32 | + |
| 33 | +The original algorithm utilized the [SA-IS](https://en.wikipedia.org/wiki/Suffix_array) |
| 34 | +sorting algorithm. There exists an enhanced one with [SACA-K](https://www.sciencedirect.com/science/article/abs/pii/S0020019016301375) |
| 35 | +for induced sorting, improving the linear-time complexity to be |
| 36 | +in-place for constant alphabets. However, this remains a single-core |
| 37 | +variant. Our AstroBWTv3 implementation has switched to |
| 38 | +[pSACAK](https://ieeexplore.ieee.org/document/8371211), a fast |
| 39 | +linear-time, in-place parallel algorithm that leverages multi-core |
| 40 | +machines. It is fully compatible with the original AstroBWTv3 Suffix |
| 41 | +Array. |
| 42 | + |
| 43 | +There are still numerous opportunities to enhance the computation of |
| 44 | +AstroBWTv3 hashes, including: |
| 45 | + |
| 46 | +* Replacing most steps with highly optimized inline assembler code on |
| 47 | + CPU. |
| 48 | +* Partitioning the Suffix Array and offloading sorting to GPUs to |
| 49 | + significantly boost performance. |
| 50 | + |
| 51 | +We encourage developers to optimize individual calculation steps to |
| 52 | +evolve the algorithm over time and mature the codebase. |
| 53 | + |
| 54 | +## Usage |
| 55 | + |
| 56 | +To include SpectreX in your project dependencies, just run the command |
| 57 | +`cargo add spectrex`. Here's a straightforward example: |
| 58 | + |
| 59 | +```rust |
| 60 | +use spectrex::astrobwtv3; |
| 61 | + |
| 62 | +fn main() { |
| 63 | + let hash_in: [u8; 32] = [88, 101, 183, 41, 212, 156, 190, 48, 230, 97, 94, 105, 177, 86, 88, 84, 60, 239, 203, 124, 63, 32, 160, 222, 34, 141, 50, 108, 138, 16, 90, 230]; |
| 64 | + let hash_out = astrobwtv3::astrobwtv3_hash(&hash_in); |
| 65 | + println!("hash_out: {:?}", hash_out); |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +## Tests |
| 70 | + |
| 71 | +Below is a basic computation test designed to ensure the accuracy of |
| 72 | +computed hashes across various byte orders. You can execute it using |
| 73 | +`cargo test`, and upon successful completion, it will display the |
| 74 | +following output: |
| 75 | + |
| 76 | +``` |
| 77 | +running 1 test |
| 78 | +test astrobwtv3_hash_10 ... ok |
| 79 | +
|
| 80 | +test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.54s |
| 81 | +``` |
| 82 | + |
| 83 | +## Benchmarks |
| 84 | + |
| 85 | +Included is a simple computation benchmark using [Criterion](https://github.com/bheisler/criterion.rs). |
| 86 | +This benchmark helps verify any performance improvements or |
| 87 | +degradations if any calculation steps have been modified. You can run |
| 88 | +it using `cargo bench`, and it will return the following results: |
| 89 | + |
| 90 | +``` |
| 91 | +astrobwtv3 time: [8.8912 ms 9.0648 ms 9.2387 ms] |
| 92 | + change: [-7.8592% -4.5050% -1.1622%] (p = 0.01 < 0.05) |
| 93 | + Performance has improved. |
| 94 | +Found 4 outliers among 100 measurements (4.00%) |
| 95 | + 4 (4.00%) high mild |
| 96 | +``` |
0 commit comments