Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chacha20: returning rand_core feature #333

Merged
merged 96 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
96 commits
Select commit Hold shift + click to select a range
b1fa885
added rand_core feature back
nstilt1 Oct 25, 2023
c5226a9
Delete .vscode directory
nstilt1 Oct 25, 2023
04aabff
fix suggestion for u128 stream_id
nstilt1 Oct 25, 2023
ec262fd
Merge branch 'master' of https://github.com/nstilt1/stream-ciphers-rng
nstilt1 Oct 25, 2023
8a1755f
Update rng.rs
nstilt1 Oct 25, 2023
2117c30
Update rng.rs
nstilt1 Oct 25, 2023
f5b9a4d
remove copied data in generate()
nstilt1 Oct 25, 2023
53a51a7
Merge branch 'master' of https://github.com/nstilt1/stream-ciphers-rng
nstilt1 Oct 25, 2023
425c159
cargo fmt
nstilt1 Oct 25, 2023
a241489
impl ZeroizeOnDrop for BlockRngResults
nstilt1 Oct 25, 2023
6af37f1
removed unnecessary impls and added u128 input for get_stream()
nstilt1 Oct 25, 2023
56462ef
revised set_stream() comment
nstilt1 Oct 25, 2023
c52c607
fix set/get_word_pos() discrepancies
nstilt1 Oct 25, 2023
aac6b9e
add chacha20rng bench code
nstilt1 Oct 26, 2023
0357ad6
aarch64 bench compatibility
Oct 26, 2023
71a1a5b
fix benches for avx2
nstilt1 Oct 26, 2023
9d992e1
adjusted aarch64 bench and added results
Oct 26, 2023
f32ee71
comment for unsafe AsRef/AsMut
nstilt1 Oct 26, 2023
63fcd30
added comment for unsafe AsRef/AsMut
nstilt1 Oct 26, 2023
e765489
fmt :/
nstilt1 Oct 26, 2023
6fb4d82
Delete .vscode directory
nstilt1 Oct 26, 2023
d1907c5
revert an unnecessary change to lib.rs
nstilt1 Oct 26, 2023
694c592
using rand_core v0.7.0 for testing
nstilt1 Oct 27, 2023
a3c3189
replace struct BlockRngResults with union
nstilt1 Oct 27, 2023
bf3f700
passing clone test; still have to update the test vectors
nstilt1 Oct 27, 2023
fd65576
fmt
nstilt1 Oct 27, 2023
4bd6e5a
all tests passing
nstilt1 Oct 27, 2023
2dbb429
safety switch
nstilt1 Oct 27, 2023
b227065
fmt
nstilt1 Oct 27, 2023
5713e7e
remove rand_core-0.7-dependent test; remove unnecessary comments; ren…
nstilt1 Oct 28, 2023
de69a46
replace KeyIvInit and considering references instead of owned inputs ?
nstilt1 Nov 3, 2023
c77dcc2
fix unused mut variable warning when zeroize isn't enabled
nstilt1 Nov 3, 2023
842d0de
if set_word_pos_bytes is desirable, pick your poison between this com…
nstilt1 Nov 3, 2023
21b0862
fix an error with set_word_pos_bytes() and reverted the pick your poi…
nstilt1 Nov 3, 2023
3b6f4cb
added Seed wrapper, moved impl of ChaChaCore to rng.rs, corrected a c…
nstilt1 Nov 3, 2023
af09157
exporting Seed for rand_core
nstilt1 Nov 3, 2023
9d46dd6
Seed's .into() seems to work without exporting it :/
nstilt1 Nov 3, 2023
fb489ef
fix benches; add example for ChaCha20Rng
nstilt1 Nov 3, 2023
03bf69a
using Generic inputs to convert to zeroizable inputs, as well as elim…
nstilt1 Nov 3, 2023
a3a2350
allow for [u8; 12] stream_id, and set_stream() is now slightly optimi…
nstilt1 Nov 4, 2023
5472286
micro-optimized set_word_pos() for [u8; 5]
nstilt1 Nov 5, 2023
72c9e0b
ensure zeroization of a u64 :/
nstilt1 Nov 5, 2023
5997225
fix ZeroizeOnDrop impl
nstilt1 Nov 5, 2023
ef30ccc
added zeroize tests; tests failed :(; tried a non-copyable seed wrapp…
nstilt1 Nov 6, 2023
ea32536
replaced some wrappers with a macro, ensured zeroization of some inpu…
nstilt1 Nov 7, 2023
c10c4b6
end of zeroizing? also adjusted WordPosInput to line up with comment
nstilt1 Nov 7, 2023
26668af
fmt
nstilt1 Nov 7, 2023
ac09d24
no more unused mut input errors
nstilt1 Nov 7, 2023
13aa494
removing AlteredState for now; updated some documentation for clarity
nstilt1 Nov 11, 2023
2d71ada
updated docs for clarity*
nstilt1 Nov 11, 2023
b013266
minor correction: WordPosInput doesn't force the bytes to be in littl…
nstilt1 Nov 11, 2023
7833e11
gitignore .DS_Store and remove impl_zeroize_from
nstilt1 Nov 12, 2023
a687937
Delete .DS_Store
nstilt1 Nov 12, 2023
73088f6
remove some minor zeroizings
nstilt1 Nov 12, 2023
bdb43c0
remove minor zeroizing
nstilt1 Nov 12, 2023
255b481
Merge https://github.com/nstilt1/stream-ciphers-rng into HEAD
nstilt1 Nov 12, 2023
a691b4a
.DS_Store, you hath been banishth
nstilt1 Nov 13, 2023
bf7c307
replace BlockRng and BlockRngCore impl to allow for Results buffer to…
nstilt1 Nov 13, 2023
5f245d5
rename 'rng' member to 'core' to more accurately reflect its purpose
nstilt1 Nov 13, 2023
65e42e8
added benches readme with results
nstilt1 Nov 13, 2023
e8c2df0
removed remnant ZeroizeOnDrop impl that didn't do anything
nstilt1 Nov 13, 2023
ac51b24
edit benches README
nstilt1 Nov 13, 2023
cec3726
ensuring consistency with the cipher block_pos and the rng set/get_wo…
nstilt1 Nov 19, 2023
5603c3c
aligned and tested set_word_pos. removed the index checks in the test…
nstilt1 Nov 19, 2023
11ebab3
removed a check that only worked for the 'default' alignment
nstilt1 Nov 19, 2023
141d54c
baby steps
nstilt1 Nov 25, 2023
d8f9732
lib.rs attempt at fixing generate(); not compiling yet
nstilt1 Nov 25, 2023
7695fb6
update TODO
nstilt1 Nov 25, 2023
0c0e338
rng progress, still working on the ciphers
nstilt1 Nov 26, 2023
14ee01e
working on ChaChaCore::new(); had issues with avx2_cpuid/sse2_cpuid w…
nstilt1 Nov 27, 2023
5e15c4a
rng passes tests; 1.04 cpb for soft; 1.07 cpb for avx2? will try some…
nstilt1 Nov 28, 2023
5d6d0ad
was benching wrong Rng lol... CPB: 0.99 for avx2, 5.16 for soft, 2.89…
nstilt1 Nov 28, 2023
a0c19e8
speedrun of implementing/copy-pasta; neon needs to be checked
nstilt1 Dec 20, 2023
32c7662
fixed neon
nstilt1 Dec 20, 2023
5df80ed
fmt
nstilt1 Dec 20, 2023
e95e3ca
corrected some rustdocs in rng.rs; also trying to pass checks
nstilt1 Dec 20, 2023
8d30f5c
fix checks
nstilt1 Dec 20, 2023
d2d980b
add missing zeroize cfg in rustdocs; still working on Rudra
nstilt1 Dec 22, 2023
8465a4d
generate() is now in rng.rs; added rng feature because rng.rs tests w…
nstilt1 Dec 24, 2023
45a78d3
fixed cargo bench --no-run
nstilt1 Dec 24, 2023
30386f4
fix cargo bench --no-run
nstilt1 Dec 24, 2023
06ff9a8
updated neon and gitignore
nstilt1 Jan 2, 2024
ed4ac1c
update neon and gitignore; not sure why it made me pull
nstilt1 Jan 2, 2024
f501a4f
rebasing #338
nstilt1 Jan 7, 2024
758a0b7
rebasing #339; also added public get/set_block_pos; added From<[u32; …
nstilt1 Jan 17, 2024
9d3d870
Merge branch 'master' into rebase2-electric-boogaloo
nstilt1 Jan 17, 2024
303714a
fmt and fixing loose ends
nstilt1 Jan 17, 2024
0295f7c
cleanup /benches/; also, .cargo/config has stopped working on my end
nstilt1 Jan 17, 2024
a8cd5c3
fixed benches; tried to make set_word_pos more coherent by changing t…
nstilt1 Jan 18, 2024
14e45bc
fmt
nstilt1 Jan 18, 2024
0f494ff
used a wrapper for the so that core methods from cipher are not visi…
nstilt1 Jan 18, 2024
70a9f2d
Merge branch 'master' into rebase2-electric-boogaloo
nstilt1 Mar 13, 2024
86fd8ed
removed getrandom dependency, removed .cargo folders that were for te…
nstilt1 Mar 13, 2024
bee5798
Update .gitignore
nstilt1 Mar 13, 2024
304f2bb
updated rand_core to alpha; impl'd BlockRngCore; moved get/set_word_p…
nstilt1 Apr 6, 2024
1d15d68
impl CryptoBlockRng and ZeroizeOnDrop for Rng and RngCore
nstilt1 Apr 11, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
target/
**/Cargo.lock
**/Cargo.lock
143 changes: 135 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 7 additions & 5 deletions benches/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ publish = false

[workspace]

[dev-dependencies]
criterion = "0.3"
criterion-cycles-per-byte = "0.1"
chacha20 = { path = "../chacha20/" }
[dependencies]
criterion = "0.5"
chacha20 = { path = "../chacha20/", features = ["rng", "zeroize"] }

[target.'cfg(any(target_arch = "x86_64", target_arch = "x86", all(target_arch = "aarch64", target_os = "linux")))'.dependencies]
criterion-cycles-per-byte = "0.6.0"

[[bench]]
name = "chacha20"
path = "src/chacha20.rs"
harness = false
harness = false
46 changes: 46 additions & 0 deletions benches/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Benching ChaCha20

## A note from the criterion-cycles-per-byte github
```
[`criterion-cycles-per-byte`] measures clock ticks rather than cycles. It will not provide accurate results on modern machines unless you calculate the ratio of ticks to cycles and take steps to ensure that that ratio remains consistent.
```

## ChaCha20 Cipher benching
You can bench the ChaCha20 cipher using `cargo bench -- apply_keystream`

## ChaCha20Rng benching
You can bench ChaCha20Rng using `cargo bench -- fill_bytes`

## Measuring CPB for aarch64
`criterion-cycles-per-byte` can work on `aarch64` with Linux, but it might produce an error. This error occurred on an up-to-date Raspberry Pi 4b (as of 12/14/2023):
```
Running src/chacha20.rs (target/release/deps/chacha20-02f555ae0af3670b)
Gnuplot not found, using plotters backend
Benchmarking stream-cipher/apply_keystream/1024: Warming up for 3.0000 serror: bench failed, to rerun pass `--bench chacha20`

Caused by:
process didn't exit successfully: `..../benches/target/release/deps/chacha20-02f555ae0af3670b --bench` (signal: 4, SIGILL: illegal instruction)
```

The following adjustment can fix this.

### Installing the cycle counter Linux Kernel Module on a Raspberry Pi 4b
```
$ sudo apt-get update
$ sudo apt-get upgrade
$ sudo apt-get install build-essential raspberrypi-kernel-headers
# cd to your chosen directory
$ cd ../..
$ git clone https://github.com/jerinjacobk/armv8_pmu_cycle_counter_el0.git
$ cd armv8_pmu_cycle_counter_el10
$ make
$ sudo insmod pmu_el0_cycle_counter.ko
# Verifying that it is installed
$ lsmod | grep pmu_el0_cycle_counter
pmu_el0_cycle_counter 16384 0
```
Without any other commands, this module will be deactivated after every reboot, and can be reactivated using
```
$ cd armv8_pmu_cycle_counter_el10
$ sudo insmod pmu_el0_cycle_counter.ko
```
38 changes: 30 additions & 8 deletions benches/src/chacha20.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
//! ChaCha20 benchmark
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion, Throughput};
use criterion_cycles_per_byte::CyclesPerByte;
use benches::{criterion_group_bench, Benchmarker};

use chacha20::{
cipher::{KeyIvInit, StreamCipher},
ChaCha20,
};

const KB: usize = 1024;

fn bench(c: &mut Criterion<CyclesPerByte>) {
fn bench(c: &mut Benchmarker) {
let mut group = c.benchmark_group("stream-cipher");

for size in &[KB, 2 * KB, 4 * KB, 8 * KB, 16 * KB] {
Expand All @@ -28,9 +27,32 @@ fn bench(c: &mut Criterion<CyclesPerByte>) {
group.finish();
}

criterion_group!(
name = benches;
config = Criterion::default().with_measurement(CyclesPerByte);
targets = bench
use chacha20::rand_core::{RngCore, SeedableRng};

fn bench_chacha20rng(c: &mut Benchmarker) {
let mut group = c.benchmark_group("ChaCha20Rng");

for size in &[KB, 2 * KB, 4 * KB, 8 * KB, 16 * KB] {
let mut buf = vec![0u8; *size];

group.throughput(Throughput::Bytes(*size as u64));

group.bench_function(BenchmarkId::new("fill_bytes", size), |b| {
let mut rng = chacha20::ChaCha20Rng::from_seed([0u8; 32]);
b.iter(|| rng.fill_bytes(&mut buf));
});
}

group.finish();
}
criterion_group_bench!(
benches_chacha20rng,
bench_chacha20rng
);
criterion_main!(benches);

criterion_group_bench!(
benches,
bench
);

criterion_main!(benches, benches_chacha20rng);
23 changes: 23 additions & 0 deletions benches/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1 +1,24 @@
use criterion::Criterion;

#[cfg(any(target_arch = "x86_64", target_arch = "x86", all(target_arch = "aarch64", target_os = "linux")))]
pub type Benchmarker = Criterion<criterion_cycles_per_byte::CyclesPerByte>;
#[cfg(not(any(target_arch = "x86_64", target_arch = "x86", all(target_arch = "aarch64", target_os = "linux"))))]
pub type Benchmarker = Criterion;

#[macro_export]
macro_rules! criterion_group_bench {
($Name:ident, $Target:ident) => {
#[cfg(any(target_arch = "x86_64", target_arch = "x86", all(target_arch = "aarch64", target_os = "linux")))]
criterion_group!(
name = $Name;
config = Criterion::default().with_measurement(criterion_cycles_per_byte::CyclesPerByte);
targets = $Target
);
#[cfg(not(any(target_arch = "x86_64", target_arch = "x86", all(target_arch = "aarch64", target_os = "linux"))))]
criterion_group!(
name = $Name;
config = Criterion::default();
targets = $Target
);
}
}
Loading
Loading