29
loading...
This website collects cookies to deliver better user experience
/
and ^
, but even plain numbers are not quite the same:#!/usr/bin/env sage
# XOR in Python, ** in SageMath
print(3^4)
# Floats in Python, Rationals in SageMath
print(2/3)
# All numbers are wrapped in special classes
print(type(2))
print(type(2/3))
$ sage syntax.sage
81
2/3
<class 'sage.rings.integer.Integer'>
<class 'sage.rings.rational.Rational'>
$ python3 syntax.sage
7
0.6666666666666666
<class 'int'>
<class 'float'>
# This file was *autogenerated* from the file syntax.sage
from sage.all_cmdline import * # import sage library
_sage_const_3 = Integer(3)
_sage_const_4 = Integer(4)
_sage_const_2 = Integer(2)
#!/usr/bin/env sage
# XOR in Python, ** in SageMath
print(_sage_const_3 ** _sage_const_4)
# Floats in Python, Rationals in SageMath
print(_sage_const_2 / _sage_const_3)
# All numbers are wrapped in special classes
print(type(_sage_const_2))
print(type(_sage_const_2 / _sage_const_3))
#!/usr/bin/env sage
print("limit of sin(x) / x as x approaches 0")
print(limit(sin(x) / x, x=0))
print("limit of sin(x) / x as x approaches infinity")
print(limit(sin(x) / x, x=oo))
print("limit of (1+1/x)^x as x approaches infinity")
print(limit((1+1/x)^x, x=oo))
$ ./limits.sage
limit of sin(x) / x as x approaches 0
1
limit of sin(x) / x as x approaches infinity
0
limit of (1+1/x)^x as x approaches infinity
e
x
, sin
, oo
are all symbolic, not what you'd expect in plain Python.oo
alias for Infinity is just two o
s, not the Unicode infinite character. It's probably best for ease of typing, but it's somewhat annoying that Python doesn't support ∞ in variable names, as it's technically not a "letter". Raku has ∞ out of the box, and Julia doesn't by default, but you can do ∞ = inf
in Julia.#!/usr/bin/env sage
print("diff of (x+2)*(x-2):")
print(diff((x+2)*(x-2), x))
print("diff of cos(x^3) / x^3:")
print(diff(cos(x^3) / x^3, x))
./diff.sage
diff of (x+2)*(x-2):
2*x
diff of cos(x^3) / x^3:
-3*sin(x^3)/x - 3*cos(x^3)/x^4
#!/usr/bin/env sage
f = 1/x
print("Integral of f(x) = 1/x from 5 to 10:")
print(f.integral(x, 5, 10))
print("Numerical approximation to 100 binary digits:")
print(f.integral(x, 5, 10).n(100))
$ ./integral.sage
Integral of f(x) = 1/x from 5 to 10:
log(10) - log(5)
Numerical approximation to 100 binary digits:
0.69314718055994530941723212146
#!/usr/bin/env sage
p = 65537
F = GF(p)
print("Some addition in GF(65537):")
for i in range(10):
a = F.random_element()
b = F.random_element()
print(f"{a} + {b} = {a + b}")
print("Some inverses in GF(65537):")
for i in range(10):
a = F.random_element()
b = a^-1
print(f"{a} * {b} = {a * b}")
./zp.sage
Some addition in GF(65537):
5993 + 52077 = 58070
34573 + 35447 = 4483
64465 + 9539 = 8467
23830 + 64214 = 22507
33632 + 5984 = 39616
32744 + 55138 = 22345
7174 + 12223 = 19397
12815 + 49721 = 62536
21107 + 51922 = 7492
32436 + 21708 = 54144
Some inverses in GF(65537):
4454 * 21880 = 1
8250 * 31990 = 1
2139 * 17403 = 1
34523 * 59411 = 1
44260 * 56663 = 1
3474 * 34655 = 1
53770 * 11490 = 1
12588 * 61575 = 1
34995 * 24902 = 1
2534 * 14354 = 1
#!/usr/bin/env sage
F = GF(101)
EC = EllipticCurve(F, [2, 3])
for i in range(10):
a = EC.random_element()
b = EC.random_element()
print(f"{a} + {b} = {a + b}")
./ecc.sage
(84 : 56 : 1) + (41 : 15 : 1) = (40 : 94 : 1)
(41 : 15 : 1) + (50 : 41 : 1) = (57 : 51 : 1)
(0 : 1 : 0) + (90 : 93 : 1) = (90 : 93 : 1)
(27 : 67 : 1) + (56 : 30 : 1) = (35 : 86 : 1)
(50 : 41 : 1) + (11 : 89 : 1) = (20 : 93 : 1)
(30 : 55 : 1) + (30 : 55 : 1) = (35 : 15 : 1)
(9 : 12 : 1) + (92 : 93 : 1) = (96 : 26 : 1)
(49 : 40 : 1) + (40 : 94 : 1) = (48 : 55 : 1)
(81 : 89 : 1) + (35 : 15 : 1) = (21 : 69 : 1)
(99 : 71 : 1) + (0 : 1 : 0) = (99 : 71 : 1)
#!/usr/bin/env sage
# "Export Grade" Diffie-Hellman Key Exchange
K = GF(57702167561280060733)
g = K(2)
# We don't know these numbers, CTF server does
secret_a = 21994334292003664313
secret_b = 5307079022839176516
# What we get from the challenge is these:
# Alice sent to Bob: g^a
# Bob sent to Alice: g^a
ga = K(20214349094702392183)
gb = K(36652172046887073403)
# This is fast only because SageMath knows all the fancy algorithms
# It would be completely non-viable to brute force
# and extremely tedious and slow to code yourself:
a = discrete_log(ga, g)
gab = gb ^ a
print(f"We hacked the key: {gab}")
# CTF server can verify our solution:
print(f"Alice shared key: {gb^secret_a}")
print(f"Bob shared key: {ga^secret_b}")
$ ./mitm.sage
We hacked the key: 44661687795688390997
Alice shared key: 44661687795688390997
Bob shared key: 44661687795688390997