applied cryptography - protocols, algorithms, and source code in c

If gcdan 1 all is not lost in this general case ax

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: other numbers except its multiples. One way to compute the greatest common divisor of two numbers is with Euclid’s algorithm . Euclid described the algorithm in his book, Elements, written around 300 B.C. He didn’t invent it. Historians believe the algorithm could be 200 years older. It is the oldest nontrivial algorithm that has survived to the present day, and it is still a good one. Knuth describes the algorithm and some modern modifications [863]. In C: /* returns gcd of x and y */ int gcd (int x, int y) { int g; if (x < 0) x = -x; if (y < 0) y = -y; if (x + y == 0) ERROR; g = y; while (x > 0) { g = x; x = y % x; y = g; } return g; } This algorithm can be generalized to return the gcd of an array of m numbers: /* returns the gcd of x1, x2...xm */ int multiple_gcd (int m, int *x) { size_t i; int g; if (m < 1) return 0; g = x[0]; for (i=1; i<m; ++i) { g = gcd(g, x[i]); /* optimization, since for random x[i], g==1 60% of the time: */ if (g == 1) return 1; } return g; } Inverses Modulo a Number Remember inverses? The multiplicative inverse of 4 is 1/4, because 4*1/4 = 1. In the modulo world, the problem is more complicated: 4*x a 1 (mod 7) This equation is equivalent to finding an x and k such that 4x = 7k + 1 where both x and k are integers. The general problem is finding an x such that 1 = (a*x) mod n This is also written as a -1 a x (mod n) The modular inverse problem is a lot more difficult to solve. Sometimes it has a solution, sometimes not. For example, the inverse of 5, modulo 14, is 3. On the other hand, 2 has no inverse modulo 14. In general, a-1 a x (mod n) has a unique solution if a and n are relatively prime. If a and n are not relatively prime, then a-1 a x (mod n) has no solution. If n is a prime number, then every number from 1 to n- 1 is relatively prime to n and has exactly one inverse modulo n in that range. Previous Table of Contents Next Products | Contact Us | About Us | Privacy | Ad Info | Home Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement....
View Full Document

This note was uploaded on 10/18/2010 for the course MATH CS 301 taught by Professor Aliulger during the Fall '10 term at Koç University.

Ask a homework question - tutors are online