Addition
The runtime for addition is O(n) where n is the number of bits in each input number.
Multiplication
The rule here is
The algorithm for performing this is recursive with a time complexity of O(n^2), where for 2 bit integers, we are bit shifting left and right and checking the parity of the last bit as well as performing addition at most once. We can write a better algorithm using divide and conquer.
Division
To divide an integer and where , we need to find a quotient and a remainder such that . Similar to multiplication, we can define an O(n^2) algorithm.
Modular Arithmetic
Basically, is the remainder when is divided by .
This can be thought of as a function where input and modded together produce .
There is also another use for the modulo.
Congruency
Two numbers are congruent if their modulo with are the same.
Note that
Note
Note that means equivalent, not equals
Note that . There is a proof in the lecture notes for this.
Rules
Look these up if you forget what they mean…
- Substitution rule
- and , then and
- As a consequence, you can apply repeatedly to simplify a modulo expression and reduce an intermediate stage to its remainder modulo N at any stage. The idea here is to try to force the base to become a 1 or -1.
- and , then and
- Associativity
- Commutativity
- Distributivity
Modular Exponentiation
The idea here is that if you want to compute and and are fairly large then, we can’t just compute the exponential and take its mod. But, is a number between .
We can define a recursive algorithm to do this… This runs in time (with respect to bits) since is reduced by 1 bit on every recursive call () and each call runs multiplication as we can see above.
Greatest Common Divisor
Given two integers a and b, find the largest integer that divides both of them. Here, you basically factor , and and multiply their common factors.
To do so, you use euclid’s rule.
Euclids Rule
This states that if , and are positive integers with then…
Basically, this tells us that we can reduce the arguments in our call until we get something that we can compute (our base case which is when , in which case we return ). Otherwise, we make a recursive call with (b, a mod b) where we actually swap the positions of a and b from the function call arguments.
Property
if then
At each call, both arguments are halved. This requires at most 2n recursive calls where each call requires a division which occurs in O(n^2) time. The runtime of this is .
An Extension
How do we verify that a number is the GCD of and ?
Property
if divides both and with no remainder and for some integers and . then d must be the greatest divisor. Note that x or y could be negative.
Well, this is because that a and b can be written as for example, so . Therefore is the GCD. This is how we check if something is the GCD where we extend Euclid’s rule by returning a triple (x,y,d) where x and y cert d. Maybe remember this algorithm.
function extended-euclid(a,b)
Input: 2 integers a and b where a>=b>=0
Output: Integers(x,y,d) such that d=gcd(a,b) and ax+by=d
if b = 0
return (1,0,a)
(x',y',d) = extended-euclid(b, a%b)
return (y', x'-floor(a/b)y',d)
Modular Division
An integer is the multiplicative inverse of if .
- For a given , there can be at most one inverse in .
- For certain choices of and , there is no multiplicative inverse.
When does an inverse exist?
which implies that for some where if we redefined , we get which says that the gcd(a,N)=1 or that and are co-prime. This inverse can be found in time as where comes from (a call to extended Euclid).