Skip to content

Commit

Permalink
reducing overhead for openssl groups
Browse files Browse the repository at this point in the history
  • Loading branch information
Janis Erdmanis committed Nov 2, 2024
1 parent 67eaa55 commit 6642538
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 20 deletions.
4 changes: 2 additions & 2 deletions 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.4"
version = "0.6.0"

[deps]
CryptoPRG = "d846c407-34c1-46cb-aa27-d51818cc05e2"
Expand All @@ -11,7 +11,7 @@ Primes = "27ebfcd6-29c5-5fa9-bf4b-fb8fc14df3ae"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"

[compat]
CryptoPRG = "0.1"
CryptoPRG = "0.2"
CryptoUtils = "0.1"
Nettle = "1"
Primes = "0.5"
Expand Down
2 changes: 0 additions & 2 deletions src/CryptoGroups.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ See also `spec`
"""
function concretize_type end

# HEllo world

function order end
function name end # Needed because of Curves.
function value end
Expand Down
2 changes: 1 addition & 1 deletion src/Curves/conversions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ end

#function (::Type{P})(po::Vector{UInt8}) where P <: AbstractPoint

function Base.convert(::Type{P}, po::Vector{UInt8}) where P <: AbstractPoint
function Base.convert(::Type{P}, po::AbstractVector{UInt8}) where P <: AbstractPoint

pc = po[1]

Expand Down
6 changes: 5 additions & 1 deletion src/Curves/ecpoint.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ function validate(x::P, order::Integer, cofactor::Integer) where P <: AbstractPo

oncurve(x) || throw(ArgumentError("Point is not in curve"))
#x * cofactor != zero(P) || throw(ArgumentError("Point is in cofactor subgroup"))
!iszero(x * cofactor) || throw(ArgumentError("Point is in cofactor subgroup"))
if cofactor != 1
!iszero(x * cofactor) || throw(ArgumentError("Point is in cofactor subgroup"))
end

return
end
Expand Down Expand Up @@ -100,6 +102,8 @@ end

ECPoint{P, S}(x; allow_zero=false, skip_validation=false) where {P <: AbstractPoint, S} = ECPoint{P, S}(convert(P, x); allow_zero, skip_validation)

Base.iszero(x::ECPoint) = iszero(x.p)

"""
concretize_type(::Type{ECPoint{P}}, order::Integer, cofactor::Integer; name=nothing) where P <: AbstractPoint
Expand Down
11 changes: 7 additions & 4 deletions src/Fields/galois_fields.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,14 @@ end
Base.convert(::Type{BigInt}, x::PrimeField) = x.x

value(x::FP) = x.x
modulus(::FP{P}) where P = BigInt(P)
modulus(::Type{FP{P}}) where P = BigInt(P)
modulus(::FP{P}) where P = modulus(FP{P})
@generated modulus(::Type{FP{P}}) where P = BigInt(P)

order(::FP{P}) where P = BigInt(P)
order(::Type{FP{P}}) where P = BigInt(P)
order(::FP{P}) where P = order(FP{P})
@generated order(::Type{FP{P}}) where P = BigInt(P)

bitlength(::F) where F <: PrimeField = bitlength(F)
@generated bitlength(::Type{F}) where F <: PrimeField = bitlength(modulus(F))

#########################################

Expand Down
15 changes: 5 additions & 10 deletions src/groups.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using .Utils: int2octet, octet2int
import .Fields: value, modulus, octet
using .Curves: ECPoint, gx, gy
#using .Specs: PRG


"""
abstract type Group end
Expand Down Expand Up @@ -30,7 +28,6 @@ Base.broadcasted(f::Function, x::NTuple{N, G}, y::G) where {N, G <: Group} = Bas
Base.broadcasted(f::Function, x::G, y::Vector{G}) where G <: Group = f.((x for i in 1:length(y)), y)
Base.broadcasted(f::Function, x::Vector{G}, y::G) where G <: Group = Base.broadcasted(f, y, x)

#Base.broadcasted(::typeof(*), x::Vector{G}, y::G) where G <: Group = x .* (y for i in 1:length(x))

"""
value(g::Group)::Union{BigInt, Tuple{BigInt, BigInt}, Tuple{BitVector, BitVector}}
Expand Down Expand Up @@ -66,11 +63,6 @@ Base.:/(x::G, y::G) where G <: Group = x * inv(y)

name(x::G) where G <: Group = name(G)

# Base.rand(prg::PRG, ::Type{G}, N::Integer; nr::Integer = 0) where G <: Group = convert(Vector{G}, rand(prg, spec(G), N; nr))

# Incorrect use
# Base.ones(x::Vector{G}) where G <: Group = [one(i) for i in x] # need to extend this

"""
iscompressable(g::Group)::Bool
Expand Down Expand Up @@ -113,8 +105,9 @@ struct ECGroup{P<:ECPoint} <: Group
end

ECGroup{P}(x, y) where {P <: ECPoint} = ECGroup{P}(P(x, y))
ECGroup{P}(x::Union{Vector{UInt8}, NTuple{2, <:Union{Integer, BitVector}}}; allow_one=false, skip_validation=false) where {P <: ECPoint} = ECGroup{P}(P(x; allow_zero=allow_one, skip_validation))
ECGroup{P}(x::Union{AbstractVector{UInt8}, NTuple{2, <:Union{Integer, BitVector}}}; allow_one=false, skip_validation=false) where {P <: ECPoint} = ECGroup{P}(P(x; allow_zero=allow_one, skip_validation))

Curves.field(::Type{ECGroup{P}}) where P <: ECPoint = field(P)

"""
convert(::Type{G}, x; allow_one=false)::G where G <: Group
Expand Down Expand Up @@ -265,11 +258,14 @@ struct PGroup{S} <: Group
new{S}(x)
end

PGroup{S}(x::Vector{UInt8}; allow_one::Bool=false, skip_validation=false) where S = PGroup{S}(octet2int(x); allow_one, skip_validation)

Base.one(::Type{PGroup{S}}) where S = new{S}(1)
end

Base.one(::PGroup{S}) where S = one(PGroup{S})


"""
octet(x::PGroup)::Vector{UInt8}
Expand All @@ -278,7 +274,6 @@ 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}; allow_one::Bool=false) = convert(group, octet2int(element); allow_one)
PGroup{S}(element::Vector{UInt8}) where S = convert(PGroup{S}, element)

"""
modulus(::Union{G, Type{G}})::BigInt where G <: PGroup
Expand Down
2 changes: 2 additions & 0 deletions test/groups.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ function testgroup(g::G) where G <: Group
@test convert(G, value(one(G)); allow_one=true) == one(G)
@test convert(G, octet(one(G)); allow_one=true) == one(G)

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

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

2 comments on commit 6642538

@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/118583

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.6.0 -m "<description of version>" 6642538070efa5ae2140088928eb388a74e6a205
git push origin v0.6.0

Please sign in to comment.