Find Square Root Under Modulo p Using Shanks-Tonelli Algorithm in C++



In this problem, we are given two values n and a prime number p. Our task is to find Square Root under Modulo p.

Let's take an example to understand the problem,

Input : n = 4, p = 11
Output : 9

Solution Approach

Here, we will be using Tonelli-Shanks Algorithm.

Tonelli-Shanks Algorithm is used in modular arithmetic to solve for a value x in congruence of the form x2 = n (mod p).

The algorithm to find square root modulo using shank's Tonelli Algorithm −

Step 1 − Find the value of (n((p1)/2))(modp), if its value is p -1, then modular square root is not possible.

Step 2 − Then, we will use the value p - 1 as (s * 2e). Where s is odd and positive and e is positive.

Step 3 − Calculate the value q^((p-1)/2)(mod p) = -1

Step 4 − use loop for m greater than 0 and update the value of x,

Find m such that b^(2^m) - 1(mod p) where 0 <= m <= r-1.

If M is 0, return x otherwise update values,

x = x * (g^(2 ^ (r - m - 1))
b = b * (g^(2 ^ (r - m))
g = (g^(2 ^ (r - m - 1))
r = m

Example

Program to illustrate the working of our solution,

Open Compiler
#include <iostream> #include <math.h> using namespace std; int powerMod(int base, int exponent, int modulus) { int result = 1; base = base % modulus; while (exponent > 0) { if (exponent % 2 == 1) result = (result * base)% modulus; exponent = exponent >> 1; base = (base * base) % modulus; } return result; } int gcd(int a, int b) { if (b == 0) return a; else return gcd(b, a % b); } int orderValues(int p, int b) { if (gcd(p, b) != 1) { return -1; } int k = 3; while (1) { if (powerMod(b, k, p) == 1) return k; k++; } } int findx2e(int x, int& e) { e = 0; while (x % 2 == 0) { x /= 2; e++; } return x; } int calcSquareRoot(int n, int p) { if (gcd(n, p) != 1) { return -1; } if (powerMod(n, (p - 1) / 2, p) == (p - 1)) { return -1; } int s, e; s = findx2e(p - 1, e); int q; for (q = 2; ; q++) { if (powerMod(q, (p - 1) / 2, p) == (p - 1)) break; } int x = powerMod(n, (s + 1) / 2, p); int b = powerMod(n, s, p); int g = powerMod(q, s, p); int r = e; while (1) { int m; for (m = 0; m < r; m++) { if (orderValues(p, b) == -1) return -1; if (orderValues(p, b) == pow(2, m)) break; } if (m == 0) return x; x = (x * powerMod(g, pow(2, r - m - 1), p)) % p; g = powerMod(g, pow(2, r - m), p); b = (b * g) % p; if (b == 1) return x; r = m; } } int main() { int n = 3; int p = 13; int sqrtVal = calcSquareRoot(n, p); if (sqrtVal == -1) cout<<"Modular square root is not exist"; else cout<<"Modular square root of the number is "<<sqrtVal; }

Output

Modular square root of the number is 9
Updated on: 2022-01-25T08:28:01+05:30

524 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements