Skip to content

Commit

Permalink
feat: pretty printing for elliptic curves and their points
Browse files Browse the repository at this point in the history
  • Loading branch information
simonbrandhorst authored and thofma committed Nov 19, 2024
1 parent 39fc9cb commit 24e566e
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 46 deletions.
6 changes: 2 additions & 4 deletions docs/src/manual/elliptic_curves/basics.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,10 @@ by setting `check = false`.
julia> E = elliptic_curve(QQ, [1, 2]);
julia> E([1, -2])
Point (1 : -2 : 1) of Elliptic curve with equation
y^2 = x^3 + x + 2
(1 : -2 : 1)
julia> E([2, -4, 2])
Point (1 : -2 : 1) of Elliptic curve with equation
y^2 = x^3 + x + 2
(1 : -2 : 1)
```

```@docs
Expand Down
5 changes: 2 additions & 3 deletions docs/src/manual/elliptic_curves/finite_fields.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@ end

Return a random point on the elliptic curve $E$ defined over a finite field.

```jldoctest; filter = r"Point.*"
```jldoctest; filter = r"\(.*"
julia> E = elliptic_curve(GF(3), [1, 2]);
julia> rand(E)
Point (2 : 0 : 1) of Elliptic curve with equation
y^2 = x^3 + x + 2
(2 : 0 : 1)
```

## Cardinality and orders
Expand Down
74 changes: 52 additions & 22 deletions src/EllCrv/EllCrv.jl
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,16 @@ disabled by setting `check = false`.
```jldoctest
julia> elliptic_curve(QQ, [1, 2, 3, 4, 5])
Elliptic curve with equation
y^2 + x*y + 3*y = x^3 + 2*x^2 + 4*x + 5
Elliptic curve
over rational field
with equation
y^2 + x*y + 3*y = x^3 + 2*x^2 + 4*x + 5
julia> elliptic_curve(GF(3), [1, 1])
Elliptic curve with equation
y^2 = x^3 + x + 1
Elliptic curve
over prime field of characteristic 3
with equation
y^2 = x^3 + x + 1
```
"""
elliptic_curve
Expand Down Expand Up @@ -283,12 +287,16 @@ disabled by setting `check = false`.
julia> Qx, x = QQ["x"];
julia> elliptic_curve(x^3 + x + 1)
Elliptic curve with equation
y^2 = x^3 + x + 1
Elliptic curve
over rational field
with equation
y^2 = x^3 + x + 1
julia> elliptic_curve(x^3 + x + 1, x)
Elliptic curve with equation
y^2 + x*y = x^3 + x + 1
Elliptic curve
over rational field
with equation
y^2 + x*y = x^3 + x + 1
```
"""
function elliptic_curve(f::PolyRingElem{T}, h::PolyRingElem{T} = zero(parent(f)); check::Bool = true) where T
Expand Down Expand Up @@ -322,8 +330,10 @@ julia> K = GF(3)
Prime field of characteristic 3
julia> elliptic_curve_from_j_invariant(K(2))
Elliptic curve with equation
y^2 + x*y = x^3 + 1
Elliptic curve
over prime field of characteristic 3
with equation
y^2 + x*y = x^3 + 1
```
"""
function elliptic_curve_from_j_invariant(j::FieldElem)
Expand Down Expand Up @@ -608,12 +618,10 @@ by setting `check = false`.
julia> E = elliptic_curve(QQ, [1, 2]);
julia> E([1, -2])
Point (1 : -2 : 1) of Elliptic curve with equation
y^2 = x^3 + x + 2
(1 : -2 : 1)
julia> E([2, -4, 2])
Point (1 : -2 : 1) of Elliptic curve with equation
y^2 = x^3 + x + 2
(1 : -2 : 1)
```
"""
function (E::EllipticCurve{T})(coords::Vector{S}; check::Bool = true) where {S, T}
Expand Down Expand Up @@ -778,8 +786,31 @@ end
#
################################################################################

function show(io::IO, ::MIME"text/plain", E::EllipticCurve)
io = pretty(io)
println(io, "Elliptic curve")
print(io, Indent(), "over ", Lowercase())
print(io, base_field(E))
println(io, Dedent())
println(io, "with equation")
print(io, Indent())
_print_equation(io, E)
print(io, Dedent())

Check warning on line 798 in src/EllCrv/EllCrv.jl

View check run for this annotation

Codecov / codecov/patch

src/EllCrv/EllCrv.jl#L797-L798

Added lines #L797 - L798 were not covered by tests
end

function show(io::IO, E::EllipticCurve)
print(io, "Elliptic curve with equation\n")
if is_terse(io)
print(io, "Elliptic curve")
return

Check warning on line 804 in src/EllCrv/EllCrv.jl

View check run for this annotation

Codecov / codecov/patch

src/EllCrv/EllCrv.jl#L804

Added line #L804 was not covered by tests
end
io = pretty(io)
print(io, "Elliptic curve over ", Lowercase())
print(terse(io), base_field(E))

Check warning on line 808 in src/EllCrv/EllCrv.jl

View check run for this annotation

Codecov / codecov/patch

src/EllCrv/EllCrv.jl#L807-L808

Added lines #L807 - L808 were not covered by tests
print(io, " with equation ")
_print_equation(io, E)

Check warning on line 810 in src/EllCrv/EllCrv.jl

View check run for this annotation

Codecov / codecov/patch

src/EllCrv/EllCrv.jl#L810

Added line #L810 was not covered by tests
end

function _print_equation(io::IO, E::EllipticCurve)
a1, a2, a3, a4, a6 = a_invariants(E)
sum = Expr(:call, :+)
push!(sum.args, Expr(:call, :^, :y, 2))
Expand Down Expand Up @@ -831,8 +862,10 @@ function show(io::IO, E::EllipticCurve)
print(io, AbstractAlgebra.expr_to_string(AbstractAlgebra.canonicalize(sum)))
end


function show(io::IO, P::EllipticCurvePoint)
print(io, "Point ($(P[1]) : $(P[2]) : $(P[3])) of $(P.parent)")
io = pretty(io)
print(io, "($(P[1]) : $(P[2]) : $(P[3]))")

Check warning on line 868 in src/EllCrv/EllCrv.jl

View check run for this annotation

Codecov / codecov/patch

src/EllCrv/EllCrv.jl#L867-L868

Added lines #L867 - L868 were not covered by tests
end


Expand All @@ -856,8 +889,7 @@ julia> E = elliptic_curve(QQ, [1, 2]);
julia> P = E([1, -2]);
julia> P + P
Point (-1 : 0 : 1) of Elliptic curve with equation
y^2 = x^3 + x + 2
(-1 : 0 : 1)
```
"""
function +(P::EllipticCurvePoint{T}, Q::EllipticCurvePoint{T}) where T
Expand Down Expand Up @@ -1150,10 +1182,8 @@ julia> E = elliptic_curve(QQ, [1, 2]);
julia> division_points(infinity(E), 2)
2-element Vector{EllipticCurvePoint{QQFieldElem}}:
Point (0 : 1 : 0) of Elliptic curve with equation
y^2 = x^3 + x + 2
Point (-1 : 0 : 1) of Elliptic curve with equation
y^2 = x^3 + x + 2
(0 : 1 : 0)
(-1 : 0 : 1)
```
"""
function division_points(P::EllipticCurvePoint, m::S) where S<:Union{Integer, ZZRingElem}
Expand Down
17 changes: 6 additions & 11 deletions src/EllCrv/Finite.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1140,22 +1140,19 @@ Return a list of generators of the group of rational points on $E$.
# Examples
```jldoctest; filter = r"Point.*"
```jldoctest; filter = r"\(.*"
julia> E = elliptic_curve(GF(101, 2), [1, 2]);
julia> gens(E)
2-element Vector{EllipticCurvePoint{FqFieldElem}}:
Point (16*o + 42 : 88*o + 97 : 1) of Elliptic curve with equation
y^2 = x^3 + x + 2
Point (88*o + 23 : 94*o + 22 : 1) of Elliptic curve with equation
y^2 = x^3 + x + 2
(13*o + 83 : 90*o + 25 : 1)
(61*o + 62 : 19*o + 24 : 1)
julia> E = elliptic_curve(GF(101), [1, 2]);
julia> gens(E)
1-element Vector{EllipticCurvePoint{FqFieldElem}}:
Point (85 : 58 : 1) of Elliptic curve with equation
y^2 = x^3 + x + 2
(27 : 57 : 1)
```
"""
function gens(E::EllipticCurve{T}) where {T <: FinFieldElem}
Expand Down Expand Up @@ -1235,12 +1232,10 @@ argument.
julia> E = elliptic_curve(GF(101), [1, 2]);
julia> P = E([6, 74])
Point (6 : 74 : 1) of Elliptic curve with equation
y^2 = x^3 + x + 2
(6 : 74 : 1)
julia> Q = E([85, 43])
Point (85 : 43 : 1) of Elliptic curve with equation
y^2 = x^3 + x + 2
(85 : 43 : 1)
julia> disc_log(P, Q)
13
Expand Down
13 changes: 7 additions & 6 deletions src/EllCrv/Isomorphisms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -615,15 +615,16 @@ end
#
################################################################################


function show(io::IO, f::EllCrvIso)
function show(io::IO, ::MIME"text/plain", f::EllCrvIso)
io = pretty(io)

Check warning on line 619 in src/EllCrv/Isomorphisms.jl

View check run for this annotation

Codecov / codecov/patch

src/EllCrv/Isomorphisms.jl#L618-L619

Added lines #L618 - L619 were not covered by tests
E1 = domain(f)
E2 = codomain(f)
fx, fy, fz = rational_maps(f)
print(io, "Isomorphism from
$(E1) to \n
$(E2) given by \n
(x : y : 1) -> ($(fx) : $(fy) : $(fz) )")
println(io, "Isomorphism")
println(io, Indent(), "from ", E1)
println(io, "to ", E2)
print(io,"given by ")
print(io, "(x : y : 1) -> ($(fx) : $(fy) : $(fz) )")

Check warning on line 627 in src/EllCrv/Isomorphisms.jl

View check run for this annotation

Codecov / codecov/patch

src/EllCrv/Isomorphisms.jl#L623-L627

Added lines #L623 - L627 were not covered by tests
end

################################################################################
Expand Down

0 comments on commit 24e566e

Please sign in to comment.