Skip to content

Commit

Permalink
Add an interface for abstract SHA types (#37091)
Browse files Browse the repository at this point in the history
This improves inference greatly in circumstances of partial inference.
  • Loading branch information
timholy committed Aug 19, 2020
1 parent 032cbc2 commit 48fc8b4
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions stdlib/SHA/src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,21 @@ mutable struct SHA2_512_CTX <: SHA2_CTX
buffer::Array{UInt8,1}
end

function Base.getproperty(ctx::SHA2_CTX, fieldname::Symbol)
if fieldname === :state
return getfield(ctx, :state)::Union{Vector{UInt32},Vector{UInt64}}
elseif fieldname === :bytecount
return getfield(ctx, :bytecount)::Union{UInt64,UInt128}
elseif fieldname === :buffer
return getfield(ctx, :buffer)::Vector{UInt8}
elseif fieldname === :W
return getfield(ctx, :W)::Vector{UInt32}
else
error("SHA2_CTX has no field ", fieldname)
end
end


# Typealias common nicknames for SHA2 family of functions
const SHA224_CTX = SHA2_224_CTX
const SHA256_CTX = SHA2_256_CTX
Expand Down Expand Up @@ -74,6 +89,20 @@ mutable struct SHA3_512_CTX <: SHA3_CTX
bc::Array{UInt64,1}
end

function Base.getproperty(ctx::SHA3_CTX, fieldname::Symbol)
if fieldname === :state
return getfield(ctx, :state)::Vector{UInt64}
elseif fieldname === :bytecount
return getfield(ctx, :bytecount)::UInt128
elseif fieldname === :buffer
return getfield(ctx, :buffer)::Vector{UInt8}
elseif fieldname === :bc
return getfield(ctx, :bc)::Vector{UInt64}
else
error("type ", typeof(ctx), " has no field ", fieldname)
end
end

# Define constants via functions so as not to bloat context objects. Yay dispatch!

# Digest lengths for SHA1, SHA2 and SHA3. This is easy to figure out from the typename
Expand Down

0 comments on commit 48fc8b4

Please sign in to comment.