Skip to content

Commit eee398d

Browse files
authored
Merge pull request #3 from x100111010/main
add initial actions workflow and some refactoring
2 parents 43027c2 + 9706b5d commit eee398d

File tree

4 files changed

+59
-41
lines changed

4 files changed

+59
-41
lines changed

.github/workflows/ci.yaml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
name: Tests
2+
on: [push, pull_request]
3+
4+
jobs:
5+
test:
6+
name: Test & Benchmark
7+
runs-on: ubuntu-latest
8+
steps:
9+
- name: Checkout sources
10+
uses: actions/checkout@v4
11+
12+
- name: Install toolchain
13+
uses: dtolnay/rust-toolchain@stable
14+
15+
- name: Run tests
16+
run: cargo test
17+
18+
- name: Run benchmarks
19+
run: cargo bench
20+
21+
lints:
22+
name: Lints
23+
runs-on: ubuntu-latest
24+
steps:
25+
- name: Checkout sources
26+
uses: actions/checkout@v4
27+
28+
- name: Install toolchain
29+
uses: dtolnay/rust-toolchain@stable
30+
with:
31+
components: rustfmt, clippy
32+
33+
- name: Run cargo fmt
34+
run: cargo fmt --all -- --check
35+
36+
- name: Run cargo clippy
37+
run: cargo clippy --tests -- -D warnings

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "spectrex"
3-
version = "0.3.18"
3+
version = "0.3.19"
44
edition = "2021"
55
authors = ["Spectre developers"]
66
repository = "https://github.com/spectre-project/rusty-spectrex"
@@ -22,7 +22,7 @@ xxhash-rust = { version = "0.8.10", features = ["xxh64"] }
2222

2323
[dev-dependencies]
2424
criterion = "0.5.1"
25-
rand = "0.8.5"
25+
rand = "0.9.0"
2626

2727
[[bench]]
2828
name = "astrobwtv3"

benches/astrobwtv3.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
// Public crates.
2-
use criterion::criterion_group;
3-
use criterion::criterion_main;
4-
use criterion::Criterion;
2+
use criterion::{criterion_group, criterion_main, Criterion};
53

64
// Private crates.
75
use spectrex::astrobwtv3;

src/astrobwtv3.rs

Lines changed: 19 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
// Public crates.
2-
use rc4::KeyInit;
3-
use rc4::Rc4;
4-
use rc4::StreamCipher;
5-
use salsa20::cipher::KeyIvInit;
6-
use salsa20::Salsa20;
7-
use sha2::Digest;
8-
use sha2::Sha256;
2+
use rc4::{KeyInit, Rc4, StreamCipher};
3+
use salsa20::{cipher::KeyIvInit, Salsa20};
4+
use sha2::{Digest, Sha256};
95
use siphasher::sip::SipHasher24;
106
use std::hash::Hasher;
117
use suffix_array::SuffixArray;
@@ -51,12 +47,9 @@ const BRANCH_TABLE: [u32; 256] = [
5147

5248
// Calculate and return sha256 hash.
5349
fn sha256_calc(input: &[u8]) -> [u8; 32] {
54-
let mut output: [u8; 32] = [0; 32];
5550
let mut hasher = Sha256::new();
5651
hasher.update(input);
57-
58-
output.copy_from_slice(hasher.finalize().as_slice());
59-
output
52+
hasher.finalize().into()
6053
}
6154

6255
// Encrypt and return salsa20 stream.
@@ -71,21 +64,17 @@ fn salsa20_calc(key: &[u8; 32]) -> [u8; 256] {
7164
fn fnv1a_calc(input: &[u8]) -> u64 {
7265
let mut hasher = fnv::FnvHasher::default();
7366
hasher.write(input);
74-
let output = hasher.finish();
75-
output
67+
hasher.finish()
7668
}
7769

7870
// Calculate and return xxh64 hash.
7971
fn xxh64_calc(input: &[u8]) -> u64 {
80-
let output = xxhash_rust::xxh64::xxh64(input, 0);
81-
output
72+
xxhash_rust::xxh64::xxh64(input, 0)
8273
}
8374

8475
// Calculate and return sip24 hash.
8576
fn sip24_calc(input: &[u8], k0: u64, k1: u64) -> u64 {
86-
let hasher = SipHasher24::new_with_keys(k0, k1);
87-
let output = hasher.hash(input);
88-
output
77+
SipHasher24::new_with_keys(k0, k1).hash(input)
8978
}
9079

9180
// The AstroBWTv3 calculation.
@@ -95,9 +84,7 @@ pub fn astrobwtv3_hash(input: &[u8]) -> [u8; 32] {
9584

9685
// Step 3: rc4.
9786
let mut rc4 = Rc4::new(&data.into());
98-
let mut stream = data.to_vec();
99-
rc4.apply_keystream(&mut stream);
100-
data.copy_from_slice(&stream);
87+
rc4.apply_keystream(&mut data);
10188

10289
// Step 4: fnv1a.
10390
let mut lhash = fnv1a_calc(&data);
@@ -146,13 +133,13 @@ pub fn astrobwtv3_hash(input: &[u8]) -> [u8; 32] {
146133
tmp = tmp.wrapping_mul(tmp); // *
147134
}
148135
0x03 => {
149-
tmp = tmp ^ data[pos2 as usize]; // XOR
136+
tmp ^= data[pos2 as usize]; // XOR
150137
}
151138
0x04 => {
152139
tmp = !tmp; // binary NOT operator
153140
}
154141
0x05 => {
155-
tmp = tmp & data[pos2 as usize]; // AND
142+
tmp &= data[pos2 as usize]; // AND
156143
}
157144
0x06 => {
158145
tmp = tmp.wrapping_shl((tmp & 3) as u32); // shift left
@@ -190,13 +177,11 @@ pub fn astrobwtv3_hash(input: &[u8]) -> [u8; 32] {
190177
}
191178
}
192179
data[i as usize] = tmp;
193-
if branch == 0 {
194-
if (pos2 - pos1) % 2 == 1 {
195-
// Reverse.
196-
data[pos1 as usize] = data[pos1 as usize].reverse_bits();
197-
data[pos2 as usize] = data[pos2 as usize].reverse_bits();
198-
data.swap(pos1 as usize, pos2 as usize);
199-
}
180+
if branch == 0 && (pos2 - pos1) % 2 == 1 {
181+
// Reverse.
182+
data[pos1 as usize] = data[pos1 as usize].reverse_bits();
183+
data[pos2 as usize] = data[pos2 as usize].reverse_bits();
184+
data.swap(pos1 as usize, pos2 as usize);
200185
}
201186
if branch == 253 {
202187
// More deviations.
@@ -230,12 +215,10 @@ pub fn astrobwtv3_hash(input: &[u8]) -> [u8; 32] {
230215
lhash = sip24_calc(&data[..pos2 as usize], tries, prev_lhash);
231216
}
232217

233-
// 25% probablility.
218+
// 25% probability.
234219
if dp_minus <= 0x40 {
235220
// Do the rc4.
236-
stream = data.to_vec();
237-
rc4.apply_keystream(&mut stream);
238-
data.copy_from_slice(&stream);
221+
rc4.apply_keystream(&mut data);
239222
}
240223

241224
data[255] ^= data[pos1 as usize] ^ data[pos2 as usize];
@@ -250,8 +233,8 @@ pub fn astrobwtv3_hash(input: &[u8]) -> [u8; 32] {
250233
}
251234

252235
// We may discard up to ~ 1KiB data from the stream to ensure that wide number of variants exists.
253-
let data_len =
254-
(tries - 4) as u32 * 256 + (((data[253] as u64) << 8 | (data[254] as u64)) as u32 & 0x3ff);
236+
let data_len = (tries - 4) as u32 * 256
237+
+ ((((data[253] as u64) << 8) | (data[254] as u64)) as u32 & 0x3ff);
255238

256239
// Step 6: build our suffix array.
257240
let scratch_sa = SuffixArray::new(&scratch_data[..data_len as usize]);

0 commit comments

Comments
 (0)