Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
31 views

Original Code

# function to generate random numbers between j<k def random_between(j,k) : a=int(random()*(k-j+1))+j return a # function to generate 1024-bit sophie germain prime def SGP : boolean = false while boolean = false repeat p = random_between(2**1023, 2**1024-1) boolean = is 2p+1 in Primes() return p # Working in Fp p = SGP G1 = FP(p) g = G1.gen() ︠de8b2318-c69e-4e91-987c-34d7ce7e39bci︠ %md Comments: function random_between: 1) Use ZZ instead of int. It'll treat the number as an integer class and allow you to call methods on it. 2) random finds a number in the range [0.0,1.0). Try ZZ.random_element(lower_bound,upper_bound) instead to get an integer in the range [lower_bound, upper_bound) function SGP: 1) You have forgotten the parentheses! SGP() =) 2) Sage true and false are True and False (note the capitalization). Not essential, but a good habit. 3) Instead of repeat, the while struct has the following format: while condition: #Do stuff #program continues after while loop 4) random_between should be ZZ.random, as in above point 5) no need for is. Remember to multiply! 2*p is a computation. 2p is not valid syntax. 6) x in Primes() is valid, but if you have an element of ZZ you can simply call the method num.is_prime() to get the same result

Comments: function random_between:

  1. Use ZZ instead of int. It'll treat the number as an integer class and allow you to call methods on it.

  2. random finds a number in the range [0.0,1.0). Try ZZ.random_element(lower_bound,upper_bound) instead to get an integer in the range [lower_bound, upper_bound)

function SGP:

  1. You have forgotten the parentheses! SGP() =)

  2. Sage true and false are True and False (note the capitalization). Not essential, but a good habit.

  3. Instead of repeat, the while struct has the following format:

while condition: #Do stuff #program continues after while loop

  1. random_between should be ZZ.random, as in above point

  2. no need for is. Remember to multiply! 2*p is a computation. 2p is not valid syntax.

  3. x in Primes() is valid, but if you have an element of ZZ you can simply call the method num.is_prime() to get the same result