source: node_modules/bcryptjs/src/bcrypt/prng/isaac.js

main
Last change on this file was 81bc7da, checked in by Klimentina Efremova <klimentina08642@…>, 3 months ago

Initial commit

  • Property mode set to 100644
File size: 5.1 KB
RevLine 
[81bc7da]1/*
2 isaac.js Copyright (c) 2012 Yves-Marie K. Rinquin
3
4 Permission is hereby granted, free of charge, to any person obtaining
5 a copy of this software and associated documentation files (the
6 "Software"), to deal in the Software without restriction, including
7 without limitation the rights to use, copy, modify, merge, publish,
8 distribute, sublicense, and/or sell copies of the Software, and to
9 permit persons to whom the Software is furnished to do so, subject to
10 the following conditions:
11
12 The above copyright notice and this permission notice shall be
13 included in all copies or substantial portions of the Software.
14
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23
24/* isaac module pattern */
25var isaac = (function(){
26
27 /* internal states */
28 var m = Array(256), // internal memory
29 acc = 0, // accumulator
30 brs = 0, // last result
31 cnt = 0, // counter
32 r = Array(256), // result array
33 gnt = 0, // generation counter
34 isd = false; // initially seeded
35
36
37 /* 32-bit integer safe adder */
38 function add(x, y) {
39 var lsb = (x & 0xffff) + (y & 0xffff),
40 msb = (x >>> 16) + (y >>> 16) + (lsb >>> 16);
41 return (msb << 16) | (lsb & 0xffff);
42 }
43
44 /* initialisation */
45 function reset() {
46 acc = brs = cnt = 0;
47 for (var i = 0; i < 256; ++i)
48 m[i] = r[i] = 0;
49 gnt = 0;
50 }
51
52 /* seeding function */
53 function seed(s) {
54 var a, b, c, d, e, f, g, h, i;
55
56 /* seeding the seeds of love */
57 a = b = c = d = e = f = g = h = 0x9e3779b9; /* the golden ratio */
58
59 if (s && typeof(s) === 'number')
60 s = [s];
61
62 if (s instanceof Array) {
63 reset();
64 for (i = 0; i < s.length; ++i)
65 r[i & 0xff] += typeof(s[i]) === 'number' ? s[i] : 0;
66 }
67
68 /* private: seed mixer */
69 function seed_mix() {
70 a ^= b << 11; d = add(d, a); b = add(b, c);
71 b ^= c >>> 2; e = add(e, b); c = add(c, d);
72 c ^= d << 8; f = add(f, c); d = add(d, e);
73 d ^= e >>> 16; g = add(g, d); e = add(e, f);
74 e ^= f << 10; h = add(h, e); f = add(f, g);
75 f ^= g >>> 4; a = add(a, f); g = add(g, h);
76 g ^= h << 8; b = add(b, g); h = add(h, a);
77 h ^= a >>> 9; c = add(c, h); a = add(a, b);
78 }
79
80 for (i = 0; i < 4; i++) /* scramble it */
81 seed_mix();
82
83 for (i = 0; i < 256; i += 8) {
84 if (s) /* use all the information in the seed */
85 a = add(a, r[i + 0]), b = add(b, r[i + 1]),
86 c = add(c, r[i + 2]), d = add(d, r[i + 3]),
87 e = add(e, r[i + 4]), f = add(f, r[i + 5]),
88 g = add(g, r[i + 6]), h = add(h, r[i + 7]);
89 seed_mix();
90 /* fill in m[] with messy stuff */
91 m[i + 0] = a; m[i + 1] = b; m[i + 2] = c; m[i + 3] = d;
92 m[i + 4] = e; m[i + 5] = f; m[i + 6] = g; m[i + 7] = h;
93 }
94 if (s)
95 /* do a second pass to make all of the seed affect all of m[] */
96 for (i = 0; i < 256; i += 8)
97 a = add(a, m[i + 0]), b = add(b, m[i + 1]),
98 c = add(c, m[i + 2]), d = add(d, m[i + 3]),
99 e = add(e, m[i + 4]), f = add(f, m[i + 5]),
100 g = add(g, m[i + 6]), h = add(h, m[i + 7]),
101 seed_mix(),
102 /* fill in m[] with messy stuff (again) */
103 m[i + 0] = a, m[i + 1] = b, m[i + 2] = c, m[i + 3] = d,
104 m[i + 4] = e, m[i + 5] = f, m[i + 6] = g, m[i + 7] = h;
105 prng(); /* fill in the first set of results */
106 gnt = 256; /* prepare to use the first set of results */;
107 }
108
109 /* isaac generator, n = number of run */
110 function prng(n) {
111 var i, x, y;
112 n = n && typeof(n) === 'number' ? Math.abs(Math.floor(n)) : 1;
113 while (n--) {
114 cnt = add(cnt, 1);
115 brs = add(brs, cnt);
116 for(i = 0; i < 256; i++) {
117 switch(i & 3) {
118 case 0: acc ^= acc << 13; break;
119 case 1: acc ^= acc >>> 6; break;
120 case 2: acc ^= acc << 2; break;
121 case 3: acc ^= acc >>> 16; break;
122 }
123 acc = add(m[(i + 128) & 0xff], acc); x = m[i];
124 m[i] = y = add(m[(x >>> 2) & 0xff], add(acc, brs));
125 r[i] = brs = add(m[(y >>> 10) & 0xff], x);
126 }
127 }
128 }
129
130 /* return a random number between */
131 return function() {
132 if (!isd) // seed from accumulator
133 isd = true,
134 accum.stop(),
135 seed(accum.fetch());
136 if (!gnt--)
137 prng(), gnt = 255;
138 return r[gnt];
139 };
140})();
Note: See TracBrowser for help on using the repository browser.