by baby · 9y ago · 0 comments
One question:
> I was shifting a uint8_t, 24 bits to the left. I failed to realise that integer promotion means this unsigned byte would be converted to a signed integer, and overflow if the byte exceeded 127.
This sounds weird to me, how is a shift (which only apply itself on the bit value, not the number value) triggering a sign extension? I don't think you're talking about integer promotion here unless you had a uint16_t = uint8_t << 24 or something like this.
uint8_t u8 = 255;
uint64_t u64 = u8 << 24;
printf("%016llx\n", (long long) u64);
prints "ffffffffff000000". If you instead do uint64_t u64 = (uint64_t) u8 << 24;
or uint64_t u64 = (uint32_t) u8 << 24;
It prints "00000000ff000000" as most people expect. (I think the second example with (uint32_t) will only work if "int" is at most 32 bits, but I might be wrong). #include <stdio.h>
#include <stdint.h>
int main(){
uint8_t start = -1;
printf("%x\n", start); // prints 0xff
uint64_t result = start << 24;
printf("%llx\n", result); // should print 00000000ff000000, but will print ffffffffff000000
result = (uint64_t)start << 24;
printf("%llx\n", result); // prints 00000000ff000000
return 0;
}If you say "foo + bar" and both are uint8_t, then both will be promoted to integer. That's how C works. Assigning the result to another uint8_t will truncate it.
Please do not write your own crypto library until you really understand how C works, especially including this kind of detail.
Your example is different, this is because you're doing an addition. I can see how this could be a problem if you want to do a rotation (a << 4) + (a >> 60) and hence why you should use a ^ instead of a + here (example: https://github.com/gvanas/KeccakCodePackage/blob/master/SnP/... )
With 32-bit ints, this operation will result in 0x80000000, shifting a bit into the sign bit, which constitutes an overflow (and undefined behavior). Think about it in terms of multiplication: you're multiplying a positive integer by 2^24, and it turns out negative!
u64 = u8 << 24
Here the integer promotion might transform u8 into a 16-bit or 32-bit int (depending on the arch). Which will then be converted to a u64 AFTER the shift. Which might have you lose half or more of the bits.
His code is not that though, it's
u32 = u8 << 24
which should work only if ints are 32-bit on the system you compile on.
> Each of the operands shall have integer type.
> The integer promotions are performed on each of the operands. The type of the result is that of the promoted left operand.
6.3.1.1 § 2
> If an int can represent all values of the original type (as restricted by the width, for a bit-field), the value is converted to an int; otherwise, it is converted to an unsigned int. These are called the integer promotions.
So if you shift a uint8_t, it is first converted to a (machine-dependent) signed int, then shifted, then converted to whatever the expression type ends up being.
I should have quoted the proper part I was replying to ("I don't think you're talking about integer promotion here")
[1] https://github.com/LoupVaillant/Monocypher/commit/347189c50c...
To see the difference, you need to first modify the makefile to use UBSan instead of just GCC (just comment the active CC line, and uncomment the right one). You may want to tone down the optimisations for faster compilation as well. Then you can run `make clean` and `./test.sh`. You should have a warning on commits prior to this patch.
#include <stddef.h>
#include <inttypes.h>
int main(int argc, char **argv)
{
uint8_t msg[] = { 255 };
size_t i = 0;
size_t c_index = 255;
return msg[i] << ((c_index % 4) * 8);
}
You get: runtime error: left shift of 255 by 24 places cannot be represented in type 'int'[1]: https://github.com/LoupVaillant/Monocypher/blob/master/src/m...