From 9561aea8e56e8987615a76ee6f2e9c6d6c071ee7 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Thu, 22 Jan 2026 12:00:49 +0100 Subject: [PATCH] Add NEON optimizations for Argon2 --- .../msvc/vs2010/libsodium/libsodium.vcxproj | 2 + .../libsodium/libsodium.vcxproj.filters | 6 + .../msvc/vs2012/libsodium/libsodium.vcxproj | 2 + .../libsodium/libsodium.vcxproj.filters | 6 + .../msvc/vs2013/libsodium/libsodium.vcxproj | 2 + .../libsodium/libsodium.vcxproj.filters | 6 + .../msvc/vs2015/libsodium/libsodium.vcxproj | 2 + .../libsodium/libsodium.vcxproj.filters | 6 + .../msvc/vs2017/libsodium/libsodium.vcxproj | 2 + .../libsodium/libsodium.vcxproj.filters | 6 + .../msvc/vs2019/libsodium/libsodium.vcxproj | 2 + .../libsodium/libsodium.vcxproj.filters | 6 + .../msvc/vs2022/libsodium/libsodium.vcxproj | 2 + .../libsodium/libsodium.vcxproj.filters | 6 + .../msvc/vs2026/libsodium/libsodium.vcxproj | 2 + .../libsodium/libsodium.vcxproj.filters | 6 + ci/appveyor/libsodium.vcxproj | 2 + ci/appveyor/libsodium.vcxproj.filters | 6 + src/libsodium/Makefile.am | 4 +- .../crypto_pwhash/argon2/argon2-core.c | 10 + .../crypto_pwhash/argon2/argon2-core.h | 2 + .../argon2/argon2-fill-block-neon.c | 216 ++++++++++++++++++ .../crypto_pwhash/argon2/blamka-round-neon.h | 161 +++++++++++++ 23 files changed, 464 insertions(+), 1 deletion(-) create mode 100644 src/libsodium/crypto_pwhash/argon2/argon2-fill-block-neon.c create mode 100644 src/libsodium/crypto_pwhash/argon2/blamka-round-neon.h diff --git a/builds/msvc/vs2010/libsodium/libsodium.vcxproj b/builds/msvc/vs2010/libsodium/libsodium.vcxproj index 7b6918ed..f9816ab6 100644 --- a/builds/msvc/vs2010/libsodium/libsodium.vcxproj +++ b/builds/msvc/vs2010/libsodium/libsodium.vcxproj @@ -94,6 +94,7 @@ + @@ -306,6 +307,7 @@ + diff --git a/builds/msvc/vs2010/libsodium/libsodium.vcxproj.filters b/builds/msvc/vs2010/libsodium/libsodium.vcxproj.filters index 3e5daeea..fc4770d8 100644 --- a/builds/msvc/vs2010/libsodium/libsodium.vcxproj.filters +++ b/builds/msvc/vs2010/libsodium/libsodium.vcxproj.filters @@ -102,6 +102,9 @@ crypto_pwhash\argon2 + + crypto_pwhash\argon2 + crypto_pwhash\argon2 @@ -734,6 +737,9 @@ crypto_pwhash\argon2 + + crypto_pwhash\argon2 + crypto_pwhash\argon2 diff --git a/builds/msvc/vs2012/libsodium/libsodium.vcxproj b/builds/msvc/vs2012/libsodium/libsodium.vcxproj index 2fc06131..56ec15fa 100644 --- a/builds/msvc/vs2012/libsodium/libsodium.vcxproj +++ b/builds/msvc/vs2012/libsodium/libsodium.vcxproj @@ -94,6 +94,7 @@ + @@ -306,6 +307,7 @@ + diff --git a/builds/msvc/vs2012/libsodium/libsodium.vcxproj.filters b/builds/msvc/vs2012/libsodium/libsodium.vcxproj.filters index 3e5daeea..fc4770d8 100644 --- a/builds/msvc/vs2012/libsodium/libsodium.vcxproj.filters +++ b/builds/msvc/vs2012/libsodium/libsodium.vcxproj.filters @@ -102,6 +102,9 @@ crypto_pwhash\argon2 + + crypto_pwhash\argon2 + crypto_pwhash\argon2 @@ -734,6 +737,9 @@ crypto_pwhash\argon2 + + crypto_pwhash\argon2 + crypto_pwhash\argon2 diff --git a/builds/msvc/vs2013/libsodium/libsodium.vcxproj b/builds/msvc/vs2013/libsodium/libsodium.vcxproj index ccd57962..2787fb75 100644 --- a/builds/msvc/vs2013/libsodium/libsodium.vcxproj +++ b/builds/msvc/vs2013/libsodium/libsodium.vcxproj @@ -94,6 +94,7 @@ + @@ -306,6 +307,7 @@ + diff --git a/builds/msvc/vs2013/libsodium/libsodium.vcxproj.filters b/builds/msvc/vs2013/libsodium/libsodium.vcxproj.filters index 3e5daeea..fc4770d8 100644 --- a/builds/msvc/vs2013/libsodium/libsodium.vcxproj.filters +++ b/builds/msvc/vs2013/libsodium/libsodium.vcxproj.filters @@ -102,6 +102,9 @@ crypto_pwhash\argon2 + + crypto_pwhash\argon2 + crypto_pwhash\argon2 @@ -734,6 +737,9 @@ crypto_pwhash\argon2 + + crypto_pwhash\argon2 + crypto_pwhash\argon2 diff --git a/builds/msvc/vs2015/libsodium/libsodium.vcxproj b/builds/msvc/vs2015/libsodium/libsodium.vcxproj index de40fa95..1c2502cd 100644 --- a/builds/msvc/vs2015/libsodium/libsodium.vcxproj +++ b/builds/msvc/vs2015/libsodium/libsodium.vcxproj @@ -94,6 +94,7 @@ + @@ -306,6 +307,7 @@ + diff --git a/builds/msvc/vs2015/libsodium/libsodium.vcxproj.filters b/builds/msvc/vs2015/libsodium/libsodium.vcxproj.filters index 3e5daeea..fc4770d8 100644 --- a/builds/msvc/vs2015/libsodium/libsodium.vcxproj.filters +++ b/builds/msvc/vs2015/libsodium/libsodium.vcxproj.filters @@ -102,6 +102,9 @@ crypto_pwhash\argon2 + + crypto_pwhash\argon2 + crypto_pwhash\argon2 @@ -734,6 +737,9 @@ crypto_pwhash\argon2 + + crypto_pwhash\argon2 + crypto_pwhash\argon2 diff --git a/builds/msvc/vs2017/libsodium/libsodium.vcxproj b/builds/msvc/vs2017/libsodium/libsodium.vcxproj index 4da1798b..7ddc2b18 100644 --- a/builds/msvc/vs2017/libsodium/libsodium.vcxproj +++ b/builds/msvc/vs2017/libsodium/libsodium.vcxproj @@ -94,6 +94,7 @@ + @@ -306,6 +307,7 @@ + diff --git a/builds/msvc/vs2017/libsodium/libsodium.vcxproj.filters b/builds/msvc/vs2017/libsodium/libsodium.vcxproj.filters index 3e5daeea..fc4770d8 100644 --- a/builds/msvc/vs2017/libsodium/libsodium.vcxproj.filters +++ b/builds/msvc/vs2017/libsodium/libsodium.vcxproj.filters @@ -102,6 +102,9 @@ crypto_pwhash\argon2 + + crypto_pwhash\argon2 + crypto_pwhash\argon2 @@ -734,6 +737,9 @@ crypto_pwhash\argon2 + + crypto_pwhash\argon2 + crypto_pwhash\argon2 diff --git a/builds/msvc/vs2019/libsodium/libsodium.vcxproj b/builds/msvc/vs2019/libsodium/libsodium.vcxproj index d11e17bc..d964cd8f 100644 --- a/builds/msvc/vs2019/libsodium/libsodium.vcxproj +++ b/builds/msvc/vs2019/libsodium/libsodium.vcxproj @@ -118,6 +118,7 @@ + @@ -330,6 +331,7 @@ + diff --git a/builds/msvc/vs2019/libsodium/libsodium.vcxproj.filters b/builds/msvc/vs2019/libsodium/libsodium.vcxproj.filters index 3e5daeea..fc4770d8 100644 --- a/builds/msvc/vs2019/libsodium/libsodium.vcxproj.filters +++ b/builds/msvc/vs2019/libsodium/libsodium.vcxproj.filters @@ -102,6 +102,9 @@ crypto_pwhash\argon2 + + crypto_pwhash\argon2 + crypto_pwhash\argon2 @@ -734,6 +737,9 @@ crypto_pwhash\argon2 + + crypto_pwhash\argon2 + crypto_pwhash\argon2 diff --git a/builds/msvc/vs2022/libsodium/libsodium.vcxproj b/builds/msvc/vs2022/libsodium/libsodium.vcxproj index 51ab0ff6..8f4cf407 100644 --- a/builds/msvc/vs2022/libsodium/libsodium.vcxproj +++ b/builds/msvc/vs2022/libsodium/libsodium.vcxproj @@ -118,6 +118,7 @@ + @@ -330,6 +331,7 @@ + diff --git a/builds/msvc/vs2022/libsodium/libsodium.vcxproj.filters b/builds/msvc/vs2022/libsodium/libsodium.vcxproj.filters index 3e5daeea..fc4770d8 100644 --- a/builds/msvc/vs2022/libsodium/libsodium.vcxproj.filters +++ b/builds/msvc/vs2022/libsodium/libsodium.vcxproj.filters @@ -102,6 +102,9 @@ crypto_pwhash\argon2 + + crypto_pwhash\argon2 + crypto_pwhash\argon2 @@ -734,6 +737,9 @@ crypto_pwhash\argon2 + + crypto_pwhash\argon2 + crypto_pwhash\argon2 diff --git a/builds/msvc/vs2026/libsodium/libsodium.vcxproj b/builds/msvc/vs2026/libsodium/libsodium.vcxproj index 3bb0375f..915f2094 100644 --- a/builds/msvc/vs2026/libsodium/libsodium.vcxproj +++ b/builds/msvc/vs2026/libsodium/libsodium.vcxproj @@ -118,6 +118,7 @@ + @@ -330,6 +331,7 @@ + diff --git a/builds/msvc/vs2026/libsodium/libsodium.vcxproj.filters b/builds/msvc/vs2026/libsodium/libsodium.vcxproj.filters index 3e5daeea..fc4770d8 100644 --- a/builds/msvc/vs2026/libsodium/libsodium.vcxproj.filters +++ b/builds/msvc/vs2026/libsodium/libsodium.vcxproj.filters @@ -102,6 +102,9 @@ crypto_pwhash\argon2 + + crypto_pwhash\argon2 + crypto_pwhash\argon2 @@ -734,6 +737,9 @@ crypto_pwhash\argon2 + + crypto_pwhash\argon2 + crypto_pwhash\argon2 diff --git a/ci/appveyor/libsodium.vcxproj b/ci/appveyor/libsodium.vcxproj index 1a3967e5..347feeb7 100644 --- a/ci/appveyor/libsodium.vcxproj +++ b/ci/appveyor/libsodium.vcxproj @@ -342,6 +342,7 @@ + @@ -554,6 +555,7 @@ + diff --git a/ci/appveyor/libsodium.vcxproj.filters b/ci/appveyor/libsodium.vcxproj.filters index 5e8e9bb5..469c8d9e 100644 --- a/ci/appveyor/libsodium.vcxproj.filters +++ b/ci/appveyor/libsodium.vcxproj.filters @@ -93,6 +93,9 @@ Source Files + + Source Files + Source Files @@ -725,6 +728,9 @@ Header Files + + Header Files + Header Files diff --git a/src/libsodium/Makefile.am b/src/libsodium/Makefile.am index 14e4d220..4531a986 100644 --- a/src/libsodium/Makefile.am +++ b/src/libsodium/Makefile.am @@ -267,7 +267,9 @@ libarmcrypto_la_SOURCES = \ crypto_aead/aegis256/aegis256_armcrypto.h \ crypto_aead/aes256gcm/armcrypto/aead_aes256gcm_armcrypto.c \ crypto_ipcrypt/ipcrypt_armcrypto.c \ - crypto_ipcrypt/ipcrypt_armcrypto.h + crypto_ipcrypt/ipcrypt_armcrypto.h \ + crypto_pwhash/argon2/argon2-fill-block-neon.c \ + crypto_pwhash/argon2/blamka-round-neon.h libaesni_la_LDFLAGS = $(libsodium_la_LDFLAGS) libaesni_la_CPPFLAGS = $(libsodium_la_CPPFLAGS) \ diff --git a/src/libsodium/crypto_pwhash/argon2/argon2-core.c b/src/libsodium/crypto_pwhash/argon2/argon2-core.c index 4230f46f..e6dc4eac 100644 --- a/src/libsodium/crypto_pwhash/argon2/argon2-core.c +++ b/src/libsodium/crypto_pwhash/argon2/argon2-core.c @@ -45,7 +45,12 @@ # define MAP_POPULATE 0 #endif +#if (defined(__aarch64__) || defined(_M_ARM64)) && \ + (defined(__ARM_NEON) || defined(__ARM_NEON__)) +static fill_segment_fn fill_segment = argon2_fill_segment_neon; +#else static fill_segment_fn fill_segment = argon2_fill_segment_ref; +#endif static void load_block(block *dst, const void *input) @@ -520,7 +525,12 @@ argon2_pick_best_implementation(void) return 0; } #endif +#if (defined(__aarch64__) || defined(_M_ARM64)) && \ + (defined(__ARM_NEON) || defined(__ARM_NEON__)) + fill_segment = argon2_fill_segment_neon; +#else fill_segment = argon2_fill_segment_ref; +#endif return 0; /* LCOV_EXCL_STOP */ diff --git a/src/libsodium/crypto_pwhash/argon2/argon2-core.h b/src/libsodium/crypto_pwhash/argon2/argon2-core.h index f7fd6d93..ecc2029d 100644 --- a/src/libsodium/crypto_pwhash/argon2/argon2-core.h +++ b/src/libsodium/crypto_pwhash/argon2/argon2-core.h @@ -260,6 +260,8 @@ void argon2_fill_segment_ssse3(const argon2_instance_t *instance, argon2_position_t position); void argon2_fill_segment_wasm32(const argon2_instance_t *instance, argon2_position_t position); +void argon2_fill_segment_neon(const argon2_instance_t *instance, + argon2_position_t position); void argon2_fill_segment_ref(const argon2_instance_t *instance, argon2_position_t position); diff --git a/src/libsodium/crypto_pwhash/argon2/argon2-fill-block-neon.c b/src/libsodium/crypto_pwhash/argon2/argon2-fill-block-neon.c new file mode 100644 index 00000000..3e3fec7a --- /dev/null +++ b/src/libsodium/crypto_pwhash/argon2/argon2-fill-block-neon.c @@ -0,0 +1,216 @@ +/* + * Argon2 source code package + * + * Written by Daniel Dinu and Dmitry Khovratovich, 2015 + * + * This work is licensed under a Creative Commons CC0 1.0 License/Waiver. + * + * You should have received a copy of the CC0 Public Domain Dedication along + * with + * this software. If not, see + * . + * + * AARCH64 NEON implementation ported from SSSE3 version. + */ + +#include +#include +#include + +#include "argon2-core.h" +#include "argon2.h" +#include "private/common.h" + +#if (defined(__aarch64__) || defined(_M_ARM64)) && \ + (defined(__ARM_NEON) || defined(__ARM_NEON__)) + +# include +# include "blamka-round-neon.h" + +static void +fill_block(uint64x2_t *state, const uint8_t *ref_block, uint8_t *next_block) +{ + uint64x2_t block_XY[ARGON2_OWORDS_IN_BLOCK]; + uint32_t i; + + for (i = 0; i < ARGON2_OWORDS_IN_BLOCK; i++) { + block_XY[i] = state[i] = veorq_u64( + state[i], vld1q_u64((const uint64_t *) (&ref_block[16 * i]))); + } + + for (i = 0; i < 8; ++i) { + BLAKE2_ROUND_NEON(state[8 * i + 0], state[8 * i + 1], state[8 * i + 2], + state[8 * i + 3], state[8 * i + 4], state[8 * i + 5], + state[8 * i + 6], state[8 * i + 7]); + } + + for (i = 0; i < 8; ++i) { + BLAKE2_ROUND_NEON(state[8 * 0 + i], state[8 * 1 + i], state[8 * 2 + i], + state[8 * 3 + i], state[8 * 4 + i], state[8 * 5 + i], + state[8 * 6 + i], state[8 * 7 + i]); + } + + for (i = 0; i < ARGON2_OWORDS_IN_BLOCK; i++) { + state[i] = veorq_u64(state[i], block_XY[i]); + vst1q_u64((uint64_t *) (&next_block[16 * i]), state[i]); + } +} + +static void +fill_block_with_xor(uint64x2_t *state, const uint8_t *ref_block, + uint8_t *next_block) +{ + uint64x2_t block_XY[ARGON2_OWORDS_IN_BLOCK]; + uint32_t i; + + for (i = 0; i < ARGON2_OWORDS_IN_BLOCK; i++) { + state[i] = veorq_u64( + state[i], vld1q_u64((const uint64_t *) (&ref_block[16 * i]))); + block_XY[i] = veorq_u64( + state[i], vld1q_u64((const uint64_t *) (&next_block[16 * i]))); + } + + for (i = 0; i < 8; ++i) { + BLAKE2_ROUND_NEON(state[8 * i + 0], state[8 * i + 1], state[8 * i + 2], + state[8 * i + 3], state[8 * i + 4], state[8 * i + 5], + state[8 * i + 6], state[8 * i + 7]); + } + + for (i = 0; i < 8; ++i) { + BLAKE2_ROUND_NEON(state[8 * 0 + i], state[8 * 1 + i], state[8 * 2 + i], + state[8 * 3 + i], state[8 * 4 + i], state[8 * 5 + i], + state[8 * 6 + i], state[8 * 7 + i]); + } + + for (i = 0; i < ARGON2_OWORDS_IN_BLOCK; i++) { + state[i] = veorq_u64(state[i], block_XY[i]); + vst1q_u64((uint64_t *) (&next_block[16 * i]), state[i]); + } +} + +static void +generate_addresses(const argon2_instance_t *instance, + const argon2_position_t *position, uint64_t *pseudo_rands) +{ + block address_block, input_block, tmp_block; + uint32_t i; + + init_block_value(&address_block, 0); + init_block_value(&input_block, 0); + + if (instance != NULL && position != NULL) { + input_block.v[0] = position->pass; + input_block.v[1] = position->lane; + input_block.v[2] = position->slice; + input_block.v[3] = instance->memory_blocks; + input_block.v[4] = instance->passes; + input_block.v[5] = instance->type; + + for (i = 0; i < instance->segment_length; ++i) { + if (i % ARGON2_ADDRESSES_IN_BLOCK == 0) { + uint64x2_t zero_block[ARGON2_OWORDS_IN_BLOCK]; + uint64x2_t zero2_block[ARGON2_OWORDS_IN_BLOCK]; + + memset(zero_block, 0, sizeof(zero_block)); + memset(zero2_block, 0, sizeof(zero2_block)); + init_block_value(&address_block, 0); + init_block_value(&tmp_block, 0); + + input_block.v[6]++; + + fill_block_with_xor(zero_block, (uint8_t *) &input_block.v, + (uint8_t *) &tmp_block.v); + fill_block_with_xor(zero2_block, (uint8_t *) &tmp_block.v, + (uint8_t *) &address_block.v); + } + + pseudo_rands[i] = address_block.v[i % ARGON2_ADDRESSES_IN_BLOCK]; + } + } +} + +void +argon2_fill_segment_neon(const argon2_instance_t *instance, + argon2_position_t position) +{ + block *ref_block = NULL, *curr_block = NULL; + uint64_t pseudo_rand, ref_index, ref_lane; + uint32_t prev_offset, curr_offset; + uint32_t starting_index, i; + uint64x2_t state[ARGON2_OWORDS_IN_BLOCK]; + int data_independent_addressing = 1; + + uint64_t *pseudo_rands = NULL; + + if (instance == NULL) { + return; + } + + if (instance->type == Argon2_id && + (position.pass != 0 || position.slice >= ARGON2_SYNC_POINTS / 2)) { + data_independent_addressing = 0; + } + + pseudo_rands = instance->pseudo_rands; + + if (data_independent_addressing) { + generate_addresses(instance, &position, pseudo_rands); + } + + starting_index = 0; + + if ((0 == position.pass) && (0 == position.slice)) { + starting_index = 2; + } + + curr_offset = position.lane * instance->lane_length + + position.slice * instance->segment_length + starting_index; + + if (0 == curr_offset % instance->lane_length) { + prev_offset = curr_offset + instance->lane_length - 1; + } else { + prev_offset = curr_offset - 1; + } + + memcpy(state, ((instance->region->memory + prev_offset)->v), + ARGON2_BLOCK_SIZE); + + for (i = starting_index; i < instance->segment_length; + ++i, ++curr_offset, ++prev_offset) { + if (curr_offset % instance->lane_length == 1) { + prev_offset = curr_offset - 1; + } + + if (data_independent_addressing) { +#pragma warning(push) +#pragma warning(disable : 6385) + pseudo_rand = pseudo_rands[i]; +#pragma warning(pop) + } else { + pseudo_rand = instance->region->memory[prev_offset].v[0]; + } + + ref_lane = ((pseudo_rand >> 32)) % instance->lanes; + + if ((position.pass == 0) && (position.slice == 0)) { + ref_lane = position.lane; + } + + position.index = i; + ref_index = index_alpha(instance, &position, pseudo_rand & 0xFFFFFFFF, + ref_lane == position.lane); + + ref_block = instance->region->memory + + instance->lane_length * ref_lane + ref_index; + curr_block = instance->region->memory + curr_offset; + if (position.pass != 0) { + fill_block_with_xor(state, (uint8_t *) ref_block->v, + (uint8_t *) curr_block->v); + } else { + fill_block(state, (uint8_t *) ref_block->v, + (uint8_t *) curr_block->v); + } + } +} + +#endif diff --git a/src/libsodium/crypto_pwhash/argon2/blamka-round-neon.h b/src/libsodium/crypto_pwhash/argon2/blamka-round-neon.h new file mode 100644 index 00000000..3362fe72 --- /dev/null +++ b/src/libsodium/crypto_pwhash/argon2/blamka-round-neon.h @@ -0,0 +1,161 @@ +#ifndef blamka_round_neon_H +#define blamka_round_neon_H + +#include + +#include "private/common.h" + +static inline uint64x2_t +fBlaMka_neon(uint64x2_t x, uint64x2_t y) +{ + const uint32x2_t x_lo = vmovn_u64(x); + const uint32x2_t y_lo = vmovn_u64(y); + const uint64x2_t z = vmull_u32(x_lo, y_lo); + + return vaddq_u64(vaddq_u64(x, y), vaddq_u64(z, z)); +} + +static inline uint64x2_t +rotr64_32_neon(uint64x2_t x) +{ + return vreinterpretq_u64_u32(vrev64q_u32(vreinterpretq_u32_u64(x))); +} + +static inline uint64x2_t +rotr64_24_neon(uint64x2_t x) +{ + static const uint8_t rot24_tbl[16] = { + 3, 4, 5, 6, 7, 0, 1, 2, + 11, 12, 13, 14, 15, 8, 9, 10 + }; + const uint8x16_t tbl = vld1q_u8(rot24_tbl); + return vreinterpretq_u64_u8(vqtbl1q_u8(vreinterpretq_u8_u64(x), tbl)); +} + +static inline uint64x2_t +rotr64_16_neon(uint64x2_t x) +{ + static const uint8_t rot16_tbl[16] = { + 2, 3, 4, 5, 6, 7, 0, 1, + 10, 11, 12, 13, 14, 15, 8, 9 + }; + const uint8x16_t tbl = vld1q_u8(rot16_tbl); + return vreinterpretq_u64_u8(vqtbl1q_u8(vreinterpretq_u8_u64(x), tbl)); +} + +static inline uint64x2_t +rotr64_63_neon(uint64x2_t x) +{ + return veorq_u64(vshrq_n_u64(x, 63), vaddq_u64(x, x)); +} + +#define G1_NEON(A0, B0, C0, D0, A1, B1, C1, D1) \ + do { \ + A0 = fBlaMka_neon(A0, B0); \ + A1 = fBlaMka_neon(A1, B1); \ + \ + D0 = veorq_u64(D0, A0); \ + D1 = veorq_u64(D1, A1); \ + \ + D0 = rotr64_32_neon(D0); \ + D1 = rotr64_32_neon(D1); \ + \ + C0 = fBlaMka_neon(C0, D0); \ + C1 = fBlaMka_neon(C1, D1); \ + \ + B0 = veorq_u64(B0, C0); \ + B1 = veorq_u64(B1, C1); \ + \ + B0 = rotr64_24_neon(B0); \ + B1 = rotr64_24_neon(B1); \ + } while ((void) 0, 0) + +#define G2_NEON(A0, B0, C0, D0, A1, B1, C1, D1) \ + do { \ + A0 = fBlaMka_neon(A0, B0); \ + A1 = fBlaMka_neon(A1, B1); \ + \ + D0 = veorq_u64(D0, A0); \ + D1 = veorq_u64(D1, A1); \ + \ + D0 = rotr64_16_neon(D0); \ + D1 = rotr64_16_neon(D1); \ + \ + C0 = fBlaMka_neon(C0, D0); \ + C1 = fBlaMka_neon(C1, D1); \ + \ + B0 = veorq_u64(B0, C0); \ + B1 = veorq_u64(B1, C1); \ + \ + B0 = rotr64_63_neon(B0); \ + B1 = rotr64_63_neon(B1); \ + } while ((void) 0, 0) + +#define DIAGONALIZE_NEON(A0, B0, C0, D0, A1, B1, C1, D1) \ + do { \ + uint64x2_t t0, t1; \ + \ + t0 = vreinterpretq_u64_u8( \ + vextq_u8(vreinterpretq_u8_u64(B0), \ + vreinterpretq_u8_u64(B1), 8)); \ + t1 = vreinterpretq_u64_u8( \ + vextq_u8(vreinterpretq_u8_u64(B1), \ + vreinterpretq_u8_u64(B0), 8)); \ + B0 = t0; \ + B1 = t1; \ + \ + t0 = C0; \ + C0 = C1; \ + C1 = t0; \ + \ + t0 = vreinterpretq_u64_u8( \ + vextq_u8(vreinterpretq_u8_u64(D0), \ + vreinterpretq_u8_u64(D1), 8)); \ + t1 = vreinterpretq_u64_u8( \ + vextq_u8(vreinterpretq_u8_u64(D1), \ + vreinterpretq_u8_u64(D0), 8)); \ + D0 = t1; \ + D1 = t0; \ + } while ((void) 0, 0) + +#define UNDIAGONALIZE_NEON(A0, B0, C0, D0, A1, B1, C1, D1) \ + do { \ + uint64x2_t t0, t1; \ + \ + t0 = vreinterpretq_u64_u8( \ + vextq_u8(vreinterpretq_u8_u64(B1), \ + vreinterpretq_u8_u64(B0), 8)); \ + t1 = vreinterpretq_u64_u8( \ + vextq_u8(vreinterpretq_u8_u64(B0), \ + vreinterpretq_u8_u64(B1), 8)); \ + B0 = t0; \ + B1 = t1; \ + \ + t0 = C0; \ + C0 = C1; \ + C1 = t0; \ + \ + t0 = vreinterpretq_u64_u8( \ + vextq_u8(vreinterpretq_u8_u64(D1), \ + vreinterpretq_u8_u64(D0), 8)); \ + t1 = vreinterpretq_u64_u8( \ + vextq_u8(vreinterpretq_u8_u64(D0), \ + vreinterpretq_u8_u64(D1), 8)); \ + D0 = t1; \ + D1 = t0; \ + } while ((void) 0, 0) + +#define BLAKE2_ROUND_NEON(A0, A1, B0, B1, C0, C1, D0, D1) \ + do { \ + G1_NEON(A0, B0, C0, D0, A1, B1, C1, D1); \ + G2_NEON(A0, B0, C0, D0, A1, B1, C1, D1); \ + \ + DIAGONALIZE_NEON(A0, B0, C0, D0, A1, B1, C1, D1); \ + \ + G1_NEON(A0, B0, C0, D0, A1, B1, C1, D1); \ + G2_NEON(A0, B0, C0, D0, A1, B1, C1, D1); \ + \ + UNDIAGONALIZE_NEON(A0, B0, C0, D0, A1, B1, C1, D1); \ + } while ((void) 0, 0) + +#endif