Advertisement
osmarks

PotatOS "Chaos"

Jul 8th, 2019
1,325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.08 KB | None | 0 0
  1. -- A PRNG-ish thing for potatOS
  2.  
  3. -- Nice SHA256 implementation, derived from Anavrins' work: https://pastebin.com/6UV4qfNF
  4.  
  5. do
  6.     local a=2^32;local b=bit32 and bit32.band or bit.band;local c=bit32 and bit32.bnot or bit.bnot;local d=bit32 and bit32.bxor or bit.bxor;local e=bit32 and bit32.lshift or bit.blshift;local f=unpack;local function g(h,i)local j=h/2^i;local k=j%1;return j-k+k*a end;local function l(m,n)local j=m/2^n;return j-j%1 end;local o={0x6a09e667,0xbb67ae85,0x3c6ef372,0xa54ff53a,0x510e527f,0x9b05688c,0x1f83d9ab,0x5be0cd19}local p={0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5,0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5,0xd807aa98,0x12835b01,0x243185be,0x550c7dc3,0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174,0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc,0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da,0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7,0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967,0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13,0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85,0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3,0xd192e819,0xd6990624,0xf40e3585,0x106aa070,0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5,0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3,0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208,0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2}local function q(r)local s,t=0,0;if 0xFFFFFFFF-s<r then t=t+1;s=r-(0xFFFFFFFF-s)-1 else s=s+r end;return t,s end;local function u(v,w)return e(v[w]or 0,24)+e(v[w+1]or 0,16)+e(v[w+2]or 0,8)+(v[w+3]or 0)end;local function x(y)local z=#y;local A={}y[#y+1]=0x80;while#y%64~=56 do y[#y+1]=0 end;local B=math.ceil(#y/64)for w=1,B do A[w]={}for C=1,16 do A[w][C]=u(y,1+(w-1)*64+(C-1)*4)end end;A[B][15],A[B][16]=q(z*8)return A end;local function D(E,F)for C=17,64 do local G=E[C-15]local H=d(d(g(E[C-15],7),g(E[C-15],18)),l(E[C-15],3))local I=d(d(g(E[C-2],17),g(E[C-2],19)),l(E[C-2],10))E[C]=(E[C-16]+H+E[C-7]+I)%a end;local J,i,K,L,M,k,N,O=f(F)for C=1,64 do local P=d(d(g(M,6),g(M,11)),g(M,25))local Q=d(b(M,k),b(c(M),N))local R=(O+P+Q+p[C]+E[C])%a;local S=d(d(g(J,2),g(J,13)),g(J,22))local T=d(d(b(J,i),b(J,K)),b(i,K))local U=(S+T)%a;O,N,k,M,L,K,i,J=N,k,M,(L+R)%a,K,i,J,(R+U)%a end;F[1]=(F[1]+J)%a;F[2]=(F[2]+i)%a;F[3]=(F[3]+K)%a;F[4]=(F[4]+L)%a;F[5]=(F[5]+M)%a;F[6]=(F[6]+k)%a;F[7]=(F[7]+N)%a;F[8]=(F[8]+O)%a;return F end;local V={__tostring=function(J)return string.char(unpack(J))end,__index={toHex=function(self,j)return("%02x"):rep(#self):format(unpack(self))end,isEqual=function(self,W)if type(W)~="table"then return false end;if#self~=#W then return false end;local X=0;for w=1,#self do X=bit32.bor(X,d(self[w],W[w]))end;return X==0 end}}local function Y(W,h)local i={}for w=1,h do i[(w-1)*4+1]=b(l(W[w],24),0xFF)i[(w-1)*4+2]=b(l(W[w],16),0xFF)i[(w-1)*4+3]=b(l(W[w],8),0xFF)i[(w-1)*4+4]=b(W[w],0xFF)end;return setmetatable(i,V)end;function sha256(y)local y=y or""y=type(y)=="table"and{f(y)}or{tostring(y):byte(1,-1)}y=x(y)local F={f(o)}for w=1,#y do F=D(y[w],F)end;return Y(F,8)end
  7. end
  8.  
  9. local io_balance = 0
  10. local current = ""
  11.  
  12. -- Serialize a value, not in a way which lets you get the original one back. Internal.
  13. local function serialize(data)
  14.     local ty = type(data)
  15.     if ty == "number" then return tostring(data)
  16.     elseif ty == "string" then return data
  17.     elseif ty == "table" then
  18.         local out = ""
  19.         for k, v in pairs(data) do
  20.             out = out .. serialize(k) .. serialize(v)
  21.         end
  22.         return out
  23.     elseif ty == "function" then
  24.         local ok, res = pcall(string.dump, data)
  25.         if ok then return res
  26.         else return tostring(data) end
  27.     else return tostring(data) end
  28. end
  29.  
  30. --Add data to the entropy pool.
  31. local function add(data)
  32.     current = sha256(tostring(current) .. serialize(data))
  33.     io_balance = io_balance + 1
  34. end
  35.  
  36. -- Initially seed it with time
  37. add(os.epoch "utc")
  38.  
  39. -- Get a new chunk (16 bytes, 128 bits) from the entropy pool. Internal.
  40. local function get_chunk()
  41.     local x = current
  42.     current = sha256(x)
  43.     io_balance = io_balance - 1
  44.     -- Limit it to only providing *half* the current thing at once
  45.     local slice = {}
  46.     for i = 1, 16 do slice[i] = x[i] end
  47.     return slice
  48. end
  49.  
  50. -- Get a single unsigned 8-bit integer
  51. local function get_u8()
  52.     return get_chunk()[1]
  53. end
  54.  
  55. -- Get a variable-length string
  56. local function get_str(len)
  57.     local str = ""
  58.     local remaining = len
  59.     repeat
  60.         local c = get_chunk()
  61.         local diff = math.min(remaining, 16)
  62.         for i = 1, diff do
  63.             str = str .. string.char(c[i])
  64.         end
  65.         remaining = remaining - diff
  66.     until remaining == 0
  67.     return str
  68. end
  69.  
  70. -- Get a single unsigned 32-bit integer
  71. local function get_u32()
  72.     local chunk = get_chunk()
  73.     local b1, b2, b3, b4 = chunk[1], chunk[2], chunk[3], chunk[4]
  74.     return b1 * 0x1000000 + b2 * 0x10000 + b3 * 0x100 + b4
  75. end
  76.  
  77. -- Get a randomish float between 0 and 1
  78. local function get_0_1_float()
  79.     return get_u32() / 0x100000000
  80. end
  81.  
  82. -- Get a rough estimate of how much entropy has been added vs. removed. I totally know what entropy is.
  83. local function balance()
  84.     return io_balance
  85. end
  86.  
  87. -- Listen to events and use them for entropy
  88. local function hook()
  89.     while true do
  90.         add {os.epoch "utc", coroutine.yield()}
  91.     end
  92. end
  93.  
  94. return {
  95.     add = add,
  96.     get_u32 = get_u32,
  97.     get_u8 = get_u8,
  98.     get_str = get_str,
  99.     balance = balance,
  100.     hook = hook
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement