ok so we talked about primarily testing of
a number we want to we need a prime number
so for our public key cryptosystem so you
want to see whether ah we can get a prime
now say suppose we need a for r s a suppose
we need one zero we we need p q large prime
say one zero two bits so we need a number
prime which is lies between ah zero less than
p q less than two to the power this because
this many bits actually so huge number so
now the question is is there any algorithm
which can give us this many bits prime number
so is there any algorithm in which we can
generate a weak is this is the prime number
algorithm say we will give an input say l
l is say are this and it will give us a prime
which is basically this many bits so now there
is no such algorithm exists which can give
us a prime number of our desirable size so
then what is the option option is we can just
choose an integer or integer and we can test
whether this is prime or not so we can choose
an integer so that is those are called primary
tests basically we can choose an integer integer
p and then we check or test this checking
means testing check whether this is odd integer
because two is usually ah is taking as a prime
which is a even number but any other even
number cannot be prime because it has a factor
two so prime means it has a prime number is
definition of prime number is it is only divisible
by 
one and p so other than one and p it shouldnt
have any factor so it should so we we we we
should not get any factor other than one and
p ok
so now we can check ah whether check whether
e is prime or not so we have seen this we
can check by suppose for checking this you
have to start with this is a naive approach
we can we can start with two three four like
this and we can check whether this divides
p or not so we take a k from this whether
this is the whether p has a factor this or
not we take a k from this set and then we
check whether k divides p or not ok
now this we can reduce by checking up to root
over of p because because we have seen if
we if a is a factor which is greater than
root over of p then so a is a factor means
there will be a b such that p is equal to
b then b has to be less than root over of
p so if we have a factor of p which will get
at the root over of p then we should have
a factor another factor b which is less than
two so it is sufficient to check this up to
so on we can leave it up to this ok so but
this is not a good idea because if p is large
so its checking too much is so this approach
is not that a good idea so this another approaches
we have seen which is called a sieve of safe
so that idea is we just remove the composite
then just we like this we just remove the
composite then the remaining number is prime
see bob eratonthams 
ok so idea is so you will just see bit i mean
you just ah delete all the composite and then
the remainings are the primes this we have
seen like we first delete suppose we want
to check p is prime or not i mean we we want
to consider one to m some numbers ah we want
to get all the primes from one to n so what
we do we delete all the we remove all the
composite from one to n and then the remaining
will be the prime number so that is the idea
so for deleting what we do we first we need
all the numbers which are ah multiple up to
we remove all the number multiple of two then
we remove all the number which are multiple
of so multiple of two means four we delete
six we delete eight we delete ten we delete
like this then we delete all the numbers which
are basically multiple of three but two six
we have deleted three nine fifteen like this
so then we delete all the numbers multiple
of four but that will come under multiple
of two so then we delete all the number multiple
of five so five fifteen is some under three
multiple like this so this way so after this
the remaining number will be the prime number
so this is also not a very ah efficient approach
because the n is large so these way it will
take much more time so now we want to see
whether we can have some better approach to
ah the better way to test whether a odd integer
is a prime or not so for that we need to take
help of some properties of prime number and
then we can so those are the necessary condition
of p to be prime and then we can use those
property to check test whether our our selected
number our chosen number is satisfying that
property if it is satisfying if it is not
satisfying then we can straight away say this
number is not a prime its a composite number
but if it is satisfied then we can say it
it could be a prime so those are basically
probably sticking up i mean those are called
pseudo prime will come to that so let us have
this for mark formate primary test ok so this
is coming ah from the former little theorem
let us recall the formal little theorem so
what is the former little theorem
so it is telling if phi is a prime let p be
a prime prime number and a is relatively prime
with g d the g c d of a p is one then a to
the power p minus one is called one to one
mod p and this is true for all p i all a this
must be true for all a where g c d of a p
must be one so this is the formal little theorem
so we want to use this so this is a necessary
condition to p to the prime so if we choose
a a number p ah or integer now and if we take
a a which is basically g c we can calculate
the g c d of a and p now if g c d is not one
then we can say p is not a prime because they
have a common factor greater than one ok
so then we can say that there p is not prime
but if the g c d is one and if if this condition
is satisfying if this condition is not satisfying
then we can say p is not a prime because this
is a necessary condition to be p to be prime
but if this condition is satisfied then we
say p is p may be a prime so thats why it
is called pseudo prime probable prime so p
is prime base a so this base a is coming into
the picture like because we are not testing
for all a if we can test for all a then we
can show that p is a prime but that test is
equivalent to that the ah seed method we have
seen ok so we will use this so let us just
write the primary testing of farmer
so we choose a p and we check whether p is
a prime or not ok so we select a choose a
such that which choose a randomly and says
that g c d of a and p we compute we choose
a such that g c d of if if we choose an a
and if we found g c d is not one greater than
one then we can straight away say this is
not a prime we chose a ah less than p less
than p ok an integer less than p then we compute
g c d of a p
now if the g c d is greater than one then
we can say sorry greater than one then they
have a they have a factor more than one so
that means ah p cannot be a prime then return
return p is composite composite number not
prime is composite else if the g c d is one
else means else means the g c d of else means
a is co prime to p that means they are g c
d is one ok
then what we do we compute this a to the power
we compute this we store it into y a to the
power p minus one mod p ok so how we can compute
this this is this is just a powering a excuse
me powering a number so powering so this powering
we can compute we have seen for r s a the
is so square and multiply method we can use
so we can one can do this powering so we check
this and then we see whether ah else if y
is congruent to one mod p then if y is not
congruent to one mod p then return p is composite
for sure ok else if it is one then it is satisfying
the necessary condition then p is we can return
as a prime but this is also published exchange
because we have only chosen one a we are not
testing all possible a if we can test for
all possible a and if this for all is which
are having g c d with p is one and then if
it is giving us that a to the power p minus
one is one then return this composite else
else return p is prime actually this is probable
prime that means p is prime base a we can
say else return p is prime but this prime
is also probable prime ok
so this is the code for a fermat primality
testing so this is this will give us probable
prime we will take some example for this suppose
we want to test whether say ah p is equal
to forty nine is prime or not ok
so ah p is equal to forty nine we want to
test by farm as fermat testing so that this
is prime or not so how to test this so we
choose we have to choose a for which the g
c d is ah g c d is one so if we choose a is
equal to say nineteen then we can compute
the g c d of a and p which is basically one
this of this for computing this we can use
the extended euclidean algorithm to compute
this so we compute this now if we compute
this so g c d is one so now if we compute
nineteen to the power p minus one p minus
one is so we compute this a to the power p
minus one so this is basically nineteen to
the power forty eight
so this will be happen to be congruent to
ah one mod p one mod forty nine we can verify
this this we can calculate and we can verify
this so if we just use the fermat primality
test it is telling us forty nine is a prime
but it should tell us probable prime because
we know forty nine is not a prime because
forty nine is basically seven into seven forty
nine is not a prime but it is reporting as
forty nine is a prime so we should use here
base nineteen
so that means if we take a is nineteen then
it is reporting as a prime which should be
a probable prime but which is not a prime
ok so this is a false alarm kind of thing
then again if we choose so we can try for
another a ok
so if we say choose a is equal to say how
much a is equal to say a thirty one ok if
you choose a is equal to thirty one and then
if we compute this a to the power p minus
one is basically thirty one to the power forty
eight so this also you can verify this is
one mark congruent to one mod forty nine so
this is also telling us for this charge of
a this is also telling us forty nine is a
prime this thirty one ok
so now suppose we choose a is equal to say
five 
suppose we choose a is equal to five then
we compute a to the power p minus one which
is basically ah five to the power forty eight
so this is not equal to one mod p if you can
check
then we this will fermat test will be for
us forty nine is not a prime so this this
this decision is correct there is no published
probability in this design but earlier decision
probable prime pseudo prime prime based these
so that is the k so if it is if our primary
test is reporting that it is composite so
that is certain ok deterministic way we can
tell this is composite number but if our probabilistic
algorithm test is telling this is prime then
it could be a a it should be probable prime
basically ok so this is the ah farmers little
a farmers test primary test now we talk about
ah miller rabin primarity test that is also
probabilistic test
so this is miller robin so we have a p and
we have a odd integer p and we have to check
whether p is prime or not so idea of this
test is if so p is odd integer so if p is
prime so let p be a prime then what we have
then p minus one must be odd and that should
be written as two to the power k into m where
to a name are relatively prime otherwise if
they have a factor two again we can take that
two here so there should be some k such that
this k could be one also ok but at least there
should be one k because this is odd yeah this
is this is the prevail number because p is
odd number ok
so this is the case now by ah prima theorem
we know k to the power p minus one is congruent
to one mod p so that means a to the power
p minus one by two these are the square root
plus minus one so now what is a to the power
p minus one by two a to the power p minus
one by two is basically a to the power two
to the power k minus one into m is congruent
to plus minus one mod p so either it is plus
one or it is minus one
so either so either this is yeah so so this
can be written as a to the power m to the
power k minus one is congruent to either it
is plus one or minus one so this is the properties
of prime so this is the necessary condition
so if p have happened to be prime it should
satisfy this so if it is not satisfying this
then we can say p is not a prime number so
that is the test basically
so we we check whether a to the power we check
whether a to the power m is congruent to one
mod p or not if it is not then we check so
if we take this as b a to the power m then
we check whether it is actually plus minus
one then we check square of it we check the
b square is congruent to minus one mod p or
not like this so this condition must satisfy
so that is that will give us the ah well that
will give us a test necessary test so let
us write the test miller rabin my mighty test
so this is this is the code for miller rabin
ok so so p p we ah write it as this p two
to the power k into m where two and m are
g c d is one
so we compute b these two a to the power m
so again we have to choose a a again we have
to choose a a such that g c d of a and p is
one so this has to be there so we choose a
if if we randomly choose a a and we we it
happened that g c d is not one greater than
on then we put p is not prime so that part
i am no writing choose a such that g c d is
one then we compute this b a to the power
m mod p where m is this ok
now if if b is one mod p then we return p
could be a prime then return p thats prime
it should be probably prime for prime base
a something like that we have seen else else
for because now we have to check whether b
square b q they are giving us this minus one
or not if they are giving us minus one then
this condition is satisfied sorry so for i
is equal to one to k minus one we keep on
compute ah b so under this loop if b is congruent
to minus one mod p this fast is checking whether
this is plus minus one or not then also we
return t is composite hi sorry p is prime
it should be probable prime else so under
this for loop else we compute this b square
b is replaced by b square mod p and then we
run this loop and once we complete this loop
so six seven eight once if it completes this
loop then we return n is composite p is composite
p is composite ok
then we return p is composite because we have
tried for all plus because this is up to k
k minus one so this is so let us take an example
quick example for testing this if it is returning
composite then it is surely a composite number
there is no probabilistic sense but if it
is returning a prime then this is a probable
prime or pseudo prime base a we can say but
anyway this is the test so let us take some
example 
ok
suppose we choose n is equal to hundred and
five so we know hundred and five can be written
as this factor but we are pretending that
we do know this we are unable to factorize
this that we are pretending ok so now we want
to write this is sorry p now we were write
p to the two to the power k into m ah m n
two is relatively prime so hundred and five
is basically so p my sorry p minus one so
p minus one is basically hundred and four
so this should be written as two cube into
thirteen so here k is three m is thirteen
now we choose a a say a is eight such that
there is the g c d of a and p is one and then
we compute a to the power thirteen is a to
the power n mod hundred and five so this is
give us sixty four and then we compute again
six is four square mod so this is b so b square
again mod hundred and five this is giving
us one so that means sixty four is the square
root of one is the square root of one and
hence hundred and five cannot be a prime hundred
and five cannot be a prime so it is a composite
number ok since sixty it has a sixty four
is the square root of one hence hundred and
five cannot be a prime ok
so let us take another example say this example
we have seen n is equal to so forty nine just
now we use this example for format so let
us apply here also suppose we take p is equal
to forty nine so what is p minus one p minus
one is forty eight which is basically two
to the power four into three so m is three
and k is four
so what we do so if we choose the if we choose
a to be but we have seen if we choose a is
eighteen then eighteen to the power m mod
forty nine is one so which is giving us this
forty nine is a prime but base eighteen ok
similarly if we choose a is equal to nineteen
so nineteen to the power three mod forty nine
so this is also so this is basically congruent
to this is basically one so this is also basically
minus one so this also is giving forty nine
is a prime base nineteen like this ok now
if we choose a is equal to five sim then what
happened then we compute a to the power m
mod forty nine so this is our b so this is
basically twenty seven then we compute twenty
seven square mod forty nine this is basically
thirty six then again we compute thirty six
square mod that for loop this is basically
twenty two now we stop because k is equal
to four three times we have to do which is
not minus one so these implies forty nine
is not a prime this will not a prime so that
is guaranteed ok that is for sure thirty a
forty nine is not a prime otherwise if it
is reporting a prime so this should be probable
prime otherwise you have to check for all
possible a ok
thank you
