Skip to content

Instantly share code, notes, and snippets.

@k4rtik
Created December 9, 2011 21:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save k4rtik/1453417 to your computer and use it in GitHub Desktop.
Save k4rtik/1453417 to your computer and use it in GitHub Desktop.
Number Theory & Cryptography - Assignement 2 - Question 1: Implement Rabin-Miller algorithm for Primality Testing. - Done with Aviral - November 9, 2011
/* Question 1: Implement Rabin-Miller algorithm for Primality Testing. */
/*
Input: n>3, an odd number, k for accuracy
Output: 'composite' or 'possibly prime'
*/
rabinmiller(n, k) = {
d = n - 1;
s = 0;
while( (d%2 == 0),
d = d / 2;
s = s + 1;
);
while( ( k>0 ),
k--;
a = random( n-3 ) + 2;
x = Mod( a^d, n );
if( ( (x==1) || (x==n-1) ),
next(1);
);
r = 1;
while( (r <= s-1),
r++;
x = Mod(x^2, n);
if((x==1),
print("Composite!");
return(0);
);
if((x==n-1),
next(2);
);
);
print("Composite!");
return(0);
);
print("Probably prime!");
return(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment