chapter 3 3.6 integers and algorithms ‒ representations of integers ‒ algorithms for integer...

16
Chapter 3 3.6 Integers and Algorithms Representations of integers Algorithms for integer operations Modular Exponentiation The Euclidean Algorithm 1

Upload: kerrie-cain

Post on 05-Jan-2016

227 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Chapter 3 3.6 Integers and Algorithms ‒ Representations of integers ‒ Algorithms for integer operations ‒ Modular Exponentiation ‒ The Euclidean Algorithm

1

Chapter 3

3.6 Integers and Algorithms‒ Representations of integers‒ Algorithms for integer operations‒ Modular Exponentiation‒ The Euclidean Algorithm

Page 2: Chapter 3 3.6 Integers and Algorithms ‒ Representations of integers ‒ Algorithms for integer operations ‒ Modular Exponentiation ‒ The Euclidean Algorithm

2

Representations of integers

• Theorem 1: Let b be a positive integer greater than 1. Then if n is a positive integer, it can be expressed uniquely in the form

where k is a nonnegative integer, a0, a1, …,ak are nonnegative integers less than b, and ak ≠0.

011

1 ... abababan kk

kk

Page 3: Chapter 3 3.6 Integers and Algorithms ‒ Representations of integers ‒ Algorithms for integer operations ‒ Modular Exponentiation ‒ The Euclidean Algorithm

3

• Example 1: What is the decimal expansion of the integer that has (1 0101 1111)2 as its binary expansion?

• Example 2: What is the decimal expansion of the hexadecimal expansion of (2AE0B)16 ?

Page 4: Chapter 3 3.6 Integers and Algorithms ‒ Representations of integers ‒ Algorithms for integer operations ‒ Modular Exponentiation ‒ The Euclidean Algorithm

4

• Example 3: Find the base 8, or octal, expansion of (12345)10

• Example 4: Find the hexadecimal expansion of (177130)10?

Page 5: Chapter 3 3.6 Integers and Algorithms ‒ Representations of integers ‒ Algorithms for integer operations ‒ Modular Exponentiation ‒ The Euclidean Algorithm

5

• Algorithm 1: Construction Base b Expansions procedure base b expansion(n:positive integer) q: = n k: =0 while q ≠ 0 begin ak : =q mod b

q: = k: =k+1 end {the base b expansion of n is (ak-1 . . . a1 a0)b}

bq /

Page 6: Chapter 3 3.6 Integers and Algorithms ‒ Representations of integers ‒ Algorithms for integer operations ‒ Modular Exponentiation ‒ The Euclidean Algorithm

6

Page 7: Chapter 3 3.6 Integers and Algorithms ‒ Representations of integers ‒ Algorithms for integer operations ‒ Modular Exponentiation ‒ The Euclidean Algorithm

7

Algorithms for integer operations• Algorithm 2: Addition of Integers Procedure add(a , b:positive integers) {the binary expansions of a and b are (an-1 . . . a1 a0)2 and

(bn-1 . . . b1 b0)2 respectively}

c : =0for j: =0 to n-1Begin d : = sj : = aj+bj+c-2d

c : =dendsn:=c {the binary expansion of the sum if (sn sn-1. . . s1 s0)2 }

2/)( cba jj

Page 8: Chapter 3 3.6 Integers and Algorithms ‒ Representations of integers ‒ Algorithms for integer operations ‒ Modular Exponentiation ‒ The Euclidean Algorithm

8

Algorithms for integer operationsExample 7: Add a=(1110)2 and b=(1011)2.

Page 9: Chapter 3 3.6 Integers and Algorithms ‒ Representations of integers ‒ Algorithms for integer operations ‒ Modular Exponentiation ‒ The Euclidean Algorithm

9

Algorithms for integer operations

• Algorithm 3 : Multiplying Integers procedure multiply(a, b : positive integers) {the binary expansions of and b are(an-1 . . . a1 a0)2 and

(bn-1 . . . b1 b0)2 respectively}

for j:=0 to n-1 Begin if bj =1 then cj=a shifted j places

else cj:=0

end{c0 c1 . . . cn-1 are the partial products}

p :=0 for j:=0 to n-1 p: = p +cj {p is the value of ab}

Page 10: Chapter 3 3.6 Integers and Algorithms ‒ Representations of integers ‒ Algorithms for integer operations ‒ Modular Exponentiation ‒ The Euclidean Algorithm

10

Algorithms for integer operations

Example 9: Find the product of a= (110)2 and b=(101)2

Page 11: Chapter 3 3.6 Integers and Algorithms ‒ Representations of integers ‒ Algorithms for integer operations ‒ Modular Exponentiation ‒ The Euclidean Algorithm

11

Algorithms for integer operations• Algorithm 4 : Computing div and mod

procedure division algorithm(a :integers ,d: positive integer) q: =0 r: =|a| while r≧d begin r := r-d q :=q+1 end if a<0 then

if r=0 then q:=-q else begin r := d-r q := -(q+1) end {q = a div d is the quotient, r = a mod d is the remainder}

Page 12: Chapter 3 3.6 Integers and Algorithms ‒ Representations of integers ‒ Algorithms for integer operations ‒ Modular Exponentiation ‒ The Euclidean Algorithm

12

Modular Exponentiation• In cryptography it is important to be able to find bn mod

m efficiently, where b, n and m and large integers. It’s impractical to first compute bn and then find its remainder when divided by m because bn will be a huge number. Instead, we can use an algorithm that employ expansion of the exponent n , say n = (ak-1 . . . a1 a0)2 .

• Before we present this algorithm, we illustrate its basic idea. We will explain how to use the binary expansion of n to compute bn .First , note that

011

1011

1 222...2 ... aaaaaan bbbbbk

kk

k

Page 13: Chapter 3 3.6 Integers and Algorithms ‒ Representations of integers ‒ Algorithms for integer operations ‒ Modular Exponentiation ‒ The Euclidean Algorithm

13

Modular Exponentiation

• To compute bn , we find the values of b, b2,(b2)2=b4, (b4)2=b8, . . . , .

• We multiply the terms in this list, where aj=1 .

This gives us .

• For example, to compute 311 we first note that 11 = (1011)2, so that 311= 383231.

• By successively squaring, we find that 32=9, 34=81, 38=6561.

• Consequently,311=383231=6561*9*3= 177,147

k

b2j

b2

nb

Page 14: Chapter 3 3.6 Integers and Algorithms ‒ Representations of integers ‒ Algorithms for integer operations ‒ Modular Exponentiation ‒ The Euclidean Algorithm

14

Modular Exponentiation• Algorithm 5: Modular Exponentiation

procedure modular exponentiation(b:integer ,

n=(ak-1 . . . a1 a0)2 ,m: positive integer)

x: = 1

power := b mod m

for i=0 to k-1

begin

if ai =1 then x :=(x*power) mod m

power :=(power*power) mod m

End

{x equals bn mod m}

Example 11: Use Algorithm 5 to find 3644 mod 645.

Page 15: Chapter 3 3.6 Integers and Algorithms ‒ Representations of integers ‒ Algorithms for integer operations ‒ Modular Exponentiation ‒ The Euclidean Algorithm

15

The Euclidean Algorithm

• Lemma 1: Let a=bq+r ,where a, b, q, and r are integers. Then gcd(a,b)=gcd(b,r).

• Algorithm 6: The Euclidean Algorithm

procedure gcd(a.b:integers)

x: = a

y: = b

while y0

begin

r := x mod y

x := y

y := r

end {gcd(a,b) is x}

Page 16: Chapter 3 3.6 Integers and Algorithms ‒ Representations of integers ‒ Algorithms for integer operations ‒ Modular Exponentiation ‒ The Euclidean Algorithm

16

The Euclidean Algorithm

• Example 12: Find the GCD of 414 and 662 using the Euclidean Algorithm.