flat assembler
Message board for the users of flat assembler.

Index > Main > 64bit Random Number Generation

Author
Thread Post new topic Reply to topic
r22



Joined: 27 Dec 2004
Posts: 805
r22
This function creates 64 random bytes. It uses a 128byte seed.
The seed has to be sufficient (not all null) but that's not really the point of this thread. Using repeated RDTSC and MUL opcodes seems to make a seed that equates to random blocks that pass ENT tests very well.

WHAT I WANT:
-Any possible bugs I may have missed in the code (I wrote it some time ago but plan on implementing it in the near future).
-Any optimizations for the current algorithm/function.
-Possibly an algorithm with less steps that produces the same or better level of randomness.

Code:
DQ1 equ 0
DQ2 equ 16
DQ3 equ 32
DQ4 equ 48
DQ5 equ 64
DQ6 equ 80
DQ7 equ 96
DQ8 equ 112
;;This function creates a random block of 64bytes
;;based on the 128 byte key.
;;The key structure is modified, so subsequent calls return different
;;random 64byte buffers
;;Return is void
;;IN:
;;Param1 rcx = address of Key struct
;;Param2 rdx = address of output buffer 64bytes 16byte ALIGNED
RandomBlock:
        mov     rax, rsp ;;save stack ptr
        and     rsp, 0xFFFFFFFFFFFFFFF0 ;; 16byte align
        sub     rsp, 16*9
;;save XMM8-15
        movdqa  [rsp+DQ8], xmm15
        movdqa  [rsp+DQ7], xmm14
        movdqa  [rsp+DQ6], xmm13
        movdqa  [rsp+DQ5], xmm12
        movdqa  [rsp+DQ4], xmm11
        movdqa  [rsp+DQ3], xmm10
        movdqa  [rsp+DQ2], xmm9
        movdqa  [rsp+DQ1], xmm8
;;setup
        movdqa  xmm0, [rcx+DQ1]
        movdqa  xmm1, [rcx+DQ2]
        movdqa  xmm2, [rcx+DQ3]
        movdqa  xmm3, [rcx+DQ4]
        movdqa  xmm4, [rcx+DQ5]
        movdqa  xmm5, [rcx+DQ6]
        movdqa  xmm6, [rcx+DQ7]
        movdqa  xmm7, [rcx+DQ8]
;;copy
        movdqa  xmm8, xmm0
        movdqa  xmm9, xmm1
        movdqa  xmm10, xmm2
        movdqa  xmm11, xmm3
        movdqa  xmm12, xmm4
        movdqa  xmm13, xmm5
        movdqa  xmm14, xmm6
        movdqa  xmm15, xmm7
;;mask highest qword bit
        pand    xmm8, dqword[SSERndMask]
        pand    xmm9, dqword[SSERndMask]
        pand    xmm10, dqword[SSERndMask]
        pand    xmm11, dqword[SSERndMask]
        pand    xmm12, dqword[SSERndMask]
        pand    xmm13, dqword[SSERndMask]
        pand    xmm14, dqword[SSERndMask]
        pand    xmm15, dqword[SSERndMask]
;;shift right logical 63bits to have the masked bit the lowest spot
        psrlq   xmm8, 63
        psrlq   xmm9, 63
        psrlq   xmm10, 63
        psrlq   xmm11, 63
        psrlq   xmm12, 63
        psrlq   xmm13, 63
        psrlq   xmm14, 63
        psrlq   xmm15, 63
;;shift left to clear the highest bit and empty the lowest
        psllq   xmm4, 1
        psllq   xmm5, 1
        psllq   xmm6, 1
        psllq   xmm7, 1
;;add masked bit
        paddq   xmm0, xmm12
        paddq   xmm1, xmm13
        paddq   xmm2, xmm14
        paddq   xmm3, xmm15
;;logical or lowest bit
        por     xmm4, xmm8
        por     xmm5, xmm9
        por     xmm6, xmm10
        por     xmm7, xmm11
;;copy
        movdqa  xmm8, xmm0
        movdqa  xmm9, xmm1
        movdqa  xmm10, xmm2
        movdqa  xmm11, xmm3
;;Bit ROLL by prime numbers 7, 5, 3, 11
        psllq   xmm0, 7
        psllq   xmm1, 5
        psllq   xmm2, 3
        psllq   xmm3, 11
        psrlq   xmm8, 57;64-7
        psrlq   xmm9, 59;64-5
        psrlq   xmm10, 61;64-3
        psrlq   xmm11, 53;64-11
        por     xmm0, xmm8
        por     xmm1, xmm9
        por     xmm2, xmm10
        por     xmm3, xmm11
;;Dword order switching
        pshufd  xmm0, xmm0, 00011011b
        pshufd  xmm1, xmm1, 00011011b
        pshufd  xmm2, xmm2, 00011011b
        pshufd  xmm3, xmm3, 00011011b
;;Modify Key with rotation of dq words
        movdqa  [rcx+DQ1], xmm1
        movdqa  [rcx+DQ2], xmm2
        movdqa  [rcx+DQ3], xmm3
        movdqa  [rcx+DQ4], xmm0
        movdqa  [rcx+DQ5], xmm4
        movdqa  [rcx+DQ6], xmm5
        movdqa  [rcx+DQ7], xmm6
        movdqa  [rcx+DQ8], xmm7
;;prepare output
        paddb   xmm0, xmm4
        paddb   xmm1, xmm5
        paddb   xmm2, xmm6
        paddb   xmm3, xmm7
        pxor    xmm0, xmm7
        pxor    xmm1, xmm6
        pxor    xmm2, xmm5
        pxor    xmm3, xmm4
        paddb   xmm0, xmm5
        paddb   xmm1, xmm4
        paddb   xmm2, xmm7
        paddb   xmm3, xmm6
        pxor    xmm0, xmm6
        pxor    xmm1, xmm7
        pxor    xmm2, xmm4
        pxor    xmm3, xmm5
;;save output
        movdqa  [rdx+DQ1], xmm0
        movdqa  [rdx+DQ2], xmm1
        movdqa  [rdx+DQ3], xmm2
        movdqa  [rdx+DQ4], xmm3
;;restore xmm8-15 and return
        movdqa  xmm15, [rsp+DQ8]
        movdqa  xmm14, [rsp+DQ7]
        movdqa  xmm13, [rsp+DQ6]
        movdqa  xmm12, [rsp+DQ5]
        movdqa  xmm11, [rsp+DQ4]
        movdqa  xmm10, [rsp+DQ3]
        movdqa  xmm9, [rsp+DQ2]
        movdqa  xmm8, [rsp+DQ1]
        mov     rsp, rax
        ret     0 ;;return 
    
Post 25 Jul 2006, 21:51
View user's profile Send private message AIM Address Yahoo Messenger Reply with quote
kohlrak



Joined: 21 Jul 2006
Posts: 1421
Location: Uncle Sam's Pad
kohlrak
not sure how your program works (need to read the assembly docs i found). Lemme tell you how most generators work. Usually, they have a fancy aligoritham that uses the number of how long it took for a certain part of the program to be called after the program has started... or something like that... C++'s rand() had this problem... If you had a program that assigned a random number to a variable immediatly at the start of the program, it would be the same every time, despite it was supposed to be a random number generator. Basically, what i liked to do was "srand(GetTickCount())" after using user input. Then it would base the number on from how long the computer has been running. Basically, no matter how your program generates random number(s), it needs user input to make it even more random.
Post 26 Jul 2006, 05:57
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger Reply with quote
UCM



Joined: 25 Feb 2005
Posts: 285
Location: Canada
UCM
His function takes an input as well, which is the previous result, or for the first call the seed (GetTickCount, RDTSC, etc..)
Post 26 Jul 2006, 12:35
View user's profile Send private message Reply with quote
r22



Joined: 27 Dec 2004
Posts: 805
r22
As I suspected, it's practically perfect.
Post 30 Jul 2006, 01:05
View user's profile Send private message AIM Address Yahoo Messenger Reply with quote
mattst88



Joined: 12 May 2006
Posts: 260
Location: South Carolina
mattst88
kohlrak wrote:
not sure how your program works (need to read the assembly docs i found). Lemme tell you how most generators work. Usually, they have a fancy aligoritham that uses the number of how long it took for a certain part of the program to be called after the program has started... or something like that... C++'s rand() had this problem... If you had a program that assigned a random number to a variable immediatly at the start of the program, it would be the same every time, despite it was supposed to be a random number generator. Basically, what i liked to do was "srand(GetTickCount())" after using user input. Then it would base the number on from how long the computer has been running. Basically, no matter how your program generates random number(s), it needs user input to make it even more random.


Anyone who didn't know this, raise your hand.

You're preaching to the choir, man. Razz
Post 30 Jul 2006, 03:55
View user's profile Send private message Visit poster's website Reply with quote
r22



Joined: 27 Dec 2004
Posts: 805
r22
Is it me or did people reply WITHOUT even reading my post?
Post 01 Aug 2006, 00:12
View user's profile Send private message AIM Address Yahoo Messenger Reply with quote
mattst88



Joined: 12 May 2006
Posts: 260
Location: South Carolina
mattst88
It would seem you are correct. I'm not an assembly master, so I have to assume your code is better than what I would be able to write.
Post 01 Aug 2006, 00:44
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.