Skip to content

Commit

Permalink
Add tests for elliptic curves
Browse files Browse the repository at this point in the history
  • Loading branch information
Samayel committed Dec 20, 2015
1 parent 8dd046d commit 99eb3d0
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Math/ec.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module EllipticCurves
@reexport module EllipticCurves

# http://jeremykun.com/2014/02/08/introducing-elliptic-curves/
# http://jeremykun.com/2014/02/24/elliptic-curves-as-python-objects/
Expand Down
1 change: 1 addition & 0 deletions src/Math/math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ macro activate_matlab(); Expr(:using, :MATLAB); end

include("gmp.jl")
include("mpfr.jl")
include("ec.jl")
include("Series/series.jl")
include("NumberTheory/numbertheory.jl")
include("Combinatorics/combinatorics.jl")
Expand Down
96 changes: 96 additions & 0 deletions test/Math/ec.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
module EllipticCurves

using Base.Test
using Brainstorm.Math

function test_wnf_QQ()
c = curve(big(-2//1), big(4//1))
p = point(c, 3, 5)
q = point(c, -2, 0)

@test p-q == point(c, 0, -2)
@test p+p+p+p+p == point(c, 2312883//1142761, -3507297955//1221611509)
@test 5p == point(c, 2312883//1142761, -3507297955//1221611509)
@test q-3p == point(c, 240, 3718)
@test -20p == point(c,
872171688955240345797378940145384578112856996417727644408306502486841054959621893457430066791656001//520783120481946829397143140761792686044102902921369189488390484560995418035368116532220330470490000,
-27483290931268103431471546265260141280423344817266158619907625209686954671299076160289194864753864983185162878307166869927581148168092234359162702751//11884621345605454720092065232176302286055268099954516777276277410691669963302621761108166472206145876157873100626715793555129780028801183525093000000)
end

function test_wnf_ZZp()
R = ResidueRing(ZZ, 5)

c = curve(R(1), R(1))
p = point(c, R(2), R(1))

@test 2p == point(c, R(2), R(4))
@test 3p == point(c)
end

function test_wnf_polyZZp()
R = ResidueRing(ZZ, 5)
S, s = PolynomialRing(R, "s")
T, t = FiniteField(3 + s^2, "t")

c = curve(T(1), T(1))
p = point(c, 2 + t, 2t)

@test -p == point(c, 2 + t, 3t)
@test p+p == point(c, 3 + t, 2 + 0t)
@test 4p == point(c, 3 + 2t, 4 + 4t)
@test 9p == point(c)
end

function test_gwnf_QQ()
c = curve(0, 0, 0, big(-2//1), big(4//1))
p = point(c, 3, 5)
q = point(c, -2, 0)

@test p-q == point(c, 0, -2)
@test p+p+p+p+p == point(c, 2312883//1142761, -3507297955//1221611509)
@test 5p == point(c, 2312883//1142761, -3507297955//1221611509)
@test q-3p == point(c, 240, 3718)
@test -20p == point(c,
872171688955240345797378940145384578112856996417727644408306502486841054959621893457430066791656001//520783120481946829397143140761792686044102902921369189488390484560995418035368116532220330470490000,
-27483290931268103431471546265260141280423344817266158619907625209686954671299076160289194864753864983185162878307166869927581148168092234359162702751//11884621345605454720092065232176302286055268099954516777276277410691669963302621761108166472206145876157873100626715793555129780028801183525093000000)
end

function test_gwnf_ZZp()
R = ResidueRing(ZZ, 5)

c = curve(R(0), R(0), R(0), R(1), R(1))
p = point(c, R(2), R(1))

@test 2p == point(c, R(2), R(4))
@test 3p == point(c)
end

function test_gwnf_polyZZp()
R = ResidueRing(ZZ, 5)
S, s = PolynomialRing(R, "s")
T, t = FiniteField(3 + s^2, "t")

c = curve(T(0), T(0), T(0), T(1), T(1))
p = point(c, 2 + t, 2t)

@test -p == point(c, 2 + t, 3t)
@test p+p == point(c, 3 + t, 2 + 0t)
@test 4p == point(c, 3 + 2t, 4 + 4t)
@test 9p == point(c)
end

function test_all()
print(rpad("Math.EllipticCurves...", 50, ' '))

test_wnf_QQ()
test_wnf_ZZp()
test_wnf_polyZZp()

test_gwnf_QQ()
test_gwnf_ZZp()
test_gwnf_polyZZp()

println("PASS")
end

end
3 changes: 3 additions & 0 deletions test/Math/math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ function test_math_setbits()
@test setbits(big(0), [1000]) == big(2)^1000
end

include("ec.jl")
include("Series/series.jl")
include("NumberTheory/numbertheory.jl")
include("Combinatorics/combinatorics.jl")
Expand All @@ -30,6 +31,8 @@ function test_all()

println("PASS")

println("")
EllipticCurves.test_all()
println("")
Series.test_all()
println("")
Expand Down

0 comments on commit 99eb3d0

Please sign in to comment.