Exemple #1
0
 def test_fermat(self):
     # Test for a known composite
     self.assertEqual(fermat.fermat(10, 3), 'composite')
     # Test for a known prime
     self.assertEqual(fermat.fermat(2621, 200), 'prime')
     # Test for a known Carmichael number
     self.assertEqual(fermat.fermat(8910, 3000), 'composite')
def batch(start,end,k=2,bases=[]):
	primesFound = 0
	if bases==[]:
		for n in xrange(start,end):
			if(fermat(n,k)): primesFound+=1
	else:
		for n in xrange(start,end):
			if(fermat(n,bases=bases)): primesFound+=1
	return primesFound
Exemple #3
0
    def test_fermat(self):
        result = fermat(5, 100)
        self.assertTrue(result)

        result = fermat(14, 100)
        self.assertFalse(result)

        result = fermat(17, 100)
        self.assertTrue(result)
    def fermat(self, fermat_timeout=60):
        # Try an attack where the primes are too close together from BKPCTF2016 - sourcekris
        # this attack module can be optional
        try:
            from fermat import fermat
        except ImportError:
            if self.args.verbose:
                print(
                    "[!] Warning: Fermat factorization module missing (fermat.py)"
                )
            return

        try:
            with timeout(seconds=fermat_timeout):
                self.pub_key.p, self.pub_key.q = fermat(self.pub_key.n)
        except FactorizationError:
            return

        if self.pub_key.q is not None:
            self.priv_key = PrivateKey(int(self.pub_key.p),
                                       int(self.pub_key.q),
                                       int(self.pub_key.e),
                                       int(self.pub_key.n))

        return
Exemple #5
0
    def fermat(self, fermat_timeout=60):
        # Try an attack where the primes are too close together from BKPCTF2016 - sourcekris
        # this attack module can be optional
        try:
            from fermat import fermat
        except ImportError:
            if self.args.verbose:
                print "[*] Warning: Fermat factorization module missing (fermat.py)"
            return

        try:
            with timeout(seconds=fermat_timeout):   
                self.pub_key.p, self.pub_key.q = fermat(self.pub_key.n)    
        except FactorizationError:
            return

        if self.pub_key.q is not None:
           self.priv_key = PrivateKey(long(self.pub_key.p), long(self.pub_key.q),
                                      long(self.pub_key.e), long(self.pub_key.n))

        return
#!/usr/bin/env python

# Find all primes in the given range using Fermat's Primality Test

import sys
from fermat import fermat
from writeresults import writeresults

args = xrange(int(sys.argv[1]),int(sys.argv[2])+1)

primesFound = 0

for num in args:
	if(fermat(num)):
		primesFound += 1

writeresults("fermat",int(sys.argv[2])-int(sys.argv[1])+1,int(primesFound))
Exemple #7
0
p=17
q=29
n=493

print("Ejercicio 6. Raiz cuadrada modular pro")
print("Parámetros: a=",a," p=",p,"q=",q," n=",n)
print("Solucion: ",sqrt_mod_2(a,p,q,n))
t = timeit.Timer(lambda: sqrt_mod_2(a,p,q,n))
print("Tiempo de ejecución medio(",n_ejec,"ejecuciones): ",t.timeit(n_ejec)/n_ejec)
print("---------------------------------------------------------")

n=5959

print("Ejercicio 7. Metodo de Fermat")
print("Parámetros: n=",n)
print("Solucion: ",fermat(n))
t = timeit.Timer(lambda: fermat(n))
print("Tiempo de ejecución medio(",n_ejec,"ejecuciones): ",t.timeit(n_ejec)/n_ejec)
print("---------------------------------------------------------")

n=455459

print("Ejercicio 7. Rho de Pollard")
print("Parámetros: n=",n)
print("Solucion: ",pollard(n))
t = timeit.Timer(lambda: pollard(n))
print("Tiempo de ejecución medio(",n_ejec,"ejecuciones): ",t.timeit(n_ejec)/n_ejec)
print("---------------------------------------------------------")


Exemple #8
0
 def test_false_primality_fermat(self):
     png = fermat(10, 100, 10000)
     self.assertFalse(png.primality_test(297))
Exemple #9
0
 def test_true_primality_fermat(self):
     png = fermat(10, 100, 10000)
     self.assertTrue(png.primality_test(8837))
Exemple #10
0
#!/usr/bin/env python

from fermat import fermat

p, q = fermat(
    0x1C20BDC017E3CAA3C579B40D439E2ECD70F12C4D7F2764784C95A3FDDBA00981BA9CE5B227ADE47B0A7A0A8ACABA4541AB95C52F6B6DE3DF9EC090C6C356445B21BE437ABE10214D0B4A398A96743BBF70C864687FB2EC929F01D6EDAB2D987FE09799AD2204A2704F33061DBF9C2E03B332F0BA1A446644C864A06CD586D480B
)

print p, q
Exemple #11
0
from lfg import LaggedFibonacciGenerator
from lcg import LinearCongruentialGenerator
from millerRabbin import millerRabbin
from fermat import fermat

lfg = LaggedFibonacciGenerator(bits=1024)
lcg = LinearCongruentialGenerator()
number = lcg.next()

while (millerRabbin(number, 10) != True):
    number = lfg.next()
    print(number)
    print(millerRabbin(number, 10))
    print(fermat(number))