From 70b904c575877c683e6c79cac4fbf7c6d89d0bde Mon Sep 17 00:00:00 2001 From: mdw Date: Thu, 25 Nov 1999 11:38:33 +0000 Subject: [PATCH] Support for conversions between MPs and C integers. --- Makefile.m4 | 10 ++- mpint.c | 181 ++++++++++++++++++++++++++++++++++++++++++++++++++++ mpint.h | 207 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ tests/mpint | 39 ++++++++++++ 4 files changed, 435 insertions(+), 2 deletions(-) create mode 100644 mpint.c create mode 100644 mpint.h create mode 100644 tests/mpint diff --git a/Makefile.m4 b/Makefile.m4 index f5461b3..8cf9edc 100644 --- a/Makefile.m4 +++ b/Makefile.m4 @@ -1,6 +1,6 @@ ## -*-makefile-*- ## -## $Id: Makefile.m4,v 1.16 1999/11/22 20:51:33 mdw Exp $ +## $Id: Makefile.m4,v 1.17 1999/11/25 11:38:31 mdw Exp $ ## ## Makefile for Catacomb ## @@ -29,6 +29,9 @@ ##----- Revision history ---------------------------------------------------- ## ## $Log: Makefile.m4,v $ +## Revision 1.17 1999/11/25 11:38:31 mdw +## Support for conversions between MPs and C integers. +## ## Revision 1.16 1999/11/22 20:51:33 mdw ## Add yet more source files. ## @@ -151,7 +154,8 @@ pkginclude_HEADERS = \ rc4.h \ rand.h noise.h \ key.h \ - mpx.h mpw.h mpscan.h mparena.h mp.h mptext.h mpmont.h mpcrt.h \ + mpx.h mpw.h mpscan.h mparena.h mp.h mptext.h mpint.h \ + mpmont.h mpcrt.h \ ptab.h pgen.h rabin.h \ dsa.h dh.h \ allwithsuffix(`ciphers', `cipher_modes', `.h') \ @@ -166,6 +170,7 @@ libcatacomb_la_SOURCES = \ mpx.c mpscan.c mparena.c \ mp-misc.c mp-mem.c mp-const.c mp-io.c mp-arith.c mp-test.c \ mp-gcd.c mp-jacobi.c \ + mpint.c \ mptext.c mptext-file.c mptext-string.c mptext-dstr.c \ mpmont.c mpmont-mexp.c \ mpcrt.c \ @@ -233,6 +238,7 @@ CTESTRIG(mptext) CTESTRIG(mp-arith) CTESTRIG(mp-gcd) CTESTRIG(mp-jacobi) +CTESTRIG(mpint) CTESTRIG(mpmont) CTESTRIG(mpmont-mexp) CTESTRIG(mpcrt) diff --git a/mpint.c b/mpint.c new file mode 100644 index 0000000..de3ed8b --- /dev/null +++ b/mpint.c @@ -0,0 +1,181 @@ +/* -*-c-*- + * + * $Id: mpint.c,v 1.1 1999/11/25 11:38:31 mdw Exp $ + * + * Conversion between MPs and standard C integers + * + * (c) 1999 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: mpint.c,v $ + * Revision 1.1 1999/11/25 11:38:31 mdw + * Support for conversions between MPs and C integers. + * + */ + +/*----- Header files ------------------------------------------------------*/ + +#include "mpint.h" + +/*----- Main code ---------------------------------------------------------*/ + +/* --- Conversion from C integers --- */ + +#define FROM(name, type) \ + mp *mp_from##name(mp *d, type i) { \ + MP_FROMINT(d, type, i); \ + return (d); \ + } + +FROM(short, short) +FROM(ushort, unsigned short) +FROM(int, int) +FROM(uint, unsigned) +FROM(long, long) +FROM(ulong, unsigned long) + +#undef FROM + +/* --- Conversion to C integers --- */ + +#define TO(name, type, max) \ + type mp_to##name(const mp *m) \ + { \ + type i; \ + MP_TOINT(m, type, max, i); \ + return (i); \ + } + +TO(short, short, SHRT_MAX) +TO(ushort, unsigned short, USHRT_MAX) +TO(int, int, INT_MAX) +TO(uint, unsigned, UINT_MAX) +TO(long, long, LONG_MAX) +TO(ulong, unsigned long, ULONG_MAX) + +#undef TO + +/*----- Test rig ----------------------------------------------------------*/ + +#ifdef TEST_RIG + +#include + +static int fromuint(dstr *v) +{ + unsigned i = *(unsigned *)v[0].buf; + mp *m = *(mp **)v[1].buf; + mp *d = mp_fromuint(MP_NEW, i); + int ok = 1; + + if (MP_CMP(d, !=, m)) { + fputs("\n*** fromint failed.\n", stderr); + fprintf(stderr, "i = %u", i); + fputs("\nexpect = ", stderr); mp_writefile(m, stderr, 10); + fputs("\nresult = ", stderr); mp_writefile(d, stderr, 10); + fputc('\n', stderr); + ok = 0; + } + + mp_drop(m); + mp_drop(d); + return (ok); +} + +static int fromint(dstr *v) +{ + int i = *(int *)v[0].buf; + mp *m = *(mp **)v[1].buf; + mp *d = mp_fromint(MP_NEW, i); + int ok = 1; + + if (MP_CMP(d, !=, m)) { + fputs("\n*** fromint failed.\n", stderr); + fprintf(stderr, "i = %i", i); + fputs("\nexpect = ", stderr); mp_writefile(m, stderr, 10); + fputs("\nresult = ", stderr); mp_writefile(d, stderr, 10); + fputc('\n', stderr); + ok = 0; + } + + mp_drop(m); + mp_drop(d); + return (ok); +} + +static int touint(dstr *v) +{ + mp *m = *(mp **)v[0].buf; + unsigned i = *(unsigned *)v[1].buf; + unsigned j = mp_touint(m); + int ok = 1; + + if (i != j) { + fputs("\n*** touint failed.\n", stderr); + fputs("m = ", stderr); mp_writefile(m, stderr, 10); + fprintf(stderr, "\nexpect = %u; result = %u\n", i, j); + ok = 0; + } + + mp_drop(m); + return (ok); +} + +static int toint(dstr *v) +{ + mp *m = *(mp **)v[0].buf; + int i = *(int *)v[1].buf; + int j = mp_toint(m); + int ok = 1; + + if (i != j) { + fputs("\n*** toint failed.\n", stderr); + fputs("m = ", stderr); mp_writefile(m, stderr, 10); + fprintf(stderr, "\nexpect = %i; result = %i\n", i, j); + ok = 0; + } + + mp_drop(m); + return (ok); +} + +static test_chunk tests[] = { + { "fromuint", fromuint, { &type_int, &type_mp, 0 } }, + { "fromint", fromint, { &type_int, &type_mp, 0 } }, + { "touint", touint, { &type_mp, &type_int, 0 } }, + { "toint", toint, { &type_mp, &type_int, 0 } }, + { 0, 0, { 0 } } +}; + +int main(int argc, char *argv[]) +{ + sub_init(); + test_run(argc, argv, tests, SRCDIR "/tests/mpint"); + return (0); +} + +#endif + +/*----- That's all, folks -------------------------------------------------*/ diff --git a/mpint.h b/mpint.h new file mode 100644 index 0000000..733292b --- /dev/null +++ b/mpint.h @@ -0,0 +1,207 @@ +/* -*-c-*- + * + * $Id: mpint.h,v 1.1 1999/11/25 11:38:31 mdw Exp $ + * + * Conversion between MPs and standard C integers + * + * (c) 1999 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: mpint.h,v $ + * Revision 1.1 1999/11/25 11:38:31 mdw + * Support for conversions between MPs and C integers. + * + */ + +#ifndef MPINT_H +#define MPINT_H + +#ifdef __cplusplus + extern "C" { +#endif + +/*----- Header files ------------------------------------------------------*/ + +#include + +#ifndef MP_H +# include "mp.h" +#endif + +/*----- Generic translation macros ----------------------------------------*/ + +/* --- @MP_FROMINT@ --- * + * + * Arguments: @d@ = destination multiprecision integer + * @type@ = type of integer which @i@ is + * @i@ = a standard C integer + * + * Use: Stores the value of @i@ in @d@. This macro is actually + * rather subtle in places. Be careful what you change. + */ + +#define MP_FROMINT(d, type, i) do { \ + type _i = (i); \ + size_t _o = 0; \ + mp *_d = (d); \ + size_t _sz = 4; \ + \ + MP_MODIFY(_d, _sz); \ + _d->f &= ~(MP_NEG | MP_UNDEF); \ + \ + /* --- Set the sign on the MP --- * \ + * \ + * If the input integer is *not* negative, then negate it. This \ + * fixes a problem with two's complement machines where the most \ + * negative value actually has larger magnitude than the most \ + * positive, and hence -TYPE_MIN == TYPE_MIN but TYPE_MIN != 0. If \ + * all the work is carried out on negative numbers there isn't a \ + * problem. \ + */ \ + \ + if (_i >= 0) \ + _i = -_i; \ + else \ + _d->f |= MP_NEG; \ + \ + while (_i) { \ + if (_o == _sz) { \ + _sz <<= 1; \ + MP_ENSURE(_d, _sz); \ + } \ + _d->v[_o++] = MPW(-_i); \ + \ + /* --- More subtlety --- * \ + * \ + * Ideally, I'd like to just shift @i@ right by @MPW_BITS@. But I \ + * can't, because that might be more than I'm allowed. I can't \ + * divide by @MPW_MAX + 1@ because that might turn out to be zero \ + * in my current type, and besides which it's unsigned which messes \ + * up all of my negative arithmetic. So do an explicit test here. \ + */ \ + \ + if (_i > -MPW_MAX) \ + break; \ + else \ + _i /= (type)MPW_MAX + 1; \ + } \ + _d->vl = _d->v + _o; \ + (d) = _d; \ +} while (0) + +/* --- @MP_TOINT@ --- * + * + * Arguments: @m@ = a multiprecision integer + * @type@ = the type of @i@ + * @max@ = the largest value @i@ can represent + * @i@ = an integer variable + * + * Use: Stores the value of a multiprecision integer in a standard C + * integer. If the value won't fit, the behaviour is determined + * by the type of @i@: if @i@ is unsigned, the value of the + * multiprecision integer modulo @max + 1@ is stored; if @i@ is + * signed, the behaviour is undefined. + * + * If you don't want to be bitten by these sorts of things, keep + * copies of @INT_MAX@ or whatever is appropriate in + * multiprecision form and compare before conversion. + */ + +#define MP_TOINT(m, type, max, i) do { \ + type _i = 0; \ + type _max = (max); \ + unsigned _s = 0; \ + const mp *_m = (m); \ + const mpw *_v = _m->v, *_vl = _m->vl; \ + \ + /* --- Do all the arithmetic in negative numbers --- */ \ + \ + while (_v < _vl && _max > 0) { \ + _i -= *_v << _s; \ + _s += MPW_BITS; \ + _v++; \ + _max /= (mpd)MPW_MAX + 1; \ + } \ + if (!(_m->f & MP_NEG)) \ + _i = -_i; \ + (i) = _i; \ +} while (0) + +/*----- Functions provided ------------------------------------------------*/ + +/* --- @mp_fromINT@ --- * + * + * Arguments: @mp *d@ = pointer to destination multiprecision integer + * @INT i@ = standard C integer to convert + * + * Returns: The resulting multiprecision integer. + * + * Use: Converts a standard C integer to a multiprecision integer. + */ + +#define mp_fromINT(name, type) \ + extern mp *mp_from##name(mp */*d*/, type /*i*/) + +mp_fromINT(short, short); +mp_fromINT(ushort, unsigned short); +mp_fromINT(int, int); +mp_fromINT(uint, unsigned); +mp_fromINT(long, long); +mp_fromINT(ulong, unsigned long); + +#undef mp_fromINT + +/* --- @mp_toINT@ --- * + * + * Arguments: @const mp *m@ = pointer to a multiprecision integer + * + * Returns: The value of the integer @m@ as a C integer. + * + * Use: Converts a multiprecision integer to a standard C integer. + * If the value of the multiprecision integer cannot be + * represented in the return type, and the return type is + * unsigned, it is reduced modulo @TYPE_MAX + 1@; if the return + * type is signed, the behaviour is undefined. + */ + +#define mp_toINT(name, type) \ + extern type mp_to##name(const mp */*m*/); + +mp_toINT(short, short); +mp_toINT(ushort, unsigned short); +mp_toINT(int, int); +mp_toINT(uint, unsigned); +mp_toINT(long, long); +mp_toINT(ulong, unsigned long); + +#undef mp_toINT + +/*----- That's all, folks -------------------------------------------------*/ + +#ifdef __cplusplus + } +#endif + +#endif diff --git a/tests/mpint b/tests/mpint new file mode 100644 index 0000000..d0c9667 --- /dev/null +++ b/tests/mpint @@ -0,0 +1,39 @@ +# Test vectors for conversion between MP ints and C ints +# +# $Id: mpint,v 1.1 1999/11/25 11:38:33 mdw Exp $ + +# The tests look messy because I'm fighting with atoi here as well as the +# integer conversion routines I'm trying to test. + +fromuint { + 0 0; + 1 1; + -5 0xfffffffb; + 0x7fffffff 0x7fffffff; + -0x80000000 0x80000000; # Bastard torture test +} + +fromint { + 0 0; + 1 1; + -5 -5; + 0x7fffffff 0x7fffffff; + -0x80000000 -0x80000000; # Bastard torture test +} + +touint { + 0 0; + 1 1; + -5 -5; + 0x7fffffff 0x7fffffff; + 0x80000000 -0x80000000; # Bastard torture test +} + +toint { + 0 0; + 1 1; + -5 -5; + 0x7fffffff 0x7fffffff; + -0x80000000 -0x80000000; # Bastard torture test +} + -- 2.11.0