Question

In this problem you will implement an algorithm for computing all the square roots of a congruenc...

In this problem you will implement an algorithm for computing all the square roots of a congruence class in ℤ/nℤ, given a complete factorization of n into its distinct prime factor powers (assuming all the prime factors are in 3 + 4ℤ).

a) Implement a Python function sqrtsPrime(a, p) that takes two arguments: an integer a and a prime number p. You may assume that a and p are coprime. If p is not in 3 + 4ℤ or a has no square roots in ℤ/pℤ, the function should return None. Otherwise, it should return the two congruence classes in ℤ/pℤ that solve the following equation:

x2 ≡ a (mod p)

>>> sqrtsPrime(2, 7)

(3, 4)

>>> sqrtsPrime(5, 7) # 5 has no square roots

None

>>> sqrtsPrime(5, 17) # 17 mod 4 =/= 3

None

>>> sqrtsPrime(763472161, 5754853343)

(27631, 5754825712)

b) Implement a Python function sqrtsPrimePower(a, p, k) that takes three arguments: an integer a, a prime number p, and a positive integer k. You may assume that a and p are coprime. If p is not in 3 + 4ℤ or a has no square roots in ℤ/pkℤ, the function should return None. Otherwise, it should return the congruence classes in ℤ/pkℤ that solve the following equation:

x2 ≡ a (mod p2 )

>>> sqrtsPrimePower(2, 7, 2)

(10, 39)

>>> sqrtsPrimePower(763472161, 5754853343, 4)

(27631, 1096824245608362247285266960246506343570)

c) Implement a Python function equation: x2 ≡ a (mod n) sqrts(a, pks) that takes two arguments: an integer a and a list of tuples pks in which each tuple is a distinct positive prime number paired with a positive integer power. You may assume that a and n are coprime. You may assume that all the primes in pks are in 3 + ℤ/4ℤ (if any are not, the function should return None). Let n be the product of all the prime powers in the list pks. Then the function should return a set of all the distinct square roots of a in ℤ/nℤ that are solutions to the following equation:

x2 ≡ a (mod n)

Here is a helpedr function

def combinations(ls):

if len(ls) == 0:

return [[]]

else:

return [ [x]+l for x in ls[0] for l in combinations(ls[1:]) ]

0 0
Add a comment Improve this question Transcribed image text
Answer #1

'''

def sqrtsPrime(a,p):
   if (pow(p,1,4)==3):
w = floo
r((p+1)/4)
       q = pow(a,w,p)
       test = a * (w % p)
       thing1 = pow(q,2,p)
       ex1 = (2 % p)
       thing2 = pow(a,1,p)
       if ( thing1 == thing2):
           return (q,-q%p)
   return None

'''
def sqrtsPrimePower(a, p, k):
   if p%4 != 3:
       return None
   x = pow(a,((p + 1)//4), p)
   c = (inv(x, p) * inv(2, p) * ((a - pow(x,2))//pow(p,1)))%p
   y = (x + (c * pow(p,1)))
   #if not (pow(y,2,pow(p,k)) == a):
   #   return None
   return (y, pow(-y,1,pow(p,k)))
'''  
def sqrtsPrimePower(a, p, k):
   if (pow(p,1,4)==3):
       if (k > 1):
           (x1, x2) = sqrtsPrimePower(a, p, k-1)
       else:
           (x1, x2) = sqrtsPrime(a, p)
       if (x1>x2):
           (x1,x2) = (x2,x1)
       r = floor((a - pow(x1,2))/pow(p, k-1))
       c = pow(((inv(x1, p))*(inv(2, p))*r), 1, p)
       som = pow(p,k)
       lemma = x1 + c * pow(p, k-1)
       con = pow(lemma, 1, som)
       if (pow(con,2,som) == pow(a, 1, som)):
           return (con, pow(-con, 1, som))
return None   

Add a comment
Know the answer?
Add Answer to:
In this problem you will implement an algorithm for computing all the square roots of a congruenc...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • You may import the following library functions in your module: from fractions import gcd from math...

    You may import the following library functions in your module: from fractions import gcd from math import floor from random import randint You may also use: • the built-in pow() function to compute modular exponents efficiently (i.e., ak mod n can be written in Python as pow(a,k,n)), • the sum() function returns the sum of a list of integers (sum(1,2,3,4) returns 10). problem 1 a. Implement a function invPrime(a, p) that takes two integers a and p > 1 where...

  • Problem 1: Implement an algorithm to generate prime numbers. You will need to implement the following...

    Problem 1: Implement an algorithm to generate prime numbers. You will need to implement the following ingredients (some of them you developed for earlier assignments): 1. A method to generate random binary numbers with n-digits (hint: for the most significant digit, you have no choice, it will be 1; similarly, for the least significant digit there is no choice, it will have to be 1; for all other position, generate 0 or 1 at random) 2. A method to compute...

  • Implement the following Python functions. These functions take advantage of the generalized Euclid's lemma to make...

    Implement the following Python functions. These functions take advantage of the generalized Euclid's lemma to make it possible to generate a random number within a specified range. Your implementations must be extremely efficient, and must handle very large inputs, as shown in the examples below. Implementations that perform exhaustive, exponential-time searches will receive no credit. a. Implement a function closest(t, ks) that takes two arguments: a target integer t and a list of integers ks. The function should return the...

  • You may import the following library functions in your module: from fractions import gcd from math...

    You may import the following library functions in your module: from fractions import gcd from math import log from math import floor You may also use: • the .bit_length() method to efficiently obtain the bit length of an integer, • the abs() function for computing the absolute value of an integer, • and the // operator for integer division (you should avoid using / because it does not work for very large integers). Implement the following Python functions. These functions...

  • Write a program in Python implement the RSA algorithm for cryptography. Set up: 1.Choose two large...

    Write a program in Python implement the RSA algorithm for cryptography. Set up: 1.Choose two large primes, p and q. (There are a number of sites on-line where you can find large primes.) 2.Compute n = p * q, and Φ = (p-1)(q-1). 3.Select an integer e, with 1 < e < Φ , gcd(e, Φ) = 1. 4.Compute the integer d, 1 < d < Φ such that ed ≡ 1 (mod Φ). The numbers e and d are...

  • Using Python 3.7 and higher, implement function egypt(numerator, denominator) that uses the greedy strategy to determine...

    Using Python 3.7 and higher, implement function egypt(numerator, denominator) that uses the greedy strategy to determine a set of distinct Egyptian fractions that sum to numerator/denominator. You cannot use any sort of division, including the floor and ceiling methods, nor the division, remainder, or modulus operators. 1. Implement function egypt(numerator, denominator) that uses the greedy strategy presented in slides 7 and 8 of Lecture 30 (April 13) to determine a set of distinct (i.e. all different) Egyptian fractions that sum...

  • python 2..fundamentals of python 1.Package Newton’s method for approximating square roots (Case Study 3.6) in a...

    python 2..fundamentals of python 1.Package Newton’s method for approximating square roots (Case Study 3.6) in a function named newton. This function expects the input number as an argument and returns the estimate of its square root. The script should also include a main function that allows the user to compute square roots of inputs until she presses the enter/return key. 2.Convert Newton’s method for approximating square roots in Project 1 to a recursive function named newton. (Hint: The estimate of...

  • This assignment requires you to write a program to implement an odd-order Magic Square. Prompt the...

    This assignment requires you to write a program to implement an odd-order Magic Square. Prompt the user to enter the “order” of the square and then produce for the user a magic square of that order. Run your code for at least four different inputs – all odd. ODD ORDER MAGIC SQUARES 1. Start by placing 1 in the middle column of the top row. 2. Continue by always placing the next number (say k+1) in the square diagonally up...

  • The code should be written with python. Question 1: Computing Polynomials [35 marks A polynomial is...

    The code should be written with python. Question 1: Computing Polynomials [35 marks A polynomial is a mathematical expression that can be built using constants and variables by means of addition, multiplication and exponentiation to a non-negative integer power. While there can be complex polynomials with multiple variable, in this exercise we limit out scope to polynomials with a single variable. The variable of a polynomial can be substituted by any values and the mapping that is associated with the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT