From: mdw Date: Sat, 19 Oct 2002 17:56:50 +0000 (+0000) Subject: Fix bit operations. Test them (a bit) better. X-Git-Url: https://git.distorted.org.uk/u/mdw/catacomb/commitdiff_plain/75263f25a1ce8e7b38ad4bd61a9a893723ec1db3 Fix bit operations. Test them (a bit) better. --- diff --git a/Makefile.m4 b/Makefile.m4 index 0f2e3c3..bd4be03 100644 --- a/Makefile.m4 +++ b/Makefile.m4 @@ -1,6 +1,6 @@ ## -*-makefile-*- ## -## $Id: Makefile.m4,v 1.57 2002/10/15 22:58:29 mdw Exp $ +## $Id: Makefile.m4,v 1.58 2002/10/19 17:56:50 mdw Exp $ ## ## Makefile for Catacomb ## @@ -29,6 +29,9 @@ ##----- Revision history ---------------------------------------------------- ## ## $Log: Makefile.m4,v $ +## Revision 1.58 2002/10/19 17:56:50 mdw +## Fix bit operations. Test them (a bit) better. +## ## Revision 1.57 2002/10/15 22:58:29 mdw ## Fast estimation of number representation lengths. ## @@ -301,7 +304,7 @@ pkginclude_HEADERS = \ blkc.h hash.h gcipher.h ghash.h gmac.h grand.h ghash-def.h \ lcrand.h fibrand.h rc4.h seal.h rand.h noise.h fipstest.h maurer.h \ key.h key-data.h passphrase.h pixie.h lmem.h \ - mpx.h mpw.h mpscan.h mparena.h mp.h mptext.h mpint.h \ + mpx.h bitops.h mpw.h mpscan.h mparena.h mp.h mptext.h mpint.h \ exp.h mpbarrett.h mpmont.h mpcrt.h mprand.h mpmul.h \ gfx.h \ primetab.h pfilt.h rabin.h \ @@ -380,7 +383,7 @@ patsubst(PGEN_SOURCES, `\.c\>', `.lo') dsig.o keyutil.o rspit.o: primetab.h bin_PROGRAMS = dsig key pixie rspit factorial hashsum mkphrase bin_SCRIPTS = catacomb-config xpixie noinst_PROGRAMS = \ - genprimes mptypes serpent-check \ + genprimes mptypes serpent-check bittest \ addsuffix(`gen_tables', `-mktab') LDADD = libcatacomb.la @@ -392,6 +395,8 @@ factorial_SOURCES = factorial.c pixie_SOURCES = pixie.c pixie-common.c lmem.c arena.c pixie_LDADD = mkphrase_SOURCES = mkphrase.c +bittest_SOURCES = bittest.c +bittest_LDADD = define(`emit', ` patsubst(_item, `[^a-zA-Z0-9]', `_')_mktab_SOURCES = _item`'-mktab.c @@ -500,7 +505,7 @@ CTESTRIG(gfshare) CTESTRIG(share) CTESTRIG(rho) -TESTS = serpent-check testprogs +TESTS = serpent-check bittest testprogs CLEANFILES = \ *.t *.to \ diff --git a/bitops.h b/bitops.h new file mode 100644 index 0000000..5b91ce3 --- /dev/null +++ b/bitops.h @@ -0,0 +1,70 @@ +/* -*-c-*- + * + * $Id: bitops.h,v 1.1 2002/10/19 17:56:50 mdw Exp $ + * + * Bit operations by truth table + * + * (c) 2002 Straylight/Edgeware + */ + +/*----- Licensing notice --------------------------------------------------* + * + * This file is part of Catacomb. + * + * Catacomb is free software; you can redistribute it and/or modify + * it under the terms of the GNU Library General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * Catacomb 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 Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with Catacomb; if not, write to the Free + * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, + * MA 02111-1307, USA. + */ + +/*----- Revision history --------------------------------------------------* + * + * $Log: bitops.h,v $ + * Revision 1.1 2002/10/19 17:56:50 mdw + * Fix bit operations. Test them (a bit) better. + * + */ + +#ifndef CATACOMB_BITOPS_H +#define CATACOMB_BITOPS_H + +#ifdef __cplusplus + extern "C" { +#endif + +/*----- Macros ------------------------------------------------------------*/ + +#define B0000(x, y) (0u) +#define B0001(x, y) ((x) & (y)) +#define B0010(x, y) ((x) & ~(y)) +#define B0011(x, y) (x) +#define B0100(x, y) (~(x) & (y)) +#define B0101(x, y) (y) +#define B0110(x, y) ((x) ^ (y)) +#define B0111(x, y) ((x) | (y)) +#define B1000(x, y) (~((x) | (y))) +#define B1001(x, y) (~((x) ^ (y))) +#define B1010(x, y) (~(y)) +#define B1011(x, y) ((x) | ~(y)) +#define B1100(x, y) (~(x)) +#define B1101(x, y) (~(x) | (y)) +#define B1110(x, y) (~((x) & (y))) +#define B1111(x, y) (~0u) + +/*----- That's all, folks -------------------------------------------------*/ + +#ifdef __cplusplus + } +#endif + +#endif diff --git a/bittest.c b/bittest.c new file mode 100644 index 0000000..54bc6d5 --- /dev/null +++ b/bittest.c @@ -0,0 +1,66 @@ +/* -*-c-*- + * + * $Id: bittest.c,v 1.1 2002/10/19 17:56:50 mdw Exp $ + * + * Check the bit operations work + * + * (c) 2002 Straylight/Edgeware + */ + +/*----- Licensing notice --------------------------------------------------* + * + * This file is part of Catacomb. + * + * Catacomb is free software; you can redistribute it and/or modify + * it under the terms of the GNU Library General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * Catacomb 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 Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with Catacomb; if not, write to the Free + * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, + * MA 02111-1307, USA. + */ + +/*----- Revision history --------------------------------------------------* + * + * $Log: bittest.c,v $ + * Revision 1.1 2002/10/19 17:56:50 mdw + * Fix bit operations. Test them (a bit) better. + * + */ + +/*----- Header files ------------------------------------------------------*/ + +#include +#include +#include "bitops.h" +#include "mpx.h" + +/*----- Main code ---------------------------------------------------------*/ + +int main(void) +{ + int rc = 0; +#define CHECK(string) do { \ + const char *ref = #string; \ + char buf[5]; \ + buf[0] = B##string(0u, 0u) & 1u? '1' : '0'; \ + buf[1] = B##string(0u, 1u) & 1u? '1' : '0'; \ + buf[2] = B##string(1u, 0u) & 1u? '1' : '0'; \ + buf[3] = B##string(1u, 1u) & 1u? '1' : '0'; \ + buf[4] = 0; \ + if (strcmp(buf, ref) != 0) { \ + fprintf(stderr, "mismatch ref `%s' != buf `%s'\n", ref, buf); \ + rc = 1; \ + } \ +} while (0); + MPX_DOBIN(CHECK) + return (rc); +} +/*----- That's all, folks -------------------------------------------------*/ diff --git a/mp-arith.c b/mp-arith.c index a070675..759056b 100644 --- a/mp-arith.c +++ b/mp-arith.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: mp-arith.c,v 1.14 2002/10/15 19:18:31 mdw Exp $ + * $Id: mp-arith.c,v 1.15 2002/10/19 17:56:50 mdw Exp $ * * Basic arithmetic on multiprecision integers * @@ -30,6 +30,9 @@ /*----- Revision history --------------------------------------------------* * * $Log: mp-arith.c,v $ + * Revision 1.15 2002/10/19 17:56:50 mdw + * Fix bit operations. Test them (a bit) better. + * * Revision 1.14 2002/10/15 19:18:31 mdw * New operation to negate numbers. * @@ -318,7 +321,7 @@ mp *mp_neg(mp *d, mp *a) \ mp *mp_bit##string(mp *d, mp *a, mp *b) \ { \ - MP_DEST(d, MAX(MP_LEN(a), MP_LEN(b)), a->f | b->f); \ + MP_DEST(d, MAX(MP_LEN(a), MP_LEN(b)), (a->f | b->f) & ~MP_NEG); \ mpx_bit##string(d->v, d->vl, a->v, a->vl, b->v, b->vl); \ d->f = (a->f | b->f) & MP_BURN; \ MP_SHRINK(d); \ diff --git a/mpx.c b/mpx.c index 4097b3e..b88bd49 100644 --- a/mpx.c +++ b/mpx.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: mpx.c,v 1.12 2002/10/06 22:52:50 mdw Exp $ + * $Id: mpx.c,v 1.13 2002/10/19 17:56:50 mdw Exp $ * * Low-level multiprecision arithmetic * @@ -30,6 +30,9 @@ /*----- Revision history --------------------------------------------------* * * $Log: mpx.c,v $ + * Revision 1.13 2002/10/19 17:56:50 mdw + * Fix bit operations. Test them (a bit) better. + * * Revision 1.12 2002/10/06 22:52:50 mdw * Pile of changes for supporting two's complement properly. * @@ -81,6 +84,7 @@ #include "mptypes.h" #include "mpx.h" +#include "bitops.h" /*----- Loading and storing -----------------------------------------------*/ @@ -611,7 +615,7 @@ void mpx_bit##string(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl, \ mpw a, b; \ a = (av < avl) ? *av++ : 0; \ b = (bv < bvl) ? *bv++ : 0; \ - *dv++ = MPX_B##string(a, b); \ + *dv++ = B##string(a, b); \ } \ } diff --git a/mpx.h b/mpx.h index bc4b356..3364137 100644 --- a/mpx.h +++ b/mpx.h @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: mpx.h,v 1.14 2002/10/09 00:36:03 mdw Exp $ + * $Id: mpx.h,v 1.15 2002/10/19 17:56:50 mdw Exp $ * * Low level multiprecision arithmetic * @@ -30,6 +30,9 @@ /*----- Revision history --------------------------------------------------* * * $Log: mpx.h,v $ + * Revision 1.15 2002/10/19 17:56:50 mdw + * Fix bit operations. Test them (a bit) better. + * * Revision 1.14 2002/10/09 00:36:03 mdw * Fix bounds on workspace for Karatsuba operations. * @@ -383,29 +386,6 @@ extern void mpx_lsr(mpw */*dv*/, mpw */*dvl*/, /*----- Bitwise operations ------------------------------------------------*/ -/* --- How to implement them --- * - * - * x: 0011 - * y: 0101 - */ - -#define MPX_B0000(x, y) (0u) -#define MPX_B0001(x, y) ((x) & (y)) -#define MPX_B0010(x, y) ((x) & ~(y)) -#define MPX_B0011(x, y) (x) -#define MPX_B0100(x, y) (~(x) & ~(y)) -#define MPX_B0101(x, y) (y) -#define MPX_B0110(x, y) ((x) ^ (y)) -#define MPX_B0111(x, y) ((x) | (y)) -#define MPX_B1000(x, y) (~((x) | (y))) -#define MPX_B1001(x, y) (~((x) ^ (y))) -#define MPX_B1010(x, y) (~(y)) -#define MPX_B1011(x, y) ((x) | ~(y)) -#define MPX_B1100(x, y) (~(x)) -#define MPX_B1101(x, y) (~(x) | (y)) -#define MPX_B1110(x, y) (~((x) & (y))) -#define MPX_B1111(x, y) (~0u) - /* --- @mpx_bitop@ --- * * * Arguments: @mpw *dv, *dvl@ = destination vector diff --git a/tests/mp b/tests/mp index 6fe61e5..c451acb 100644 --- a/tests/mp +++ b/tests/mp @@ -1,6 +1,6 @@ # Test vectors for MP functions # -# $Id: mp,v 1.12 2002/10/15 19:18:31 mdw Exp $ +# $Id: mp,v 1.13 2002/10/19 17:56:50 mdw Exp $ add { 5 4 9; 5 -4 1; -5 4 -1; -5 -4 -9; @@ -26,6 +26,7 @@ bin2c { or 5 3 7; xor 5 3 6; 1111 0 0 -1; + or 45 -7 -3; xor 0x343cd5 -0x6a49c -0x32984f; }