Skip to content

Commit

Permalink
fixing convert methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Janis Erdmanis committed Oct 31, 2024
1 parent 09a3dff commit c7a3f91
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "CryptoGroups"
uuid = "bc997328-bedd-407e-bcd3-5758e064a52d"
authors = ["Janis Erdmanis <[email protected]>"]
version = "0.5.2"
version = "0.5.3"

[deps]
CryptoPRG = "d846c407-34c1-46cb-aa27-d51818cc05e2"
Expand Down
2 changes: 1 addition & 1 deletion src/Curves/ecpoint.jl
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ end

Base.:*(n::Integer, x::ECPoint; skip_validation = false) = *(x, n; skip_validation)

Base.convert(::Type{ECPoint{P, S}}, x::NTuple{2}; allow_zero=false) where {P <: AbstractPoint, S} = ECPoint{P, S}(convert(P, x); allow_zero)
Base.convert(::Type{ECPoint{P, S}}, x; allow_zero=false) where {P <: AbstractPoint, S} = ECPoint{P, S}(convert(P, x); allow_zero)
Base.convert(::Type{P}, x::P) where P <: ECPoint = x

"""
Expand Down
4 changes: 2 additions & 2 deletions src/groups.jl
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ Converts representation of `x` into a group element type `G`. The conversion is
group constructors. In case identity element needs to be read into `allow_one` flag can be used to allow constructing identity elements.
"""
Base.convert(::Type{ECGroup{P}}, x; allow_one=false) where P <: ECPoint = ECGroup{P}(convert(P, x; allow_zero=allow_one))
Base.convert(::Type{G}, x::G) where G <: ECGroup = x
Base.convert(::Type{G}, x::G) where G <: Group = x


"""
Expand Down Expand Up @@ -277,7 +277,7 @@ Converts modulus prime group element into octet representation. A padding is add
"""
octet(x::PGroup) = int2octet(value(x), bitlength(modulus(x)))

Base.convert(group::Type{<:PGroup}, element::Vector{UInt8}) = convert(group, octet2int(element))
Base.convert(group::Type{<:PGroup}, element::Vector{UInt8}; allow_one::Bool=false) = convert(group, octet2int(element); allow_one)
PGroup{S}(element::Vector{UInt8}) where S = convert(PGroup{S}, element)

"""
Expand Down
39 changes: 38 additions & 1 deletion src/spec.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using .Utils: StaticBitVector, StaticBigInt, static
using .Utils: StaticBitVector, StaticBigInt, static, @check
using .Fields: F2GNB, F2PB, @F2PB, FP, Field, PrimeField, BinaryField, tobits, value, reducer
using .Curves: AbstractPoint, ECPoint, AffinePoint, Weierstrass, BinaryCurve, gx, gy, field, eq, a, b, cofactor
using .Specs: MODP, Koblitz, ECP, EC2N, GroupSpec, PB, GNB, curve
Expand Down Expand Up @@ -160,3 +160,40 @@ spec(::Type{G}) where G <: PGroup = MODP(; p = modulus(G), q = order(G))
(::Type{P})() where P <: ECPoint = P(generator(curve(name(P))))
(::Type{ECGroup{P}})() where P <: ECPoint = ECGroup{P}(P())
(::Type{G})() where G <: PGroup = G(generator(modp_spec(name(G))))


# Shall be added to CryptoGroups
iscompatable(x::GroupSpec, y::GroupSpec) = false

function iscompatable(x::ECP, y::ECP)

x.p == y.p || return false
x.n == y.n || return false
x.a == y.a || return false
x.b == y.b || return false
x.cofactor == y.cofactor || return false

return true
end

function iscompatable(x::EC2N, y::EC2N)

x.basis == y.basis || return false
x.n == y.n || return false
x.a == y.a || return false
x.b == y.b || return false
x.cofactor == y.cofactor || return false

return true
end

iscompatable(G::Type{<:Group}, Q::Type{<:Group}) = iscompatable(spec(G), spec(Q))


function _convert(G::Type{<:Group}, q::Group)
@check iscompatable(G, typeof(q)) "Conversion not possible as group types represent different specifications"
return G(octet(q))
end

Base.convert(G::Type{<:Group}, q::Group) = _convert(G, q)
Base.convert(G::Type{ECGroup{P}}, q::Group) where P <: ECPoint = _convert(G, q)
6 changes: 5 additions & 1 deletion test/groups.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Test
import CryptoGroups: PGroup, @PGroup, order, modulus, value, concretize_type, Specs, generator
import CryptoGroups: PGroup, @PGroup, order, modulus, value, concretize_type, Specs, generator, octet

G = @PGroup{p = 23, q = 11}
q = order(G)
Expand Down Expand Up @@ -57,6 +57,10 @@ function testgroup(g::G) where G <: Group

@test one(g) * g == g
@test g * one(g) == g

@test convert(G, value(one(G)); allow_one=true) == one(G)
@test convert(G, octet(one(G)); allow_one=true) == one(G)

end

G = @PGroup{p = 23, q = 11}
Expand Down

2 comments on commit c7a3f91

@JanisErdmanis
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/118461

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.5.3 -m "<description of version>" c7a3f91f459870fb5a132110eec2fb20af22cbea
git push origin v0.5.3

Please sign in to comment.