SageMath 7.4
File
Edit
View
Insert
Cell
Kernel
Widgets
Help
CellToolbar
In [3]:
ry GCD Algorithm
# 1.3 Binary GCD Algorithm
def gcd(u, v):
""" This function will calcluate
GCD of given two numbers.
If the input is negative, both the
numbers are converted to positive
before the calculation
"""
if u == v:
return u
elif u == 0:
return v
elif v == 0:
return u
# u is even
elif u & 1 == 0:
# v is even
print 'v is even'
if v & 1 == 0:
print 'u is even as well'
return 2*gcd(u >> 1, v >> 1)
# v is odd
else:
return gcd(u >> 1, v)
# u is odd
elif u & 1 != 0:
# v is even
if v & 1 == 0:
return gcd(u, v >> 1)
# v is odd and u is greater than v
elif u > v and v & 1 != 0:
return gcd((u-v) >> 1, v)
# v is odd and u is smaller than v
else:
return gcd((v-u) >> 1, u)
gcd(87654321,12345678)
?
#use this when solving the binary gcd
?
u = 87654321
v = 6172839
z = (u-v)/2
?
print z
?
v is even
v is even
v is even
v is even
v is even
v is even
v is even
v is even
40740741
In [26]:
6
# 2.1 FIND PRIME FACTORS OF A NUMBER
factor(50)
(8*6)%10
Out[442]:
8
In [53]:
5 ^ 1 = 5 mod 613
5 ^ 2 = 25 mod 613
5 ^ 4 = 12 mod 613
5 ^ 8 = 144 mod 613
5 ^ 16 = 507 mod 613
5 ^ 32 = 202 mod 613
5 ^ 64 = 346 mod 613
5 ^ 128 = 181 mod 613
5 ^ 256 = 272 mod 613
5 ^ 512 = 424 mod 613
5 ^ 1024 = 167 mod 613
In [90]:
var ('y,x')
e = EllipticCurve(y^2==(x^3)+(2*x)-2)
P = e(1,1)
print 5*P
#5.1 Farmats Pseudoprime checkers
a = 3
N = 91
if a^(N-1)%N==1:
print 'It is a prime according to farmats little theorem but in real it is '
print is_prime(N)
###################################################################################
##############################################
#5.2 Carmicheal Number checker
a = 2
N = 561
countCoP = 0
count = 0
for i in [2..N-1]:
if gcd(i,N)==1:
countCoP = countCoP+1
if a^(N-1)%N==1:
count = count+1
print 'This is the total numbers of co primes for the number',countCoP
print 'This is the total numbers of co primes for which the farmats little theorem
holds',count
if countCoP == count:
print 'Entered Number is indeed a carmicheal number'
print factor(561)
###################################################################################
##############################################
#5.3 Prove a number is Carmicheal
var('n,k')
eq = [n==((6*k)+1)*((12*k)+1)*((18*k)+1)]
print solve(eq,n)
?
###################################################################################
##############################################
#5.4 Rabin Miller Test
n = 561
print 'Rabin Miller Test '
print factor(n-1)
#Take the values from the output in the form of p-1 = q x a^k
k = 4
q = 35
#Step 1 before entering the loop
R = (2^q)%n
print '2 ^',q, '=',R,' mod ',n
if R==1:
print 'The number is composite'
for i in [1..k-1]:
q=q*2
R = (R^2)%n
print '2 ^',q, '=',R,' mod ',n
if(R==1):
print 'The number is composite'
break
if(R==-1):
print 'The number is prime'
break
?
var ('y,x')
e = EllipticCurve(y^2==(x^3)+(2*x)-2)
P = e(1,1)
print 5*P
?
?
It is a prime according to farmats little theorem but in real it is
False
This is the total numbers of co primes for the number 319
This is the total numbers of co primes for which the farmats little theorem holds
319
Entered Number is indeed a carmicheal number
3 * 11 * 17
[
n == 1296*k^3 + 396*k^2 + 36*k + 1
]
Rabin Miller Test
2^4 * 5 * 7
2 ^ 35 = 263 mod 561
2 ^ 70 = 166 mod 561
2 ^ 140 = 67 mod 561
2 ^ 280 = 1 mod 561
The number is composite
(162905681/19321 : -2079238703569/2685619 : 1)
In [85]:
g = 2
k = 141312
p = 194813
?
#Generation of public key
a = (g^k)%p
print 'VALUE OF a',a
print 'a = ',a,' ? ',g,'^',k,' mod ',p
?
#Encrypting message
m = 173124
r = 136297
e1 = (g^r)%p
print 'VALUE OF e1',e1
print 'e1 = ',e1,' ? ',g,'^',r,' mod ',p
?
e2 = (m*(a^r))%p
print 'VALUE OF e2',e2
print 'e2 = ',e2,' ? ',m,'*',a,'^',r,' mod ',p
?
print 'Thus the message sent is (',e1,',',e2,')'
?
#Decrypting Message
c = (e1^k)%p
print 'VALUE OF c',c
print 'c = ',c,' ? ',e1,'^',k,' mod ',p
?
#now we calculate inverse of the mod
im = inverse_mod(c,p)
dM = (e2*im)%p
print 'Decyrpted Message = ',dM,' ? ',e2,'*',im,' mod ',p
?
?
?
sqrt(34).n()
125%11
print (7^6)%11
print 3^4
print 5^3
print 7^2
print 49%11
for i in range(10):
z=i^2
if (z)%11==4:
print i
?
factor(253*5)
a = 2
2^6
gcd(4,13)
?
In [143]:
?
VALUE OF a IS 12
THIS IS THE ANSWER 4717
I am the factor 89 113
False
2^4 * 7
1
In [449]:
for r in A:
for t in B:
if r == t:
x1 = A.index(r)
x2 = B.index(t)
print x1,x2
break
#8.1 Calculate the third point on elliptic curve if the first and second points are
known
# eq =[(y^2) == (x^3)+a*(x^2)+(b*x)+c]
a = 0
b = 0
c = 17
x1 = -2
y1 = 3
x2 = -1
y2 = 4
m = (y2-y1)/(x2-x1)
x3 = (-a)+(m^2)-x1-x2
y3 = y1+m*(-a+(m^2)-(2*x1)-x2)
print x3,',',y3
4 , 9
In [264]:
#8.2 Calculate the second point on an elliptic curve if only one point is known
# eq =[(y^2) == (x^3)+a*(x^2)+(b*x)+c]
a = 0
b = 0
c = 17
x1 = -2
y1 = 3
m = (3*(x1^2)+(2*a*x1)+b)/(2*y1)
x2 = (-a)+(m^2)-2*x1
y2 = y1+m*(-a+(m^2)-(3*x1))
print x2,',',y2
8 , 23
In [445]:
2^5
38 1446
1
I am counting 0
1446 2027 581 1
Value of x2 is 2027
value of x1 is 2027
I am counting 1
30420 30846 426 71
done bro
71
Out[410]:
32
In [418]:
#polards ro -1 factors
B = 12
factorOfB = factorial(4)
N = 1711
for i in [2..20]:
k = ((i^factorOfB))%N
g = gcd(k-1,N)
if gcd(k-1,N)>1:
print 'VALUE OF a IS ',i
print 'THIS IS THE ANSWER',k-1
otherfactor = N/g
print 'I am the factor',g,otherfactor
break
?
VALUE OF a IS 12
THIS IS THE ANSWER 174
I am the factor 29 59
In [436]:
var ('y,x')
e = EllipticCurve(y^2==(x^3)+(2*x)-2)
P = e(1,1)
print P
(1 : 1 : 1)
Signature: factor(n, proof=None, int_=False, algorithm='pari', verbose=0,
**kwds)
Docstring:
Returns the factorization of "n". The result depends on the type
of "n".
sage: f(n)=n^2
sage: is_prime(f(3))
False
sage: factor(f(3))
9
INPUT:
* "algorithm" - string
OUTPUT:
* factorization of n
EXAMPLES:
sage: factor(500)
2^2 * 5^3
sage: factor(-20)
-1 * 2^2 * 5
sage: f=factor(-20)
sage: list(f)
[(2, 2), (5, 1)]
sage: f.unit()
-1
sage: f.value()
-20
sage: factor( -next_prime(10^2) * next_prime(10^7) )
-1 * 101 * 10000019
sage: factor(0)
Traceback (most recent call last):
...
ArithmeticError: Prime factorization of 0 not defined.
sage: factor(1)
1
sage: factor(-1)
-1
sage: factor(2^(2^7)+1)
59649589127497217 * 5704689200685129054721
Sage calls PARI's factor, which has proof False by default. Sage
has a global proof flag, set to True by default (see
"sage.structure.proof.proof", or proof.[tab]). To override the
default, call this function with proof=False.
Any object which has a factor method can be factored like this:
sage: f = factor(420); f
2^2 * 3 * 5 * 7
sage: [x for x in f]
[(2, 2), (3, 1), (5, 1), (7, 1)]
sage: [p for p,e in f]
[2, 3, 5, 7]
sage: [e for p,e in f]
[2, 1, 1, 1]
sage: [p^e for p,e in f]
[4, 3, 5, 7]
Init docstring: x.__init__(...) initializes x; see help(type(x)) for signature
File: /opt/sagemath-7.4/local/lib/python2.7/site-
packages/sage/arith/misc.py
Type: function