From d03abe720a68490746addb351ba729b6e328c66a Mon Sep 17 00:00:00 2001 From: Anjan Roy Date: Tue, 3 Dec 2024 20:19:40 +0400 Subject: [PATCH] Manually unroll Ascon permutation loop This optimization makes Ascon permutation with even rounds, ~(2-3)% faster. ```bash Benchmark Time CPU Time Old Time New CPU Old CPU New --------------------------------------------------------------------------------------------------------------------------------------------- ascon_[permutation vs. permutation]<1>_mean +0.0017 +0.0018 7 7 7 7 ascon_[permutation vs. permutation]<1>_median +0.0015 +0.0015 7 7 7 7 ascon_[permutation vs. permutation]<1>_stddev +0.6419 +0.2762 0 0 0 0 ascon_[permutation vs. permutation]<1>_cv +0.6391 +0.2739 0 0 0 0 ascon_[permutation vs. permutation]<1>_min +0.0017 +0.0020 7 7 7 7 ascon_[permutation vs. permutation]<1>_max +0.0026 +0.0025 7 7 7 7 ascon_[permutation vs. permutation]<8>_mean -0.0226 -0.0227 27 27 27 27 ascon_[permutation vs. permutation]<8>_median -0.0232 -0.0233 27 27 27 27 ascon_[permutation vs. permutation]<8>_stddev +3.1631 +3.1249 0 0 0 0 ascon_[permutation vs. permutation]<8>_cv +3.2596 +3.2209 0 0 0 0 ascon_[permutation vs. permutation]<8>_min -0.0239 -0.0238 27 27 27 27 ascon_[permutation vs. permutation]<8>_max -0.0196 -0.0197 27 27 27 27 ascon_[permutation vs. permutation]<12>_mean -0.0300 -0.0300 39 38 39 38 ascon_[permutation vs. permutation]<12>_median -0.0299 -0.0298 39 38 39 38 ascon_[permutation vs. permutation]<12>_stddev -0.2839 -0.4537 0 0 0 0 ascon_[permutation vs. permutation]<12>_cv -0.2618 -0.4368 0 0 0 0 ascon_[permutation vs. permutation]<12>_min -0.0299 -0.0299 39 38 39 38 ascon_[permutation vs. permutation]<12>_max -0.0302 -0.0305 39 38 39 38 ``` Signed-off-by: Anjan Roy --- include/ascon/permutation/ascon.hpp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/include/ascon/permutation/ascon.hpp b/include/ascon/permutation/ascon.hpp index 3d7454b..43cd5b8 100644 --- a/include/ascon/permutation/ascon.hpp +++ b/include/ascon/permutation/ascon.hpp @@ -110,8 +110,15 @@ struct ascon_perm_t { constexpr size_t BEG = ASCON_PERMUTATION_MAX_ROUNDS - R; - for (size_t i = BEG; i < ASCON_PERMUTATION_MAX_ROUNDS; i++) { - round(ASCON_PERMUTATION_ROUND_CONSTANTS[i]); + if constexpr (R % 2 == 0) { + for (size_t i = BEG; i < ASCON_PERMUTATION_MAX_ROUNDS; i += 2) { + round(ASCON_PERMUTATION_ROUND_CONSTANTS[i]); + round(ASCON_PERMUTATION_ROUND_CONSTANTS[i + 1]); + } + } else { + for (size_t i = BEG; i < ASCON_PERMUTATION_MAX_ROUNDS; i++) { + round(ASCON_PERMUTATION_ROUND_CONSTANTS[i]); + } } } };