Skip to content
Snippets Groups Projects
Select Git revision
  • eb48fb68925e7b3b14ca68118d87585db63c43c8
  • master default protected
  • 9.0
  • 8.0
  • 7.8
  • 7.6
  • 7.4
  • 7.2
  • 7.0
  • 0.6
  • rosuav/latex-markdown-renderer
  • rxnpatch/rxnpatch
  • marcus/gobject-introspection
  • rxnpatch/8.0
  • rosuav/pre-listening-ports
  • nt-tools
  • rosuav/async-annotations
  • rosuav/pgsql-ssl
  • rxnpatch/rxnpatch-broken/2023-10-06T094250
  • grubba/fdlib
  • grubba/wip/sakura/8.0
  • v8.0.2000
  • v8.0.1998
  • v8.0.1996
  • v8.0.1994
  • v8.0.1992
  • v8.0.1990
  • v8.0.1988
  • v8.0.1986
  • rxnpatch/clusters/8.0/2025-04-29T124414
  • rxnpatch/2025-04-29T124414
  • v8.0.1984
  • v8.0.1982
  • v8.0.1980
  • v8.0.1978
  • v8.0.1976
  • v8.0.1974
  • v8.0.1972
  • v8.0.1970
  • v8.0.1968
  • v8.0.1966
41 results

array.c

Blame
  • bignum-test.c 2.03 KiB
    #include "testutils.h"
    
    #if HAVE_CONFIG_H
    #include "config.h"
    #endif
    
    #include <stdlib.h>
    #include <string.h>
    
    #if WITH_HOGWEED
    #include "bignum.h"
    
    static void
    test_bignum(const char *hex, const struct tstring *base256)
    {
      mpz_t a;
      mpz_t b;
      uint8_t *buf;
      
      mpz_init_set_str(a, hex, 16);
      nettle_mpz_init_set_str_256_s(b, base256->length, base256->data);
    
      ASSERT(mpz_cmp(a, b) == 0);
    
      buf = xalloc(base256->length + 1);
      memset(buf, 17, base256->length + 1);
    
      nettle_mpz_get_str_256(base256->length, buf, a);
      ASSERT(MEMEQ(base256->length, buf, base256->data));
    
      ASSERT(buf[base256->length] == 17);
    
      mpz_clear(a); mpz_clear(b);
      free(buf);
    }
    
    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);
    }
    #endif /* WITH_HOGWEED */
    
    
    void
    test_main(void)
    {
    #if WITH_HOGWEED
      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 */
    
      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"));
      
    #else /* !WITH_HOGWEED */
      SKIP();
    #endif /* !WITH_HOGWEED */
    }