Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2008-12-16
00:06 • Ticket [2413689fff] Bug in function s_mp_sqr() status still Closed with 4 ot... artifact: ce46ba45b7 user: gcramer
2008-12-11
04:21 • Closed ticket [2413689fff]. artifact: 81671c566b user: kennykb
04:21 • Ticket [2413689fff]: 4 changes artifact: dcf6b74d0e user: kennykb
2008-12-10
11:58 • New ticket [2413689fff]. artifact: cd89880e03 user: gcramer

Bug in function s_mp_sqr()

Closed / Invalid2413689fffffffffffffffffffffffffffffffffCreated 18 years agoUpdated 18 years ago
Descriptionplain2008-12-10 11:58:14
Function s_mp_sqr() has a bug. I have no example, but I developed a multiple precision library by myself, and I had the same bug in my code. In line bn_s_mp_sqr.c:59 "r       = ((mp_word) *tmpt) + r + r + ((mp_word) u);" an integer overflow may occur! This leads to wrong results in general. Inside the function "HAC pp.596-597, Algorithm 14.16" is referenced, but "Note 14.17 (i)" is not regarded appropriately.
kennykbplain2008-12-11 04:21:53
In this case, overflow appears impossible. It is important to remember that not all the bits of an mp_digit are actually used; the maximum value for an mp_digit is (1<<28)-1.

So here I see that r (a 64-bit integer) is set to the product of two mp_digits (which is less than 2**56). It's subsequently doubled, and then two more mp_digits are added to it, so it winds up still less than 2**58 + 2**29.  This is comfortably less than 2**63-1, the maximum size of an mp_word.

Even if MP_31BIT is defined - which Tcl does not do - the result of this addition is at most 2**62 + 2**32, still less than 2**63-1.  (Tcl does not define MP_31BIT because observed performance is better with the Comba multipliers, which are incompatible with that option.)
Overflow would be possible if tommath used all 32 bits of an mp_digit.  But it doesn't.
kennykb Closed ticket [2413689fff]. [History diff]2008-12-11 04:21:54
gcramerplain2008-12-16 00:06:28
You are right. I overlooked the maximum value (1<<28)-1, in my library I'm using all 32 bits. Many tanks!
Log in (anonymous is fine) to add a comment.