Created
January 12, 2018 02:35
-
-
Save iancoleman/1a4635c1156759049fd3df903dfc681d to your computer and use it in GitHub Desktop.
number of tries to generate a specific key with a specific probability
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Prints out a table of the number of tries to generate a specific key with a | |
# specific probability | |
# | |
# More info at https://forum.safedev.org/t/secure-random-relocation/1321/41 | |
# | |
# It's messy but the maths is correct. | |
import math | |
def tidyNum(n): | |
if n < 1000: | |
return str(n) | |
if n < 1000000: | |
return str(round(n/1000)) + "K" | |
if n < 1000000000: | |
return str(round(n/1000000)) + "M" | |
return str(round(n/1000000000)) + "B" | |
# Initiate the results | |
probabilities = [0.1, 0.5, 0.9, 0.99, 0.9999] | |
probabilityHeading = [""] | |
probabilityHeading.extend(probabilities) | |
probabilityHeading.extend(["", ""]) | |
rows = [ | |
probabilityHeading, | |
["Prefix Length", "", "", "", "", "", "Sections", "Vaults"], | |
] | |
# calculate the values | |
for prefixLength in range(3,41): | |
row = [prefixLength] | |
for probability in probabilities: | |
totalGuesses = round(math.log(1-probability) / math.log(1-1/(2**prefixLength))) | |
tidyGuesses = tidyNum(totalGuesses) | |
row.append(tidyGuesses) | |
numSections = 2**prefixLength | |
numVaults = numSections * 50 | |
row.extend([tidyNum(numSections), tidyNum(numVaults)]) | |
rows.append(row) | |
# print the output to stdout | |
rowFmt = "{:>13} | {:>8} {:>8} {:>6} {:>6} {:>6} | {:>6} {:>6}" | |
print("Number of guesses to find a key matching a prefix\n") | |
print(" "*13 + " | probability of generating a valid key |") | |
for row in rows: | |
print(rowFmt.format(*row)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment