Project UltiMatrix Tarun Pondicherry Michael Navara Katrina Pizarro - - PowerPoint PPT Presentation

project ultimatrix
SMART_READER_LITE
LIVE PREVIEW

Project UltiMatrix Tarun Pondicherry Michael Navara Katrina Pizarro - - PowerPoint PPT Presentation

Project UltiMatrix Tarun Pondicherry Michael Navara Katrina Pizarro Alex Rodriguez Aaron Schaff Overview Requirements Responsibilities Schedule Encryption Algorithm C+ + Function Overview Function Algorithms Final


slide-1
SLIDE 1

Project UltiMatrix

Tarun Pondicherry Michael Navara Katrina Pizarro Alex Rodriguez Aaron Schaff

slide-2
SLIDE 2

2

UltiMatrix

Overview

 Requirements  Responsibilities  Schedule  Encryption Algorithm  Function Overview  Function Algorithms  Final Product

C+ +

slide-3
SLIDE 3

3

UltiMatrix

Requirements

WRI TE A C+ + PROGRAM TO ENCRYPT & DECRYPT A TEXT MESSAGE USI NG A 3x3 MATRI X AS A KEY TO ENCODE THE MESSAGE AND THE I NVERSE MATRI X TO DECODE THE MESSAGE

Hmmm…

I wonder if I can transfer to the

  • ther class?
slide-4
SLIDE 4

4

UltiMatrix

Responsibilities

 Team Leader: Tarun Pondicherry

User Interface Functions

 startApp – Tarun Pondicherry  init – Tarun Pondicherry

Cryptography.h

 Encrypt – Katrina Pizarro  Decrypt – Katrina Pizarro  createKey – Alex Rodriguez

slide-5
SLIDE 5

5

UltiMatrix

Responsibilities

MatrixMath.h Functions

 matrixInverse – Tarun Pondicherry  matrixDeterminant – Aaron Schaff  matrixCofactor –Tarun Pondicherry  matrixMinor – Tarun Pondicherry  matrixAdjoint – Michael Navera  matrixTranspose – Aaron Schaff  matrixMultiply – Alex Rodriguez  matrixScalerMultiply – Michael Navera

slide-6
SLIDE 6

6

UltiMatrix

Implementing Matrices in C+ +

 The first method that comes to mind is to use

double subscripted arrays where the first subscript corresponds to the row and the second corresponds to the column

 However, this poses two issues:

 Arrays are zero based but matrices are one based  Passing a double subscripted array to a function

without pointers poses limitations on expanding the program to handle larger matrices

slide-7
SLIDE 7

7

UltiMatrix

Implementing Matrices in C+ +

Consider the matrix:

          = 9 8 7 6 5 4 3 2 1 A

1 2 3 4 5 6 7 8 9

A[0] = A[1] = A[2] =

In effect, this is any array of arrays where the first subscript refers to another array

char A[3][3] = { 1, 2, 3, 4, 5, 6, 7, 8, 9} ;

slide-8
SLIDE 8

8

UltiMatrix

Implementing Matrices in C+ +

A different perspective of memory: Why not implement this as a single subscripted array?

1 2 3 4 5 6 7 8 9

A[0] A[1] A[2] [0] [1] [2] [0] [1] [2] [0] [1] [2]

1 2 3 4 5 6 7 8 9

A[0] A[1] A[2] [0] [1] [2] [0] [1] [2] [0] [1] [2]

1 2 3 4 5 6 7 8 9

[0] [1] [2] [3] [4] [5] [6] [7] [8]

slide-9
SLIDE 9

9

UltiMatrix

Implementing Matrices in C+ +

 However, now we cannot distinguish between

matrices of different sizes (1x9, 3x3, 9x1)

 Unless… we know the dimensions and can

virtually recreate the matrix

 So this would mean passing the dimensions

to each and every matrix function

 This can become extremely tedious and

unproductive

slide-10
SLIDE 10

10

UltiMatrix

typedef struct { short* values; char rows; char cols; } Matrix;

Oh no! A pointer! Fear not, as far as we are concerned this is an array containing the values The number of rows The number of columns

The Matrix Structure

 To make things easier to pass to functions,

the Matrix structure is used

 It contains the array and the dimensions  Now all that needs to be passed is the matrix

slide-11
SLIDE 11

11

UltiMatrix

Implementing Matrices in C+ +

          = 9 8 7 6 5 4 3 2 1 A

Reconsider the matrix:

Matrix A; short vals[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9} ; A.values = vals; A.rows = 3; A.cols = 3;

To virtually recreate the matrix from the array, use the formula:

Aij = (i-1)* (columns) + (j-1)

slide-12
SLIDE 12

12

UltiMatrix

Implementing Matrices in C+ +

This formula is the basis for Matrix access which is encapsulated in the functions:

Aij = (i-1)* (columns) + (j-1)

short getElement(Matrix iMatrix, char i, char j) { return iMatrix.values[(i-1)* (iMatrix.cols) + (j-1)]; } void setElement(Matrix iMatrix, char i, char j, short newVal) { Matrix.values[(i-1)* (iMatrix.cols) + (j-1)] = newVal; }

These functions are overloaded for different data types

slide-13
SLIDE 13

13

UltiMatrix

Implementing Matrices in C+ +

 Now, we can pass Matrices on any size  The functions handle the issue of altering

the zero base of arrays to the one of matrices

 For the sacrifice of memory, this makes

coding matrix functions much easier, more functional and expandable

slide-14
SLIDE 14

14

UltiMatrix

Function Dependencies

main

Encrypt Decrypt createKey

MatrixMath.h

bool I nverse short Determinant short Cofactor void Adjoint void Transpose void ScalerMultiply void Multiply void Minor

slide-15
SLIDE 15

15

UltiMatrix

MainLooper.h

 Contains function main  Resizes window  Sets title  Calls init()  Prints title  Calls startApp()  Asks “Would You Like to Continue?”  Clears Screen and Loops

slide-16
SLIDE 16

16

UltiMatrix

startApp

 Asks user whether to encrypt or decrypt  Encrypt

 Asks for an output file  Creates it if necessary  Clears the new line in buffer (cin.ignore())  Asks for user input through cin.getline()  Calls Encrypt and outputs to the file

 Decrypt

 Asks for input file  Checks for existence  Reads it line by line  Calls Decrypt  Displays results

slide-17
SLIDE 17

17

UltiMatrix

Encrypt

 Put input string into Matrix array  Convert into long integers and skew  Get random generated key (createKey)  Multiply

key and Matrix array (matrixMultiply)

 Place result, key, key size, delimiter and

skew into encrypted output array

slide-18
SLIDE 18

18

UltiMatrix

Decrypt

 Parse output array into key, key size,

data and skew

 Find inverse of key  Multiply inverse of key and encrypted

array

 Convert result to ASCII (chars) and

correct skew

slide-19
SLIDE 19

19

UltiMatrix

Example

 Message: Hello Computer  A random key is generated:  Determinant function checks if the key is

invertible

          − − 9 8 7 6 5 4 3 2 1

slide-20
SLIDE 20

20

UltiMatrix

Example (cont’d)

0 1 2 3 4 5 6 7 … Puts delimiter

,

Key size

3 , 94

Skew value

, -1

1st value In key

, 2

2nd value

… , Value , Value

1st value In encrypted array 2nd value

  • Message is converted into long and skew value

is subtracted from the values

  • This becomes the output array:
slide-21
SLIDE 21

21

UltiMatrix

Example (cont’d)

Creates empty key matrix:

Value , Value

2nd value

1st value In key

          _ _ _ _ _ _ _ _ _

Puts values into matrix and removes delimiter

slide-22
SLIDE 22

22

UltiMatrix

Example (cont’d)

 The key is inverted using matrixInverse:  Product Matrix is created:

          − − − − − 6 0541 . 6 091 . 0125 . 075 . 05 . 325 . 0125 . 175 . 3875 .

     _ _ _ _ _ _ _ _ _

1 2 0 1 2…

slide-23
SLIDE 23

23

UltiMatrix

Example (cont’d)

 Encrypted matrix is created:  Finally, the inverted key and encrypted matrix

are multiplied using matrixMultiply and the result is placed into the empty product matrix and is converted back to ascii!

     _ _ _ _ _ _ _ _ _

Value , Value

2nd value 1st value

slide-24
SLIDE 24

24

UltiMatrix

createKey

 Since the number a rows and columns in the

input matrix is unknown, the function loops for the number of rows and columns in the input matrix.

 The function then places a random value in

the matrix using the rand and srand functons.

 After looping through the entire matrix the

function calls matrixDeterminant and if it returns zero it creates an new key. If it returns non-zero then the function returns the generated key.

slide-25
SLIDE 25

25

UltiMatrix

matrixInverse

 First,

the function ensures that the determinant is not zero

 Memory is dynamically allocated to hold the

adjoint of the matrix

 This matrix is populated via matrixAdjoint  The adjoint matrix is multiplied by the inverse

  • f the determinant and stored in the output

matrix

 Allocated memory is given back to the system

slide-26
SLIDE 26

26

UltiMatrix

matrixDeterminant

 First, the size of the matrix is checked  If it is a 1 x 1 matrix, the determinant is the value of

the element

 Otherwise, each element in the first row is multiplied

by its cofactor

 The sum of these products is returned as the

determinant

 This function calls cofactor, which calls determinant

so they are in effect recursive

 The stop case is a 1 x 1 matrix (whose determinant is

the value)

slide-27
SLIDE 27

27

UltiMatrix

matrixCofactor

 Memory is dynamically allocated for a matrix that has

  • ne less row and column than the input matrix

 That matrix is then populated with the requested

minor via the matrixMinor function for element i,j

 The determinant of that minor is found via the

matrixDeterminant function and signed appropriately based on parity

 Allocated memory is given back to the system  The appropriately signed determinant is returned

slide-28
SLIDE 28

28

UltiMatrix

matrixMinor

          =

33 32 31 23 22 21 13 12 11

a a a a a a a a a A

      = x x x x B

Minor for Element: row= 2, column= 1

i= 1 j= 1

i= = Row? j= = Column?

j= 2 j= 3 i= 2 i= 3 j= 1 j= 2 j= 3 j= 1 j= 2 j= 3

NO YES

      = x x x a12 B       = x x a a

13 12

B       = x a a a

32 13 12

B       =

33 32 13 12

a a a a B

NO YES NO YES NO

slide-29
SLIDE 29

29

UltiMatrix

matrixTranspose

          =

33 32 31 23 22 21 13 12 11

a a a a a a a a a A

The function loops through the rows and columns

  • f the first matrix and places each element i,j in

element j,i of the output matrix

          = x x x x x x x x x B           = x x a x x a x x a

13 12 11

B           = x a a x a a x a a

23 13 22 12 21 11

B           =

33 23 13 32 22 12 31 21 11

B a a a a a a a a a

slide-30
SLIDE 30

30

UltiMatrix

matrixAdjoint

 The function loops through the rows

and columns of the first matrix and places each element i,j in element j,i of the cofactor matrix.

slide-31
SLIDE 31

31

UltiMatrix

matrixMultiply

          =                     C c b a c b a c b a B c c c b b b a a a A *

2

3a

2

a

Is the number of columns in matrix A the same as the rows in Matrix B? YES This pattern continues until matrix C is complete, then function is complete. Example: + + = 3x3 3x3 3 x Matrix A’s columns Matrix B’s rows 3

2

a

2

a

slide-32
SLIDE 32

32

UltiMatrix

matrixScalerMultiply

 The

function loops through each element

  • f

the input matrix

 Each element is multiplied by a

scaler and placed in the same position in the output matrix

slide-33
SLIDE 33

33

UltiMatrix

Full Demonstration

slide-34
SLIDE 34

34

UltiMatrix

slide-35
SLIDE 35

35

UltiMatrix

slide-36
SLIDE 36

36

UltiMatrix

Contents of message.txt

,4,0,0,2,-5,9,-3,-3,1,-6,-9,-5,1,6,9,-1,-8,8,-339,-74,-429,-569,

  • 1077,-1391,-348,337

,4,0,1,2,6,-6,6,-1,9,-5,7,2,-10,0,-7,9,3,3,325,443,-366,280,729, 937,524,866,855,1079,-97,999,1276,1400,772,987,-269,

  • 281,32,-190,-131,73,612,558,1098,958,487,841,613,631,

440,1105 ,4,0,9,-4,-8,-4,7,-5,3,4,8,0,2,-4,8,-8,8,-10,-300,-29,-580,-123,- 76,860,989,644,906,855,240,596,-164,824,1150,-328,-210,- 954,628,1352

slide-37
SLIDE 37

37

UltiMatrix

slide-38
SLIDE 38

38

UltiMatrix

Acknowledgments/Sources

Acknowledgements:

 Mr. Paterno  Canned Doit Software – Matrix

Calculator Sources:

 C+ + book  Mr. Paterno’s slide show on Matrix

Algebra and Cryptography