Skip to content

Commit bd61481

Browse files
committed
Initial version
0 parents  commit bd61481

File tree

8 files changed

+2420
-0
lines changed

8 files changed

+2420
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/target

Cargo.toml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
[package]
2+
name = "spectrex"
3+
version = "0.3.14"
4+
edition = "2021"
5+
authors = ["Spectre developers"]
6+
repository = "https://github.com/spectre-project/rusty-spectrex"
7+
license = "ISC"
8+
description = "SpectreX is the AstroBWTv3 CPU mining algorithm in Rust."
9+
readme = "README.md"
10+
keywords = ["hash", "algorithm", "library", "spectrex", "astrobwt"]
11+
categories = ["algorithms", "cryptography"]
12+
homepage = "https://spectre-network.org"
13+
14+
[dependencies]
15+
fnv = "1.0.7"
16+
psacak = "0.1.0"
17+
rc4 = "0.1.0"
18+
salsa20 = "0.10.2"
19+
sha2 = "0.10.8"
20+
siphasher = "1.0.1"
21+
xxhash-rust = { version = "0.8.10", features = ["xxh64"] }
22+
23+
[dev-dependencies]
24+
criterion = "0.5.1"
25+
rand = "0.8.5"
26+
27+
[[bench]]
28+
name = "astrobwtv3"
29+
harness = false

LICENSE

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
ISC License
2+
3+
Copyright (c) 2024-2024 Spectre developers
4+
5+
Permission to use, copy, modify, and distribute this software for any
6+
purpose with or without fee is hereby granted, provided that the above
7+
copyright notice and this permission notice appear in all copies.
8+
9+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

README.md

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# SpectreX
2+
3+
[![Crates.io](https://img.shields.io/crates/v/spectrex.svg)](https://crates.io/crates/spectrex)
4+
[![GitHub license](https://img.shields.io/github/license/spectre-project/rusty-spectrex.svg)](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+
```

benches/astrobwtv3.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Public crates.
2+
use criterion::criterion_group;
3+
use criterion::criterion_main;
4+
use criterion::Criterion;
5+
6+
// Private crates.
7+
use spectrex::astrobwtv3;
8+
9+
fn astrobwtv3_bench() {
10+
let input: [u8; 32] = rand::random();
11+
astrobwtv3::astrobwtv3_hash(&input);
12+
}
13+
14+
fn criterion_benchmark(c: &mut Criterion) {
15+
c.bench_function("astrobwtv3", |b| b.iter(|| astrobwtv3_bench()));
16+
}
17+
18+
criterion_group!(
19+
benches,
20+
criterion_benchmark);
21+
criterion_main!(
22+
benches
23+
);

0 commit comments

Comments
 (0)