Skip to content

Commit

Permalink
fix dialyzer issues, dependency upgrade, dialyzer check in circle, as…
Browse files Browse the repository at this point in the history
…df tool version file
  • Loading branch information
Ino Murko committed Oct 6, 2018
1 parent d228ce9 commit 96be147
Show file tree
Hide file tree
Showing 16 changed files with 94 additions and 42 deletions.
26 changes: 23 additions & 3 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,29 @@ jobs:
- run: mix local.hex --force
- run: mix local.rebar --force
- run: mix deps.get
- run: mix test
- run: mix dialyzer
#- run: mix format --check-formatted
- run:
command: mix compile
environment:
MIX_ENV: test

- restore_cache:
keys:
- v2-plt-cache-{{ arch }}-{{ checksum "mix.lock" }}
- v2-plt-cache-{{ arch }}
- v2-plt-cache

- run: mix dialyzer --plt

- save_cache:
key: _build
key: v2-plt-cache-{{ arch }}-{{ checksum "mix.lock" }}
paths:
- _build
- ~/.mix

- run: mix dialyzer --halt-exit-status

- save_cache:
key: _build
paths:
- _build
2 changes: 2 additions & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
erlang 21.1
elixir 1.7-otp-21
12 changes: 6 additions & 6 deletions lib/aes/aes.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ defmodule ExthCrypto.AES do
iex> ExthCrypto.AES.block_size
32
"""
@spec block_size :: integer()
@spec block_size :: 32
def block_size, do: @block_size

@doc """
Expand Down Expand Up @@ -58,7 +58,7 @@ defmodule ExthCrypto.AES do
iex> ExthCrypto.AES.encrypt("jedi knight", :ecb, ExthCrypto.Test.symmetric_key)
<<98, 60, 215, 107, 189, 132, 176, 63, 62, 225, 92, 13, 70, 53, 187, 240>>
"""
@spec encrypt(ExthCrypto.Cipher.plaintext, ExthCrypto.Cipher.mode, ExthCrypto.symmetric_key, ExthCrypto.Cipher.init_vector) :: ExthCrypto.Cipher.ciphertext
@spec encrypt(ExthCrypto.Cipher.plaintext, ExthCrypto.Cipher.mode, ExthCrypto.Key.symmetric_key, ExthCrypto.Cipher.init_vector) :: ExthCrypto.Cipher.ciphertext
def encrypt(plaintext, :cbc, symmetric_key, init_vector) do
padding_bits = ( 16 - rem(byte_size(plaintext), 16) ) * 8

Expand All @@ -73,7 +73,7 @@ defmodule ExthCrypto.AES do
ciphertext
end

@spec encrypt(ExthCrypto.Cipher.plaintext, ExthCrypto.Cipher.mode, ExthCrypto.symmetric_key) :: ExthCrypto.Cipher.ciphertext
@spec encrypt(ExthCrypto.Cipher.plaintext, ExthCrypto.Cipher.mode, ExthCrypto.Key.symmetric_key) :: ExthCrypto.Cipher.ciphertext
def encrypt(plaintext, :ecb, symmetric_key) do
padding_bits = ( 16 - rem(byte_size(plaintext), 16) ) * 8

Expand Down Expand Up @@ -130,7 +130,7 @@ defmodule ExthCrypto.AES do
iex> ExthCrypto.AES.decrypt(<<98, 60, 215, 107, 189, 132, 176, 63, 62, 225, 92, 13, 70, 53, 187, 240>>, :ecb, ExthCrypto.Test.symmetric_key)
<<0, 0, 0, 0, 0>> <> "jedi knight"
"""
@spec decrypt(ExthCrypto.Cipher.ciphertext, ExthCrypto.Cipher.mode, ExthCrypto.symmetric_key, ExthCrypto.Cipher.init_vector) :: ExthCrypto.Cipher.plaintext
@spec decrypt(ExthCrypto.Cipher.ciphertext, ExthCrypto.Cipher.mode, ExthCrypto.Key.symmetric_key, ExthCrypto.Cipher.init_vector) :: ExthCrypto.Cipher.plaintext
def decrypt(ciphertext, :cbc, symmetric_key, init_vector) do
:crypto.block_decrypt(:aes_cbc, symmetric_key, init_vector, ciphertext)
end
Expand All @@ -143,7 +143,7 @@ defmodule ExthCrypto.AES do
plaintext
end

@spec decrypt(ExthCrypto.Cipher.ciphertext, ExthCrypto.Cipher.mode, ExthCrypto.symmetric_key) :: ExthCrypto.Cipher.plaintext
@spec decrypt(ExthCrypto.Cipher.ciphertext, ExthCrypto.Cipher.mode, ExthCrypto.Key.symmetric_key) :: ExthCrypto.Cipher.plaintext
def decrypt(ciphertext, :ecb, symmetric_key) do
:crypto.block_decrypt(:aes_ecb, symmetric_key, ciphertext)
end
Expand Down Expand Up @@ -192,7 +192,7 @@ defmodule ExthCrypto.AES do
iex> plaintext
"hello"
"""
@spec stream_decrypt(ExthCrypto.Cipher.ciphertext, ExthCrypto.Cipher.stream) :: { ExthCrypto.Cipher.stream, ExthCrypto.Cipher.plaintrxt }
@spec stream_decrypt(ExthCrypto.Cipher.ciphertext, ExthCrypto.Cipher.stream) :: { ExthCrypto.Cipher.stream, ExthCrypto.Cipher.plaintext }
def stream_decrypt(plaintext, stream) do
:crypto.stream_decrypt(stream, plaintext)
end
Expand Down
4 changes: 2 additions & 2 deletions lib/cipher.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ defmodule ExthCrypto.Cipher do
@type plaintext :: iodata()
@type ciphertext :: binary()
@type init_vector :: binary()
@opaque stream :: :crypto.ctr_state
@type stream :: :crypto.stream_state()

@doc """
Encrypts the given plaintext for the given block cipher.
Expand Down Expand Up @@ -75,7 +75,7 @@ defmodule ExthCrypto.Cipher do
iex> ExthCrypto.Cipher.generate_init_vector(32) == ExthCrypto.Cipher.generate_init_vector(32)
false
"""
@spec generate_init_vector(integer()) :: init_vector
@spec generate_init_vector(non_neg_integer()) :: init_vector
def generate_init_vector(block_size) do
:crypto.strong_rand_bytes(block_size)
end
Expand Down
8 changes: 4 additions & 4 deletions lib/ecies/ecdh.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@ defmodule ExthCrypto.ECIES.ECDH do
@default_curve :secp256k1

@doc """
Generates a new keypair for elliptic curve diffie-hellman.
Generates a new key_pair for elliptic curve diffie-hellman.
These keys should be used as ephemeral keys in the key-exchange protocol.
## Examples
iex> {public_key, private_key} = ExthCrypto.ECIES.ECDH.new_ecdh_keypair()
iex> {public_key, private_key} = ExthCrypto.ECIES.ECDH.new_ecdh_key_pair()
iex> byte_size(public_key)
65
iex> byte_size(private_key)
32
iex> {public_key, private_key} == :crypto.generate_key(:ecdh, :secp256k1, private_key)
true
"""
@spec new_ecdh_keypair(ExthCrypto.named_curve) :: ExthCrypto.Key.keypair
def new_ecdh_keypair(curve \\ @default_curve) when is_atom(curve) do
@spec new_ecdh_key_pair(ExthCrypto.named_curve) :: ExthCrypto.Key.key_pair
def new_ecdh_key_pair(curve \\ @default_curve) when is_atom(curve) do
:crypto.generate_key(:ecdh, curve)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/ecies/ecies.ex
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ defmodule ExthCrypto.ECIES do
# First, create a new ephemeral key pair (SEC1 - §5.1.3 - Step 1)
{my_ephemeral_public_key, my_ephemeral_private_key} = case my_ephemeral_key_pair do
{my_ephemeral_public_key, my_ephemeral_private_key} -> {my_ephemeral_public_key, my_ephemeral_private_key}
nil -> ECDH.new_ecdh_keypair(@curve_name)
nil -> ECDH.new_ecdh_key_pair(@curve_name)
end

init_vector = if init_vector, do: init_vector, else: Cipher.generate_init_vector(key_len)
Expand Down
6 changes: 3 additions & 3 deletions lib/ecies/parameters.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ defmodule ExthCrypto.ECIES.Parameters do
]

@type t :: %__MODULE__{
mac: :crypto.hash_algorithms,
hasher: ExthCrypto.hash_type,
cipher: ExthCrypto.cipher,
mac: ExthCrypto.Hash.hash_algorithm(),
hasher: ExthCrypto.Hash.hash_type(),
cipher: ExthCrypto.Cipher.cipher(),
key_len: integer()
}

Expand Down
1 change: 1 addition & 0 deletions lib/exth_crypto.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ defmodule ExthCrypto do

@type curve :: nil
@type curve_params :: nil
@type named_curve :: :crypto.ec_named_curve()

end
42 changes: 32 additions & 10 deletions lib/hash/hash.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,66 @@ defmodule ExthCrypto.Hash do
defined by Ethereum.
"""

@type hash_algorithm :: atom()
# @type hash_algorithm :: :md4 | :md5 | :sha | :sha224 | :sha256 | :sha384 | :sha512 | :sha3_224 | :sha3_256 | :sha3_384 | :sha3_512
# @type hash_algorithms :: [:md4 | :md5 | :sha | :sha224 | :sha256 | :sha384 | :sha512 | :sha3_224 | :sha3_256 | :sha3_384 | :sha3_512]
@type hash_algorithm ::
:md4
| :md5
| :sha
| :sha224
| :sha256
| :sha384
| :sha3_224
| :sha3_256
| :sha3_384
| :sha3_512
| :sha512
@type hash_algorithms :: [hash_algorithm]
@type hash :: binary()
@type hasher :: (binary() -> binary())
@type hash_type :: {hasher, integer() | nil, integer()}

@doc """
Returns a list of supported hash algorithms.
"""
@hash_algorithms [ :md5, :ripemd160, :sha, :sha224, :sha256, :sha384, :sha512 ]
@spec hash_algorithms() :: [hash_algorithm]
@hash_algorithms [
:md4,
:md5,
:sha,
:sha224,
:sha256,
:sha384,
:sha512,
:sha3_224,
:sha3_256,
:sha3_384,
:sha3_512
]
@spec hash_algorithms() :: nonempty_list(hash_algorithm)
def hash_algorithms, do: @hash_algorithms

@doc """
The SHA1 hasher.
"""
@spec sha1() :: ExthCrypto.hash_type
@spec sha1() :: hash_type
def sha1, do: {&ExthCrypto.Hash.SHA.sha1/1, nil, 20}

@doc """
The KECCAK hasher, as defined by Ethereum.
"""
@spec kec() :: ExCrpyto.hash_type
@spec kec() :: hash_type
def kec, do: {&ExthCrypto.Hash.Keccak.kec/1, nil, 256}

@doc """
Runs the specified hash type on the given data.
## Examples
iex> ExthCrypto.Hash.hash("hello world", ExthCrypto.Hash.kec) |> ExthCrypto.Math.bin_to_hex
"47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad"
iex> ExthCrypto.Hash.hash("hello world", ExthCrypto.Hash.sha1) |> ExthCrypto.Math.bin_to_hex
"2aae6c35c94fcfb415dbe95f408b9ce91ee846ed"
"""
@spec hash(iodata(), hash_type) :: hash
def hash(data, {hash_fun, _, _}=_hasher) do
def hash(data, {hash_fun, _, _} = _hasher) do
hash_fun.(data)
end

end
2 changes: 1 addition & 1 deletion lib/hash/keccak.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ defmodule ExthCrypto.Hash.Keccak do
been changed prior to adoption by NIST, but after adoption by Ethereum.
"""

@type keccak_hash :: ExthCrypto.hash
@type keccak_hash :: ExthCrypto.Hash.hash()
@type keccak_mac :: {atom(), binary()}

@doc """
Expand Down
2 changes: 1 addition & 1 deletion lib/key/key.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ defmodule ExthCrypto.Key do

@type symmetric_key :: binary()
@type public_key_der :: binary()
@type public_key :: binary()
@type public_key :: <<_::8, _::_*8>>
@type private_key_der :: binary()
@type private_key :: binary()
@type key_pair :: {public_key, private_key}
Expand Down
2 changes: 1 addition & 1 deletion lib/mac/mac.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ defmodule ExthCrypto.MAC do
iex> ExthCrypto.MAC.mac("The quick brown fox jumps over the lazy dog", "key", :sha256, 8)
<<247, 188, 131, 244, 48, 83, 132, 36>>
"""
@spec mac(iodata(), iodata(), Hash.hash_algorithm, integer()) :: mac
@spec mac(iodata(), iodata(), Hash.hash_algorithm, integer() | nil) :: mac
def mac(data, key, hash_algorithm, length \\ nil) when is_atom(hash_algorithm) do
cond do
Enum.member?(Hash.hash_algorithms, hash_algorithm) ->
Expand Down
4 changes: 2 additions & 2 deletions lib/math/math.ex
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ defmodule ExthCrypto.Math do
iex> ExthCrypto.Math.pad(<<>>, 0)
<<>>
"""
@spec pad(binary(), integer()) :: binary()
@spec pad(binary(), non_neg_integer()) :: binary()
def pad(bin, length) do
padding_bits = ( length - byte_size(bin) ) * 8

Expand Down Expand Up @@ -90,7 +90,7 @@ defmodule ExthCrypto.Math do
iex> ExthCrypto.Math.nonce(32) == ExthCrypto.Math.nonce(32)
false
"""
@spec nonce(integer()) :: binary()
@spec nonce(non_neg_integer()) :: binary()
def nonce(nonce_size) do
:crypto.strong_rand_bytes(nonce_size)
end
Expand Down
6 changes: 3 additions & 3 deletions lib/signature/signature.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ defmodule ExthCrypto.Signature do
functionality.
"""

@type signature :: binary()
@type r :: integer()
@type s :: integer()
@type signature :: <<_::512>>
@type r :: non_neg_integer()
@type s :: non_neg_integer()
@type recovery_id :: integer()

@doc """
Expand Down
12 changes: 9 additions & 3 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ defmodule ExthCrypto.Mixfile do
],
build_embedded: Mix.env == :prod,
start_permanent: Mix.env == :prod,
deps: deps()]
deps: deps(),
dialyzer: [
flags: [:underspecs, :unknown, :unmatched_returns],
plt_add_apps: [:mix, :iex, :logger],
plt_add_deps: :transitive
]
]
end

# Configuration for the OTP application
Expand All @@ -37,9 +43,9 @@ defmodule ExthCrypto.Mixfile do
[
{:libsecp256k1, "~> 0.1.9"},
{:keccakf1600, "~> 2.0.0", hex: :keccakf1600_orig},
{:credo, "~> 0.8", only: [:dev, :test], runtime: false},
{:credo, "~> 0.10.2", only: [:dev, :test], runtime: false},
{:ex_doc, "~> 0.17", only: :dev, runtime: false},
{:dialyxir, "~> 0.5", only: [:dev], runtime: false},
{:dialyxir, "~> 1.0.0-rc.3", only: [:dev], runtime: false},
{:binary, "~> 0.0.4"},
]
end
Expand Down
5 changes: 3 additions & 2 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
%{
"binary": {:hex, :binary, "0.0.4", "dd077db70c0ded3e85c132b802338e14b80694684a7e2277666bfa4004b7fa66", [:mix], [], "hexpm"},
"bunt": {:hex, :bunt, "0.2.0", "951c6e801e8b1d2cbe58ebbd3e616a869061ddadcc4863d0a2182541acae9a38", [:mix], [], "hexpm"},
"credo": {:hex, :credo, "0.8.6", "335f723772d35da499b5ebfdaf6b426bfb73590b6fcbc8908d476b75f8cbca3f", [:mix], [{:bunt, "~> 0.2.0", [hex: :bunt, repo: "hexpm", optional: false]}], "hexpm"},
"dialyxir": {:hex, :dialyxir, "0.5.1", "b331b091720fd93e878137add264bac4f644e1ddae07a70bf7062c7862c4b952", [:mix], [], "hexpm"},
"credo": {:hex, :credo, "0.10.2", "03ad3a1eff79a16664ed42fc2975b5e5d0ce243d69318060c626c34720a49512", [:mix], [{:bunt, "~> 0.2.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm"},
"dialyxir": {:hex, :dialyxir, "1.0.0-rc.3", "774306f84973fc3f1e2e8743eeaa5f5d29b117f3916e5de74c075c02f1b8ef55", [:mix], [], "hexpm"},
"earmark": {:hex, :earmark, "1.2.5", "4d21980d5d2862a2e13ec3c49ad9ad783ffc7ca5769cf6ff891a4553fbaae761", [:mix], [], "hexpm"},
"ex_doc": {:hex, :ex_doc, "0.19.1", "519bb9c19526ca51d326c060cb1778d4a9056b190086a8c6c115828eaccea6cf", [:mix], [{:earmark, "~> 1.1", [hex: :earmark, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.7", [hex: :makeup_elixir, repo: "hexpm", optional: false]}], "hexpm"},
"jason": {:hex, :jason, "1.1.1", "d3ccb840dfb06f2f90a6d335b536dd074db748b3e7f5b11ab61d239506585eb2", [:mix], [{:decimal, "~> 1.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm"},
"keccakf1600": {:hex, :keccakf1600_orig, "2.0.0", "0a7217ddb3ee8220d449bbf7575ec39d4e967099f220a91e3dfca4dbaef91963", [:rebar3], [], "hexpm"},
"libsecp256k1": {:hex, :libsecp256k1, "0.1.9", "e725f31364cda7b554d56ce2bb976241303dde5ffd1ad59598513297bf1f2af6", [:make, :mix], [{:mix_erlang_tasks, "0.1.0", [hex: :mix_erlang_tasks, repo: "hexpm", optional: false]}], "hexpm"},
"makeup": {:hex, :makeup, "0.5.1", "966c5c2296da272d42f1de178c1d135e432662eca795d6dc12e5e8787514edf7", [:mix], [{:nimble_parsec, "~> 0.2.2", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm"},
Expand Down

0 comments on commit 96be147

Please sign in to comment.