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 C55945A0274 for ; Thu, 28 Sep 2023 03:21:08 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=201602; t=1695864064; bh=FwLWJZYfswh/L3/e6l7Gk3RSdI6XLufhmZ6KzwOI2ao=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=cYV0oxVR0N5bXrLC5l5/cqMymS2LP8A4kNdJhDhjXgT4q9yLb6bQkPotD9TK2ZLCs xluaBYLaxkiAdbrXy4szqR7UIXIn2M1QM3hHO2NOJVp9+wazvHklmdOZDGSBk1m3nz 4f8oP/eEeWEho11fauw8c/UBMNB67Bz9Y5bv+uQ8= Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4RwwfS70Mxz4xRs; Thu, 28 Sep 2023 11:21:04 +1000 (AEST) From: David Gibson To: Stefano Brivio , passt-dev@passt.top Subject: [PATCH v2 03/10] siphash: Add siphash_feed() helper Date: Thu, 28 Sep 2023 11:20:55 +1000 Message-ID: <20230928012102.1446180-4-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230928012102.1446180-1-david@gibson.dropbear.id.au> References: <20230928012102.1446180-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: D336QCRDPVHSIM7DQQ4FHRDVCXNMH7G2 X-Message-ID-Hash: D336QCRDPVHSIM7DQQ4FHRDVCXNMH7G2 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: We have macros or inlines for a number of common operations in the siphash functions. However, in a number of places we still open code feeding another 64-bits of data into the hash function: an xor, followed by 2 rounds of shuffling, followed by another xor. Implement an inline function for this, which results in somewhat shortened code. Signed-off-by: David Gibson --- siphash.c | 52 +++++++++++++++++++++------------------------------- 1 file changed, 21 insertions(+), 31 deletions(-) diff --git a/siphash.c b/siphash.c index e1fcf18..716ab62 100644 --- a/siphash.c +++ b/siphash.c @@ -91,11 +91,21 @@ static inline void sipround(uint64_t *v, int n) } } +/** + * siphash_feed() - Fold 64-bits of data into the hash state + * @v: siphash state (4 x 64-bit integers) + * @in: New value to fold into hash + */ +static inline void siphash_feed(uint64_t *v, uint64_t in) +{ + v[3] ^= in; + sipround(v, 2); + v[0] ^= in; +} + #define POSTAMBLE \ do { \ - v[3] ^= b; \ - sipround(v, 2); \ - v[0] ^= b; \ + siphash_feed(v, b); \ v[2] ^= 0xff; \ sipround(v, 4); \ b = (v[0] ^ v[1]) ^ (v[2] ^ v[3]); \ @@ -123,9 +133,7 @@ __attribute__((optimize("-fno-strict-aliasing"))) uint64_t siphash_8b(const uint8_t *in, const uint64_t *k) { PREAMBLE(8); - v[3] ^= *(uint64_t *)in; - sipround(v, 2); - v[0] ^= *(uint64_t *)in; + siphash_feed(v, *(uint64_t *)in); POSTAMBLE; return b; @@ -144,14 +152,9 @@ __attribute__((optimize("-fno-strict-aliasing"))) /* See siphash_8b() */ uint64_t siphash_12b(const uint8_t *in, const uint64_t *k) { uint32_t *in32 = (uint32_t *)in; - uint64_t combined; - - combined = (uint64_t)(*(in32 + 1)) << 32 | *in32; PREAMBLE(12); - v[3] ^= combined; - sipround(v, 2); - v[0] ^= combined; + siphash_feed(v, (uint64_t)(*(in32 + 1)) << 32 | *in32); b |= *(in32 + 2); POSTAMBLE; @@ -174,13 +177,8 @@ uint64_t siphash_20b(const uint8_t *in, const uint64_t *k) PREAMBLE(20); - for (i = 0; i < 2; i++, in32 += 2) { - uint64_t combined = (uint64_t)(*(in32 + 1)) << 32 | *in32; - - v[3] ^= combined; - sipround(v, 2); - v[0] ^= combined; - } + for (i = 0; i < 2; i++, in32 += 2) + siphash_feed(v, (uint64_t)(*(in32 + 1)) << 32 | *in32); b |= *in32; POSTAMBLE; @@ -205,11 +203,8 @@ uint64_t siphash_32b(const uint8_t *in, const uint64_t *k) PREAMBLE(32); - for (i = 0; i < 4; i++, in64++) { - v[3] ^= *in64; - sipround(v, 2); - v[0] ^= *in64; - } + for (i = 0; i < 4; i++, in64++) + siphash_feed(v, *in64); POSTAMBLE; @@ -232,13 +227,8 @@ uint64_t siphash_36b(const uint8_t *in, const uint64_t *k) PREAMBLE(36); - for (i = 0; i < 4; i++, in32 += 2) { - uint64_t combined = (uint64_t)(*(in32 + 1)) << 32 | *in32; - - v[3] ^= combined; - sipround(v, 2); - v[0] ^= combined; - } + for (i = 0; i < 4; i++, in32 += 2) + siphash_feed(v, (uint64_t)(*(in32 + 1)) << 32 | *in32); b |= *in32; POSTAMBLE; -- 2.41.0