gmp-5.1.0 mini-gmp.c mpz_import have a bug

shuax admin at
Fri Jan 4 14:11:07 CET 2013

My first language is not English,Please bear with me.

I used the mini-GMP some tests. For example:

unsigned char input_p[] = {0xFD, 0x47, 0x52, 0xCF, 0x0D, 0xEF, 0x06, 0x34, 0xDD, 0x61, 0x1C, 0x96, 0x9B, 0xB6, 0x5F, 0x66, 0x21, 0x81, 0x7E, 0xEB, 0x6C, 0xAC, 0xC3, 0x1D, 0x89, 0x1E, 0x86, 0xE8, 0x9D, 0xEC, 0xB4, 0xD9, 0x12, 0x02, 0x4F, 0xB4, 0x48, 0x74, 0xA7, 0xC6, 0x72, 0x77, 0x03, 0x44, 0x31, 0x7A, 0x7A, 0xBA, 0xC2, 0xA8, 0x81, 0x85, 0x41, 0x98, 0x68, 0x4D, 0x99, 0x84, 0x45, 0x59, 0x59, 0xE1, 0xD4, 0xA7, 0x6D, 0x51, 0x36, 0x5D, 0xE1, 0x10, 0x15, 0x56, 0xA8, 0x57, 0x92, 0xE0, 0x30, 0x68, 0x37, 0xFD, 0x8E, 0x5C, 0x11, 0xA9, 0xC5, 0x48, 0x53, 0x35, 0xC9, 0x13, 0x7C, 0x72, 0x35, 0xA1, 0x1A, 0x7E, 0xF7, 0x7D, 0x17, 0xB2, 0x76, 0xD5, 0xCB, 0x6F, 0xE0, 0x36, 0x59, 0x99, 0x3A, 0xA1, 0x94, 0x9B, 0xD3, 0x6D, 0x49, 0x4C, 0xEA, 0x7D, 0x54, 0x22, 0x53, 0x3A, 0x07, 0x58, 0xC1, 0x20, 0xC6, 0xAE, 0x17, 0xC9, 0xFF, 0x4E, 0xD9, 0xFC, 0x65, 0x17, 0x0E, 0xAE, 0xE1, 0x3C, 0x55, 0x00, 0xC9, 0xCF, 0x99, 0x5F, 0x84, 0x89, 0x0C, 0xEE, 0xE9, 0xAD, 0x26, 0x37, 0x77, 0xFE, 0x0E, 0x41, 0x71, 0x2E, 0xD6, 0x2E, 0xBA, 0xE4, 0xEC, 0xA8, 0x86, 0x10, 0x6F, 0x55, 0x45, 0xBA, 0xD9, 0xD2, 0x47, 0x96, 0xED, 0xA2, 0x6A, 0xA6, 0xC4, 0xFB, 0xC6, 0xAD, 0xB1, 0x39, 0xFB, 0xAE, 0xC1, 0x47, 0x20, 0x92, 0x68, 0x67, 0xFF, 0xF4, 0x74, 0x47, 0x60, 0x65, 0xF8, 0x66, 0xB7, 0xBF, 0xC9, 0x0F, 0xA9, 0x0A, 0x89, 0x30, 0x3C, 0xC6, 0x92, 0x90, 0xA9, 0x1C, 0xF3, 0x8D, 0x4E, 0xBA, 0xF5, 0xF0, 0xCA, 0x2B, 0xD3, 0xF1, 0xD9, 0x8F, 0x5E, 0x90, 0xFF, 0x34, 0xC4, 0xA1, 0x45, 0x1A, 0x28, 0x6C, 0x7E, 0x6B, 0xC6, 0xFB, 0x88, 0xFF, 0x49, 0xE5, 0x40, 0x10, 0x48, 0x84, 0x91, 0xFF, 0x17, 0x69, 0x6D, 0x2B};
mpz_t p;
mpz_import(p, sizeof(input_p), 1, sizeof(input_p[0]), 0, 0, input_p);

Then I found the value of p is incorrect.

        char *buf = mpz_get_str(NULL, 16, p);
        printf("%s \n%x", buf,sizeof(input_p[0]));

I think there should be such a change:
Starting with the 4059 line.

  for (limb = 0, bytes = 0, i = 0; count > 0; count--, p += word_step)
      size_t j;
      for (j = 0; j < size; j++, p -= (ptrdiff_t) endian) //remove the red
	  limb |= *p << (bytes++ * CHAR_BIT);
	  if (bytes == sizeof(mp_limb_t))
	      rp[i++] = limb;
	      bytes = 0;
	      limb = 0;  // add this

More information about the gmp-bugs mailing list