Contents
- Description
- Assets
- Notes
- Recipe
- Examples
Description
This recipe describes a method of using dice to generate entropy bytes.
Assets
Asset: A script that converts dice roll results into bytes.
convert_dice_rolls_to_hex_bytes_3.py [paywalled]
Notes
Using this recipe to generate 32 bytes of entropy (enough for a Bitcoin private key) should take about 10 minutes.
Suggestions:
- Roll the dice on a flat tray with raised edges to stop them from scattering out of reach. Add a dust cloth on top of the tray to muffle the sound of the dice rolls.
- After a roll, gather the dice into a line to make it easier to read the results and type them into a text file.
- Listen to music to make the dice rolling a more tolerable experience.
5 dice is just a convenient number of dice to roll repeatedly. This recipe will still work with a different number of dice.
Some dice roll result values will be discarded so that the value domain becomes base4, which can be then be converted to base16 (i.e. hex bytes). About 66% of the entropy will be preserved.
Previous version of this recipe:
Recipe for generating entropy bytes using dice
This recipe was originally published in the article Generating entropy with dice. Please read the linked article for more information about the development of the recipe, the algorithm behind it, its disadvantages, and suggestions for further work.
The code used in this recipe has been run in Python 2.7.5 on CentOS 7.6. It should run successfully in Python 2.7.x.
Recipe
1) Download the script convert_dice_rolls_to_hex_bytes_3.py. It is linked in the Assets section of this article.
2) Obtain 5 dice.
3) Choose a desired number of bytes of entropy. Multiply this number by 8 to convert to bits. Divide the result by the expected-bit-rate-per-dice-roll value (1.3333) to find the expected number of dice rolls that should generate this number of entropy bits. Multiply this result by 1.1 to add a 10% margin, then round up to the nearest integer.
Example: To get 32 bytes of entropy, you should do 212 dice rolls.
Calculations:
32 * 8 = 256
256 / 1.3333 ~= 192
192 * 1.1 ~= 211.2
Round up to 212
256 / 1.3333 ~= 192
192 * 1.1 ~= 211.2
Round up to 212
4) Perform this number of dice rolls and record the results in a text file. Save this file as "dice_rolls.txt" in the same directory as the script convert_dice_rolls_to_hex_bytes_3.py.
An individual dice roll must be recorded as one individual character from the list "123456". Whitespace (newline, tab, space) can be used to separate groups of dice roll values.
Easiest approach: Roll 5 dice at a time, record 5 dice rolls per line, and separate groups of 5 lines with an extra newline. Each group will then consist of 25 dice rolls, making it straightforward to count the total number of dice rolls.
Example group of 25 dice rolls in a text file:
12224
32455
43512
63552
22164
32455
43512
63552
22164
5) In the script convert_dice_rolls_to_hex_bytes_3.py, scroll to the section of text that lies between
##### START CONTROLS
and
##### END CONTROLS
- Set the variable
desired_n
to be the desired number of bytes of entropy. By default it is 32.- Set the variable
dice_rolls_file_path
to be the path to the text file containing the dice roll results. By default it is "dice_rolls.txt".6) Open a terminal. Change directory to the directory containing this script. Run the script using the following command:
python convert_dice_rolls_to_hex_bytes_3.py
7) The output of the script should contain the generated entropy as hex bytes.
8) (optional) If more dice rolls are needed in order to reach the desired number of bytes of entropy, the script will calculate a suggested number of new dice rolls. Perform the new dice rolls, add the results to the dice roll results text file, and run the script again.
Examples
Here are 212 dice rolls, stored in a file.
dice_rolls.txt
12224
32455
43512
63552
22164
14133
41133
33313
32336
61214
24161
64534
43126
65454
13563
25466
26462
61651
25156
45236
33631
13365
22211
42146
15162
61523
56355
65313
43624
32555
33364
26155
52361
11545
66244
15162
64235
63343
21244
12313
32633
42546
46
Next, we run the script:
[spiano@shovel work]$ python convert_dice_rolls_to_hex_bytes_3.py
### START CONVERSION OF DICE ROLLS TO HEX BYTES
- number of dice rolls: 212
- number of dice rolls in the list [1234]: 144
- number of hex characters after conversion: 72
- number of hex bytes: 36
- desired number of hex bytes: 32
- the hex bytes produced are sufficient.
- hex byte output:
15e7e195332b0aa8a684dc3be1f29dd04daa0a5434c11a8b9e6ad180df076ae47c629adf
- hex byte output shortened to the desired length (32 bytes):
15e7e195332b0aa8a684dc3be1f29dd04daa0a5434c11a8b9e6ad180df076ae4
- remaining hex bytes:
7c629adf
Result: Desired amount of entropy (32 bytes) has been produced.
Entropy (32 bytes):
15e7e195332b0aa8a684dc3be1f29dd04daa0a5434c11a8b9e6ad180df076ae4
Recommendation: Perhaps preserve the 4 extra hex bytes in an entropy storage file.
Extra hex bytes: 7c629adf
### END CONVERSION OF DICE ROLLS TO HEX BYTES
The desired result (32 bytes of entropy) is:
15e7e195332b0aa8a684dc3be1f29dd04daa0a5434c11a8b9e6ad180df076ae4
If we delete the last 5 groups of rolls, and keep only the first 4 (i.e. 100 rolls in total), and run the script again, we see:
[spiano@shovel work]$ python convert_dice_rolls_to_hex_bytes_3.py
### START CONVERSION OF DICE ROLLS TO HEX BYTES
- number of dice rolls: 100
- number of dice rolls in the list [1234]: 69
- there is one extra base4 dice roll that can't be used for conversion to base16.
- number of hex characters after conversion: 34
- number of hex bytes: 17
- desired number of hex bytes: 32
- the hex bytes produced are NOT sufficient.
- hex byte output:
15e7e195332b0aa8a684dc3be1f29dd04d
- 15 hex bytes are still needed.
- expected entropy byte rate per dice roll: 0.1667
- expected necessary number of new dice rolls: 91
- expected necessary number of new dice rolls plus a 10% margin: 101
Result: Desired amount of entropy (32 bytes) has NOT been produced.
Entropy (17 bytes):
15e7e195332b0aa8a684dc3be1f29dd04d
Recommendation: Perform 101 new dice rolls and add the results to the dice rolls data file, then run this script again.
### END CONVERSION OF DICE ROLLS TO HEX BYTES