Skip to content

Commit 43027c2

Browse files
committed
Format warnings fixes
1 parent 90d12da commit 43027c2

File tree

3 files changed

+107
-57
lines changed

3 files changed

+107
-57
lines changed

benches/astrobwtv3.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,5 @@ fn criterion_benchmark(c: &mut Criterion) {
1515
c.bench_function("astrobwtv3", |b| b.iter(|| astrobwtv3_bench(&input)));
1616
}
1717

18-
criterion_group!(
19-
benches,
20-
criterion_benchmark);
21-
criterion_main!(
22-
benches
23-
);
18+
criterion_group!(benches, criterion_benchmark);
19+
criterion_main!(benches);

src/astrobwtv3.rs

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
use rc4::KeyInit;
33
use rc4::Rc4;
44
use rc4::StreamCipher;
5-
use salsa20::Salsa20;
65
use salsa20::cipher::KeyIvInit;
6+
use salsa20::Salsa20;
77
use sha2::Digest;
88
use sha2::Sha256;
99
use siphasher::sip::SipHasher24;
@@ -46,7 +46,7 @@ const BRANCH_TABLE: [u32; 256] = [
4646
0x0C0B0D06, 0x0407080D, 0x08010203, 0x04060105, 0x00070009, 0x0D0A0C09, 0x02050A0A, 0x0D070308,
4747
0x02020E0F, 0x0B090D09, 0x05020703, 0x0C020D04, 0x03000501, 0x0F060C0D, 0x00000D01, 0x0F0B0205,
4848
0x04000506, 0x0E09030B, 0x00000103, 0x0F0C090B, 0x040C080F, 0x010F0C07, 0x000B0700, 0x0F0C0F04,
49-
0x0401090F, 0x080E0E0A, 0x050A090E, 0x0009080C, 0x080E0C06, 0x0D0C030D, 0x090D0C0D, 0x090D0C0D
49+
0x0401090F, 0x080E0E0A, 0x050A090E, 0x0009080C, 0x080E0C06, 0x0D0C030D, 0x090D0C0D, 0x090D0C0D,
5050
];
5151

5252
// Calculate and return sha256 hash.
@@ -90,7 +90,6 @@ fn sip24_calc(input: &[u8], k0: u64, k1: u64) -> u64 {
9090

9191
// The AstroBWTv3 calculation.
9292
pub fn astrobwtv3_hash(input: &[u8]) -> [u8; 32] {
93-
9493
// Step 1+2: calculate sha256 and expand data using Salsa20.
9594
let mut data: [u8; 256] = salsa20_calc(&(sha256_calc(input)));
9695

@@ -129,7 +128,6 @@ pub fn astrobwtv3_hash(input: &[u8]) -> [u8; 32] {
129128

130129
let opcode = BRANCH_TABLE[branch as usize];
131130
if branch == 254 || branch == 255 {
132-
133131
// Use a new key.
134132
rc4 = Rc4::new(&data.into());
135133
}
@@ -139,71 +137,68 @@ pub fn astrobwtv3_hash(input: &[u8]) -> [u8; 32] {
139137
let op = (opcode >> (j * 8)) & 0xFF;
140138
match op {
141139
0x00 => {
142-
tmp = tmp.wrapping_add(tmp); // +
140+
tmp = tmp.wrapping_add(tmp); // +
143141
}
144142
0x01 => {
145-
tmp = tmp.wrapping_sub(tmp ^ 97); // XOR and -
143+
tmp = tmp.wrapping_sub(tmp ^ 97); // XOR and -
146144
}
147145
0x02 => {
148-
tmp = tmp.wrapping_mul(tmp); // *
146+
tmp = tmp.wrapping_mul(tmp); // *
149147
}
150148
0x03 => {
151-
tmp = tmp ^ data[pos2 as usize]; // XOR
149+
tmp = tmp ^ data[pos2 as usize]; // XOR
152150
}
153151
0x04 => {
154-
tmp = !tmp; // binary NOT operator
152+
tmp = !tmp; // binary NOT operator
155153
}
156154
0x05 => {
157-
tmp = tmp & data[pos2 as usize]; // AND
155+
tmp = tmp & data[pos2 as usize]; // AND
158156
}
159157
0x06 => {
160-
tmp = tmp.wrapping_shl((tmp & 3) as u32); // shift left
158+
tmp = tmp.wrapping_shl((tmp & 3) as u32); // shift left
161159
}
162160
0x07 => {
163-
tmp = tmp.wrapping_shr((tmp & 3) as u32); // shift right
161+
tmp = tmp.wrapping_shr((tmp & 3) as u32); // shift right
164162
}
165163
0x08 => {
166-
tmp = tmp.reverse_bits(); // reverse bits
164+
tmp = tmp.reverse_bits(); // reverse bits
167165
}
168166
0x09 => {
169-
tmp = tmp ^ tmp.count_ones() as u8; // ones count bits
167+
tmp = tmp ^ tmp.count_ones() as u8; // ones count bits
170168
}
171169
0x0A => {
172-
tmp = tmp.rotate_left(tmp as u32); // rotate bits by random
170+
tmp = tmp.rotate_left(tmp as u32); // rotate bits by random
173171
}
174172
0x0B => {
175-
tmp = tmp.rotate_left(1); // rotate bits by 1
173+
tmp = tmp.rotate_left(1); // rotate bits by 1
176174
}
177175
0x0C => {
178-
tmp = tmp ^ tmp.rotate_left(2); // rotate bits by 2
176+
tmp = tmp ^ tmp.rotate_left(2); // rotate bits by 2
179177
}
180178
0x0D => {
181-
tmp = tmp.rotate_left(3); // rotate bits by 3
179+
tmp = tmp.rotate_left(3); // rotate bits by 3
182180
}
183181
0x0E => {
184-
tmp = tmp ^ tmp.rotate_left(4); // rotate bits by 4
182+
tmp = tmp ^ tmp.rotate_left(4); // rotate bits by 4
185183
}
186184
0x0F => {
187-
tmp = tmp.rotate_left(5); // rotate bits by 5
185+
tmp = tmp.rotate_left(5); // rotate bits by 5
188186
}
189187
_ => {
190188
unreachable!();
191189
}
192190
}
193-
194191
}
195192
data[i as usize] = tmp;
196193
if branch == 0 {
197194
if (pos2 - pos1) % 2 == 1 {
198-
199195
// Reverse.
200196
data[pos1 as usize] = data[pos1 as usize].reverse_bits();
201197
data[pos2 as usize] = data[pos2 as usize].reverse_bits();
202198
data.swap(pos1 as usize, pos2 as usize);
203199
}
204200
}
205201
if branch == 253 {
206-
207202
// More deviations.
208203
prev_lhash = prev_lhash.wrapping_add(lhash);
209204
lhash = xxh64_calc(&data[..pos2 as usize]);
@@ -216,31 +211,27 @@ pub fn astrobwtv3_hash(input: &[u8]) -> [u8; 32] {
216211

217212
// 6.25 % probability.
218213
if dp_minus < 0x10 {
219-
220214
// More deviations.
221215
prev_lhash = prev_lhash.wrapping_add(lhash);
222216
lhash = xxh64_calc(&data[..pos2 as usize]);
223217
}
224218

225219
// 12.5 % probability.
226220
if dp_minus < 0x20 {
227-
228221
// More deviations.
229222
prev_lhash = prev_lhash.wrapping_add(lhash);
230223
lhash = fnv1a_calc(&data[..pos2 as usize]);
231224
}
232225

233226
// 18.75 % probability.
234227
if dp_minus < 0x30 {
235-
236228
// More deviations.
237229
prev_lhash = prev_lhash.wrapping_add(lhash);
238230
lhash = sip24_calc(&data[..pos2 as usize], tries, prev_lhash);
239231
}
240232

241233
// 25% probablility.
242234
if dp_minus <= 0x40 {
243-
244235
// Do the rc4.
245236
stream = data.to_vec();
246237
rc4.apply_keystream(&mut stream);
@@ -253,19 +244,19 @@ pub fn astrobwtv3_hash(input: &[u8]) -> [u8; 32] {
253244
scratch_data[((tries - 1) * 256) as usize..(tries * 256) as usize].copy_from_slice(&data);
254245

255246
// Keep looping until condition is satisfied.
256-
if tries > 260+16 || (data[255] >= 0xf0 && tries > 260) {
247+
if tries > 260 + 16 || (data[255] >= 0xf0 && tries > 260) {
257248
break;
258249
}
259250
}
260251

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

264256
// Step 6: build our suffix array.
265257
let scratch_sa = SuffixArray::new(&scratch_data[..data_len as usize]);
266258
let mut scratch_sa_bytes: Vec<u8> = vec![];
267259
for vector in &scratch_sa.into_parts().1[1..(data_len as usize + 1)] {
268-
269260
// Little and big endian.
270261
if cfg!(target_endian = "little") {
271262
scratch_sa_bytes.extend_from_slice(&vector.to_le_bytes());

tests/astrobwtv3.rs

Lines changed: 84 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,99 @@ use spectrex::astrobwtv3;
33

44
// Source hashes.
55
const INPUT_HASHES: [[u8; 32]; 10] = [
6-
[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],
7-
[131, 190, 160, 208, 86, 121, 144, 207, 78, 235, 188, 253, 251, 0, 62, 161, 72, 225, 55, 184, 202, 212, 91, 125, 56, 204, 174, 214, 100, 28, 150, 15],
8-
[179, 131, 188, 255, 206, 72, 67, 215, 143, 44, 37, 210, 230, 54, 1, 114, 91, 104, 33, 126, 162, 70, 130, 110, 176, 105, 75, 122, 212, 119, 158, 216],
9-
[39, 225, 250, 46, 101, 197, 139, 171, 106, 53, 239, 59, 245, 146, 31, 67, 140, 201, 190, 86, 193, 24, 88, 104, 86, 69, 166, 231, 214, 240, 167, 63],
10-
[245, 133, 43, 254, 160, 213, 125, 60, 90, 42, 50, 123, 188, 209, 219, 125, 176, 250, 51, 158, 201, 167, 122, 244, 98, 108, 163, 110, 66, 26, 110, 177],
11-
[209, 138, 166, 16, 5, 177, 37, 64, 133, 13, 211, 172, 189, 223, 107, 148, 194, 78, 132, 23, 181, 25, 69, 149, 229, 95, 41, 73, 236, 217, 1, 119],
12-
[74, 36, 121, 180, 94, 215, 192, 195, 231, 0, 113, 44, 66, 171, 214, 173, 223, 29, 224, 201, 26, 217, 139, 162, 153, 31, 15, 158, 221, 165, 97, 74],
13-
[49, 189, 89, 13, 141, 86, 53, 198, 164, 148, 121, 198, 40, 219, 66, 13, 251, 73, 38, 16, 94, 71, 244, 44, 236, 119, 243, 202, 211, 199, 161, 164],
14-
[36, 23, 55, 103, 91, 244, 203, 137, 143, 244, 115, 22, 197, 152, 241, 34, 94, 40, 61, 246, 64, 251, 232, 23, 91, 203, 48, 233, 13, 70, 19, 6],
15-
[19, 247, 78, 146, 5, 164, 136, 199, 248, 218, 200, 24, 110, 186, 2, 253, 192, 139, 161, 70, 92, 156, 177, 77, 63, 124, 209, 236, 105, 130, 229, 218]
6+
[
7+
88, 101, 183, 41, 212, 156, 190, 48, 230, 97, 94, 105, 177, 86, 88, 84, 60, 239, 203, 124,
8+
63, 32, 160, 222, 34, 141, 50, 108, 138, 16, 90, 230,
9+
],
10+
[
11+
131, 190, 160, 208, 86, 121, 144, 207, 78, 235, 188, 253, 251, 0, 62, 161, 72, 225, 55,
12+
184, 202, 212, 91, 125, 56, 204, 174, 214, 100, 28, 150, 15,
13+
],
14+
[
15+
179, 131, 188, 255, 206, 72, 67, 215, 143, 44, 37, 210, 230, 54, 1, 114, 91, 104, 33, 126,
16+
162, 70, 130, 110, 176, 105, 75, 122, 212, 119, 158, 216,
17+
],
18+
[
19+
39, 225, 250, 46, 101, 197, 139, 171, 106, 53, 239, 59, 245, 146, 31, 67, 140, 201, 190,
20+
86, 193, 24, 88, 104, 86, 69, 166, 231, 214, 240, 167, 63,
21+
],
22+
[
23+
245, 133, 43, 254, 160, 213, 125, 60, 90, 42, 50, 123, 188, 209, 219, 125, 176, 250, 51,
24+
158, 201, 167, 122, 244, 98, 108, 163, 110, 66, 26, 110, 177,
25+
],
26+
[
27+
209, 138, 166, 16, 5, 177, 37, 64, 133, 13, 211, 172, 189, 223, 107, 148, 194, 78, 132, 23,
28+
181, 25, 69, 149, 229, 95, 41, 73, 236, 217, 1, 119,
29+
],
30+
[
31+
74, 36, 121, 180, 94, 215, 192, 195, 231, 0, 113, 44, 66, 171, 214, 173, 223, 29, 224, 201,
32+
26, 217, 139, 162, 153, 31, 15, 158, 221, 165, 97, 74,
33+
],
34+
[
35+
49, 189, 89, 13, 141, 86, 53, 198, 164, 148, 121, 198, 40, 219, 66, 13, 251, 73, 38, 16,
36+
94, 71, 244, 44, 236, 119, 243, 202, 211, 199, 161, 164,
37+
],
38+
[
39+
36, 23, 55, 103, 91, 244, 203, 137, 143, 244, 115, 22, 197, 152, 241, 34, 94, 40, 61, 246,
40+
64, 251, 232, 23, 91, 203, 48, 233, 13, 70, 19, 6,
41+
],
42+
[
43+
19, 247, 78, 146, 5, 164, 136, 199, 248, 218, 200, 24, 110, 186, 2, 253, 192, 139, 161, 70,
44+
92, 156, 177, 77, 63, 124, 209, 236, 105, 130, 229, 218,
45+
],
1646
];
1747

1848
// Expected hashes.
1949
const OUTPUT_HASHES: [[u8; 32]; 10] = [
20-
[245, 38, 108, 237, 82, 247, 189, 19, 237, 82, 86, 55, 2, 184, 61, 119, 46, 92, 244, 227, 116, 149, 198, 219, 96, 114, 78, 176, 95, 100, 70, 108],
21-
[253, 32, 214, 213, 188, 194, 70, 228, 112, 24, 6, 44, 126, 57, 113, 161, 136, 239, 160, 105, 186, 240, 50, 226, 251, 170, 152, 240, 56, 198, 210, 155],
22-
[65, 74, 3, 63, 226, 164, 225, 88, 106, 53, 218, 166, 144, 241, 106, 229, 179, 70, 126, 4, 251, 123, 47, 222, 21, 59, 215, 107, 158, 120, 220, 231],
23-
[155, 216, 182, 230, 186, 65, 127, 57, 112, 0, 112, 151, 217, 159, 28, 9, 108, 203, 111, 197, 28, 57, 187, 106, 119, 43, 187, 152, 185, 232, 76, 39],
24-
[142, 168, 196, 185, 54, 135, 241, 38, 229, 163, 12, 77, 61, 148, 126, 102, 91, 39, 191, 175, 181, 12, 18, 132, 39, 161, 222, 157, 170, 145, 13, 135],
25-
[35, 178, 78, 186, 52, 161, 140, 179, 209, 163, 146, 6, 40, 117, 215, 227, 154, 174, 92, 246, 212, 77, 96, 243, 94, 87, 135, 193, 151, 155, 220, 19],
26-
[176, 117, 190, 212, 164, 159, 17, 220, 116, 184, 102, 106, 61, 148, 204, 230, 46, 171, 41, 20, 214, 91, 151, 11, 48, 93, 240, 8, 37, 189, 240, 125],
27-
[60, 248, 1, 238, 24, 248, 240, 206, 56, 147, 254, 66, 226, 246, 107, 22, 164, 135, 124, 15, 102, 244, 33, 117, 126, 87, 71, 160, 243, 245, 34, 38],
28-
[76, 190, 131, 164, 182, 28, 224, 104, 232, 94, 56, 15, 28, 185, 196, 63, 149, 235, 83, 228, 139, 1, 112, 29, 30, 83, 30, 92, 29, 71, 75, 217],
29-
[147, 234, 41, 103, 132, 162, 41, 23, 249, 180, 98, 124, 32, 81, 107, 133, 162, 73, 44, 135, 179, 55, 228, 194, 113, 179, 161, 235, 85, 5, 111, 121]
50+
[
51+
245, 38, 108, 237, 82, 247, 189, 19, 237, 82, 86, 55, 2, 184, 61, 119, 46, 92, 244, 227,
52+
116, 149, 198, 219, 96, 114, 78, 176, 95, 100, 70, 108,
53+
],
54+
[
55+
253, 32, 214, 213, 188, 194, 70, 228, 112, 24, 6, 44, 126, 57, 113, 161, 136, 239, 160,
56+
105, 186, 240, 50, 226, 251, 170, 152, 240, 56, 198, 210, 155,
57+
],
58+
[
59+
65, 74, 3, 63, 226, 164, 225, 88, 106, 53, 218, 166, 144, 241, 106, 229, 179, 70, 126, 4,
60+
251, 123, 47, 222, 21, 59, 215, 107, 158, 120, 220, 231,
61+
],
62+
[
63+
155, 216, 182, 230, 186, 65, 127, 57, 112, 0, 112, 151, 217, 159, 28, 9, 108, 203, 111,
64+
197, 28, 57, 187, 106, 119, 43, 187, 152, 185, 232, 76, 39,
65+
],
66+
[
67+
142, 168, 196, 185, 54, 135, 241, 38, 229, 163, 12, 77, 61, 148, 126, 102, 91, 39, 191,
68+
175, 181, 12, 18, 132, 39, 161, 222, 157, 170, 145, 13, 135,
69+
],
70+
[
71+
35, 178, 78, 186, 52, 161, 140, 179, 209, 163, 146, 6, 40, 117, 215, 227, 154, 174, 92,
72+
246, 212, 77, 96, 243, 94, 87, 135, 193, 151, 155, 220, 19,
73+
],
74+
[
75+
176, 117, 190, 212, 164, 159, 17, 220, 116, 184, 102, 106, 61, 148, 204, 230, 46, 171, 41,
76+
20, 214, 91, 151, 11, 48, 93, 240, 8, 37, 189, 240, 125,
77+
],
78+
[
79+
60, 248, 1, 238, 24, 248, 240, 206, 56, 147, 254, 66, 226, 246, 107, 22, 164, 135, 124, 15,
80+
102, 244, 33, 117, 126, 87, 71, 160, 243, 245, 34, 38,
81+
],
82+
[
83+
76, 190, 131, 164, 182, 28, 224, 104, 232, 94, 56, 15, 28, 185, 196, 63, 149, 235, 83, 228,
84+
139, 1, 112, 29, 30, 83, 30, 92, 29, 71, 75, 217,
85+
],
86+
[
87+
147, 234, 41, 103, 132, 162, 41, 23, 249, 180, 98, 124, 32, 81, 107, 133, 162, 73, 44, 135,
88+
179, 55, 228, 194, 113, 179, 161, 235, 85, 5, 111, 121,
89+
],
3090
];
3191

3292
// AstroBWTv3 tests.
3393
#[test]
3494
fn astrobwtv3_hash_10() {
3595
for index in 0..INPUT_HASHES.len() {
36-
assert_eq!(OUTPUT_HASHES[index], astrobwtv3::astrobwtv3_hash(&INPUT_HASHES[index]));
96+
assert_eq!(
97+
OUTPUT_HASHES[index],
98+
astrobwtv3::astrobwtv3_hash(&INPUT_HASHES[index])
99+
);
37100
}
38101
}

0 commit comments

Comments
 (0)