[pycrypto] Possible problem in src/_fastmath.c

Sergey Chernov sergey.chernov at thrift.ru
Thu Oct 16 20:03:02 CST 2008


Sorry, of course, that sould be 

mpz_set_ui( m, 0 );

- ot it will add the value of p to m instead of assigning. I'm not using
_fastmath myself, but found this function a little strange when writing
different RSA implementation with GMP. 

-----Original Message-----
From: pycrypto-bounces at lists.dlitz.net
[mailto:pycrypto-bounces at lists.dlitz.net] On Behalf Of Dwayne C.
Litzenberger
Sent: Thursday, October 16, 2008 4:14 PM
To: PyCrypto discussion list
Subject: Re: [pycrypto] Possible problem in src/_fastmath.c

On Thu, Oct 16, 2008 at 08:08:32AM -0400, Dwayne C. Litzenberger wrote:
> On Thu, Oct 16, 2008 at 03:39:29AM +0400, Sergey Chernov wrote:
>> See patch:
>> ---------------------------------------------------------------------
>> ------- diff --git a/src/_fastmath.c b/src/_fastmath.c index 
>> d06d697..a0aa181 100755
>> --- a/src/_fastmath.c
>> +++ b/src/_fastmath.c
>> @@ -28,6 +28,7 @@ longObjToMPZ (mpz_t m, PyLongObject * p)
>> 		size = p->ob_size;
>> 	else
>> 		size = -p->ob_size;
>> +	mpz_clear( m );
>> 	for (i = 0; i < size; i++)
>> 	{
>> 		mpz_set_ui (temp, p->ob_digit[i]);
>> ---------------------------------------------------------------------
>> -------
>>
>> Current usage do not discover the bug as 'm' is always just-initialized.
>> Still it, as I think, may cause trouble later.
>
> Sergey,
>
> What is the purpose of your patch?  What bug are you referring to?
>
> Are you trying to set m to zero at the top of the loop?  Looking at 
> the GMP documentation 
> (http://gmplib.org/manual/Initializing-Integers.html), I see that 
> mpz_init() and mpz_clear() have similar usages as malloc() and free(), so
I'm pretty sure that I do not want to apply your patch as-is.

Perhaps you meant this?

----------------------------------------------------------------------------
---
diff --git a/src/_fastmath.c b/src/_fastmath.c index d06d697..caf8e7b 100755
--- a/src/_fastmath.c
+++ b/src/_fastmath.c
@@ -28,6 +28,7 @@ longObjToMPZ (mpz_t m, PyLongObject * p)
                 size = p->ob_size;
         else
                 size = -p->ob_size;
+       mpz_set_ui (m, 0);
         for (i = 0; i < size; i++)
         {
                 mpz_set_ui (temp, p->ob_digit[i]);
----------------------------------------------------------------------------
---


--
Dwayne C. Litzenberger <dlitz at dlitz.net>
  Key-signing key   - 19E1 1FE8 B3CF F273 ED17  4A24 928C EC13 39C2 5CF7
  Annual key (2008) - 4B2A FD82 FC7D 9E38 38D9  179F 1C11 B877 E780 4B45



More information about the pycrypto mailing list