public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
From: David Gibson <david@gibson.dropbear.id.au>
To: Stefano Brivio <sbrivio@redhat.com>, passt-dev@passt.top
Cc: David Gibson <david@gibson.dropbear.id.au>
Subject: [PATCH v2 02/10] siphash: Make sip round calculations an inline function rather than macro
Date: Thu, 28 Sep 2023 11:20:54 +1000	[thread overview]
Message-ID: <20230928012102.1446180-3-david@gibson.dropbear.id.au> (raw)
In-Reply-To: <20230928012102.1446180-1-david@gibson.dropbear.id.au>

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 <david@gibson.dropbear.id.au>
---
 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;
 	}
 
-- 
@@ -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


  parent reply	other threads:[~2023-09-28  1:21 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-28  1:20 [PATCH v2 00/10] siphash: cleanups and fixes David Gibson
2023-09-28  1:20 ` [PATCH v2 01/10] siphash: Make siphash functions consistently return 64-bit results David Gibson
2023-09-28  1:20 ` David Gibson [this message]
2023-09-28  1:20 ` [PATCH v2 03/10] siphash: Add siphash_feed() helper David Gibson
2023-09-28  1:20 ` [PATCH v2 04/10] siphash: Clean up hash finalisation with posthash_final() function David Gibson
2023-09-28  1:20 ` [PATCH v2 05/10] siphash: Fix bug in state initialisation David Gibson
2023-09-28  1:20 ` [PATCH v2 06/10] siphash: Use more hygienic state initialiser David Gibson
2023-09-28  1:20 ` [PATCH v2 07/10] siphash: Use specific structure for internal state David Gibson
2023-09-28  1:21 ` [PATCH v2 08/10] siphash: Make internal helpers public David Gibson
2023-09-28  1:21 ` [PATCH v2 09/10] siphash, checksum: Move TBAA explanation to checksum.c David Gibson
2023-09-28  1:21 ` [PATCH v2 10/10] siphash: Use incremental rather than all-at-once siphash functions David Gibson
2023-09-30 10:43 ` [PATCH v2 00/10] siphash: cleanups and fixes Stefano Brivio

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230928012102.1446180-3-david@gibson.dropbear.id.au \
    --to=david@gibson.dropbear.id.au \
    --cc=passt-dev@passt.top \
    --cc=sbrivio@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://passt.top/passt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for IMAP folder(s).