bignum-test.c 2.02 KB
Newer Older
1
#include "testutils.h"
Niels Möller's avatar
Niels Möller committed
2 3 4 5 6 7 8 9

#if HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdlib.h>
#include <string.h>

Niels Möller's avatar
Niels Möller committed
10 11 12
#if HAVE_LIBGMP
#include "bignum.h"

Niels Möller's avatar
Niels Möller committed
13
static void
14
test_bignum(const char *hex, const struct tstring *base256)
Niels Möller's avatar
Niels Möller committed
15 16 17 18 19 20
{
  mpz_t a;
  mpz_t b;
  uint8_t *buf;
  
  mpz_init_set_str(a, hex, 16);
21
  nettle_mpz_init_set_str_256_s(b, base256->length, base256->data);
Niels Möller's avatar
Niels Möller committed
22

23
  ASSERT(mpz_cmp(a, b) == 0);
Niels Möller's avatar
Niels Möller committed
24

25 26
  buf = xalloc(base256->length + 1);
  memset(buf, 17, base256->length + 1);
Niels Möller's avatar
Niels Möller committed
27

28 29
  nettle_mpz_get_str_256(base256->length, buf, a);
  ASSERT(MEMEQ(base256->length, buf, base256->data));
Niels Möller's avatar
Niels Möller committed
30

31
  ASSERT(buf[base256->length] == 17);
Niels Möller's avatar
Niels Möller committed
32 33

  mpz_clear(a); mpz_clear(b);
34
  free(buf);
Niels Möller's avatar
Niels Möller committed
35
}
36 37 38 39 40 41 42 43 44 45

static void
test_size(long x, unsigned size)
{
  mpz_t t;

  mpz_init_set_si(t, x);
  ASSERT(nettle_mpz_sizeinbase_256_s(t) == size);
  mpz_clear(t);
}
Niels Möller's avatar
Niels Möller committed
46 47
#endif /* HAVE_LIBGMP */

48

49
void
Niels Möller's avatar
Niels Möller committed
50
test_main(void)
51
{
Niels Möller's avatar
Niels Möller committed
52
#if HAVE_LIBGMP
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
  test_size(0, 1);
  test_size(1, 1);
  test_size(0x7f, 1);
  test_size(0x80, 2);
  test_size(0x81, 2);
  test_size(0xff, 2);
  test_size(0x100, 2);
  test_size(0x101, 2);
  test_size(0x1111, 2);
  test_size(0x7fff, 2);
  test_size(0x8000, 3);
  test_size(0x8001, 3);

  test_size(-      1, 1); /*     ff */
  test_size(-   0x7f, 1); /*     81 */
  test_size(-   0x80, 1); /*     80 */
  test_size(-   0x81, 2); /*   ff7f */
  test_size(-   0xff, 2); /*   ff01 */
  test_size(-  0x100, 2); /*   ff00 */
  test_size(-  0x101, 2); /*   feff */
  test_size(- 0x1111, 2); /*   eeef */
  test_size(- 0x7fff, 2); /*   8001 */
  test_size(- 0x8000, 2); /*   8000 */
  test_size(- 0x8001, 3); /* ff7fff */

78 79 80 81 82 83 84 85 86 87 88
  test_bignum("0", SHEX("00"));
  test_bignum("010203040506", SHEX("010203040506"));
  test_bignum("80010203040506", SHEX("0080010203040506"));

  test_bignum(   "-1", SHEX(    "ff"));
  test_bignum(  "-7f", SHEX(    "81"));
  test_bignum(  "-80", SHEX(    "80"));
  test_bignum(  "-81", SHEX(  "ff7f"));
  test_bignum("-7fff", SHEX(  "8001"));
  test_bignum("-8000", SHEX(  "8000"));
  test_bignum("-8001", SHEX("ff7fff"));
89
  
Niels Möller's avatar
Niels Möller committed
90
#else /* !HAVE_LIBGMP */
91
  SKIP();
Niels Möller's avatar
Niels Möller committed
92
#endif /* !HAVE_LIBGMP */
93
}