bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bitrotate


From: Simon Josefsson
Subject: bitrotate
Date: Thu, 28 Aug 2008 14:33:37 +0200
User-agent: Gnus/5.110011 (No Gnus v0.11) Emacs/22.2 (gnu/linux)

How about a bitrotate module?

I needed bitrotate functions in a project, and noticed that arctwo in
gnulib also included definitions.  This patch adds a new module
bitrotate and makes arctwo use it.

If anyone has ideas on better names (currently rotl16, rotr16 etc) or
header filename (currently bitrotate.h), that would be great.  I
couldn't find any bit rotation functions in any semi-official header
file on my debian system, otherwise I'd simulate those names.

Naturally, I'll include ChangeLog entries when I commit and push it.

/Simon

>From 35a7e73947d560c0a28a16c7ba7d1a05936d75a6 Mon Sep 17 00:00:00 2001
From: Simon Josefsson <address@hidden>
Date: Thu, 28 Aug 2008 14:30:51 +0200
Subject: [PATCH] Add bitrotate module.  Make arctwo use it.

---
 lib/arctwo.c             |    7 ++--
 lib/bitrotate.h          |   48 ++++++++++++++++++++++++
 modules/bitrotate        |   21 +++++++++++
 modules/bitrotate-tests  |   10 +++++
 modules/crypto/arctwo    |    1 +
 modules/crypto/gc-arctwo |    1 +
 tests/test-bitrotate.c   |   91 ++++++++++++++++++++++++++++++++++++++++++++++
 7 files changed, 175 insertions(+), 4 deletions(-)
 create mode 100644 lib/bitrotate.h
 create mode 100644 modules/bitrotate
 create mode 100644 modules/bitrotate-tests
 create mode 100644 tests/test-bitrotate.c

diff --git a/lib/arctwo.c b/lib/arctwo.c
index ccaa4b6..baf3c61 100644
--- a/lib/arctwo.c
+++ b/lib/arctwo.c
@@ -1,5 +1,5 @@
 /* arctwo.c --- The RC2 cipher as described in RFC 2268.
- * Copyright (C) 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
+ * Copyright (C) 2003, 2004, 2005, 2006, 2008 Free Software Foundation, Inc.
  *
  * This file is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published
@@ -31,6 +31,8 @@
 
 #include "arctwo.h"
 
+#include "bitrotate.h"
+
 static const uint8_t arctwo_sbox[] = {
   217, 120, 249, 196, 25, 221, 181, 237,
   40, 233, 253, 121, 74, 160, 216, 157,
@@ -66,9 +68,6 @@ static const uint8_t arctwo_sbox[] = {
   10, 166, 32, 104, 254, 127, 193, 173
 };
 
-#define rotl16(x,n)   (((x) << ((uint16_t)(n))) | ((x) >> (16 - 
(uint16_t)(n))))
-#define rotr16(x,n)   (((x) >> ((uint16_t)(n))) | ((x) << (16 - 
(uint16_t)(n))))
-
 /* C89 compliant way to cast 'char' to 'unsigned char'. */
 static inline unsigned char
 to_uchar (char ch)
diff --git a/lib/bitrotate.h b/lib/bitrotate.h
new file mode 100644
index 0000000..7c2c395
--- /dev/null
+++ b/lib/bitrotate.h
@@ -0,0 +1,48 @@
+/* bitrotate.h - Rotate bits in integers
+   Copyright (C) 2008 Free Software Foundation, Inc.
+
+   This program is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+/* Written by Simon Josefsson <address@hidden>, 2008. */
+
+#ifndef _GL_BITROTATE_H
+#define _GL_BITROTATE_H
+
+#include <stdint.h>
+
+/* Given an unsigned 32-bit argument X, return the value corresponding
+   to rotating the bits N steps to the left.  N must be between 1 and
+   31 inclusive. */
+#define rotl32(x,n) \
+  ((((x) << ((uint16_t)(n))) | ((x) >> (32 - (uint16_t)(n)))) & 0xFFFFFFFF)
+
+/* Given an unsigned 32-bit argument X, return the value corresponding
+   to rotating the bits N steps to the right.  N must be between 1 to
+   31 inclusive.*/
+#define rotr32(x,n) \
+  ((((x) >> ((uint16_t)(n))) | ((x) << (32 - (uint16_t)(n)))) & 0xFFFFFFFF)
+
+/* Given an unsigned 16-bit argument X, return the value corresponding
+   to rotating the bits N steps to the left.  N must be between 1 to
+   15 inclusive. */
+#define rotl16(x,n) \
+  ((((x) << ((uint16_t)(n))) | ((x) >> (16 - (uint16_t)(n)))) & 0xFFFF)
+
+/* Given an unsigned 16-bit argument X, return the value corresponding
+   to rotating the bits N steps to the right.  N must be in 1 to 15
+   inclusive. */
+#define rotr16(x,n) \
+  ((((x) >> ((uint16_t)(n))) | ((x) << (16 - (uint16_t)(n)))) & 0xFFFF)
+
+#endif /* _GL_BITROTATE_H */
diff --git a/modules/bitrotate b/modules/bitrotate
new file mode 100644
index 0000000..df94a61
--- /dev/null
+++ b/modules/bitrotate
@@ -0,0 +1,21 @@
+Description:
+Rotate bits in 16 and 32 bit integers.
+
+Files:
+lib/bitrotate.h
+
+Depends-on:
+
+configure.ac:
+
+Makefile.am:
+lib_SOURCES += bitrotate.h
+
+Include:
+"bitrotate.h"
+
+License:
+LGPLv2+
+
+Maintainer:
+Simon Josefsson
diff --git a/modules/bitrotate-tests b/modules/bitrotate-tests
new file mode 100644
index 0000000..405607a
--- /dev/null
+++ b/modules/bitrotate-tests
@@ -0,0 +1,10 @@
+Files:
+tests/test-bitrotate.c
+
+Depends-on:
+
+configure.ac:
+
+Makefile.am:
+TESTS += test-bitrotate
+check_PROGRAMS += test-bitrotate
diff --git a/modules/crypto/arctwo b/modules/crypto/arctwo
index 502b471..c6ac35f 100644
--- a/modules/crypto/arctwo
+++ b/modules/crypto/arctwo
@@ -8,6 +8,7 @@ m4/arctwo.m4
 
 Depends-on:
 stdint
+bitrotate
 
 configure.ac:
 gl_ARCTWO
diff --git a/modules/crypto/gc-arctwo b/modules/crypto/gc-arctwo
index 479120a..b265402 100644
--- a/modules/crypto/gc-arctwo
+++ b/modules/crypto/gc-arctwo
@@ -10,6 +10,7 @@ m4/arctwo.m4
 Depends-on:
 stdint
 crypto/gc
+bitrotate
 
 configure.ac:
 gl_GC_ARCTWO
diff --git a/tests/test-bitrotate.c b/tests/test-bitrotate.c
new file mode 100644
index 0000000..619ed41
--- /dev/null
+++ b/tests/test-bitrotate.c
@@ -0,0 +1,91 @@
+/* Test of <bitrotate.h> substitute.
+   Copyright (C) 2007-2008 Free Software Foundation, Inc.
+
+   This program is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+/* Written by Simon Josefsson <address@hidden>, 2008.  */
+
+#include <config.h>
+
+#include "bitrotate.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#define ASSERT(expr)                                                   \
+  do                                                                   \
+    {                                                                  \
+      if (!(expr))                                                     \
+       {                                                               \
+         fprintf (stderr, "%s:%d: assertion failed\n",                 \
+                  __FILE__, __LINE__);                                 \
+         fflush (stderr);                                              \
+         abort ();                                                     \
+       }                                                               \
+    }                                                                  \
+  while (0)
+
+int
+main (void)
+{
+  ASSERT (rotl16 (43981, 1) == 22427);
+  ASSERT (rotl16 (43981, 2) == 44854);
+  ASSERT (rotl16 (43981, 3) == 24173);
+  ASSERT (rotl16 (43981, 4) == 48346);
+  ASSERT (rotl16 (43981, 5) == 31157);
+  ASSERT (rotl16 (43981, 6) == 62314);
+  ASSERT (rotl16 (43981, 7) == 59093);
+  ASSERT (rotl16 (43981, 8) == 52651);
+  ASSERT (rotl16 (43981, 9) == 39767);
+  ASSERT (rotl16 (43981, 10) == 13999);
+  ASSERT (rotl16 (43981, 11) == 27998);
+  ASSERT (rotl16 (43981, 12) == 55996);
+  ASSERT (rotl16 (43981, 13) == 46457);
+  ASSERT (rotl16 (43981, 14) == 27379);
+  ASSERT (rotl16 (43981, 15) == 54758);
+
+  ASSERT (rotl32 (2309737967U, 1) == 324508639U);
+  ASSERT (rotl32 (2309737967U, 2) == 649017278U);
+  ASSERT (rotl32 (2309737967U, 3) == 1298034556U);
+  ASSERT (rotl32 (2309737967U, 4) == 2596069112U);
+  ASSERT (rotl32 (2309737967U, 5) == 897170929U);
+  ASSERT (rotl32 (2309737967U, 6) == 1794341858U);
+  ASSERT (rotl32 (2309737967U, 7) == 3588683716U);
+  ASSERT (rotl32 (2309737967U, 8) == 2882400137U);
+  ASSERT (rotl32 (2309737967U, 9) == 1469832979U);
+  ASSERT (rotl32 (2309737967U, 10) == 2939665958U);
+  ASSERT (rotl32 (2309737967U, 11) == 1584364621U);
+  ASSERT (rotl32 (2309737967U, 12) == 3168729242U);
+  ASSERT (rotl32 (2309737967U, 13) == 2042491189U);
+  ASSERT (rotl32 (2309737967U, 14) == 4084982378U);
+  ASSERT (rotl32 (2309737967U, 15) == 3874997461U);
+  ASSERT (rotl32 (2309737967U, 16) == 3455027627U);
+  ASSERT (rotl32 (2309737967U, 17) == 2615087959U);
+  ASSERT (rotl32 (2309737967U, 18) == 935208623U);
+  ASSERT (rotl32 (2309737967U, 19) == 1870417246U);
+  ASSERT (rotl32 (2309737967U, 20) == 3740834492U);
+  ASSERT (rotl32 (2309737967U, 21) == 3186701689U);
+  ASSERT (rotl32 (2309737967U, 22) == 2078436083U);
+  ASSERT (rotl32 (2309737967U, 23) == 4156872166U);
+  ASSERT (rotl32 (2309737967U, 24) == 4018777037U);
+  ASSERT (rotl32 (2309737967U, 25) == 3742586779U);
+  ASSERT (rotl32 (2309737967U, 26) == 3190206263U);
+  ASSERT (rotl32 (2309737967U, 27) == 2085445231U);
+  ASSERT (rotl32 (2309737967U, 28) == 4170890462U);
+  ASSERT (rotl32 (2309737967U, 29) == 4046813629U);
+  ASSERT (rotl32 (2309737967U, 30) == 3798659963U);
+  ASSERT (rotl32 (2309737967U, 31) == 3302352631U);
+
+  return 0;
+}
-- 
1.5.6.3





reply via email to

[Prev in Thread] Current Thread [Next in Thread]