• Subject: Re: [ANN] Lua 5.3.0 (work2) now available
• From: Dirk Laurie <dirk.laurie@...>
• Date: Wed, 26 Mar 2014 07:21:11 +0200

```2014-03-26 0:57 GMT+02:00 Enrico Colombini <erix@erix.it>:

> Of course there may be particolr cases where one may want to operate on
> signed integer values at bit level, but they are much less common than
> generic bit manipulation or bit level operations on unsigned values.

I spent all of yesterday translating 32-bit ISAAC to Lua (code
attached for those
who are interested). The original code operates on unsigned 32-bit integers
using add, xor, shr and shl operators. Making it work with signed
32-bit integers
involved nothing heavier than clearing the leading 32 bits after each addition.

In the process, I changed all the original left-shifts to negative right-shifts
to simplify the coding. I could just as well have changed all the
original right-shifts
to negative left-shifts.

Maybe we could have our cake and eat it. One of x>>4 or x<<-4 to mean logical,
the other to mean arithmetic, shift.
```
```-- isaac.lua
-- A factory for the ISAAC cryptographically secure random number generator
-- Original version (c) Bob Jenkins, March 1996, Public Domain
--    <http://burtleburtle.net/bob/rand/isaacafa.html>

-- Usage:
--    isaac = require"isaac"
--    rand = isaac(...)  -- up to 256 seeds can be utilized
--    rand() then returns a pseudorandom 32-bit number
-- More than one instance of ISAAC may be in use at the same time.

local mixer = {-11,2,-8,16,-10,4,-8,9}  -- shifts for randinit
local mover = {[0]=-13,6,-2,16}         -- shifts for isaac
local wordmask = 0xffffffff             -- to force 32-bit results
local floor = math.floor

function filltable(first,last,...)
-- return: a new table with entries first to last filled cyclically from ...
local t={}
local len=select('#',...)
if len==0 then
local seed = os.time()
for k=first,last do t[k]=seed end
return t
end
for k=first,last do
end
return t
end

return function (...)
for k=1,select('#',...) do if type(select(k,...))~='number' then
error('invalid argument '..k..' to isaac: expected number, got '..
type(select(k,...)))
end end
local randrsl = filltable(0,255,...)
local aa, bb, cc = 0,0,0
local mm = {}

local isaac = function()  -- generate 256 new random numbers
for i=0,255 do
local x=mm[i]
aa = aa~aa>>mover[i%4]
aa = wordmask & (mm[(i+128)%256] + aa)
local y = wordmask & (mm[(x>>2)%256] + aa + bb)
mm[i] = y
bb = wordmask & (mm[(y>>10)%256] + x)
randrsl[i] = bb
end
end

-- randinit
local t=filltable(1,8,0x9e3779b9)

local mix = function()
for a=1,8 do
local b,c,d = (a%8)+1, (a+1)%8+1, (a+2)%8+1;
end
end

for i=1,4 do mix() end
for i=0,255,8 do
for j=1,8 do t[j]=wordmask & (t[j]+randrsl[i+j-1]) end
mix()
for j=1,8 do mm[i+j-1]=t[j] end
end
for i=0,255,8 do
for j=1,8 do t[j]=wordmask & (t[j]+mm[i+j-1]) end
mix()
for j=1,8 do mm[i+j-1]=t[j] end
end
local count=255
isaac()
return function()
if count>=255 then isaac(); count=-1 end
count=count+1
return randrsl[count]
end
end

--[[ test code
isaac = require"isaac"
rand = isaac(0)
for j=0,511 do
io.write(("%.8x"):format(rand()))
if (j&7)==7 then io.write"\n" end
if (j&255)==255 then io.write"\n" end
end

Result should be:
5de3743e88381097f1d444eb823cedb66a83e1e04a5f6355c744243325890e2e
7452e31957161df638a824f3002ed71329f5544951c08d83d78cb99ea0cc74f3
f3b2612917e38f8529cf72ce349947b0c998f9ffb5e13dae32ae2a2bf7cf814c
8ebfa303cf22e0640b923200eca4d58aef53cec4d0f7b37d9c411a2affdf8a80
b40e27bcb4d2f97644b89b08f37c71d51a70e7e90bdb9c3060dc5207b3c3f24b
d7386806229749b54e232cd091dabc65a70e11018b87437e5781414fcdbc62e2
8107c9ff69d2e4ae3b18e752b143b6886f4e077295138769943c3c74afc17a97
0fd439636a529b0bd8c58a6aa8bcc22d2db35dfea7a2f4026cb167db538e1f4e
7275e2771d3b8e97ecc5dc9115e3a5b90369661430ab93ecac9fe69d7bc76811
64af0fef0d0286843aea3decb058bafbb8b0ccfcf2b5cc05e3a662d9814bc24c
2364a1aa37c0ed052b36505c451e7ec85d2a542fe43d0fbb91c8d92560d4d5f8
12a0594b9e8a51dacd49ebdb1b0dcdc1cd57c7f7e63444517ded386f2f36fa86
a6d1210133bc405db388d96cdb6dbe96fe29661c13edc0cbcb0eee4a70cc94ae
de11ed340606cf9f3a6ce38923d74f4ea37f63ff917bdec2d73f72d40e7e0e67
34105a8c6a123f57bd2e50247364944be89b1a3b21835c4d9f39e2d9d405ded8
294d37e5bccaaeed35a124b56708a2bcb00960ba2a98121a4d8fae820bb3263f
12595a196a1075890809e49421c171ec884d682514c8009bb0b84e7b03fb88f4
28e7cb789388b13bdd2dc1d5848f520a07c28cd168a3935872c9137d127dd430
c613f1578c2f0d55f7d3f39f309bfb788406b13746c0a6f53718d59708607f04
76904b6d04db4e13cd7411a7b510ce0ebfc7f7ccb83f957afdfef62dc35e4580
3ff1e5244112d96c02c9b944d5990dfbe7e265810d9c7e7e826dfa8966f1e0ab
1ed3ff4eec46b0b6d2a93b51e75b41c97e315aeb61119a5a53245b7933f6d7b1
cae8deba50fc8194afa92a6dc87c80064188bfcd8bace62e78ffa5685597ec0f
79ae4c10cb9e15637cdd662f62d31911a4ca0cf15cf824cd3b708f991e16614c
b6b9d7665de87abb7229ea81d5b2d75056e6cd21fe1e42d596da2655c2b9aa36
b8f6fd4a6a158d1001913fd3af7d1fb80b5e435f90c107576554abda7a68710f

82ac484fd7e1c7be95c85eaa94a302f44d3cfbda786b29081010b27582d53d12
21e2a51c3d1e9150b059261dd0638e1a31860f0581f2864dff4cfc350451516d
bd086f26bc5654c165dfa427a82427f5582e3014b8d2486dc79a17499a1d7745
d8c423fbd0ef3cc9eb244925ba5590c8a5f48ac433c5321c613b67b2479c3a22
e21339cc10d210aa931dd7e2ef05ee06b82f2703a385cb2c5d67133c877eb7b4
1e3437f75afb43ae53c078f394d904811d96458908063a85e13222281956b1e5
31860f132e7b022f21182ca396f703ac46819e2e0d28fe523724d4dca0eabe6b
c66699fdc6112fdd19c1e69c04d3658a4b55dd9931907d62f854b5224d678f26
22ae0582eafed133e4a51d2184bd6dd6c1a513753f28ee63fb737b1a70a1660e
8a8dfaa31be79937f7476978513c1764531ac6bf12c06908001cdb951a4b6a53
d067fce512b2cfb69ddb477f740e006639ddf25acc8bfa2df1b20eaf64f2632c
9783cdee63bfd4d80084cfe575f4e9e219b48fd06c48ddd87a36af9371865c4c
9ce0199d867027d72cb7b77f84ef01da72f5972f040f7074df9afa29c921f94e
bbb8abc480fac4c2ecfcf20bd9d711f9e2a4ef71b5fe87c0be8b06b2aafef5a7
85a13f8574096ae7a949bea229322d0dd568385882846526403dae086dd1943a
e1279bff9e7e4f041c3a4524484525e481d4cc5fe24124c0037464c0bf1bd691