Answered step by step
Verified Expert Solution
Question
1 Approved Answer
* Install the Python library ` pycryptodome ` by executing the following in a code cell. ` ` ` ! pip install pycryptodome ` `
Install the Python library pycryptodome by executing the following in a code cell.
pip install pycryptodome
Import some functions we will use by executing the following code. This will raise an error if pycryptodome hasn't been successfully installed.
from Crypto.Util.number import getPrime, getRandomNBitInteger
from sympyntheory import norder
from LabHelper import
onlyletters,
weave,
shiftstring,
addspaces,
tobaseb
frombaseb
def naivedlogg h p:
output
for i in rangep:
output outputgp
if output h:
return i
return None
import string
import textwrap
def stringforcodeblockX linewidth:
return
jointextwrapwrapX widthlinewidth
def addspacesX width linewidth:
oneline jointextwrapwrapX widthwidth
manylines stringforcodeblockoneline, linewidthlinewidth
return manylines
def onlylettersX caseNone:
Returns the string obtained from X by removing everything but the letters.
If case"upper" or case"lower", then the letters are all
converted to the same case.
X joinc for c in X if c in string.asciiletters
if lenX:
return None
if case is None:
return X
elif case "lower":
return Xlower
elif case "upper":
return Xupper
def shiftcharch shiftamt:
Shifts a specific character by shiftamt.
Example:
shiftcharY returns B
if ch in string.asciilowercase:
base a
elif ch in string.asciiuppercase:
base A
# It's not clear what shifting should mean in other cases
# so if the character is not upper or lowercase, we leave it unchanged
else:
return ch
return chrordchordbaseshiftamtordbase
def shiftstringX shiftamt:
Shifts all characters in X by the same amount.
return joinshiftcharch shiftamt for ch in X
def weavestringlist:
output joinjointup for tup in zipstringlist
# The rest is just to deal with the case of unequal string lengths
# We assume the only possibility is that the early strings are one character longer
lastlength lenstringlist
extra s for s in stringlist if lens lastlength
return output joinextra
def tobasebnum base:
Returns the digits of num when written in base base
Adapted from code on Stack Overflow.
digits
while num :
num, rem divmodnum base
digits.appendrem
return digits::
def frombasebdigitlist, base:
Converts from a list of digits in base base to an integer.
return sumdbasei for i d in enumeratedigitlist::
Choose a prime p using the getPrime function. See the next few instructions to help you decide how large the prime should be
Find a generator g for $mathbbZp$Hint Use the norder function that we imported from SymPy Call helpnorder to see how this function works. What order do we need to have a generator? You will have to do some guessingandchecking or better, use a for loop, calling break when you've found a generator but it shouldn't take many guesses before you find a generator.
Call naivedlogg p and time how long it takes using time at the top of the cell. We already know that no discrete logarithm exists in this case why so the point of this is just to see how long the naive discrete logarithm takes to run in the worstcase scenario.
We want a prime for which this naive discrete logarithm calculation takes seconds. If your prime takes an amount of time outside of this range, then change the values of p and g above until you find values for which it takes seconds.
Again using the getPrime function, find a prime p which is bits larger add don't multiply than the prime p you found above.
As above, find a generator g for $mathbbZp$
We estimate that multiplying p by M will multiply the amount of time the naive discrete logarithm takes by M Using this estimate, how many days would you expect the naive discrete logarithm to take for pYour answer should depend on pp and on the number of seconds given by your timing computation above. Do not try to run the naive discrete logarithm function on p directly. Be sure your answer is in days, not in seconds.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started