Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a function modInv(a, b) that returns the multiplicative inverse of a, mod b (i.e., it should return an integer x {1, 2, 3,

Write a function modInv(a, b) that returns the multiplicative inverse of a, mod b (i.e., it should return an integer x ∈ {1, 2, 3, … , b – 1} such that (ax) mod b = 1). You can assume that the inverse exists, i.e., gcd(a, b) = 1 (in other words, a and b are relatively prime).

You can use the extended Euclidean algorithm to find the inverse. In addition to computing the gcd of two numbers a and b, the extended algorithm returns the Bézout coefficients s and t. These are two integers that express the gcd as a linear combination of a and b: gcd(a, b) = sa + tb.

When a and b are relatively prime, gcd(a, b) = 1, so we can write

sa + tb = 1

sa – 1 = -tb

Recall one of the results we proved earlier: x mod n = y mod n iff n | (x – y). From the second equation above, it’s clear that b | (sa – 1). Therefore, (sa) mod b = 1 mod b = 1. To ensure that s is in the allowable set of values {1, 2, 3, … , b – 1}, we simply have the algorithm return s mod b to find the inverse.

To run the extended Euclidean algorithm on two numbers a and b (assuming a > b), we keep track of four quantities that are repeatedly calculated: the remainder ri, the quotient qi, and the coefficients si and ti.

First set some initial values ro a ri b Then proceed as follows, starting at i 1: o Compute q ri-1 ri (using integer division o Compute ri 1 r-1 mod ri o Compute si 1 s 1 qisi o Compute t+1 ti-1 qil Repeat this set of calculations until the remainder becomes 0. The gcd is then the remainder from the previous round, and the Bézout coefficients are the s and t from the previous round

The table below shows an example of running the extended Euclidean algorithm on a 1073, b 462. The shaded cells are the initial values; the algorithm starts running by computing q1. 1073 462 1073 462 2 462 149 3 1073 mod 462 149 S3 S1 S2 1-3 (-2)- 7 149 15 9 462 mod 149 15 149 mod 15 14 15 14 1 1-9 (-3) 28 -2 9 7* -65 15 mod 14 1 7-1 (-65) 72 14/13 14 14 mod 1

We stop as soon as we reach a remainder of 0 at r6. Then the gcd is the remainder from the previous round, r5 (which is 1, indicating 1073 and 462 are relatively prime). The two Bézout coefficients are the s5 and t5 from the previous round: s = -31 and t = 72. We can easily verify that sa + tb = -31(1073) + 72(462) = 1, which matches the gcd of 1073 and 462.
Since a (1073) and b (462) are relatively prime, s mod b is the multiplicative inverse of a, mod b. (-31) mod 462 = 431. Verify: (as) mod b = (1073*431) mod 462 = 462463 mod 462 = 1.

Python tip: Use // for integer division; a single forward slash results in floating-point division.
 
 

First set some initial values: r1 = b S1 = 0 t = 1 roa So = 1 to = 0 Then proceed as follows starting at i = 1: O Compute qiri-1/ ri (using integer division) o Compute 1+1 = 1-1 mod ri O Compute S+1= Si-1-qisi O Compute t+1 = ti-1-qiti Repeat this set of calculations until the remainder becomes 0. The gcd is then the remainder from the previous round, and the Bzout coefficients are the s and t from the previous round.

Step by Step Solution

3.39 Rating (158 Votes )

There are 3 Steps involved in it

Step: 1

Answer definition of egcda b r a s 1 t 0 u b v 0 w 1 whereas u 0 q r u rs r r u u rs q u ss s s v v ss q v ts t t w w ts q w bring back r s t q egcd72 7 print egcd24 30 print Explanation The Extended ... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Income Tax Fundamentals 2013

Authors: Gerald E. Whittenburg, Martha Altus Buller, Steven L Gill

31st Edition

1111972516, 978-1285586618, 1285586611, 978-1285613109, 978-1111972516

More Books

Students also viewed these Programming questions

Question

Are there values of r and s for which 02+3 1 2 0 1000

Answered: 1 week ago