]> git.ipfire.org Git - thirdparty/nettle.git/commitdiff
ocb: Rewrite trailing-zeros loop to not use __builtin_ctzll.
authorNiels Möller <nisse@lysator.liu.se>
Sat, 14 May 2022 20:33:28 +0000 (22:33 +0200)
committerNiels Möller <nisse@lysator.liu.se>
Mon, 6 Feb 2023 19:20:01 +0000 (20:20 +0100)
ocb.c

diff --git a/ocb.c b/ocb.c
index 404c5b5a24a27f9878bd51a0de945519b2cc89e7..880c11a400d92c4f221c38f66bb169b788f46a24 100644 (file)
--- a/ocb.c
+++ b/ocb.c
@@ -79,22 +79,23 @@ ocb_set_key (struct ocb_key *key, const void *cipher, nettle_cipher_func *f)
   block16_mulx_be (&key->L[2], &key->L[1]);
 }
 
+/* Add x^k L[2], where k is the number of trailing bits in i. */
 static void
 update_offset(const struct ocb_key *key,
              union nettle_block16 *offset, size_t i)
 {
-  unsigned ntz = __builtin_ctzll(i);
-  if (ntz > 0)
+  if (i & 1)
+    block16_xor (offset, &key->L[2]);
+  else
     {
+      assert (i > 0);
       union nettle_block16 diff;
       block16_mulx_be (&diff, &key->L[2]);
-      while (--ntz > 0)
+      for (i >>= 1; !(i&1); i >>= 1)
        block16_mulx_be (&diff, &diff);
 
       block16_xor (offset, &diff);
     }
-  else
-    block16_xor (offset, &key->L[2]);
 }
 
 static void