GMP Bug Report

Brett Kuntz kuntz at shaw.ca
Sat Feb 22 05:08:55 CET 2025


The bug likely exists because the source uses "int" to store sizes, for example in this macro: 

#ifndef HTON_LIMB_STORE 
#define HTON_LIMB_STORE(dst, limb) \ 
do { \ 
mp_limb_t __limb = (limb); \ 
char *__p = (char *) (dst); \ 
int __i; \ 
for (__i = 0; __i < GMP_LIMB_BYTES; __i++) \ 
__p[__i] = (char) (__limb >> ((GMP_LIMB_BYTES-1 - __i) * 8)); \ 
} while (0) 
#endif 

There is also an "int bits;" on line 107. 

Change all of these int's to long long's and recompile, it should fix the issue for you. 

-Brett 



From: "James Cumberbatch" <james.a.cumberbatch at gmail.com> 
To: gmp-bugs at gmplib.org 
Sent: Friday, February 21, 2025 7:00:00 AM 
Subject: GMP Bug Report 

Hello, 
Here's a bug report, concerning mpz_inp_raw. 

Version number: 6.3.0, also 6.2.1. (Tested it on two computers) 6.3.0 
was installed using homebrew (No special options, just "brew install 
gmp"), 6.2.1 was not installed by me. 

Test program: Attached. 

Description of what is wrong: When using mpz_inp_raw to load a number 
which is at least 2^(2^34), a different value to the one which is 
written is loaded. Arithmetic works fine with these numbers and 
doesn't go wrong until larger numbers where gmp doesn't claim to work, 
only loading the number from a file goes wrong. (Or theoretically the 
error might be in mpz_out_raw, but doing a hexdump it looks like it's 
probably correct). mpz_inp_str has a similar issue, though I have done 
less testing with that one. 
Pseudocode of the attached program: 
Initialize mpz_t X and set it to 2^(2^34), write it to a file with 
mpz_out_raw, initialize mpz_t X2 and set it using mpz_inp_raw on the 
file which was just written, display how many bytes were written and 
how many bytes were read, display the result of using mpz_cmp on X and 
X2. 

Output of the program I get, same on both computers: 
Exponentiated 
80000005 bits written 
80000003 bits read 
The sign of X-X2 is the sign of 1. 

Expected output: 
Exponentiated 
80000005 bits written 
80000005 bits read 
The sign of X-X2 is the sign of 0. 

Additionally, while not demonstrated in this program, the number of 
bits read shrinks when larger numbers are written. Or at least, when 
numbers which are a little bit larger are written, then strange things 
happen as you go larger (Only 5 bits are read from 2^(2^35)). 

Configure things: I didn't build gmp myself so I don't know. On one 
computer I used homebrew, on the other someone else installed gmp. 

Outputs of gcc -v and uname -a: Attached. 

There's also a similar glitch when using mpz_inp_str, though I haven't 
done as much testing with that. 

Best, 

_______________________________________________ 
gmp-bugs mailing list 
gmp-bugs at gmplib.org 
https://gmplib.org/mailman/listinfo/gmp-bugs 


More information about the gmp-bugs mailing list