Random Generator with 4 DWORD seeds. need advise and opinion

Started by white scorpion, May 30, 2006, 09:46:11 AM

Previous topic - Next topic

white scorpion

Hi all,

Based on another thread in the lab about needing a better random function i decided to write one on my own.
This one uses 4 seeds which are initialized by the pseed function.
it is partially based on the code of Park Miller and i would like to get your opinion about it.

Should i make some changes?
This is only the first version, so i can imagine i might need to change some things to optimize the process:

pseed PROC s1:DWORD,s2:DWORD,s3:DWORD,s4:DWORD
.data
seed1  dd 0AAAABBBBh
seed2  dd 0CCCCDDDDh
seed3  dd 0EEEEFFFFh
seed4  dd 11112222h

.code
mov eax,s1 ;if s1 = 0 then use default value
.if eax!=0
mov seed1,eax
.endif
mov eax,s2 ;if s2 = 0 then use default value
.if eax!=0
mov seed2,eax
.endif
mov eax,s3 ;if s3 = 0 then use default value
.if eax!=0
mov seed3,eax
.endif
mov eax,s4 ;if s4 = 0 then use default value
.if eax!=0
mov seed4,eax
.endif
ret

pseed ENDP

prand PROC base:DWORD
;seed1 = AAAABBBB
;seed2 = CCCCDDDD
;seed3 = EEEEFFFF
;seed4 = 11112222

mov eax,seed1 ;AAAABBBB
mov ebx,seed2 ;CCCCDDDD
mov ecx,seed3 ;EEEEFFFF
mov edx,seed4 ;11112222
;start shifting
xchg ax,bx    ;eax = AAAADDDD, ebx = CCCCBBBB
xchg cx,dx   ;ecx = EEEE2222, edx = 1111FFFF
xchg al,cl   ;eax = AAAADD22, ecx = EEEE22DD
xchg bl,dl   ;ebx = CCCCBBFF, edx = 1111FFBB
push eax   ;AAAADD22
push ecx      ;EEEE22DD
shl eax,8   ;AADD2200
shr ecx,24   ;000000EE
add eax,ecx   ;AADD22EE
mov seed1,eax   ;s1 = AADD22EE
pop ecx   ;EEEE22DD
pop eax   ;AAAADD22
push ecx   ;EEEE22DD
shr eax,24   ;000000AA
push edx   ;1111FFBB
shl edx,8   ;11FFBB00
add edx,eax   ;11FFBBAA
mov seed2,edx    ;s2 = 11FFBBAA
pop edx   ;1111FFBB
shr edx,24   ;00000011
push ebx   ;CCCCBBFF
shl ebx,8   ;CCBBFF11
mov seed3,ebx   ;s3 = CCBBFF11
pop ebx   ;CCCCBBFF
shr ebx,24   ;000000CC
pop ecx   ;EEEE22DD
shl ecx,8   ;EE22DD00
add ecx,ebx   ;EE22DDCC
mov seed4,ecx    ;s4 = EE22DDCC
;start calculating
mov eax,seed1
mov ecx,16587
xor edx,edx
div ecx   ;AADD22EE / 16587, result in eax, remainder in edx
mov ebx,seed2    ;11FFBBAA
xchg ebx,eax 
sub eax,ebx   ;11FFBBAA - remainder
mov ecx,edx
xor edx,edx
mul ecx
mov seed2,eax    ;seed2 = (s1 / 16587)*(s2 - (s1 % 16587))

mov ecx,29753
xor edx,edx
div ecx ; (s2 / 29753)
mov ebx,seed3   ;CCBBFF11
xchg ebx,eax
sub eax,ebx  ;CCBBFF11 - remainder
mov ecx,edx
xor edx,edx
mul ecx
mov seed3,eax   ;seed3 = (s2 / 29753)*(s3 - (s2 % 29753))

mov ecx,39744
xor edx,edx
div ecx ; (s3 / 39744)
mov ebx,seed4   ;EE22DDCC
xchg ebx,eax
sub eax,ebx  ;EE22DDCC - remainder
mov ecx,edx
xor edx,edx
mul ecx
mov seed4,eax   ;seed4 = (s3 / 39744)*(s4 - (s3 % 39744))

mov ecx,59721
xor edx,edx
div ecx ; (s4 / 59721)
mov ebx,seed1   ;AADD22EE
xchg ebx,eax
sub eax,ebx  ;AADD22EE - remainder
mov ecx,edx
xor edx,edx
mul ecx
mov seed1,eax   ;seed1 = (s4 / 59721)*(s1 - (s4 % 59721))
;use every last byte of each new seed
shl eax,24
mov ebx,seed2
shl ebx,24
shr ebx,8
add eax,ebx
mov ebx,seed3
shl ebx,24
shr ebx,16
add eax,ebx
mov ebx,seed4
add al,bl
mov ebx,seed1
xor eax,ebx
xor edx,edx
div base
    mov eax,edx
    ret

prand ENDP


Attached is a file which contains above procedures and calls prand 1000 times and writes the result to a file.
I've used static seeds in this program, so you might want to change them.

Thanks for your time!

[attachment deleted by admin]

Mark Jones

Mark, I think you're missing the difference between "random" and "pseudo-random."

A good bet for a truly random bit sequence might be something like this: http://willware.net:8080/hw-rng.html

"Johnny von Neumann once said that anybody who contemplates arithmetic methods for the generation of random numbers is in a state of sin." :wink
"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08

white scorpion

Mark,
thanks for your reply.
I know truly random is impossible with a computer, that's why the proc is called prand (Pseudo Random). :toothy
The biggest issue i have is that the function from Park Miller  only accepts one seed of 1 DWORD which results in only 4,294,967,296 different seeds which in cryptographic sense is not enough.
Thats why i wrote a similar one for 4 DWORD's. this way it should be too much to bruteforce the sequence generated. even if the sequence is not truly random.
So i would like your opinion on its design. Didn't i miss some parts which could be disastrous for its security.

Sorry that i didn't made this clear in my first post.

Mark