patch for cipher/sha512.c (u64 numbers)

Nelson H. F. Beebe beebe at math.utah.edu
Thu May 8 18:14:01 CEST 2003


Christoph Moench-Tegeder <cmt at rz.uni-karlsruhe.de> writes on
Tue, 6 May 2003 16:50:52 +0200:

>> HP's cc in ANSI mode truncates these consts to integers (32 bit), so
>> SHA384 and SHA512 break (MDS test fails).

That is definitely a compiler error.  The 1989 ISO C Standard has this
to say on pp. 55--56:

>> ...
>> The type of an integer constant is the first of the corresponding list
>> in which its value can be represented.
>>
>> --------------------------------------------------------------------------------
>>                                                            Octal or Hexadecimal
>> Suffix               Decimal Constant                      Constant
>> --------------------------------------------------------------------------------
>> none                int int
>>                     long int                               unsigned int
>>                     long long int                          long int
>>                                                            unsigned long int
>>                                                            long long int
>>                                                            unsigned long long int
>> ...
>> --------------------------------------------------------------------------------
>> ...

Prior to the 1989 Standard, there were no such guarantees.

I made experiments with compilers on 16 different UNIX platforms, and
found another one where the wrong answer is produced with the LL
suffix is omitted: IBM AIX 4.2 cc.

The attached test program reports:

% cc longlong.c && ./a.out
"longlong.c", line 15.9: 1506-207 (W) Integer constant 9223372036854775807 out of range.
Integer constant erroneously truncated!
Expect   9223372036854775807
m =      9223372036854775807 (had LL suffix)
n =               4294967295 (lacked LL suffix)

However, this compiler has an option to enable long long, but it still
doesn't help:

% cc -qlonglong longlong.c && ./a.out
"longlong.c", line 15.9: 1506-207 (W) Integer constant 9223372036854775807 out of range.
Integer constant erroneously truncated!
Expect   9223372036854775807
m =      9223372036854775807 (had LL suffix)
n =               4294967295 (lacked LL suffix)

The lesson is: for maximal portability and minimal surprises, integer
constants larger than 2^15 - 1 (32767) should carry a L suffix, and
ones larger than 2^31 - 1 (2147483647) should carry a LL suffix.

Here is the test program:

% cat longlong.c
#include <stdio.h>
#include <stdlib.h>

int
main(void)
{
    long long m;
    long long n;
    
    m = 9223372036854775807LL;		/* LL suffix! */
    n = 9223372036854775807;		/* NB: NO LL suffix! This is a test */
    
    if (m != n)
	(void)printf("Integer constant erroneously truncated!\n");


    (void)printf("Expect\t 9223372036854775807\n");
    (void)printf("m =\t%20lld (had LL suffix)\n", m);
    (void)printf("n =\t%20lld (lacked LL suffix)\n", n);

    return (EXIT_SUCCESS);
}

-------------------------------------------------------------------------------
- Nelson H. F. Beebe                    Tel: +1 801 581 5254                  -
- Center for Scientific Computing       FAX: +1 801 581 4148                  -
- University of Utah                    Internet e-mail: beebe at math.utah.edu  -
- Department of Mathematics, 110 LCB        beebe at acm.org  beebe at computer.org -
- 155 S 1400 E RM 233                       beebe at ieee.org                    -
- Salt Lake City, UT 84112-0090, USA    URL: http://www.math.utah.edu/~beebe  -
-------------------------------------------------------------------------------




More information about the Gnupg-devel mailing list