Alyssa

Official Krist Addr Gen

Feb 5th, 2016
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.93 KB | None | 0 0
  1. local MOD = 2^32
  2. local MODM = MOD-1
  3. local function memoize(f)
  4.   local mt = {}
  5.   local t = setmetatable({}, mt)
  6.   function mt:__index(k)
  7.     local v = f(k)
  8.     t[k] = v
  9.     return v
  10.   end
  11.   return t
  12. end
  13. local function make_bitop_uncached(t, m)
  14.   local function bitop(a, b)
  15.     local res,p = 0,1
  16.     while a ~= 0 and b ~= 0 do
  17.       local am, bm = a % m, b % m
  18.       res = res + t[am][bm] * p
  19.       a = (a - am) / m
  20.       b = (b - bm) / m
  21.       p = p*m
  22.     end
  23.     res = res + (a + b) * p
  24.     return res
  25.   end
  26.   return bitop
  27. end
  28. local function make_bitop(t)
  29.   local op1 = make_bitop_uncached(t,2^1)
  30.   local op2 = memoize(function(a) return memoize(function(b) return op1(a, b) end) end)
  31.   return make_bitop_uncached(op2, 2 ^ (t.n or 1))
  32. end
  33. local bxor1 = make_bitop({[0] = {[0] = 0,[1] = 1}, [1] = {[0] = 1, [1] = 0}, n = 4})
  34. local function bxor(a, b, c, ...)
  35.   local z = nil
  36.   if b then
  37.     a = a % MOD
  38.     b = b % MOD
  39.     z = bxor1(a, b)
  40.     if c then z = bxor(z, c, ...) end
  41.     return z
  42.   elseif a then return a % MOD
  43.   else return 0 end
  44. end
  45. local function band(a, b, c, ...)
  46.   local z
  47.   if b then
  48.     a = a % MOD
  49.     b = b % MOD
  50.     z = ((a + b) - bxor1(a,b)) / 2
  51.     if c then z = bit32_band(z, c, ...) end
  52.     return z
  53.   elseif a then return a % MOD
  54.   else return MODM end
  55. end
  56. local function bnot(x) return (-1 - x) % MOD end
  57. local function rshift1(a, disp)
  58.   if disp < 0 then return lshift(a,-disp) end
  59.   return math.floor(a % 2 ^ 32 / 2 ^ disp)
  60. end
  61. local function rshift(x, disp)
  62.   if disp > 31 or disp < -31 then return 0 end
  63.   return rshift1(x % MOD, disp)
  64. end
  65. local function lshift(a, disp)
  66.   if disp < 0 then return rshift(a,-disp) end
  67.   return (a * 2 ^ disp) % 2 ^ 32
  68. end
  69. local function rrotate(x, disp)
  70.   x = x % MOD
  71.   disp = disp % 32
  72.   local low = band(x, 2 ^ disp - 1)
  73.   return rshift(x, disp) + lshift(low, 32 - disp)
  74. end
  75. local k = {
  76.   0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
  77.   0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
  78.   0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
  79.   0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
  80.   0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
  81.   0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  82.   0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
  83.   0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
  84.   0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
  85.   0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
  86.   0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
  87.   0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  88.   0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
  89.   0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
  90.   0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
  91.   0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
  92. }
  93. local function str2hexa(s)
  94.   return (string.gsub(s, ".", function(c) return string.format("%02x", string.byte(c)) end))
  95. end
  96. local function num2s(l, n)
  97.   local s = ""
  98.   for i = 1, n do
  99.     local rem = l % 256
  100.     s = string.char(rem) .. s
  101.     l = (l - rem) / 256
  102.   end
  103.   return s
  104. end
  105. local function s232num(s, i)
  106.   local n = 0
  107.   for i = i, i + 3 do n = n*256 + string.byte(s, i) end
  108.   return n
  109. end
  110. local function preproc(msg, len)
  111.   local extra = 64 - ((len + 9) % 64)
  112.   len = num2s(8 * len, 8)
  113.   msg = msg .. "\128" .. string.rep("\0", extra) .. len
  114.   assert(#msg % 64 == 0)
  115.   return msg
  116. end
  117. local function initH256(H)
  118.   H[1] = 0x6a09e667
  119.   H[2] = 0xbb67ae85
  120.   H[3] = 0x3c6ef372
  121.   H[4] = 0xa54ff53a
  122.   H[5] = 0x510e527f
  123.   H[6] = 0x9b05688c
  124.   H[7] = 0x1f83d9ab
  125.   H[8] = 0x5be0cd19
  126.   return H
  127. end
  128. local function digestblock(msg, i, H)
  129.   local w = {}
  130.   for j = 1, 16 do w[j] = s232num(msg, i + (j - 1)*4) end
  131.   for j = 17, 64 do
  132.     local v = w[j - 15]
  133.     local s0 = bxor(rrotate(v, 7), rrotate(v, 18), rshift(v, 3))
  134.     v = w[j - 2]
  135.     w[j] = w[j - 16] + s0 + w[j - 7] + bxor(rrotate(v, 17), rrotate(v, 19), rshift(v, 10))
  136.   end
  137.  
  138.   local a, b, c, d, e, f, g, h = H[1], H[2], H[3], H[4], H[5], H[6], H[7], H[8]
  139.   for i = 1, 64 do
  140.     local s0 = bxor(rrotate(a, 2), rrotate(a, 13), rrotate(a, 22))
  141.     local maj = bxor(band(a, b), band(a, c), band(b, c))
  142.     local t2 = s0 + maj
  143.     local s1 = bxor(rrotate(e, 6), rrotate(e, 11), rrotate(e, 25))
  144.     local ch = bxor (band(e, f), band(bnot(e), g))
  145.     local t1 = h + s1 + ch + k[i] + w[i]
  146.     h, g, f, e, d, c, b, a = g, f, e, d + t1, c, b, a, t1 + t2
  147.   end
  148.  
  149.   H[1] = band(H[1] + a)
  150.   H[2] = band(H[2] + b)
  151.   H[3] = band(H[3] + c)
  152.   H[4] = band(H[4] + d)
  153.   H[5] = band(H[5] + e)
  154.   H[6] = band(H[6] + f)
  155.   H[7] = band(H[7] + g)
  156.   H[8] = band(H[8] + h)
  157. end
  158. local function sha256(msg)
  159.   msg = preproc(msg, #msg)
  160.   local H = initH256({})
  161.   for i = 1, #msg, 64 do digestblock(msg, i, H) end
  162.   return str2hexa(num2s(H[1], 4) .. num2s(H[2], 4) .. num2s(H[3], 4) .. num2s(H[4], 4) ..
  163.           num2s(H[5], 4) .. num2s(H[6], 4) .. num2s(H[7], 4) .. num2s(H[8], 4))
  164. end
  165. local function makeaddressbyte(j)
  166.   if j <= 6 then return "0"
  167.   elseif j <= 13 then return "1"
  168.   elseif j <= 20 then return "2"
  169.   elseif j <= 27 then return "3"
  170.   elseif j <= 34 then return "4"
  171.   elseif j <= 41 then return "5"
  172.   elseif j <= 48 then return "6"
  173.   elseif j <= 55 then return "7"
  174.   elseif j <= 62 then return "8"
  175.   elseif j <= 69 then return "9"
  176.   elseif j <= 76 then return "a"
  177.   elseif j <= 83 then return "b"
  178.   elseif j <= 90 then return "c"
  179.   elseif j <= 97 then return "d"
  180.   elseif j <= 104 then return "e"
  181.   elseif j <= 111 then return "f"
  182.   elseif j <= 118 then return "g"
  183.   elseif j <= 125 then return "h"
  184.   elseif j <= 132 then return "i"
  185.   elseif j <= 139 then return "j"
  186.   elseif j <= 146 then return "k"
  187.   elseif j <= 153 then return "l"
  188.   elseif j <= 160 then return "m"
  189.   elseif j <= 167 then return "n"
  190.   elseif j <= 174 then return "o"
  191.   elseif j <= 181 then return "p"
  192.   elseif j <= 188 then return "q"
  193.   elseif j <= 195 then return "r"
  194.   elseif j <= 202 then return "s"
  195.   elseif j <= 209 then return "t"
  196.   elseif j <= 216 then return "u"
  197.   elseif j <= 223 then return "v"
  198.   elseif j <= 230 then return "w"
  199.   elseif j <= 237 then return "x"
  200.   elseif j <= 244 then return "y"
  201.   elseif j <= 251 then return "z"
  202.   else return "e"
  203.   end
  204. end
  205. function makev2address(key)
  206.   local protein = {}
  207.   local stick = sha256(sha256(key))
  208.   local n = 0
  209.   local link = 0
  210.   local v2 = "k"
  211.   repeat
  212.     if n < 9 then protein[n] = string.sub(stick,0,2)
  213.     stick = sha256(sha256(stick)) end
  214.     n = n + 1
  215.   until n == 9
  216.   n = 0
  217.   repeat
  218.     link = tonumber(string.sub(stick,1+(2*n),2+(2*n)),16) % 9
  219.     if string.len(protein[link]) ~= 0 then
  220.       v2 = v2 .. makeaddressbyte(tonumber(protein[link],16))
  221.       protein[link] = ''
  222.       n = n + 1
  223.     else
  224.       stick = sha256(stick)
  225.     end
  226.   until n == 9
  227.   return v2
  228. end
  229. function gen(pass_key, lean)
  230.     if not lean then
  231.         password = sha256("KRISTWALLET"..pass_key)
  232.         masterkey = password.."-000"
  233.     else
  234.         masterkey = pass_key
  235.     end
  236.     addressv1 = string.sub(sha256(masterkey),0,10)
  237.     address = makev2address(masterkey)
  238.     return address, masterkey
  239. end
Advertisement
Add Comment
Please, Sign In to add comment