[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Bit library in Lua 5.2
- From: duck <duck@...>
- Date: Sun, 10 Jan 2010 12:02:49 +1100 (EST)
I _almost_ wrote off Mike Pall's arguments on the basis of tone and style
alone. (Mike, they were so gruesomely flamesome that they almost sank your
ship -- perhaps you can try vehemence AND respect rather than vehemence
XOR respect next time :). Also, I have never really liked the fact that
his library returns twos-complement-style 32-bit signed numbers in
[-2^31,2^31).
But if the implementors of Lua are serious about supporting Lua for
environments in which Lua numbers aren't doubles (such as 32-bit ints)
then Mike Pall is quite right about return values.
There is simply no choice but to have the bitops return 32-bit signed
numbers, or else the library can never work on 32-bit-number platforms.
And I agree with the idea that, as a core Lua library, the bit operations
need formally to convert (not to cast) their operands to bitmapped
integers before carrying out any operation, in some standards-compliant
way (read: "in a fashion which has only one possible correct result
regardless of platform").
Also, since I expect one common usage of the bit operations to be
crypto-related, I'd like to see standard library functions to return a
32-bit style number as a 4-character string, in either endianness., e.g.
bit.tou32le(number)
bit.tou32be(number)
So, bit.tou32le(1) == '\x01\x00\x00\x00', and bit.tou32be(1) ==
'\x00\x00\x00\x01'.
To go with these functions, I also think a function (which probably
belongs in string, not bit) to XOR two strings together would be very
handy. It's awfully slow to do char-by-char in Lua, but trivial and quick
in C.
The reason for this esoteric-sounding function is that XORing strings
together is one of the most fundamental "final steps" in a stream cipher,
a class which includes not just dedicated stream ciphers such as ARC4, but
also stream-base block cipher modes such as Cipher Feedback (CFB) and
Counter (CTR) mode.
Lastly, since I hate the idea of operations which are permitted "but
undefined" (this invariably ends up in the emergence of platform-specific
tricks), so it might be worth having a mode for the bit library --
non-default, perhaps, but run-time selectable -- which would
trigger an exception if any functions were called with numbers
which aren't directly representable as integers, e.g. if
x ~= math.floor(x).
Ideally, I'd like to see Lua have offical support for a range of default
number types -- 32-bit, double, 128-bit, etc., definitely including one
which gives sufficient integer range to be closed under 64-bit addition.
We live in an increasingly 64-bit world. (In particular, if I add 1 to
2^64-1 I need to know there has been an overflow, and in general I would
like to be closed under 64-bit addition, so I can add any two 64-bit ints
together and trust the result.)