From 9cceb5a5be55e7c62162dfd234f6134bf91fda7d Mon Sep 17 00:00:00 2001 From: Mathias Berchtold Date: Thu, 3 Sep 2026 16:44:59 +0200 Subject: [PATCH 1/2] Implement SIMD optimizations for tag comparison Add SIMD support for poly1305 tag comparison --- src/chacha20poly1305.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/chacha20poly1305.c b/src/chacha20poly1305.c index 86157dc0..707ae304 100644 --- a/src/chacha20poly1305.c +++ b/src/chacha20poly1305.c @@ -10,6 +10,12 @@ #include "mystdint.h" #include +#if defined(__wasm_simd128__) +#include +#elif defined(__SSE4_1__) +#include +#endif + #define ROL32(x, c) (((x) << (c)) | ((x) >> (32-(c)))) #define ROR32(x, c) (((x) >> (c)) | ((x) << (32-(c)))) @@ -215,6 +221,16 @@ void poly1305(const uint8_t* msg, size_t n, const uint8_t key[32], SQLITE_PRIVATE int poly1305_tagcmp(const uint8_t tag1[16], const uint8_t tag2[16]) { +#if defined(__wasm_simd128__) + v128_t tagDifference = wasm_v128_xor( + wasm_v128_load(tag1), wasm_v128_load(tag2)); + return wasm_v128_any_true(tagDifference); +#elif defined(__SSE4_1__) + __m128i tag1_vec = _mm_loadu_si128((const __m128i*)tag1); + __m128i tag2_vec = _mm_loadu_si128((const __m128i*)tag2); + __m128i tagDifference = _mm_xor_si128(tag1_vec, tag2_vec); + return !_mm_testz_si128(tagDifference, tagDifference); +#else uint8_t d = 0; d |= tag1[ 0] ^ tag2[ 0]; d |= tag1[ 1] ^ tag2[ 1]; @@ -233,6 +249,7 @@ int poly1305_tagcmp(const uint8_t tag1[16], const uint8_t tag2[16]) d |= tag1[14] ^ tag2[14]; d |= tag1[15] ^ tag2[15]; return (int) d; +#endif } /* From 64d9a8a0cb703eac9575b5e4f5d96277197a0cde Mon Sep 17 00:00:00 2001 From: Mathias Berchtold Date: Thu, 3 Sep 2026 17:05:49 +0200 Subject: [PATCH 2/2] added AArch64 (ARMv8) implementation --- src/chacha20poly1305.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/chacha20poly1305.c b/src/chacha20poly1305.c index 707ae304..a48bcdc4 100644 --- a/src/chacha20poly1305.c +++ b/src/chacha20poly1305.c @@ -14,6 +14,8 @@ #include #elif defined(__SSE4_1__) #include +#elif defined(__aarch64__) +#include #endif #define ROL32(x, c) (((x) << (c)) | ((x) >> (32-(c)))) @@ -230,6 +232,11 @@ int poly1305_tagcmp(const uint8_t tag1[16], const uint8_t tag2[16]) __m128i tag2_vec = _mm_loadu_si128((const __m128i*)tag2); __m128i tagDifference = _mm_xor_si128(tag1_vec, tag2_vec); return !_mm_testz_si128(tagDifference, tagDifference); +#elif defined(__aarch64__) + uint8x16_t tag1_vec = vld1q_u8(tag1); + uint8x16_t tag2_vec = vld1q_u8(tag2); + uint8x16_t tagDifference = veorq_u8(tag1_vec, tag2_vec); + return vmaxvq_u8(tagDifference) != 0; #else uint8_t d = 0; d |= tag1[ 0] ^ tag2[ 0];