| 1 |
unit ISAAC; |
| 2 |
|
| 3 |
{Bob Jenkins' public domain random number generator ISAAC} |
| 4 |
|
| 5 |
interface |
| 6 |
|
| 7 |
{$i std.inc} |
| 8 |
|
| 9 |
(************************************************************************* |
| 10 |
|
| 11 |
DESCRIPTION : Bob Jenkins' public domain random number generator ISAAC |
| 12 |
(Indirection, Shift, Accumulate, Add, and Count) |
| 13 |
Period at least 2^40, average 2^8295 |
| 14 |
|
| 15 |
REQUIREMENTS : TP5-7, D1-D7/D9-D10/D12, FPC, VP, WDOSX |
| 16 |
|
| 17 |
EXTERNAL DATA : --- |
| 18 |
|
| 19 |
MEMORY USAGE : --- |
| 20 |
|
| 21 |
DISPLAY MODE : --- |
| 22 |
|
| 23 |
REFERENCES : http://burtleburtle.net/bob/rand/isaacafa.html |
| 24 |
(ISAAC: a fast cryptographic random number generator) |
| 25 |
|
| 26 |
Version Date Author Modification |
| 27 |
------- -------- ------- ------------------------------------------ |
| 28 |
0.10 23.07.05 W.Ehrhardt Initial BP7 port of rand.c with RANDSIZ=256 |
| 29 |
0.11 23.07.05 we Some tweaking in isaac_generate |
| 30 |
0.12 23.07.05 we non crypt option (RANDSIZ=16), much slower |
| 31 |
0.13 23.07.05 we use RANDSIZ=256 only, procedure Mix |
| 32 |
0.14 23.07.05 we use mix array m, use inc where possible |
| 33 |
0.15 23.07.05 we BASM16 in isaac_generate |
| 34 |
0.16 24.07.05 we routines for word, long, double etc. |
| 35 |
0.17 01.09.05 we byte typecast in init0 |
| 36 |
0.18 05.11.08 we isaac_dword function |
| 37 |
0.19 02.12.08 we BTypes/Ptr2Inc |
| 38 |
0.20 06.01.09 we Uses BTypes moved to implementation |
| 39 |
0.21 14.06.12 we Fix bug in _read for trailing max 3 bytes |
| 40 |
**************************************************************************) |
| 41 |
|
| 42 |
|
| 43 |
(*------------------------------------------------------------------------- |
| 44 |
(C) Copyright 2005-2012 Wolfgang Ehrhardt |
| 45 |
|
| 46 |
This software is provided 'as-is', without any express or implied warranty. |
| 47 |
In no event will the authors be held liable for any damages arising from |
| 48 |
the use of this software. |
| 49 |
|
| 50 |
Permission is granted to anyone to use this software for any purpose, |
| 51 |
including commercial applications, and to alter it and redistribute it |
| 52 |
freely, subject to the following restrictions: |
| 53 |
|
| 54 |
1. The origin of this software must not be misrepresented; you must not |
| 55 |
claim that you wrote the original software. If you use this software in |
| 56 |
a product, an acknowledgment in the product documentation would be |
| 57 |
appreciated but is not required. |
| 58 |
|
| 59 |
2. Altered source versions must be plainly marked as such, and must not be |
| 60 |
misrepresented as being the original software. |
| 61 |
|
| 62 |
3. This notice may not be removed or altered from any source distribution. |
| 63 |
----------------------------------------------------------------------------*) |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
{$ifdef BIT16} |
| 68 |
{$N+} |
| 69 |
{$endif} |
| 70 |
|
| 71 |
type |
| 72 |
isaac_ctx = record {context of random number generator} |
| 73 |
randmem: array[0..255] of longint; {the internal state} |
| 74 |
randrsl: array[0..255] of longint; {the results given to the user} |
| 75 |
randa : longint; {accumulator} |
| 76 |
randb : longint; {the last result} |
| 77 |
randc : longint; {counter, guarantees cycle >= 2^40 } |
| 78 |
nextres: longint; {the next result } |
| 79 |
randidx: word; {the index in randrsl[] } |
| 80 |
end; |
| 81 |
|
| 82 |
|
| 83 |
procedure isaac_init (var ctx: isaac_ctx; seed: longint); |
| 84 |
{-Init context from randrsl[0]=seed, randrsl[i]=0 otherwise} |
| 85 |
|
| 86 |
procedure isaac_init0(var ctx: isaac_ctx); |
| 87 |
{-Init context from randseed} |
| 88 |
|
| 89 |
{$ifdef CONST} |
| 90 |
procedure isaac_inita(var ctx: isaac_ctx; const key: array of longint; klen: integer); |
| 91 |
{-Init all context variables with separate seeds, klen: number of seeds} |
| 92 |
{$else} |
| 93 |
procedure isaac_inita(var ctx: isaac_ctx; var KArr; klen: integer); |
| 94 |
{-Init all context variables with separate seeds, klen: number of seeds} |
| 95 |
{$endif} |
| 96 |
|
| 97 |
procedure isaac_next(var ctx: isaac_ctx); |
| 98 |
{-Next step of PRNG} |
| 99 |
|
| 100 |
procedure isaac_read(var ctx: isaac_ctx; dest: pointer; len: longint); |
| 101 |
{-Read len bytes from the PRNG to dest} |
| 102 |
|
| 103 |
function isaac_long(var ctx: isaac_ctx): longint; |
| 104 |
{-Next random positive longint} |
| 105 |
|
| 106 |
function isaac_dword(var ctx: isaac_ctx): {$ifdef HAS_CARD32}cardinal{$else}longint{$endif}; |
| 107 |
{-Next 32 bit random dword (cardinal or longint)} |
| 108 |
|
| 109 |
function isaac_word(var ctx: isaac_ctx): word; |
| 110 |
{-Next random word} |
| 111 |
|
| 112 |
function isaac_double(var ctx: isaac_ctx): double; |
| 113 |
{-Next random double [0..1) with 32 bit precision} |
| 114 |
|
| 115 |
function isaac_double53(var ctx: isaac_ctx): double; |
| 116 |
{-Next random double in [0..1) with full double 53 bit precision} |
| 117 |
|
| 118 |
function isaac_selftest: boolean; |
| 119 |
{-Simple self-test of ISAAC PRNG} |
| 120 |
|
| 121 |
|
| 122 |
{$ifdef testing} |
| 123 |
{interfaced for cycle testing without overhead, do not use for normal use} |
| 124 |
procedure isaac_generate(var ctx: isaac_ctx); |
| 125 |
{-generate next 256 result values, ie refill randrsl} |
| 126 |
{$endif} |
| 127 |
|
| 128 |
|
| 129 |
implementation |
| 130 |
|
| 131 |
uses |
| 132 |
BTypes; |
| 133 |
|
| 134 |
{---------------------------------------------------------------------------} |
| 135 |
procedure isaac_generate(var ctx: isaac_ctx); |
| 136 |
{-generate next 256 result values, ie refill randrsl} |
| 137 |
var |
| 138 |
x,y: longint; |
| 139 |
xi : integer absolute x; {better performance for BIT16} |
| 140 |
i : integer; |
| 141 |
{$ifdef BASM16} |
| 142 |
pra: pointer; {pointer to cxt.randa for faster BASM16 access} |
| 143 |
{$endif} |
| 144 |
begin |
| 145 |
{$ifdef BASM16} |
| 146 |
pra := @ctx.randa; |
| 147 |
{$endif} |
| 148 |
with ctx do begin |
| 149 |
inc(randc); |
| 150 |
inc(randb, randc); |
| 151 |
for i:=0 to 255 do begin |
| 152 |
{$ifdef BASM16} |
| 153 |
case i and 3 of |
| 154 |
0: asm |
| 155 |
les di,[pra] |
| 156 |
db $66; mov ax,es:[di] |
| 157 |
db $66; shl ax,13 |
| 158 |
db $66; xor es:[di],ax |
| 159 |
end; |
| 160 |
1: asm |
| 161 |
les di,[pra] |
| 162 |
db $66; mov ax,es:[di] |
| 163 |
db $66; shr ax,6 |
| 164 |
db $66; xor es:[di],ax |
| 165 |
end; |
| 166 |
2: asm |
| 167 |
les di,[pra] |
| 168 |
db $66; mov ax,es:[di] |
| 169 |
db $66; shl ax,2 |
| 170 |
db $66; xor es:[di],ax |
| 171 |
end; |
| 172 |
3: asm |
| 173 |
{shr 16 is special, use word [pra+2]} |
| 174 |
les di,[pra] |
| 175 |
mov ax, es:[di+2] |
| 176 |
xor es:[di],ax |
| 177 |
end; |
| 178 |
end; |
| 179 |
{$else} |
| 180 |
case i and 3 of |
| 181 |
0: randa := randa xor (randa shl 13); |
| 182 |
1: randa := randa xor (randa shr 6); |
| 183 |
2: randa := randa xor (randa shl 2); |
| 184 |
3: randa := randa xor (randa shr 16); |
| 185 |
end; |
| 186 |
{$endif} |
| 187 |
x := randmem[i]; |
| 188 |
inc(randa,randmem[(i+128) and 255]); |
| 189 |
y := randmem[(xi shr 2) and 255] + randa + randb; |
| 190 |
randmem[i] := y; |
| 191 |
randb := randmem[(y shr 10) and 255] + x; |
| 192 |
randrsl[i] := randb; |
| 193 |
end; |
| 194 |
{reset result index} |
| 195 |
randidx:=0; |
| 196 |
end; |
| 197 |
end; |
| 198 |
|
| 199 |
|
| 200 |
{---------------------------------------------------------------------------} |
| 201 |
procedure internal_init(var ctx: isaac_ctx; flag: boolean); |
| 202 |
{-Init state, use randrsl if flag=true} |
| 203 |
var |
| 204 |
i,j: integer; |
| 205 |
m: array[0..7] of longint; |
| 206 |
|
| 207 |
procedure Mix; |
| 208 |
{-mix the array} |
| 209 |
begin |
| 210 |
m[0] := m[0] xor (m[1] shl 11); inc(m[3], m[0]); inc(m[1], m[2]); |
| 211 |
m[1] := m[1] xor (m[2] shr 2); inc(m[4], m[1]); inc(m[2], m[3]); |
| 212 |
m[2] := m[2] xor (m[3] shl 8); inc(m[5], m[2]); inc(m[3], m[4]); |
| 213 |
m[3] := m[3] xor (m[4] shr 16); inc(m[6], m[3]); inc(m[4], m[5]); |
| 214 |
m[4] := m[4] xor (m[5] shl 10); inc(m[7], m[4]); inc(m[5], m[6]); |
| 215 |
m[5] := m[5] xor (m[6] shr 4); inc(m[0], m[5]); inc(m[6], m[7]); |
| 216 |
m[6] := m[6] xor (m[7] shl 8); inc(m[1], m[6]); inc(m[7], m[0]); |
| 217 |
m[7] := m[7] xor (m[0] shr 9); inc(m[2], m[7]); inc(m[0], m[1]); |
| 218 |
end; |
| 219 |
|
| 220 |
begin |
| 221 |
with ctx do begin |
| 222 |
randa := 0; |
| 223 |
randb := 0; |
| 224 |
randc := 0; |
| 225 |
|
| 226 |
for i:=0 to 7 do m[i] := longint($9e3779b9); {the golden ratio} |
| 227 |
for i:=0 to 3 do Mix; |
| 228 |
|
| 229 |
i := 0; |
| 230 |
while i<256 do begin |
| 231 |
{fill in randmem[] with messy stuff} |
| 232 |
if flag then begin |
| 233 |
{use all the information in the seed} |
| 234 |
for j:=0 to 7 do inc(m[j], randrsl[i+j]); |
| 235 |
end; |
| 236 |
Mix; |
| 237 |
move(m, randmem[i], sizeof(m)); |
| 238 |
inc(i,8); |
| 239 |
end; |
| 240 |
|
| 241 |
if flag then begin |
| 242 |
{do a second pass to make all of the seed affect all of randmem} |
| 243 |
i := 0; |
| 244 |
while i<256 do begin |
| 245 |
for j:=0 to 7 do inc(m[j], randmem[i+j]); |
| 246 |
Mix; |
| 247 |
move(m, randmem[i], sizeof(m)); |
| 248 |
inc(i,8); |
| 249 |
end; |
| 250 |
end; |
| 251 |
|
| 252 |
{generate first set of results} |
| 253 |
isaac_generate(ctx); |
| 254 |
{prepare to use the first set of results } |
| 255 |
randidx := 0; |
| 256 |
end; |
| 257 |
end; |
| 258 |
|
| 259 |
|
| 260 |
{---------------------------------------------------------------------------} |
| 261 |
procedure isaac_init(var ctx: isaac_ctx; seed: longint); |
| 262 |
{-Init context from randrsl[0]=seed, randrsl[i]=0 otherwise} |
| 263 |
begin |
| 264 |
with ctx do begin |
| 265 |
fillchar(randrsl, sizeof(randrsl),0); |
| 266 |
randrsl[0] := seed; |
| 267 |
end; |
| 268 |
internal_init(ctx, true); |
| 269 |
end; |
| 270 |
|
| 271 |
|
| 272 |
{---------------------------------------------------------------------------} |
| 273 |
procedure isaac_init0(var ctx: isaac_ctx); |
| 274 |
{-Init context from randseed and randrsl[i]:=random} |
| 275 |
var |
| 276 |
i,j: integer; |
| 277 |
tl: longint; |
| 278 |
ta: packed array[0..3] of byte absolute tl; |
| 279 |
begin |
| 280 |
with ctx do begin |
| 281 |
for i:=0 to 255 do begin |
| 282 |
for j:=0 to 3 do ta[j] := byte(random(256)); |
| 283 |
randrsl[i] := tl; |
| 284 |
end; |
| 285 |
end; |
| 286 |
internal_init(ctx, true); |
| 287 |
end; |
| 288 |
|
| 289 |
|
| 290 |
{---------------------------------------------------------------------------} |
| 291 |
{$ifdef CONST} |
| 292 |
procedure isaac_inita(var ctx: isaac_ctx; const key: array of longint; klen: integer); |
| 293 |
{-Init all context variables with separate seeds, klen: number of seeds} |
| 294 |
{$else} |
| 295 |
procedure isaac_inita(var ctx: isaac_ctx; var KArr; klen: integer); |
| 296 |
{-Init all context variables with separate seeds, klen: number of seeds} |
| 297 |
var |
| 298 |
key: packed array[0..255] of longint absolute KArr; {T5-6 do not have open arrrays} |
| 299 |
{$endif} |
| 300 |
var |
| 301 |
i: integer; |
| 302 |
begin |
| 303 |
{$ifdef CONST} |
| 304 |
if klen>high(key)+1 then klen := high(key)+1; |
| 305 |
{$endif} |
| 306 |
with ctx do begin |
| 307 |
for i:=0 to 255 do begin |
| 308 |
if i<klen then randrsl[i]:=key[i] else randrsl[i]:=0; |
| 309 |
end; |
| 310 |
end; |
| 311 |
internal_init(ctx, true); |
| 312 |
end; |
| 313 |
|
| 314 |
|
| 315 |
{---------------------------------------------------------------------------} |
| 316 |
procedure isaac_next(var ctx: isaac_ctx); |
| 317 |
{-Next step of PRNG} |
| 318 |
begin |
| 319 |
with ctx do begin |
| 320 |
if randidx>255 then isaac_generate(ctx); |
| 321 |
nextres := randrsl[randidx]; |
| 322 |
inc(randidx); |
| 323 |
end; |
| 324 |
end; |
| 325 |
|
| 326 |
|
| 327 |
{---------------------------------------------------------------------------} |
| 328 |
procedure isaac_read(var ctx: isaac_ctx; dest: pointer; len: longint); |
| 329 |
{-Read len bytes from the PRNG to dest} |
| 330 |
type |
| 331 |
plong = ^longint; |
| 332 |
begin |
| 333 |
{not optimized} |
| 334 |
while len>3 do begin |
| 335 |
isaac_next(ctx); |
| 336 |
plong(dest)^ := ctx.nextres; |
| 337 |
inc(Ptr2Inc(dest),4); |
| 338 |
dec(len, 4); |
| 339 |
end; |
| 340 |
if len>0 then begin |
| 341 |
isaac_next(ctx); |
| 342 |
move(ctx.nextres, dest^, len and 3); |
| 343 |
end; |
| 344 |
end; |
| 345 |
|
| 346 |
|
| 347 |
{---------------------------------------------------------------------------} |
| 348 |
function isaac_long(var ctx: isaac_ctx): longint; |
| 349 |
{-Next random positive longint} |
| 350 |
begin |
| 351 |
isaac_next(ctx); |
| 352 |
isaac_long := ctx.nextres shr 1; |
| 353 |
end; |
| 354 |
|
| 355 |
|
| 356 |
{---------------------------------------------------------------------------} |
| 357 |
function isaac_dword(var ctx: isaac_ctx): {$ifdef HAS_CARD32}cardinal{$else}longint{$endif}; |
| 358 |
{-Next 32 bit random dword (cardinal or longint)} |
| 359 |
begin |
| 360 |
isaac_next(ctx); |
| 361 |
{$ifdef HAS_CARD32} |
| 362 |
isaac_dword := cardinal(ctx.nextres); |
| 363 |
{$else} |
| 364 |
isaac_dword := ctx.nextres; |
| 365 |
{$endif} |
| 366 |
end; |
| 367 |
|
| 368 |
|
| 369 |
{---------------------------------------------------------------------------} |
| 370 |
function isaac_word(var ctx: isaac_ctx): word; |
| 371 |
{-Next random word} |
| 372 |
type |
| 373 |
TwoWords = packed record |
| 374 |
L,H: word |
| 375 |
end; |
| 376 |
begin |
| 377 |
isaac_next(ctx); |
| 378 |
isaac_word := TwoWords(ctx.nextres).H; |
| 379 |
end; |
| 380 |
|
| 381 |
|
| 382 |
{---------------------------------------------------------------------------} |
| 383 |
function isaac_double(var ctx: isaac_ctx): double; |
| 384 |
{-Next random double [0..1) with 32 bit precision} |
| 385 |
begin |
| 386 |
isaac_next(ctx); |
| 387 |
isaac_double := (ctx.nextres + 2147483648.0) / 4294967296.0; |
| 388 |
end; |
| 389 |
|
| 390 |
|
| 391 |
{---------------------------------------------------------------------------} |
| 392 |
function isaac_double53(var ctx: isaac_ctx): double; |
| 393 |
{-Next random double in [0..1) with full double 53 bit precision} |
| 394 |
var |
| 395 |
hb,lb: longint; |
| 396 |
begin |
| 397 |
isaac_next(ctx); |
| 398 |
hb := ctx.nextres shr 5; |
| 399 |
isaac_next(ctx); |
| 400 |
lb := ctx.nextres shr 6; |
| 401 |
isaac_double53 := (hb*67108864.0+lb)/9007199254740992.0; |
| 402 |
end; |
| 403 |
|
| 404 |
|
| 405 |
{---------------------------------------------------------------------------} |
| 406 |
function isaac_selftest: boolean; |
| 407 |
{-Simple self-test of ISAAC PRNG} |
| 408 |
var |
| 409 |
ctx: isaac_ctx; |
| 410 |
begin |
| 411 |
fillchar(ctx, sizeof(ctx),0); |
| 412 |
internal_init(ctx, true); |
| 413 |
isaac_generate(ctx); |
| 414 |
{check first and last longint of randvec.txt} |
| 415 |
if ctx.randrsl[0]<>longint($f650e4c8) then begin |
| 416 |
isaac_selftest := false; |
| 417 |
exit; |
| 418 |
end; |
| 419 |
isaac_generate(ctx); |
| 420 |
isaac_selftest := ctx.randrsl[255] = longint($4bb5af29); |
| 421 |
end; |
| 422 |
|
| 423 |
end. |