Skip to content

Commit d58dc77

Browse files
committed
add keccak create
Quoting @oli-obk: > The keccak crate is a great benchmark for ridiculous numbers of locals and basic blocks. We should add it to the list of perf tests.
1 parent 2af67e3 commit d58dc77

File tree

5 files changed

+344
-0
lines changed

5 files changed

+344
-0
lines changed

collector/benchmarks/keccak/Cargo.lock

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[package]
2+
name = "keccak"
3+
version = "0.1.0"
4+
authors = ["RustCrypto Developers"]
5+
license = "CC0-1.0"
6+
description = "Keccak-f sponge function"
7+
documentation = "https://docs.rs/keccak"
8+
repository = "https://github.com/RustCrypto/sponges"
9+
keywords = ["crypto", "sponge", "keccak-f"]
10+
categories = ["cryptography", "no-std"]
11+
12+
[features]
13+
no_unroll = []
14+
15+
[badges]
16+
travis-ci = { repository = "RustCrypto/sponges" }

collector/benchmarks/keccak/LICENSE

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
Creative Commons Legal Code
2+
3+
CC0 1.0 Universal
4+
5+
CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE
6+
LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN
7+
ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS
8+
INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES
9+
REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS
10+
PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM
11+
THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED
12+
HEREUNDER.
13+
14+
Statement of Purpose
15+
16+
The laws of most jurisdictions throughout the world automatically confer
17+
exclusive Copyright and Related Rights (defined below) upon the creator
18+
and subsequent owner(s) (each and all, an "owner") of an original work of
19+
authorship and/or a database (each, a "Work").
20+
21+
Certain owners wish to permanently relinquish those rights to a Work for
22+
the purpose of contributing to a commons of creative, cultural and
23+
scientific works ("Commons") that the public can reliably and without fear
24+
of later claims of infringement build upon, modify, incorporate in other
25+
works, reuse and redistribute as freely as possible in any form whatsoever
26+
and for any purposes, including without limitation commercial purposes.
27+
These owners may contribute to the Commons to promote the ideal of a free
28+
culture and the further production of creative, cultural and scientific
29+
works, or to gain reputation or greater distribution for their Work in
30+
part through the use and efforts of others.
31+
32+
For these and/or other purposes and motivations, and without any
33+
expectation of additional consideration or compensation, the person
34+
associating CC0 with a Work (the "Affirmer"), to the extent that he or she
35+
is an owner of Copyright and Related Rights in the Work, voluntarily
36+
elects to apply CC0 to the Work and publicly distribute the Work under its
37+
terms, with knowledge of his or her Copyright and Related Rights in the
38+
Work and the meaning and intended legal effect of CC0 on those rights.
39+
40+
1. Copyright and Related Rights. A Work made available under CC0 may be
41+
protected by copyright and related or neighboring rights ("Copyright and
42+
Related Rights"). Copyright and Related Rights include, but are not
43+
limited to, the following:
44+
45+
i. the right to reproduce, adapt, distribute, perform, display,
46+
communicate, and translate a Work;
47+
ii. moral rights retained by the original author(s) and/or performer(s);
48+
iii. publicity and privacy rights pertaining to a person's image or
49+
likeness depicted in a Work;
50+
iv. rights protecting against unfair competition in regards to a Work,
51+
subject to the limitations in paragraph 4(a), below;
52+
v. rights protecting the extraction, dissemination, use and reuse of data
53+
in a Work;
54+
vi. database rights (such as those arising under Directive 96/9/EC of the
55+
European Parliament and of the Council of 11 March 1996 on the legal
56+
protection of databases, and under any national implementation
57+
thereof, including any amended or successor version of such
58+
directive); and
59+
vii. other similar, equivalent or corresponding rights throughout the
60+
world based on applicable law or treaty, and any national
61+
implementations thereof.
62+
63+
2. Waiver. To the greatest extent permitted by, but not in contravention
64+
of, applicable law, Affirmer hereby overtly, fully, permanently,
65+
irrevocably and unconditionally waives, abandons, and surrenders all of
66+
Affirmer's Copyright and Related Rights and associated claims and causes
67+
of action, whether now known or unknown (including existing as well as
68+
future claims and causes of action), in the Work (i) in all territories
69+
worldwide, (ii) for the maximum duration provided by applicable law or
70+
treaty (including future time extensions), (iii) in any current or future
71+
medium and for any number of copies, and (iv) for any purpose whatsoever,
72+
including without limitation commercial, advertising or promotional
73+
purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each
74+
member of the public at large and to the detriment of Affirmer's heirs and
75+
successors, fully intending that such Waiver shall not be subject to
76+
revocation, rescission, cancellation, termination, or any other legal or
77+
equitable action to disrupt the quiet enjoyment of the Work by the public
78+
as contemplated by Affirmer's express Statement of Purpose.
79+
80+
3. Public License Fallback. Should any part of the Waiver for any reason
81+
be judged legally invalid or ineffective under applicable law, then the
82+
Waiver shall be preserved to the maximum extent permitted taking into
83+
account Affirmer's express Statement of Purpose. In addition, to the
84+
extent the Waiver is so judged Affirmer hereby grants to each affected
85+
person a royalty-free, non transferable, non sublicensable, non exclusive,
86+
irrevocable and unconditional license to exercise Affirmer's Copyright and
87+
Related Rights in the Work (i) in all territories worldwide, (ii) for the
88+
maximum duration provided by applicable law or treaty (including future
89+
time extensions), (iii) in any current or future medium and for any number
90+
of copies, and (iv) for any purpose whatsoever, including without
91+
limitation commercial, advertising or promotional purposes (the
92+
"License"). The License shall be deemed effective as of the date CC0 was
93+
applied by Affirmer to the Work. Should any part of the License for any
94+
reason be judged legally invalid or ineffective under applicable law, such
95+
partial invalidity or ineffectiveness shall not invalidate the remainder
96+
of the License, and in such case Affirmer hereby affirms that he or she
97+
will not (i) exercise any of his or her remaining Copyright and Related
98+
Rights in the Work or (ii) assert any associated claims and causes of
99+
action with respect to the Work, in either case contrary to Affirmer's
100+
express Statement of Purpose.
101+
102+
4. Limitations and Disclaimers.
103+
104+
a. No trademark or patent rights held by Affirmer are waived, abandoned,
105+
surrendered, licensed or otherwise affected by this document.
106+
b. Affirmer offers the Work as-is and makes no representations or
107+
warranties of any kind concerning the Work, express, implied,
108+
statutory or otherwise, including without limitation warranties of
109+
title, merchantability, fitness for a particular purpose, non
110+
infringement, or the absence of latent or other defects, accuracy, or
111+
the present or absence of errors, whether or not discoverable, all to
112+
the greatest extent permissible under applicable law.
113+
c. Affirmer disclaims responsibility for clearing rights of other persons
114+
that may apply to the Work or any use thereof, including without
115+
limitation any person's Copyright and Related Rights in the Work.
116+
Further, Affirmer disclaims responsibility for obtaining any necessary
117+
consents, permissions or other rights required for any use of the
118+
Work.
119+
d. Affirmer understands and acknowledges that Creative Commons is not a
120+
party to this document and has no duty or obligation with respect to
121+
this CC0 or use of the Work.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#![feature(test)]
2+
extern crate keccak;
3+
extern crate test;
4+
5+
#[bench]
6+
fn f1600(b: &mut test::Bencher) {
7+
let mut data = [0u64; 25];
8+
b.iter(|| keccak::f1600(&mut data));
9+
}
+194
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
//! Keccak [sponge function](https://en.wikipedia.org/wiki/Sponge_function).
2+
//!
3+
//! The function code is fully unrolled and is nearly as fast as the Keccak
4+
//! team's optimized implementation.
5+
//!
6+
//! If you are looking for SHA-3 hash functions take a look at [`sha3`][1] and
7+
//! [`tiny-keccak`][2] crates.
8+
//!
9+
//! To disable loop unrolling (e.g. for constraint targets) use `no_unroll`
10+
//! feature.
11+
//!
12+
//! ```
13+
//! // Test vectors are from KeccakCodePackage
14+
//! let mut data = [0u64; 25];
15+
//!
16+
//! keccak::f1600(&mut data);
17+
//! assert_eq!(data, [
18+
//! 0xF1258F7940E1DDE7, 0x84D5CCF933C0478A, 0xD598261EA65AA9EE, 0xBD1547306F80494D,
19+
//! 0x8B284E056253D057, 0xFF97A42D7F8E6FD4, 0x90FEE5A0A44647C4, 0x8C5BDA0CD6192E76,
20+
//! 0xAD30A6F71B19059C, 0x30935AB7D08FFC64, 0xEB5AA93F2317D635, 0xA9A6E6260D712103,
21+
//! 0x81A57C16DBCF555F, 0x43B831CD0347C826, 0x01F22F1A11A5569F, 0x05E5635A21D9AE61,
22+
//! 0x64BEFEF28CC970F2, 0x613670957BC46611, 0xB87C5A554FD00ECB, 0x8C3EE88A1CCF32C8,
23+
//! 0x940C7922AE3A2614, 0x1841F924A2C509E4, 0x16F53526E70465C2, 0x75F644E97F30A13B,
24+
//! 0xEAF1FF7B5CECA249,
25+
//! ]);
26+
//!
27+
//! keccak::f1600(&mut data);
28+
//! assert_eq!(data, [
29+
//! 0x2D5C954DF96ECB3C, 0x6A332CD07057B56D, 0x093D8D1270D76B6C, 0x8A20D9B25569D094,
30+
//! 0x4F9C4F99E5E7F156, 0xF957B9A2DA65FB38, 0x85773DAE1275AF0D, 0xFAF4F247C3D810F7,
31+
//! 0x1F1B9EE6F79A8759, 0xE4FECC0FEE98B425, 0x68CE61B6B9CE68A1, 0xDEEA66C4BA8F974F,
32+
//! 0x33C43D836EAFB1F5, 0xE00654042719DBD9, 0x7CF8A9F009831265, 0xFD5449A6BF174743,
33+
//! 0x97DDAD33D8994B40, 0x48EAD5FC5D0BE774, 0xE3B8C8EE55B7B03C, 0x91A0226E649E42E9,
34+
//! 0x900E3129E7BADD7B, 0x202A9EC5FAA3CCE8, 0x5B3402464E1C3DB6, 0x609F4E62A44C1059,
35+
//! 0x20D06CD26A8FBF5C,
36+
//! ]);
37+
//! ```
38+
//!
39+
//! [1]: https://docs.rs/sha3
40+
//! [2]: https://docs.rs/tiny-keccak
41+
#![no_std]
42+
#![allow(non_upper_case_globals)]
43+
44+
const PLEN: usize = 25;
45+
46+
const RHO: [u32; 24] = [
47+
1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 2, 14, 27, 41, 56, 8, 25, 43, 62, 18,
48+
39, 61, 20, 44,
49+
];
50+
51+
const PI: [usize; 24] = [
52+
10, 7, 11, 17, 18, 3, 5, 16, 8, 21, 24, 4, 15, 23, 19, 13, 12, 2, 20, 14,
53+
22, 9, 6, 1,
54+
];
55+
56+
const RC: [u64; 24] = [
57+
0x0000000000000001,
58+
0x0000000000008082,
59+
0x800000000000808a,
60+
0x8000000080008000,
61+
0x000000000000808b,
62+
0x0000000080000001,
63+
0x8000000080008081,
64+
0x8000000000008009,
65+
0x000000000000008a,
66+
0x0000000000000088,
67+
0x0000000080008009,
68+
0x000000008000000a,
69+
0x000000008000808b,
70+
0x800000000000008b,
71+
0x8000000000008089,
72+
0x8000000000008003,
73+
0x8000000000008002,
74+
0x8000000000000080,
75+
0x000000000000800a,
76+
0x800000008000000a,
77+
0x8000000080008081,
78+
0x8000000000008080,
79+
0x0000000080000001,
80+
0x8000000080008008,
81+
];
82+
83+
#[cfg(not(feature = "no_unroll"))]
84+
macro_rules! unroll5 {
85+
($var:ident, $body:block) => {
86+
{ const $var: usize = 0; $body; }
87+
{ const $var: usize = 1; $body; }
88+
{ const $var: usize = 2; $body; }
89+
{ const $var: usize = 3; $body; }
90+
{ const $var: usize = 4; $body; }
91+
};
92+
}
93+
94+
#[cfg(feature = "no_unroll")]
95+
macro_rules! unroll5 {
96+
($var:ident, $body:block) => {
97+
for $var in 0..5 $body
98+
}
99+
}
100+
101+
#[cfg(not(feature = "no_unroll"))]
102+
macro_rules! unroll24 {
103+
($var: ident, $body: block) => {
104+
{ const $var: usize = 0; $body; }
105+
{ const $var: usize = 1; $body; }
106+
{ const $var: usize = 2; $body; }
107+
{ const $var: usize = 3; $body; }
108+
{ const $var: usize = 4; $body; }
109+
{ const $var: usize = 5; $body; }
110+
{ const $var: usize = 6; $body; }
111+
{ const $var: usize = 7; $body; }
112+
{ const $var: usize = 8; $body; }
113+
{ const $var: usize = 9; $body; }
114+
{ const $var: usize = 10; $body; }
115+
{ const $var: usize = 11; $body; }
116+
{ const $var: usize = 12; $body; }
117+
{ const $var: usize = 13; $body; }
118+
{ const $var: usize = 14; $body; }
119+
{ const $var: usize = 15; $body; }
120+
{ const $var: usize = 16; $body; }
121+
{ const $var: usize = 17; $body; }
122+
{ const $var: usize = 18; $body; }
123+
{ const $var: usize = 19; $body; }
124+
{ const $var: usize = 20; $body; }
125+
{ const $var: usize = 21; $body; }
126+
{ const $var: usize = 22; $body; }
127+
{ const $var: usize = 23; $body; }
128+
};
129+
}
130+
131+
#[cfg(feature = "no_unroll")]
132+
macro_rules! unroll24 {
133+
($var:ident, $body:block) => {
134+
for $var in 0..24 $body
135+
}
136+
}
137+
138+
#[allow(unused_assignments)]
139+
/// Keccak-f[1600] sponge function
140+
pub fn f1600(a: &mut [u64; PLEN]) {
141+
let mut arrays: [[u64; 5]; 24] = [[0; 5]; 24];
142+
143+
unroll24!(i, {
144+
// Theta
145+
unroll5!(x, {
146+
// This looks useless but it gets way slower without it. I tried
147+
// using `mem::uninitialized` for the initialisation of `arrays` but
148+
// that also makes it slower, although not by as much as removing
149+
// this assignment. Optimisers are weird. Maybe a different version
150+
// of LLVM will react differently, so if you see this comment
151+
// in the future try deleting this assignment and using uninit
152+
// above and see how it affects the benchmarks.
153+
arrays[i][x] = 0;
154+
155+
unroll5!(y, {
156+
arrays[i][x] ^= a[5 * y + x];
157+
});
158+
});
159+
160+
unroll5!(x, {
161+
unroll5!(y, {
162+
let t1 = arrays[i][(x + 4) % 5];
163+
let t2 = arrays[i][(x + 1) % 5].rotate_left(1);
164+
a[5 * y + x] ^= t1 ^ t2;
165+
});
166+
});
167+
168+
// Rho and pi
169+
let mut last = a[1];
170+
unroll24!(x, {
171+
arrays[i][0] = a[PI[x]];
172+
a[PI[x]] = last.rotate_left(RHO[x]);
173+
last = arrays[i][0];
174+
});
175+
176+
// Chi
177+
unroll5!(y_step, {
178+
let y = 5 * y_step;
179+
180+
unroll5!(x, {
181+
arrays[i][x] = a[y + x];
182+
});
183+
184+
unroll5!(x, {
185+
let t1 = !arrays[i][(x + 1) % 5];
186+
let t2 = arrays[i][(x + 2) % 5];
187+
a[y + x] = arrays[i][x] ^ (t1 & t2);
188+
});
189+
});
190+
191+
// Iota
192+
a[0] ^= RC[i];
193+
});
194+
}

0 commit comments

Comments
 (0)