mini-gmp: mpz_init_set_str fails on leading zeroes
Torbjörn Granlund
tg at gmplib.org
Thu Jul 28 12:33:25 UTC 2016
nisse at lysator.liu.se (Niels Möller) writes:
Austyn Krutsinger <akrutsinger at gmail.com> writes:
> We can fix this in the mpn_set_str_other function by changing the
> comparison in line 1321. If we accept that w can be > or = to 0, then there
> is no issue if the number has leading zeros. So line 1321 in
> mpn_set_str_other becomes:
>
> 1321 for (rn = (w >= 0); j < sn;)
Checking what the fine manual says on mpn_set_str:
The converted value is {RP,RN} where RN is the return value. If
the most significant input byte STR[0] is non-zero, then RP[RN-1]
will be non-zero, else RP[RN-1] and some number of subsequent limbs
may be zero.
So I think it's fine to always initialize rn to 1. And then mpz_set_str
will have to either strip leading zeros in the input to mpn_set_str, or
normalize the result afterwards.
Since this is mini-gmp, another option which is fine if it turns
out to be simpler, is to add an if (!n) return 0; to the start of
mpn_mul_1, and an appropriate comment somewhere. Then mpn_set_str will
produce always normalized output, with no changes.
Let's not diverge foo in GMP and foo in mini-gmp. Or at least let's not
rely on divergence. Because if we do, that will some day that will come
back and bite us.
So please make mini-gmp use any re-implemented gmp interface function
only so that instead using the gmp function (intentionally or
accidentally) would work.
--
Torbjörn
Please encrypt, key id 0xC8601622
More information about the gmp-bugs
mailing list