Results 1 to 8 of 8

Thread: Hardware Random Number Generator - ubld.it TrueRNG v3

  1. #1

    Thread Starter
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Hardware Random Number Generator - ubld.it TrueRNG v3

    Several weeks ago I purchased a Hardware Random Number Generator from ubld.it. Since then I have been working on a class that implements the device. This class should work on any serial port device that sends a steady stream of random numbers. To get the device to work I had to install this driver, http://ubld.it/wp-content/uploads/20...ws-Driver1.zip

    If you don't have a device you can still use the random methods by instantiating the class with a buffer of random bytes.

    Code:
                Private WithEvents TRNG As RandomTrueRNG
    
                Dim PRNG As New Random
                Dim byts((1024 * 1024 * 20) - 1) As Byte
                PRNG.NextBytes(byts)
                TRNG = New RandomTrueRNG(byts)
    Before I had the device I used this to begin development. I modeled the Random methods on System.Random and added several of my own, like Next.Date.

    Here is the class in a text file, as a link to my Google Drive. The class.

    Let me know if you have a problem with it.

    I have a demo app that I will post later. This is what it looks like.

    Name:  TrueRNG.jpg
Views: 581
Size:  49.0 KB

    Enjoy.
    Last edited by dbasnett; Jan 1st, 2024 at 08:18 AM.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  2. #2
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    6,187

    Re: Hardware Random Number Generator - ubld.it TrueRNG v3

    At 50KB/s input this would be dog slow for real-time operations.

    The idea is that you can to use such h/w device RNG just to seed a PRF. For instance a 128-bit seed/key of SHA-3 based SHAKE or BLAKE3 or anything else can produce GBs of random data instantly.

    cheers,
    </wqw>

  3. #3

    Thread Starter
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Hardware Random Number Generator - ubld.it TrueRNG v3

    Quote Originally Posted by wqweto View Post
    At 50KB/s input this would be dog slow for real-time operations.

    The idea is that you can to use such h/w device RNG just to seed a PRF. For instance a 128-bit seed/key of SHA-3 based SHAKE or BLAKE3 or anything else can produce GBs of random data instantly.

    cheers,
    </wqw>
    Yes, not fast. 12K integers per second is slow compared to software.

    The class could be used to do exactly what you said.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  4. #4

    Thread Starter
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Hardware Random Number Generator - ubld.it TrueRNG v3

    Quote Originally Posted by wqweto View Post
    At 50KB/s input this would be dog slow for real-time operations.

    The idea is that you can to use such h/w device RNG just to seed a PRF. For instance a 128-bit seed/key of SHA-3 based SHAKE or BLAKE3 or anything else can produce GBs of random data instantly.

    cheers,
    </wqw>
    I am thinking about including pseudo Random Number Generators(PRNG) in the class and seeding them from the TRNG to help with performance.

    I have found these two but don't know if they are correct or any good. Any help is appreciated.

    This is supposed to be XorPlus128...

    Code:
        Public Class Xoroshiro128Plus
            Private _lock As New Threading.AutoResetEvent(True)
            Private State(1) As UInt64
            Const HObit As UInt64 = 1UL << 63
    
            Protected Friend Sub New(seed As UInt64)
                Me.DoSeed(seed)
            End Sub
    
            Protected Friend Sub DoSeed(seed As UInt64)
                Me._lock.WaitOne()
                State(0) = seed
                State(1) = AddTwoULongs(seed, &H9E3779B97F4A7C15UL)
                Me._lock.Set()
            End Sub
    
            Private Shared Function AddTwoULongs(U1 As UInt64, U2 As UInt64) As UInt64
                Dim rv As ULong = 0UL
                Try
                    rv = U1 + U2
                Catch ex As OverflowException
                    Dim U1L As Long = 0UL
                    Dim U2L As Long = 0UL
                    If (U1 And HObit) = HObit Then
                        U1L = CLng(U1 Xor HObit) And Long.MaxValue
                    Else
                        U1L = CLng(U1) And Long.MaxValue
                    End If
                    If (U2 And HObit) = HObit Then
                        U2L = CLng(U2 Xor HObit) And Long.MaxValue
                    Else
                        U2L = CLng(U2) And Long.MaxValue
                    End If
                    rv = CULng(U1L) + CULng(U2L)
                    If (U1 And HObit) = HObit Then
                        rv = rv Or HObit
                    End If
                    If (U2 And HObit) = HObit Then
                        rv = rv And HObit
                    End If
                End Try
                Return rv
            End Function
    
            Public Function NextUInt64() As ULong
                Me._lock.WaitOne()
                Dim s0 As ULong = State(0)
                Dim s1 As ULong = State(1)
                NextUInt64 = AddTwoULongs(s0, s1)
                s1 = s1 Xor s0
                State(0) = ((s0 << 24) Or (s0 >> 40))
                State(0) = State(0) Xor s1 Xor (s1 << 16)
                State(1) = ((s1 << 37) Or (s1 >> 27))
                Me._lock.Set()
            End Function
    
            Public Function NextInt64() As Int64
                Dim rv As Int64
                Dim U1 As UInt64 = Me.NextUInt64
                If (U1 And HObit) = HObit Then
                    rv = CLng(U1 Xor HObit) And Long.MaxValue
                Else
                    rv = CLng(U1) And Long.MaxValue
                End If
                Return rv
            End Function
    
            Public Function NextInt32() As Int32
                Dim rv As Int32
                Dim U1 As Int64 = Me.NextInt64
                rv = CInt(U1 And Int32.MaxValue)
                Return rv
            End Function
    
            Public Function NextInt32(minValue As Int32,
                                       maxValue As Int32) As Int32
                Dim rv As Int32 = 0
                If minValue >= maxValue Then
                    Throw New ArgumentOutOfRangeException("Min >= Max")
                End If
                Dim range As Integer = (maxValue - minValue)
                Dim smpl As Double = Me.IntSample()
                Dim s1 As Double = smpl
                s1 = s1 * range
                rv = CInt(Math.Truncate(s1)) + minValue
                If rv < minValue OrElse rv >= maxValue Then
                    Stop
                End If
                Return rv
            End Function
    
            Public Function NextDouble() As Double
                Const UINT64_TO_DOUBLE_MULTIPLIER As Double = 1.0# / ULong.MaxValue '  1.8446744073709552E+19 ' 2^64
    
                Return NextUInt64() * UINT64_TO_DOUBLE_MULTIPLIER
            End Function
    
            Private Function IntSample() As Double
                Const DIV As UInteger = CUInt(Integer.MaxValue) + 1
                Const MBIG As Double = 1.0# / DIV
                Dim rv As Double = 0.0#
                Dim I1 As Integer = Me.NextInt32
                rv = I1 * MBIG
                Return rv
            End Function
    
        End Class
    This is a Mersenne Twister...maybe???

    Code:
        ''' <summary>
        ''' from https://www.panopticoncentral.net/2006/09/22/pseudo-random-numbers-vb-and-doing-the-mersenne-twist/
        ''' </summary>
        ''' <remarks></remarks>
        Public Class MersenneTwister
            Private Const N As Integer = 624
            Private Const M As Integer = 397
            Private Const MATRIX_A As UInteger = &H9908B0DFUI
            Private Const UPPER_MASK As UInteger = &H80000000UI
            Private Const LOWER_MASK As UInteger = &H7FFFFFFFUI
    
            Private mt(N - 1) As UInteger
            Private mti As Integer = N + 1
    
            Protected Friend Sub New(seed As UInteger)
                Me.mt(0) = seed
                For Me.mti = 1 To N - 1
                    Me.mt(Me.mti) = CUInt((1812433253UL * (Me.mt(Me.mti - 1) Xor (Me.mt(Me.mti - 1) >> 30)) + CUInt(Me.mti)) And &HFFFFFFFFUL)
                Next
            End Sub
    
            Public Function GenerateUInt32() As UInteger
                Dim y As UInteger
                Static mag01() As UInteger = {&H0UI, MATRIX_A}
    
                If Me.mti >= N Then
                    Dim kk As Integer
    
                    For kk = 0 To N - M - 1
                        y = (Me.mt(kk) And UPPER_MASK) Or (Me.mt(kk + 1) And LOWER_MASK)
                        Me.mt(kk) = Me.mt(kk + M) Xor (y >> 1) Xor mag01(CInt(y And &H1))
                    Next
    
                    For kk = kk To N - 2
                        y = (Me.mt(kk) And UPPER_MASK) Or (Me.mt(kk + 1) And LOWER_MASK)
                        Me.mt(kk) = Me.mt(kk + (M - N)) Xor (y >> 1) Xor mag01(CInt(y And &H1))
                    Next
    
                    y = (Me.mt(N - 1) And UPPER_MASK) Or (Me.mt(0) And LOWER_MASK)
                    Me.mt(N - 1) = Me.mt(M - 1) Xor (y >> 1) Xor mag01(CInt(y And &H1))
    
                    Me.mti = 0
                End If
    
                y = Me.mt(mti)
                Me.mti += 1
    
                ' Tempering
                y = y Xor (y >> 11)
                y = y Xor ((y << 7) And &H9D2C5680UI)
                y = y Xor ((y << 15) And &HEFC60000UI)
                y = y Xor (y >> 18)
    
                Return y
            End Function
    
            Public Function GenerateInt32() As Integer
                Return CInt(Me.GenerateUInt32() >> 1)
            End Function
    
            Public Function GenerateInt32(maxValue As Integer) As Integer
                Return Me.GenerateInt32(0, maxValue)
            End Function
    
            Public Function GenerateInt32(minValue As Integer, maxValue As Integer) As Integer
                Return CInt(Math.Floor((maxValue - minValue + 1) * Me.GenerateDouble() + minValue))
            End Function
    
            Public Function GenerateDouble() As Double
                Return Me.GenerateUInt32() * (1.0# / UInt32.MaxValue)
            End Function
        End Class
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  5. #5
    New Member
    Join Date
    Feb 2024
    Posts
    1

    Re: Hardware Random Number Generator - ubld.it TrueRNG v3

    Thank you for sharing your work. It has saved me a lot of preparatory work that I can now use for the topic itself. I've been working for over a decade on random generators and more natural ways to make the distribution visible and understandable. The topic of security does not interest me in this context. It's more a general, perhaps philosophical interest in randomness and the fact that it's almost impossible to replicate "natural" randomness on computers with highly accurate clocks. I think mathematical algorithms are totally the wrong approach. The best idea in this context is natural noise and electronic solutions that mostly come from analog electronics. It's not for nothing that Random.Org earns good money with this solution. A few years ago you could generate unlimited numbers there as a private user and I wrote applications that used this.

    BTW: The speed is ok and sufficient for me and my applications.

    Actually, I only registered here to thank you. So thanks again.

  6. #6

    Thread Starter
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Hardware Random Number Generator - ubld.it TrueRNG v3

    Thanks for the kind words.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: Hardware Random Number Generator - ubld.it TrueRNG v3

    Well, that was a random post.
    My usual boring signature: Nothing

  8. #8

    Thread Starter
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Hardware Random Number Generator - ubld.it TrueRNG v3

    Quote Originally Posted by Shaggy Hiker View Post
    Well, that was a random post.
    Yes it was. Better than if it had gone the other way.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width