Skip to content

Commit 9b127ed

Browse files
committed
Improve the performance of the pure rust implementations
1 parent a3bba70 commit 9b127ed

File tree

11 files changed

+1916
-1753
lines changed

11 files changed

+1916
-1753
lines changed

benches/benchmark.rs

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ use aegis::aegis128x2::Aegis128X2;
33
use aegis::aegis128x4::Aegis128X4;
44
use aegis::aegis256::Aegis256;
55

6-
#[cfg(not(feature = "pure-rust"))]
76
use aegis::aegis256x2::Aegis256X2;
8-
#[cfg(not(feature = "pure-rust"))]
97
use aegis::aegis256x4::Aegis256X4;
108

119
#[cfg(not(feature = "pure-rust"))]
@@ -100,15 +98,13 @@ fn test_aegis256(m: &mut [u8]) {
10098
state.encrypt_in_place(m, &[]);
10199
}
102100

103-
#[cfg(not(feature = "pure-rust"))]
104101
fn test_aegis256x2(m: &mut [u8]) {
105102
let key = [0u8; 32];
106103
let nonce = [0u8; 32];
107104
let state = Aegis256X2::<16>::new(&nonce, &key);
108105
state.encrypt_in_place(m, &[]);
109106
}
110107

111-
#[cfg(not(feature = "pure-rust"))]
112108
fn test_aegis256x4(m: &mut [u8]) {
113109
let key = [0u8; 32];
114110
let nonce = [0u8; 32];
@@ -238,20 +234,17 @@ fn main() {
238234
res.throughput_bits(m.len() as _)
239235
);
240236

241-
#[cfg(not(feature = "pure-rust"))]
242-
{
243-
let res = bench.run(options, || test_aegis256x2(&mut m));
244-
println!(
245-
"aegis256x2 : {}",
246-
res.throughput_bits(m.len() as _)
247-
);
237+
let res = bench.run(options, || test_aegis256x2(&mut m));
238+
println!(
239+
"aegis256x2 : {}",
240+
res.throughput_bits(m.len() as _)
241+
);
248242

249-
let res = bench.run(options, || test_aegis256x4(&mut m));
250-
println!(
251-
"aegis256x4 : {}",
252-
res.throughput_bits(m.len() as _)
253-
);
254-
}
243+
let res = bench.run(options, || test_aegis256x4(&mut m));
244+
println!(
245+
"aegis256x4 : {}",
246+
res.throughput_bits(m.len() as _)
247+
);
255248

256249
let res = bench.run(options, || test_aegis256(&mut m));
257250
println!(

src/pure_rust/aegis128l.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,21 @@ struct State {
1414
}
1515

1616
impl State {
17+
#[inline(always)]
1718
fn update(&mut self, d1: AesBlock, d2: AesBlock) {
1819
let blocks = &mut self.blocks;
1920
let tmp = blocks[7];
20-
let mut i = 7;
21-
while i > 0 {
22-
blocks[i] = blocks[i - 1].round(blocks[i]);
23-
i -= 1;
24-
}
25-
blocks[0] = tmp.round(blocks[0]);
26-
blocks[0] = blocks[0].xor(d1);
27-
blocks[4] = blocks[4].xor(d2);
21+
blocks[7] = blocks[6].round(blocks[7]);
22+
blocks[6] = blocks[5].round(blocks[6]);
23+
blocks[5] = blocks[4].round(blocks[5]);
24+
blocks[4] = blocks[3].round(blocks[4]).xor(d2);
25+
blocks[3] = blocks[2].round(blocks[3]);
26+
blocks[2] = blocks[1].round(blocks[2]);
27+
blocks[1] = blocks[0].round(blocks[1]);
28+
blocks[0] = tmp.round(blocks[0]).xor(d1);
2829
}
2930

31+
#[inline(always)]
3032
pub fn new(key: &Key, nonce: &Nonce) -> Self {
3133
let c0 = AesBlock::from_bytes(&[
3234
0x00, 0x01, 0x01, 0x02, 0x03, 0x05, 0x08, 0x0d, 0x15, 0x22, 0x37, 0x59, 0x90, 0xe9,
@@ -62,6 +64,7 @@ impl State {
6264
self.update(msg0, msg1);
6365
}
6466

67+
#[inline(always)]
6568
fn enc(&mut self, dst: &mut [u8; 32], src: &[u8; 32]) {
6669
let blocks = &self.blocks;
6770
let z0 = blocks[6].xor(blocks[1]).xor(blocks[2].and(blocks[3]));
@@ -75,6 +78,7 @@ impl State {
7578
self.update(msg0, msg1);
7679
}
7780

81+
#[inline(always)]
7882
fn dec(&mut self, dst: &mut [u8; 32], src: &[u8; 32]) {
7983
let blocks = &self.blocks;
8084
let z0 = blocks[6].xor(blocks[1]).xor(blocks[2].and(blocks[3]));
@@ -86,6 +90,7 @@ impl State {
8690
self.update(msg0, msg1);
8791
}
8892

93+
#[inline(always)]
8994
fn dec_partial(&mut self, dst: &mut [u8; 32], src: &[u8]) {
9095
let len = src.len();
9196
let mut src_padded = [0u8; 32];
@@ -106,6 +111,7 @@ impl State {
106111
self.update(msg0, msg1);
107112
}
108113

114+
#[inline(always)]
109115
fn mac<const TAG_BYTES: usize>(&mut self, adlen: usize, mlen: usize) -> Tag<TAG_BYTES> {
110116
let tmp = {
111117
let blocks = &self.blocks;

0 commit comments

Comments
 (0)