pyb.rng() why 30 bits?

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
Post Reply
manitou
Posts: 73
Joined: Wed Feb 25, 2015 12:15 am

pyb.rng() why 30 bits?

Post by manitou » Sat Mar 07, 2015 11:53 am

why does pyb.rng() provide only 30 bits of the 32-bit hardware random number generator?

hal rng.c shifts off lower 2 bits HAL_RNG_GetRandomNumber(&RNGHandle) >> 2

This app note on NIST tests of STM32Fxx random number generator doesn't report any problems?
http://www.st.com/web/en/resource/techn ... 073853.pdf

inquiring minds want to know ...

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: pyb.rng() why 30 bits?

Post by dhylands » Sat Mar 07, 2015 7:37 pm

If I had to guess I would say that it because at the time the rng code was written, MicroPython probably only supported small ints, which are 31-bit signed, or 30-bit unsigned numbers.

Now that there is multi-precision integer support, rng can probably be reworked to support the full 32-bits.

Damien
Site Admin
Posts: 647
Joined: Mon Dec 09, 2013 5:02 pm

Post by Damien » Sun Mar 08, 2015 8:13 am

Yes it's for efficiency: 30 bits gives you a positive integer that doesn't require allocating memory.

You can also use the standard os.urandom which returns a bytes object made using the hardware rng.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: pyb.rng() why 30 bits?

Post by pythoncoder » Sun Mar 15, 2015 9:17 am

Perhaps it should stay at 30 bits to ensure it can be called from within an interrupt handler.
Peter Hinch
Index to my micropython libraries.

Post Reply