Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better random number generation from RSSI #150

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

unprovable
Copy link

Hello,
The API call LoRa.random() gave users really bad random numbers. They need some level of whitening in order to be useful, so I've added some rather dirty (sorry!) code to implement a von Neumann Extractor. It's normally implemented in three functions, but I didn't want to bloat the class - feel free to edit it to make it more stylistic if you like :) See here for more information: https://en.wikipedia.org/wiki/Randomness_extractor#Von_Neumann_extractor

I tested the code that was originally there, and the random numbers were disappointing - I can send samples if you would like. This code will generate better random numbers. I also added a warning that these are not cryptographically secure, as I haven't proven that they are otherwise yet. :P

The speed is slightly slower, but very fast in the grander scheme of things (other entropy pools take a long time to generate entropy). I'm happy to answer any questions.

Many thanks! M.

@sergio303
Copy link
Contributor

sergio303 commented May 28, 2018

FYI, here is Semtech implementation on SX1276 driver (no Von Neumann Extractor, but maybe the 1ms delay between samples is useful):

uint32_t SX1276Random( void )
{
    uint8_t i;
    uint32_t rnd = 0;

    /*
     * Radio setup for random number generation
     */
    // Set LoRa modem ON
    SX1276SetModem( MODEM_LORA );

    // Disable LoRa modem interrupts
    SX1276Write( REG_LR_IRQFLAGSMASK, RFLR_IRQFLAGS_RXTIMEOUT |
                  RFLR_IRQFLAGS_RXDONE |
                  RFLR_IRQFLAGS_PAYLOADCRCERROR |
                  RFLR_IRQFLAGS_VALIDHEADER |
                  RFLR_IRQFLAGS_TXDONE |
                  RFLR_IRQFLAGS_CADDONE |
                  RFLR_IRQFLAGS_FHSSCHANGEDCHANNEL |
                  RFLR_IRQFLAGS_CADDETECTED );

    // Set radio in continuous reception
    SX1276SetOpMode( RF_OPMODE_RECEIVER );

    for( i = 0; i < 32; i++ )
    {
        DelayMs( 1 );
        // Unfiltered RSSI value reading. Only takes the LSB value
        rnd |= ( ( uint32_t )SX1276Read( REG_LR_RSSIWIDEBAND ) & 0x01 ) << i;
    }

    SX1276SetSleep( );

    return rnd;
}

@unprovable
Copy link
Author

Aye - the extractor just helps to normalise the probabilities (P(0)=P(1)=0.5 under this scheme of output). It's not critical, but it's recommended to do some sort of whitening of this kind. :) Many thanks! M.

@morganrallen
Copy link
Collaborator

In testing this PR it just hangs for me. Presumably a != read... is never evaluating true

@sandeepmistry
Copy link
Owner

Any updates on the hanging?

@ricaun
Copy link
Contributor

ricaun commented Oct 3, 2018

I'm messing with random this weekend and I found https://gist.github.com/endolith/2568571
I have tested some codes and get great results using left rotate and XOR.
Check #188

@wero1414
Copy link
Collaborator

wero1414 commented Sep 7, 2020

There is another PR #395 where is added the seed necessary to have better random numbers

@wero1414 wero1414 mentioned this pull request Sep 7, 2020
@plybrd plybrd mentioned this pull request Aug 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants