Thanks for using Compiler Explorer
Sponsors
Jakt
C++
Ada
Algol68
Analysis
Android Java
Android Kotlin
Assembly
C
C3
Carbon
C with Coccinelle
C++ with Coccinelle
C++ (Circle)
CIRCT
Clean
Clojure
CMake
CMakeScript
COBOL
C++ for OpenCL
MLIR
Cppx
Cppx-Blue
Cppx-Gold
Cpp2-cppfront
Crystal
C#
CUDA C++
D
Dart
Elixir
Erlang
Fortran
F#
GLSL
Go
Haskell
HLSL
Helion
Hook
Hylo
IL
ispc
Java
Julia
Kotlin
LLVM IR
LLVM MIR
Modula-2
Mojo
Nim
Numba
Nix
Objective-C
Objective-C++
OCaml
Odin
OpenCL C
Pascal
Pony
PTX
Python
Racket
Raku
Ruby
Rust
Sail
Snowball
Scala
Slang
Solidity
Spice
SPIR-V
Swift
LLVM TableGen
Toit
Triton
TypeScript Native
V
Vala
Visual Basic
Vyper
WASM
Yul (Solidity IR)
Zig
Javascript
GIMPLE
Ygen
sway
zig source #1
Output
Compile to binary object
Link to binary
Execute the code
Intel asm syntax
Demangle identifiers
Verbose demangling
Filters
Unused labels
Library functions
Directives
Comments
Horizontal whitespace
Debug intrinsics
Compiler
zig 0.10.0
zig 0.11.0
zig 0.12.0
zig 0.12.1
zig 0.13.0
zig 0.14.0
zig 0.14.1
zig 0.15.1
zig 0.15.2
zig 0.2.0
zig 0.3.0
zig 0.4.0
zig 0.5.0
zig 0.6.0
zig 0.7.0
zig 0.7.1
zig 0.8.0
zig 0.9.0
zig trunk
Options
Source code
const std = @import("std"); const assert = std.debug.assert; const math = std.math; const mem = std.mem; /// The l-way Keccak-f permutation. pub fn KeccakFx(comptime f: u11, comptime l: usize) type { comptime assert(f > 200 and f <= 1600 and f % 200 == 0); // invalid bit size const T = std.meta.Int(.unsigned, f / 25); const Block = [25]@Vector(l, T); const PI = [_]u5{ 10, 7, 11, 17, 18, 3, 5, 16, 8, 21, 24, 4, 15, 23, 19, 13, 12, 2, 20, 14, 22, 9, 6, 1, }; return struct { const Self = @This(); /// Number of bytes in of the state. pub const block_bytes = l * f / 8; /// Maximum number of rounds for the given f parameter. pub const max_rounds = 12 + 2 * math.log2(f / 25); // Round constants const RC = rc: { const RC64 = [_]u64{ 0x0000000000000001, 0x0000000000008082, 0x800000000000808a, 0x8000000080008000, 0x000000000000808b, 0x0000000080000001, 0x8000000080008081, 0x8000000000008009, 0x000000000000008a, 0x0000000000000088, 0x0000000080008009, 0x000000008000000a, 0x000000008000808b, 0x800000000000008b, 0x8000000000008089, 0x8000000000008003, 0x8000000000008002, 0x8000000000000080, 0x000000000000800a, 0x800000008000000a, 0x8000000080008081, 0x8000000000008080, 0x0000000080000001, 0x8000000080008008, }; var rc: [max_rounds]T = undefined; for (&rc, RC64[0..max_rounds]) |*t, c| t.* = @truncate(T, c); break :rc rc; }; st: Block = [_]@Vector(l, T){@splat(l, @as(T, 0))} ** 25, inline fn round(self: *Self, rc: T) void { const st = &self.st; // theta var t = [_]@Vector(l, T){@splat(l, @as(T, 0))} ** 5; inline for (0..5) |i| { inline for (0..5) |j| { t[i] ^= st[j * 5 + i]; } } inline for (0..5) |i| { inline for (0..5) |j| { st[j * 5 + i] ^= t[(i + 4) % 5] ^ math.rotl(@Vector(l, T), t[(i + 1) % 5], 1); } } // rho+pi var last = st[1]; comptime var rotc = 0; inline for (0..24) |i| { const x = PI[i]; const tmp = st[x]; rotc = (rotc + i + 1) % @bitSizeOf(T); st[x] = math.rotl(@Vector(l, T), last, rotc); last = tmp; } inline for (0..5) |i| { inline for (0..5) |j| { t[j] = st[i * 5 + j]; } inline for (0..5) |j| { st[i * 5 + j] = t[j] ^ (~t[(j + 1) % 5] & t[(j + 2) % 5]); } } // iota st[0] ^= @splat(l, rc); } /// Apply a (possibly) reduced-round permutation to the state. pub fn permuteR(self: *Self, comptime rounds: u5) void { var i = RC.len - rounds; while (i < RC.len) : (i += 1) { self.round(RC[i]); } } /// Apply a full-round permutation to the state. pub fn permute(self: *Self) void { self.permuteR(max_rounds); } }; } export fn do(in: u64) u64 { var st :KeccakFx(1600, 2) = undefined; st.st[0] = @splat(2, in); st.permuteR(24); return st.st[0][0]; }
Become a Patron
Sponsor on GitHub
Donate via PayPal
Compiler Explorer Shop
Source on GitHub
Mailing list
Installed libraries
Wiki
Report an issue
How it works
Contact the author
CE on Mastodon
CE on Bluesky
Statistics
Changelog
Version tree