RandomRDRand.st
author Claus Gittinger <cg@exept.de>
Fri, 02 Mar 2018 14:15:56 +0100
changeset 4598 409af0c80e09
parent 4595 39b2f30679c0
child 4667 35e1ebb8020e
permissions -rw-r--r--
#REFACTORING by cg class: RandomRDRand changed: #nextInteger class: RandomRDRand class comment/format in: #documentation #new #new:

"{ Encoding: utf8 }"

"
 COPYRIGHT (c) 2014 Claus Gittinger
              All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"
"{ Package: 'stx:libbasic2' }"

"{ NameSpace: Smalltalk }"

Object subclass:#RandomRDRand
	instanceVariableNames:'x y z c'
	classVariableNames:''
	poolDictionaries:''
	category:'Magnitude-Numbers-Random'
!

!RandomRDRand class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 2014 Claus Gittinger
              All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"
!

documentation
"
    This generator uses the rdgen random generator which is built into modern intel chips.
    Before using, you should check via the isSupported query.

    Warning: 
        there have been discussions about the security of the intel rdgen instruction
        and whether there are NSA backdoors built into it.
        Linus Torwalds refuses to use it for /dev/urandom in the linux kernel, for that very reason.
        Be sure you know what you are doing, if you use this generator for sensitive cryptographic stuff.
        We recommend using one of the libcrypt-based generators and use this only to get additional
        entropy for the seed.

    NO WARRANTY

    RandomRDGen new nextInteger

    [see also:]
        RandomGenerator - the default; uses the machine's /dev/random if available
        Random  - fast, but generates less quality random numbers
        RandomTT800 - another random generator
        RandomParkMiller - another random generator
        RandomMT19937 - another random generator
        RandomKISS - another random generator
        exept:libcrypt - a library containing more stuff based on hashes and cyphers

    [author:]
        Claus Gittinger.
"
! !

!RandomRDRand class methodsFor:'instance creation'!

new
    self isSupported ifFalse:[ self error:'this generator needs a cpu with rdgen instruction' ].
    ^ self basicNew initialize

    "
     self new nextInteger
    "
!

new:seed
    "seed is actually ignored"

    self isSupported ifFalse:[ self error:'this generator needs a cpu with rdgen instruction' ].
    ^ self basicNew initialize; seed:seed
! !

!RandomRDRand class methodsFor:'queries'!

isSupported
    "true if this architecture supports hardware random numbers"

    ^ OperatingSystem getCPUType = 'x86_64'
        and:[ (OperatingSystem getSystemInfo at:#extendedInstructions ifAbsent:#())
              includes:#aes ]

    "
     self isSupported
    "
! !

!RandomRDRand methodsFor:'initialization'!

initialize
!

seed:seed
    "/ ignored
! !

!RandomRDRand methodsFor:'random numbers'!

nextBoolean
    "generates a boolean random"

    ^ self nextInteger > 16r7FFFFFFF
!

nextInteger
    "generates the next integer in 0..FFFFFFFF.
     Notice, it may raise an illegal instruction exception on some cpu chips,
     even though the cpuid instruction says that it is available"

%{
    unsigned INT r = 0;
    int cf;

    do {
#ifdef USE_DRAND64
        cf = _rdrand64_step(&r);
#else
# ifdef USE_DRAND32
        cf = _rdrand32_step(&r);
# else
#  if defined(__x86__) && defined(__GNUC__) && (__GNUC__ >= 2)
        // Encoding of rdrand %eax
        asm(".byte 0x0F, 0xC7, 0xF0; adcl $0,%1" 
            : "=a" (r), "=r" (cf) 
            : "0" (r), "1" (cf) 
            : "cc");

#  else
        goto unsupported;
#  endif
# endif
#endif
    } while (cf != 0);
    RETURN (__MKUINT(r));

unsupported: ;
%}.
    self primitiveFailed:'unsupported on this architecture'

    "
     self new nextInteger
    "
! !

!RandomRDRand class methodsFor:'documentation'!

version
    ^ '$Header$'
!

version_CVS
    ^ '$Header$'
! !