Skip to content

Commit b23fbfa

Browse files
authored
Merge pull request #20 from itzmeanjan/do-not-depend-on-unstable-toolchain
Do not depend on the `unstable` Rust Toolchain
2 parents d28cf3d + 812cd6b commit b23fbfa

18 files changed

+876
-1721
lines changed

.cargo/config.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[build]
2+
rustflags = ["-C", "target-cpu=native"]

.github/workflows/rust.yml

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,34 @@ name: TurboSHAKE
22

33
on:
44
push:
5-
branches: [ "master" ]
5+
branches: ["master"]
66
pull_request:
7-
branches: [ "master" ]
7+
branches: ["master"]
88

99
env:
1010
CARGO_TERM_COLOR: always
1111

1212
jobs:
1313
build:
14-
15-
runs-on: ubuntu-latest
14+
runs-on: ${{matrix.os}}
15+
strategy:
16+
matrix:
17+
# See https://docs.github.com/en/actions/writing-workflows/choosing-where-your-workflow-runs/choosing-the-runner-for-a-job#standard-github-hosted-runners
18+
# for available targets.
19+
os: [
20+
ubuntu-latest, # x86-64
21+
ubuntu-24.04-arm, # aarch64
22+
macos-latest, # aarch64
23+
macos-13, # x86_64
24+
windows-latest, # x86-64
25+
]
1626

1727
steps:
18-
- uses: actions/checkout@v3
19-
- name: Run tests
20-
run: RUSTFLAGS="-C opt-level=3 -C target-cpu=native" cargo test --lib --features simdx2,simdx4
21-
- name: Run examples
22-
run: cargo run --example turboshake128 && cargo run --example turboshake256
28+
- uses: actions/checkout@v4
29+
- uses: actions-rust-lang/setup-rust-toolchain@v1
30+
31+
- name: Build and Test on ${{ matrix.os }}
32+
run: cargo test
33+
34+
- name: Run Example on ${{ matrix.os }}
35+
run: cargo run --example turboshake128 && cargo run --example turboshake256

.rustfmt.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
max_width = 160

Cargo.toml

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,28 @@
11
[package]
22
name = "turboshake"
3-
version = "0.2.0"
3+
version = "0.3.0"
44
edition = "2021"
5+
resolver = "2"
6+
rust-version = "1.84.0"
57
authors = ["Anjan Roy <[email protected]>"]
68
description = "A family of extendable output functions based on keccak-p[1600, 12] permutation"
7-
documentation = "https://github.com/itzmeanjan/turboshake"
89
readme = "README.md"
910
repository = "https://github.com/itzmeanjan/turboshake.git"
1011
license = "MIT"
1112
keywords = ["cryptography", "keccak", "xof", "hashing", "turboshake"]
12-
categories = ["cryptography"]
13+
categories = ["cryptography", "algorithms"]
1314

1415
[features]
1516
dev = []
16-
simdx2 = []
17-
simdx4 = []
1817

1918
[dependencies]
20-
crunchy = "=0.2.2"
2119

2220
[dev-dependencies]
23-
rand = "=0.8.5"
21+
rand = "=0.9.0"
22+
rand_chacha = "=0.9.0"
2423
hex = "=0.4.3"
25-
criterion = "=0.5.1"
26-
test-case = "=3.2.1"
27-
28-
[target.'cfg(any(target_arch = "x86", target_arch = "x86_64", target_arch = "aarch64", target_arch = "loongarch64"))'.dev-dependencies]
29-
criterion-cycles-per-byte = "=0.6.0"
30-
31-
[lib]
32-
bench = false
24+
test-case = "=3.3.1"
25+
divan = "=0.1.17"
3326

3427
[[bench]]
3528
name = "keccak"
@@ -40,10 +33,8 @@ required-features = ["dev"]
4033
name = "turboshake"
4134
harness = false
4235

43-
[[example]]
44-
name = "keccak"
45-
required-features = ["dev"]
46-
47-
[[example]]
48-
name = "simd_keccak"
49-
required-features = ["dev", "simdx4"]
36+
[profile.optimized]
37+
inherits = "release"
38+
codegen-units = 1
39+
lto = "thin"
40+
panic = "abort"

README.md

Lines changed: 60 additions & 440 deletions
Large diffs are not rendered by default.

benches/keccak.rs

Lines changed: 17 additions & 157 deletions
Original file line numberDiff line numberDiff line change
@@ -1,164 +1,24 @@
1-
use criterion::{black_box, criterion_group, criterion_main, BatchSize, Criterion, Throughput};
2-
use rand::{thread_rng, Rng};
1+
use divan;
2+
use divan::counter::{BytesCount, BytesFormat, ItemsCount};
3+
use rand::prelude::*;
4+
use rand_chacha::ChaCha8Rng;
35
use turboshake::keccak;
46

5-
#[cfg(any(
6-
target_arch = "x86_64",
7-
target_arch = "x86",
8-
target_arch = "aarch64",
9-
target_arch = "loongarch64"
10-
))]
11-
use criterion_cycles_per_byte::CyclesPerByte;
12-
13-
#[cfg(any(
14-
target_arch = "x86_64",
15-
target_arch = "x86",
16-
target_arch = "aarch64",
17-
target_arch = "loongarch64"
18-
))]
19-
type CriterionHandler = Criterion<CyclesPerByte>;
20-
21-
#[cfg(not(any(
22-
target_arch = "x86_64",
23-
target_arch = "x86",
24-
target_arch = "aarch64",
25-
target_arch = "loongarch64"
26-
)))]
27-
type CriterionHandler = Criterion;
28-
29-
#[cfg(not(any(feature = "simdx2", feature = "simdx4")))]
30-
fn keccak(c: &mut CriterionHandler) {
31-
let mut rng = thread_rng();
32-
33-
let mut group = c.benchmark_group("keccak");
34-
group.throughput(Throughput::Bytes(200u64)); // size of keccak-p[1600] permutation state
35-
36-
group.bench_function("keccak-p[1600, 12] (cached)", |bench| {
37-
let mut state = [0u64; 25];
38-
rng.fill(&mut state);
39-
40-
bench.iter(|| keccak::permute(black_box(&mut state)))
41-
});
42-
group.bench_function("keccak-p[1600, 12] (random)", |bench| {
43-
let mut state = [0u64; 25];
44-
rng.fill(&mut state);
45-
46-
bench.iter_batched(
47-
|| state.clone(),
48-
|mut state| keccak::permute(black_box(&mut state)),
49-
BatchSize::SmallInput,
50-
)
51-
});
52-
53-
group.finish();
7+
fn main() {
8+
divan::Divan::default().bytes_format(BytesFormat::Binary).run_benches();
549
}
5510

56-
#[cfg(any(feature = "simdx2", feature = "simdx4"))]
57-
fn keccak(c: &mut CriterionHandler) {
58-
let mut rng = thread_rng();
59-
60-
let mut group = c.benchmark_group("keccak");
61-
62-
#[cfg(feature = "simdx2")]
63-
{
64-
group.throughput(Throughput::Bytes(200u64 * 2));
11+
#[divan::bench]
12+
fn permute_12_rounds(bencher: divan::Bencher) {
13+
let mut rng = ChaCha8Rng::from_os_rng();
6514

66-
group.bench_function("keccak-p[1600, 12] x2 (cached)", |bench| {
67-
let mut state0 = [0u64; 25];
68-
let mut state1 = [0u64; 25];
69-
rng.fill(&mut state0);
70-
rng.fill(&mut state1);
15+
let mut state = [0u64; 25];
16+
let state_byte_len = state.len() * 8;
17+
rng.fill(&mut state);
7118

72-
bench.iter(|| keccak::permutex2(black_box(&mut state0), black_box(&mut state1)));
73-
});
74-
group.bench_function("keccak-p[1600, 12] x2 (random)", |bench| {
75-
let mut state0 = [0u64; 25];
76-
let mut state1 = [0u64; 25];
77-
rng.fill(&mut state0);
78-
rng.fill(&mut state1);
79-
80-
bench.iter_batched(
81-
|| (state0.clone(), state1.clone()),
82-
|(mut state0, mut state1)| {
83-
keccak::permutex2(black_box(&mut state0), black_box(&mut state1))
84-
},
85-
BatchSize::SmallInput,
86-
)
87-
});
88-
}
89-
90-
#[cfg(feature = "simdx4")]
91-
{
92-
group.throughput(Throughput::Bytes(200u64 * 4));
93-
94-
group.bench_function("keccak-p[1600, 12] x4 (cached)", |bench| {
95-
let mut state0 = [0u64; 25];
96-
let mut state1 = [0u64; 25];
97-
let mut state2 = [0u64; 25];
98-
let mut state3 = [0u64; 25];
99-
rng.fill(&mut state0);
100-
rng.fill(&mut state1);
101-
rng.fill(&mut state2);
102-
rng.fill(&mut state3);
103-
104-
bench.iter(|| {
105-
keccak::permutex4(
106-
black_box(&mut state0),
107-
black_box(&mut state1),
108-
black_box(&mut state2),
109-
black_box(&mut state3),
110-
)
111-
});
112-
});
113-
group.bench_function("keccak-p[1600, 12] x4 (random)", |bench| {
114-
let mut state0 = [0u64; 25];
115-
let mut state1 = [0u64; 25];
116-
let mut state2 = [0u64; 25];
117-
let mut state3 = [0u64; 25];
118-
rng.fill(&mut state0);
119-
rng.fill(&mut state1);
120-
rng.fill(&mut state2);
121-
rng.fill(&mut state3);
122-
123-
bench.iter_batched(
124-
|| {
125-
(
126-
state0.clone(),
127-
state1.clone(),
128-
state2.clone(),
129-
state3.clone(),
130-
)
131-
},
132-
|(mut state0, mut state1, mut state2, mut state3)| {
133-
keccak::permutex4(
134-
black_box(&mut state0),
135-
black_box(&mut state1),
136-
black_box(&mut state2),
137-
black_box(&mut state3),
138-
)
139-
},
140-
BatchSize::SmallInput,
141-
)
142-
});
143-
}
144-
145-
group.finish();
19+
bencher
20+
.counter(BytesCount::new(state_byte_len))
21+
.counter(ItemsCount::new(1usize))
22+
.with_inputs(|| (state.clone()))
23+
.bench_values(|mut state| keccak::permute(divan::black_box(&mut state)));
14624
}
147-
148-
#[cfg(any(
149-
target_arch = "x86_64",
150-
target_arch = "x86",
151-
target_arch = "aarch64",
152-
target_arch = "loongarch64"
153-
))]
154-
criterion_group!(name = permutation; config = Criterion::default().with_measurement(CyclesPerByte); targets = keccak);
155-
156-
#[cfg(not(any(
157-
target_arch = "x86_64",
158-
target_arch = "x86",
159-
target_arch = "aarch64",
160-
target_arch = "loongarch64"
161-
)))]
162-
criterion_group!(permutation, keccak);
163-
164-
criterion_main!(permutation);

0 commit comments

Comments
 (0)