From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from gandalf.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by passt.top (Postfix) with ESMTPS id C5B155A026D for ; Fri, 22 Sep 2023 16:06:39 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=201602; t=1695391594; bh=oHmN9Fvoq3rg3Ft7aVlzinMcVDCt9Ifu9EQjZrySsBI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=YY52hnAP4N5rDRlvrWCgP9CsInV6Nx/gMVoh+l4cAfl7CE53aAxxCr55UGZYWcV9e WYEI5PkbEtrbsYf3+pOr/sEwZQEB3NFMRR9qNBj+JPEdovqmaVwPpcOGlAbKUVYW00 eFK0IH9DMYFxrcPx1EiVrmY8EpmdUa0YwIaKw5ng= Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4RsYwV0YXNz4wy7; Sat, 23 Sep 2023 00:06:34 +1000 (AEST) From: David Gibson To: Stefano Brivio , passt-dev@passt.top Subject: [PATCH 02/10] siphash: Make sip round calculations an inline function rather than macro Date: Sat, 23 Sep 2023 00:06:22 +1000 Message-ID: <20230922140630.3184256-3-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230922140630.3184256-1-david@gibson.dropbear.id.au> References: <20230922140630.3184256-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: WVCQRZQIZGXYMIONIDWOLZU7QYWBPOAU X-Message-ID-Hash: WVCQRZQIZGXYMIONIDWOLZU7QYWBPOAU X-MailFrom: dgibson@gandalf.ozlabs.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: David Gibson X-Mailman-Version: 3.3.8 Precedence: list List-Id: Development discussion and patches for passt Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: The SIPROUND(n) macro implements n rounds of SipHash shuffling. It relies on 'v' and '__i' variables being available in the context it's used in which isn't great hygeine. Replace it with an inline function instead. Signed-off-by: David Gibson --- siphash.c | 51 +++++++++++++++++++++++++++++---------------------- 1 file changed, 29 insertions(+), 22 deletions(-) diff --git a/siphash.c b/siphash.c index 20009fe..e1fcf18 100644 --- a/siphash.c +++ b/siphash.c @@ -68,29 +68,36 @@ v[__i] = k[__i % 2]; \ } while (0) -#define SIPROUND(n) \ - do { \ - for (__i = 0; __i < (n); __i++) { \ - v[0] += v[1]; \ - v[1] = ROTL(v[1], 13) ^ v[0]; \ - v[0] = ROTL(v[0], 32); \ - v[2] += v[3]; \ - v[3] = ROTL(v[3], 16) ^ v[2]; \ - v[0] += v[3]; \ - v[3] = ROTL(v[3], 21) ^ v[0]; \ - v[2] += v[1]; \ - v[1] = ROTL(v[1], 17) ^ v[2]; \ - v[2] = ROTL(v[2], 32); \ - } \ - } while (0) +/** + * sipround() - Perform rounds of SipHash scrambling + * @v: siphash state (4 x 64-bit integers) + * @n: Number of rounds to apply + */ +static inline void sipround(uint64_t *v, int n) +{ + int i; + + for (i = 0; i < n; i++) { + v[0] += v[1]; + v[1] = ROTL(v[1], 13) ^ v[0]; + v[0] = ROTL(v[0], 32); + v[2] += v[3]; + v[3] = ROTL(v[3], 16) ^ v[2]; + v[0] += v[3]; + v[3] = ROTL(v[3], 21) ^ v[0]; + v[2] += v[1]; + v[1] = ROTL(v[1], 17) ^ v[2]; + v[2] = ROTL(v[2], 32); + } +} #define POSTAMBLE \ do { \ v[3] ^= b; \ - SIPROUND(2); \ + sipround(v, 2); \ v[0] ^= b; \ v[2] ^= 0xff; \ - SIPROUND(4); \ + sipround(v, 4); \ b = (v[0] ^ v[1]) ^ (v[2] ^ v[3]); \ } while (0) @@ -117,7 +124,7 @@ uint64_t siphash_8b(const uint8_t *in, const uint64_t *k) { PREAMBLE(8); v[3] ^= *(uint64_t *)in; - SIPROUND(2); + sipround(v, 2); v[0] ^= *(uint64_t *)in; POSTAMBLE; @@ -143,7 +150,7 @@ uint64_t siphash_12b(const uint8_t *in, const uint64_t *k) PREAMBLE(12); v[3] ^= combined; - SIPROUND(2); + sipround(v, 2); v[0] ^= combined; b |= *(in32 + 2); POSTAMBLE; @@ -171,7 +178,7 @@ uint64_t siphash_20b(const uint8_t *in, const uint64_t *k) uint64_t combined = (uint64_t)(*(in32 + 1)) << 32 | *in32; v[3] ^= combined; - SIPROUND(2); + sipround(v, 2); v[0] ^= combined; } @@ -200,7 +207,7 @@ uint64_t siphash_32b(const uint8_t *in, const uint64_t *k) for (i = 0; i < 4; i++, in64++) { v[3] ^= *in64; - SIPROUND(2); + sipround(v, 2); v[0] ^= *in64; } @@ -229,7 +236,7 @@ uint64_t siphash_36b(const uint8_t *in, const uint64_t *k) uint64_t combined = (uint64_t)(*(in32 + 1)) << 32 | *in32; v[3] ^= combined; - SIPROUND(2); + sipround(v, 2); v[0] ^= combined; } -- 2.41.0