Advertisement
Guest User

affds

a guest
Jun 19th, 2015
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.66 KB | None | 0 0
  1. --------------------------------------------------------------------------------------------
  2. -- LUA Big Number Library
  3. -- Created by Jayden Koedijk (elcius@gmail.com)
  4. -- Inspired by the works of Frederico Macedo Pessoa and Marco Serpa Molinaro.
  5. -- Heavly optimised for the LUA implementation in World of Warcraft, minimizing
  6. -- table creation/seeks and the use of external variables.
  7. -- Numbers are stored as tables containing words of [radix length] decimal digits.
  8. -- [0] being the most significant, [n-1] being the least: 1234567890 = {[0]=123,4567890}.
  9. -- ["n"] stores the length of the number, words in indexes >= n may contain zeros, all
  10. -- words are stored as primitive type number.
  11. -- ["neg"] indicates if the value is negative, true for negative false for positive, this
  12. -- field should not be nil.
  13. --------------------------------------------------------------------------------------------
  14.  
  15. --[[
  16. -- Adaptation of the Secure Hashing Algorithm (SHA-244/256)
  17. -- Found Here: http://lua-users.org/wiki/SecureHashAlgorithm
  18. --
  19. -- Using an adapted version of the bit library
  20. -- Found Here: https://bitbucket.org/Boolsheet/bslf/src/1ee664885805/bit.lua
  21. --]]
  22.  
  23. local MOD = 2^32
  24. local MODM = MOD-1
  25.  
  26. local function memoize(f)
  27. local mt = {}
  28. local t = setmetatable({}, mt)
  29. function mt:__index(k)
  30. local v = f(k); t[k] = v
  31. return v
  32. end
  33. return t
  34. end
  35.  
  36. local function make_bitop_uncached(t, m)
  37. local function bitop(a, b)
  38. local res,p = 0,1
  39. while a ~= 0 and b ~= 0 do
  40. local am, bm = a%m, b%m
  41. res = res + t[am][bm]*p
  42. a = (a - am) / m
  43. b = (b - bm) / m
  44. p = p*m
  45. end
  46. res = res + (a+b)*p
  47. return res
  48. end
  49. return bitop
  50. end
  51.  
  52. local function make_bitop(t)
  53. local op1 = make_bitop_uncached(t,2^1)
  54. local op2 = memoize(function(a)
  55. return memoize(function(b)
  56. return op1(a, b)
  57. end)
  58. end)
  59. return make_bitop_uncached(op2, 2^(t.n or 1))
  60. end
  61.  
  62. local bxor1 = make_bitop {[0]={[0]=0,[1]=1},[1]={[0]=1,[1]=0}, n=4}
  63.  
  64. local function bxor(a, b, c, ...)
  65. local z = nil
  66. if b then
  67. a = a % MOD
  68. b = b % MOD
  69. z = bxor1(a, b)
  70. if c then z = bxor(z, c, ...) end
  71. return z
  72. elseif a then return a % MOD
  73. else return 0 end
  74. end
  75.  
  76. local function band(a, b, c, ...)
  77. local z
  78. if b then
  79. a = a % MOD
  80. b = b % MOD
  81. z = ((a + b) - bxor1(a,b)) / 2
  82. if c then z = band(z, c, ...) end
  83. return z
  84. elseif a then return a % MOD
  85. else return MODM end
  86. end
  87.  
  88. local function bnot(x)
  89. return (-1 - x) % MOD
  90. end
  91.  
  92. local function rshift1(a, disp)
  93. if disp < 0 then return lshift(a,-disp) end
  94. return math.floor(a % 2 ^ 32 / 2 ^ disp)
  95. end
  96.  
  97.  
  98. local function rshift(a,disp) -- Lua5.2 insipred
  99. if disp < 0 then return lshift(a,-disp) end
  100. return math.floor(a % MOD / 2^disp)
  101. end
  102.  
  103. local function lshift(a,disp) -- Lua5.2 inspired
  104. if disp < 0 then return rshift(a,-disp) end
  105. return (a * 2^disp) % MOD
  106. end
  107.  
  108. local function rrotate(x, disp)
  109. x = x % MOD
  110. disp = disp % 32
  111. local low = band(x, 2 ^ disp - 1)
  112. return rshift(x, disp) + lshift(low, 32 - disp)
  113. end
  114.  
  115.  
  116. local k = {
  117. 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
  118. 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
  119. 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
  120. 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
  121. 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
  122. 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  123. 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
  124. 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
  125. 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
  126. 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
  127. 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
  128. 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  129. 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
  130. 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
  131. 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
  132. 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
  133. }
  134.  
  135. local function str2hexa(s)
  136. return (string.gsub(s, ".", function(c) return string.format("%02x", string.byte(c)) end))
  137. end
  138.  
  139. local function num2s(l, n)
  140. local s = ""
  141. for i = 1, n do
  142. local rem = l % 256
  143. s = string.char(rem) .. s
  144. l = (l - rem) / 256
  145. end
  146. return s
  147. end
  148.  
  149. local function s232num(s, i)
  150. local n = 0
  151. for i = i, i + 3 do n = n*256 + string.byte(s, i) end
  152. return n
  153. end
  154.  
  155. local function preproc(msg, len)
  156. local extra = 64 - ((len + 9) % 64)
  157. len = num2s(8 * len, 8)
  158. msg = msg .. "\128" .. string.rep("\0", extra) .. len
  159. assert(#msg % 64 == 0)
  160. return msg
  161. end
  162.  
  163. local function initH256(H)
  164. H[1] = 0x6a09e667
  165. H[2] = 0xbb67ae85
  166. H[3] = 0x3c6ef372
  167. H[4] = 0xa54ff53a
  168. H[5] = 0x510e527f
  169. H[6] = 0x9b05688c
  170. H[7] = 0x1f83d9ab
  171. H[8] = 0x5be0cd19
  172. return H
  173. end
  174.  
  175. local function digestblock(msg, i, H)
  176. local w = {}
  177. for j = 1, 16 do w[j] = s232num(msg, i + (j - 1)*4) end
  178. for j = 17, 64 do
  179. local v = w[j - 15]
  180. local s0 = bxor(rrotate(v, 7), rrotate(v, 18), rshift(v, 3))
  181. v = w[j - 2]
  182. w[j] = w[j - 16] + s0 + w[j - 7] + bxor(rrotate(v, 17), rrotate(v, 19), rshift(v, 10))
  183. end
  184.  
  185. 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]
  186. for i = 1, 64 do
  187. local s0 = bxor(rrotate(a, 2), rrotate(a, 13), rrotate(a, 22))
  188. local maj = bxor(band(a, b), band(a, c), band(b, c))
  189. local t2 = s0 + maj
  190. local s1 = bxor(rrotate(e, 6), rrotate(e, 11), rrotate(e, 25))
  191. local ch = bxor (band(e, f), band(bnot(e), g))
  192. local t1 = h + s1 + ch + k[i] + w[i]
  193. h, g, f, e, d, c, b, a = g, f, e, d + t1, c, b, a, t1 + t2
  194. end
  195.  
  196. H[1] = band(H[1] + a)
  197. H[2] = band(H[2] + b)
  198. H[3] = band(H[3] + c)
  199. H[4] = band(H[4] + d)
  200. H[5] = band(H[5] + e)
  201. H[6] = band(H[6] + f)
  202. H[7] = band(H[7] + g)
  203. H[8] = band(H[8] + h)
  204. end
  205.  
  206. local function sha256(msg)
  207. msg = preproc(msg, #msg)
  208. local H = initH256({})
  209. for i = 1, #msg, 64 do digestblock(msg, i, H) end
  210. return str2hexa(num2s(H[1], 4) .. num2s(H[2], 4) .. num2s(H[3], 4) .. num2s(H[4], 4) ..
  211. num2s(H[5], 4) .. num2s(H[6], 4) .. num2s(H[7], 4) .. num2s(H[8], 4))
  212. end
  213.  
  214. --------------------------------------------------------------------------------------------
  215. -- LUA Digital Signature Algorithm Library
  216. -- Created by Jayden Koedijk (elcius@gmail.com)
  217. -- http://csrc.nist.gov/publications/fips/fips186-3/fips_186-3.pdf
  218. -- Uses the (Big Number) BN library for calculations.
  219. -- Uses sha256 for hashing.
  220. --------------------------------------------------------------------------------------------
  221. BN = {
  222. RL = 7, -- radix length
  223. R = 10^7, -- radix
  224.  
  225. -- create a new BN (big number)
  226. new = function(v)
  227. local neg = string.find(v,"-")==1
  228. v = v:gsub("[^%d]",""):gsub("^0+","");
  229. local num = {n=math.ceil(string.len(v)/BN.RL), neg = neg};
  230. for i = 0,num.n-1 do
  231. num[i] = tonumber( v:sub( 0-((i+1)*BN.RL), -1-(i*BN.RL) ) );
  232. end
  233. return num;
  234. end,
  235. fromHex = function(h)
  236. local result = BN.new("0");
  237. local temp = BN.new("0");
  238. for i = 1, string.len(h) do
  239. BN.smul( result, 16, temp );
  240. BN.add( {n=1,neg=false,[0]=tonumber( h:sub(i, i), 16 )}, temp, result );
  241. end
  242. return result;
  243. end,
  244.  
  245. -- adds a and b into c
  246. add = function(a,b,c)
  247. if a.neg ~= b.neg then --[ x+-y == x-y ]
  248. if a.neg then a,b = b,a; end -- move negative number to b
  249. b.neg = false; -- flag b as positive
  250. BN.sub(a,b,c); -- subtract positives
  251. if b ~= c then b.neg = true; end -- revert flag if b is not a pointer to c.
  252. return;
  253. end
  254. -- actual addition
  255. local radix = BN.R;
  256. local carry = 0;
  257. local n = math.max(a.n,b.n);
  258. for i = 0, n-1 do
  259. local s = (a[i] or 0) + (b[i] or 0) + carry;
  260. if s >= radix then
  261. s = s - radix;
  262. carry = 1;
  263. else
  264. carry = 0;
  265. end
  266. c[i] = s;
  267. end
  268. if carry == 1 then
  269. c[n] = 1;
  270. c.n = n+1;
  271. else
  272. c.n = n;
  273. end
  274. c.neg = a.neg;
  275. end,
  276.  
  277. -- subtracts b from a into c
  278. sub = function(a,b,c)
  279. if a.neg ~= b.neg then --[ x--y == x+y && -x-y == -(x+y) ]
  280. local neg = a.neg; -- used to restore flags
  281. a.neg = false;
  282. b.neg = false;
  283. BN.add(a,b,c);
  284. a.neg = neg; -- revert flags
  285. b.neg = not neg;
  286. c.neg = neg;
  287. elseif a.neg then -- both negative --[ -x--y == y-x ]
  288. a.neg = false;
  289. b.neg = false;
  290. BN.sub(b,a,c);
  291. if a ~= c then a.neg = true; end -- revert flags
  292. if b ~= c then b.neg = true; end
  293. elseif BN.eqAbs(a,b) == -1 then --[ x-y == -(y-x) when y>x ]
  294. BN.sub(b,a,c);
  295. c.neg = true;
  296. else -- a > b, both numbers are positive
  297. -- actual subtraction
  298. local radix = BN.R;
  299. local carry = 0;
  300. local n;
  301. for i = 0, a.n-1 do
  302. local s = (a[i] or 0) - (b[i] or 0) - carry;
  303. if s < 0 then
  304. s = radix + s;
  305. carry = 1;
  306. else
  307. carry = 0;
  308. end
  309. if s ~= 0 then n = i+1; end
  310. c[i] = s;
  311. end
  312. if not n then -- zero/empty answer
  313. n = 1;
  314. c[1] = 0;
  315. end
  316. c.n = n;
  317. c.neg = false;
  318. -- clear un-used values
  319. while c[n+1] do
  320. n = n+1;
  321. c[n] = nil;
  322. end
  323. end
  324. end,
  325.  
  326. -- multiplies a nd b into c
  327. mul = function(a,b,c)
  328. --assert( c ~= a and c ~= b ); -- c gets cleared and can not reference an input
  329. if a.neg ~= b.neg then -- [-a*b == -(a*b)]
  330. if a.neg then a,b = b,a; end -- move negative number to b
  331. b.neg = false; -- flag b as positive
  332. BN.mul(a,b,c); -- multiply positives
  333. b.neg = true; -- revert flag
  334. c.neg = true; -- flag c as negative
  335. return;
  336. end
  337. -- actual multiplication
  338. local radix = BN.R;
  339. c.neg = false;
  340. local carry = 0;
  341. local an = a.n;
  342. local bn = b.n;
  343. local fmod = math.fmod;
  344. for i = 0, (an+bn)-1 do c[i] = 0; end -- clear and zero fill c
  345. for i = 0, an-1 do
  346. local ai = a[i];
  347. for j = 0, bn-1 do
  348. carry = ( ai * b[j] + carry ) + c[i+j];
  349. c[i+j] = fmod( carry, radix );
  350. carry = math.floor( carry / radix );
  351. end
  352. if carry ~= 0 then
  353. c[i+bn] = carry;
  354. carry = 0;
  355. end
  356. end
  357. -- update n for c, also clear zeros
  358. for i = (an+bn)-1, 0, -1 do
  359. if c[i] ~= 0 then
  360. c.n = i+1;
  361. return;
  362. else
  363. c[i] = nil;
  364. end
  365. end
  366. if not c[0] then
  367. c[0] = 0;
  368. end
  369. end,
  370.  
  371. -- equivalent to a = b*(BN.R^n)
  372. put = function(a,b,n)
  373. for i = 0, n-1 do a[i] = 0; end
  374. for i = n+1, a.n do a[i] = nil; end
  375. a[n] = b;
  376. a.n = n;
  377. end,
  378.  
  379. -- divide
  380. div = function(a,b,c,d)
  381. --assert( a ~= c and a ~= d and b ~= c and b ~= d and c ~= d );
  382. -- actual division
  383. local radix = BN.R;
  384. local temp1 = {n=1,neg=false};
  385. local temp2 = {n=1,neg=false};
  386. if not c then c = {}; end
  387. if not d then d = {}; end
  388. for i = 0, c.n or 0 do c[i] = nil; end -- clear c
  389. for i = 0, a.n do d[i] = a[i]; end -- copy a into d
  390. c.n = 1;
  391. d.n = a.n;
  392. c.neg = false;
  393. d.neg = false;
  394.  
  395. while BN.eqAbs( d, b ) ~= -1 do
  396. if d[d.n-1] >= b[b.n-1] then
  397. BN.put( temp1, math.floor( d[d.n-1] / b[b.n-1] ), d.n-b.n );
  398. temp1.n = d.n-b.n + 1 ;
  399. else
  400. BN.put( temp1, math.floor( ( d[d.n - 1] * radix + d[d.n - 2] ) / b[b.n -1] ) , d.n-b.n - 1 ) ;
  401. temp1.n = d.n-b.n;
  402. end
  403. temp1.neg = d.neg;
  404. BN.add( temp1, c, c );
  405. BN.mul( temp1, b, temp2 );
  406. temp2.neg = temp1.neg;
  407. BN.sub( d, temp2, d );
  408. end
  409.  
  410. if d.neg then
  411. c[c.n-1] = c[c.n-1]-1; -- decr c
  412. BN.add( b, d, d );
  413. end
  414.  
  415. -- adjustments
  416. if a.neg and d.neg then -- remainder is negative
  417. c[c.n-1] = c[c.n-1]+1; -- inc c
  418. if b.neg then
  419. b.neg = false;
  420. BN.sub( b, d, d );
  421. b.neg = true;
  422. else
  423. BN.sub( b, d, d );
  424. end
  425. end
  426. if a.neg ~= b.neg then --[ a/-b | -a/b == -(a/b) ]
  427. c.neg = true;
  428. end
  429. if not c[0] then c[0] = 0; end
  430. end,
  431.  
  432. -- small divide, faster than normal div, (returns remainder as number)
  433. sdiv = function(a,b,c)
  434. local radix = BN.R;
  435. local carry = 0;
  436. for i = a.n, c.n-1 do c[i] = nil; end -- clear c
  437. c.n = a.n;
  438. for i = a.n-1, 0, -1 do
  439. c[i] = (a[i]/b) + (carry*radix);
  440. carry = c[i]%1;
  441. c[i] = math.floor(c[i]);
  442. if c[i] == 0 then
  443. c.n = c.n-1;
  444. end
  445. end
  446. return math.floor(0.5+(carry*b));
  447. end,
  448.  
  449. -- small multiplication
  450. smul = function(a,b,c)
  451. local radix = BN.R;
  452. local carry = 0;
  453. for i = a.n, c.n-1 do c[i] = nil; end -- clear c
  454. for i = 0, a.n-1 do
  455. c[i] = (a[i]*b)+carry
  456. carry = math.floor(c[i]/radix);
  457. c[i] = c[i]%radix;
  458. end
  459. if carry ~= 0 then
  460. c[a.n] = carry;
  461. c.n = a.n + 1;
  462. else
  463. c.n = a.n;
  464. end
  465. end,
  466.  
  467. -- modular exponentiation, (b^e)%m
  468. -- TODO: replace divide's with dedicated modulo's
  469. mpow = function(b,e,m)
  470. local result = BN.new("1");
  471. e = BN.copy(e,BN.new("0"));
  472. local base = {n=0};
  473. BN.copy(b,base);
  474. local temp = BN.new("0");
  475. while e[0] ~= 0 or e.n > 1 do -- e != 0
  476. if BN.sdiv( e, 2, e ) == 1 then
  477. BN.mul( result, base, temp );
  478. BN.div( temp, m, nil, result );
  479. end
  480. BN.mul( base, base, temp );
  481. BN.div( temp, m, nil, base );
  482. end
  483. return result;
  484. end,
  485.  
  486. -- modular multiplicative inverse, fips_186-3, C.1
  487. modInverse = function(z,a)
  488. local i = BN.copy(a,BN.new("0"));
  489. local j = BN.copy(z,BN.new("0"));
  490. local y1 = BN.new("1");
  491. local y2 = BN.new("0");
  492. local r = BN.new("0");
  493. local q = BN.new("0");
  494. local y = BN.new("0");
  495. while j[0] > 0 or j.n > 1 do
  496. BN.div(i,j,q,r);
  497. BN.mul(y1,q,y);
  498. BN.sub(y2,y,y);
  499. BN.copy(j,i);
  500. BN.copy(r,j);
  501. BN.copy(y1,y2);
  502. BN.copy(y,y1);
  503. end
  504. if y2.neg or ( y2[0] == 0 and y2.n == 1 ) then
  505. BN.add(y2,a,y2);
  506. end
  507. return y2;
  508. end,
  509.  
  510. -- -1 = a<b, 0 = a==b, 1 = a>b
  511. eq = function(a,b)
  512. if a.neg ~= b.neg then return b.neg and 1 or -1; end -- positive > negative
  513. if a.neg then return BN.eqAbs(a,b) * -1; end -- both negative so inverse
  514. return BN.eqAbs(a,b); -- both positive
  515. end,
  516. eqAbs = function(a,b)
  517. if a == b then return 0; end -- same object
  518. if a.n ~= b.n then return ( a.n > b.n ) and 1 or -1; end
  519. for i = a.n-1, 0, -1 do
  520. if a[i] ~= b[i] then return ( a[i] > b[i] ) and 1 or -1; end
  521. end
  522. return 0;
  523. end,
  524.  
  525. -- copys a into b
  526. copy = function(a,b)
  527. for i = 0, math.max(a.n,b.n)-1 do
  528. b[i] = a[i];
  529. end
  530. b.n = a.n;
  531. b.neg = a.neg;
  532. return b;
  533. end,
  534. }
  535. local BN = BN;
  536.  
  537. LibDSA = {
  538.  
  539. -- validate a signature
  540. Validate = function(key,sig,msg)
  541. local q,p,g,y = key.q, key.p, key.g, key.y;
  542. local r,s = sig.r, sig.s;
  543. if not ( q and p and g and y and r and s and type(msg) == "string" ) then
  544. return false,"Invalid Input.";
  545. elseif not sha256 then
  546. return false,"Hash function unavailable 'sha256'."
  547. end
  548.  
  549. -- 0 < r < q, 0 < s < q
  550. local temp = BN.new("0");
  551. if BN.eqAbs(r,temp) ~= 1 or BN.eq(r,q) ~= -1
  552. or BN.eqAbs(s,temp) ~= 1 or BN.eq(s,q) ~= -1 then
  553. return false,"Signature out of range.";
  554. end
  555.  
  556. -- w = s^-1 % q
  557. local w = BN.modInverse(s,q);
  558.  
  559. -- u1 = H(m)*w % q
  560. local m = BN.fromHex( sha256(msg):sub(0,40) ); -- H(m)
  561. local u1 = BN.new("0");
  562. BN.mul( m, w, temp )
  563. BN.div( temp, q, nil, u1 );
  564.  
  565. -- u2 = r*w % q
  566. local u2 = BN.new("0");
  567. BN.mul( r, w, temp );
  568. BN.div( temp, q, nil, u2 );
  569.  
  570. -- ((g^u1*g^u2)%p)%q == (((g^u1%q)*(y^u2%q))%p)%q
  571. -- these first two operations are about 80% of the work
  572. local gu1 = BN.mpow(g,u1,p); -- (g^u1%q)
  573. local yu2 = BN.mpow(y,u2,p); -- (y^u2%q)
  574.  
  575. local v = BN.new("0");
  576. BN.mul( gu1, yu2, v ); -- gu1*yu2
  577. BN.div( v, p, nil, temp ); -- %p
  578. BN.div( temp, q, nil, v ); -- %q
  579.  
  580. return BN.eq(v,r) == 0;
  581. end,
  582.  
  583. -- generate a signature
  584. -- this function is not needed by users and should not be packaged
  585. Sign = function(key,x,msg)
  586. local q,p,g,y = key.q, key.p, key.g, key.y;
  587. if not ( q and p and g and y and x and type(msg) == "string" ) then
  588. return false,"Invalid Input.";
  589. end
  590.  
  591. local r = BN.new("0");
  592. local s = BN.new("0");
  593. local m = BN.fromHex( sha256(msg):sub(0,40) ); -- H(m)
  594. local temp1 = BN.new("0");
  595. local temp2 = BN.new("0");
  596.  
  597. repeat
  598. -- seed k
  599. local k = BN.new((math.random()..math.random()..math.random()):gsub("0%.",""));
  600. -- (g^k %p)%q
  601. temp1 = BN.mpow( g, k, p );
  602. BN.div( temp1, q, nil, r );
  603.  
  604. -- restart if r is 0
  605. if r[0] ~= 0 or s.n >= 1 then
  606. -- (k^-1) * (H(m) + x*r) % q
  607. k = BN.modInverse(k,q); -- k^-1%q
  608. BN.mul( x, r, temp1 ); -- x*r
  609. BN.div( temp1, q, nil, temp2 ); -- (x*r)%q
  610. BN.add( m, temp2, temp2 ); -- m+((x*r)%q)
  611. BN.mul( k, temp2, temp1 ); -- (k^-1%q)*(m+((x*r)%q))
  612. BN.div( temp1, q, nil, s ); -- s = ((k^-1%q)*(m+((x*r)%q)))%q
  613. end
  614. until s[0] ~= 0 or s.n >= 1; -- restart if s is 0
  615.  
  616. return {r=r,s=s};
  617. end,
  618.  
  619. test = function(this)
  620. -- values generated using OpenSSL
  621. local key = { -- public key
  622. q = this.BN.fromHex("e12271ec020adfe604bceafa55610a000f1f6c9f"),
  623. p = this.BN.fromHex("b53316faa1f842a44bebefa177674cb6cde6ba7894f33eff55522a73cbdb4b6390789dcb6b305c5970939e7c041859e7fd411ab747803663f8b94110ecb86b4b"),
  624. g = this.BN.fromHex("3f9b423021dc91663693a48f38c84d3986ccfd0a0c91ec578e83806275f07db1cae9170190b5d739863f7af1a7c38f381b53e7ef75be08d38eab3de5d61a8f88"),
  625. y = this.BN.fromHex("9341ba0b2aaa9a1e8d9dd1f5f58d83dd1b9fbaab39a026dbbe1e746ced9d1468c244e9e512353fe5909a3b1adb109c46c408780405a7711773047c2d85d6aa10")
  626. };
  627. local msg = "hello world";
  628.  
  629. -- yeild
  630. -- sleep(0)
  631.  
  632. -- validate test
  633. local sig = {
  634. r = this.BN.fromHex("3bde3048d29076582dba3db7c72a242934aacf61"),
  635. s = this.BN.fromHex("813d6cd6e2196f029ccc19054c3f18ccd4201c40")
  636. };
  637. print("Pregenerated Signature Result: ", this.Validate(key,sig,msg));
  638.  
  639. -- sign test
  640. local x = this.BN.fromHex("3fa4d6629e2688807b87eddb93c601e71eb5dbf9"); -- private key
  641. local sig,err = this.Sign(key,x,msg);
  642. print("Signature Generation: ", err or "okay.");
  643.  
  644. -- yeild
  645. sleep(0)
  646.  
  647. print("Generated Signature Result: ", this.Validate(key,sig,msg));
  648. end,
  649.  
  650. BN = BN,
  651. }
  652.  
  653. return LibDSA
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement