Skip to content

Commit

Permalink
Merge pull request #8 from ayrat555/master
Browse files Browse the repository at this point in the history
Update rustler to support OTP 24
  • Loading branch information
tzumby committed Jun 17, 2021
2 parents a4eb2e5 + a2dacc8 commit 2fa8cd0
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 167 deletions.
2 changes: 2 additions & 0 deletions .tool-versions
@@ -0,0 +1,2 @@
elixir 1.12.1-otp-24
erlang 24.0.1
5 changes: 5 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,10 @@
# Changelog

## v0.2.0

* Update rustler to support OTP 24
* Leave only throwing function

## v0.1.3

* Move NIF interface to module `ExKeccak.Impl`, making `ExKeccak` a wrapper
Expand Down
15 changes: 4 additions & 11 deletions README.md
Expand Up @@ -11,27 +11,20 @@ The package can be installed by adding `ex_keccak` to your list of dependencies
```elixir
def deps do
[
{:ex_keccak, "~> 0.1.3"}
{:ex_keccak, "~> 0.2.0"}
]
end
```

## Usage

To calculate KECCAK-256 hash, use `ExKeccak.hash_256/1` function. It returns `{:ok, result}` on success:
To calculate KECCAK-256 hash, use `ExKeccak.hash_256/1` function. It returns `result` on success:

```elixir
{
:ok,
<<28, 138, 255, 149, 6, 133, 194, 237, 75, 195, 23, 79, 52, 114, 40, 123, 86, 217, 81, 123, 156, 148, 129, 39, 49, 154, 9, 167, 163, 109, 234, 200>>
} = ExKeccak.hash_256("hello")
<<28, 138, 255, 149, 6, 133, 194, 237, 75, 195, 23, 79, 52, 114, 40, 123, 86, 217, 81, 123, 156, 148, 129, 39, 49, 154, 9, 167, 163, 109, 234, 200>> = ExKeccak.hash_256("hello")
```

And it returns `{:error, :invalid_type}` if the provided data is not binary:

```elixir
{:error, :invalid_type} = ExKeccak.hash_256(11)
```
And it returns `ArgumentError` is provided data is not binary

## Contributing

Expand Down
11 changes: 2 additions & 9 deletions lib/ex_keccak.ex
@@ -1,12 +1,5 @@
defmodule ExKeccak do
defdelegate hash_256(data), to: ExKeccak.Impl
use Rustler, otp_app: :ex_keccak, crate: :exkeccak

def hash_256!(data) do
case hash_256(data) do
{:ok, hash} ->
hash
{:error, :invalid_type} ->
raise ArgumentError, "#{inspect(data)} is not hashable"
end
end
def hash_256(_data), do: :erlang.nif_error(:nif_not_loaded)
end
6 changes: 0 additions & 6 deletions lib/ex_keccak/impl.ex

This file was deleted.

6 changes: 3 additions & 3 deletions mix.exs
Expand Up @@ -4,10 +4,10 @@ defmodule ExKeccak.MixProject do
def project do
[
app: :ex_keccak,
version: "0.1.3",
version: "0.2.0",
elixir: "~> 1.10",
description: description(),
compilers: [:rustler] ++ Mix.compilers(),
compilers: Mix.compilers(),
build_embedded: Mix.env() == :prod,
start_permanent: Mix.env() == :prod,
rustler_crates: rustler_crates(),
Expand Down Expand Up @@ -64,7 +64,7 @@ defmodule ExKeccak.MixProject do
[
{:ex_doc, "~> 0.21", only: :dev, runtime: false},
{:benchee, "~> 1.0.1", only: :test},
{:rustler, "~> 0.21.1"}
{:rustler, "~> 0.22.0-rc.1"}
]
end
end
2 changes: 1 addition & 1 deletion mix.lock
Expand Up @@ -6,6 +6,6 @@
"makeup": {:hex, :makeup, "1.0.3", "e339e2f766d12e7260e6672dd4047405963c5ec99661abdc432e6ec67d29ef95", [:mix], [{:nimble_parsec, "~> 0.5", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "2e9b4996d11832947731f7608fed7ad2f9443011b3b479ae288011265cdd3dad"},
"makeup_elixir": {:hex, :makeup_elixir, "0.14.1", "4f0e96847c63c17841d42c08107405a005a2680eb9c7ccadfd757bd31dabccfb", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "f2438b1a80eaec9ede832b5c41cd4f373b38fd7aa33e3b22d9db79e640cbde11"},
"nimble_parsec": {:hex, :nimble_parsec, "0.6.0", "32111b3bf39137144abd7ba1cce0914533b2d16ef35e8abc5ec8be6122944263", [:mix], [], "hexpm", "27eac315a94909d4dc68bc07a4a83e06c8379237c5ea528a9acff4ca1c873c52"},
"rustler": {:hex, :rustler, "0.21.1", "5299980be32da997c54382e945bacaa015ed97a60745e1e639beaf6a7b278c65", [:mix], [{:toml, "~> 0.5.2", [hex: :toml, repo: "hexpm", optional: false]}], "hexpm", "6ee1651e10645b2b2f3bb70502bf180341aa058709177e9bc28c105934094bc6"},
"rustler": {:hex, :rustler, "0.22.0-rc.1", "46668ba0c5f97e8aa978ee965c473f7d4ab5560182ebf2c3bb1cf18956f92afb", [:mix], [{:toml, "~> 0.5.2", [hex: :toml, repo: "hexpm", optional: false]}], "hexpm", "c0eb17d6c52885153218e89b2877122cdb094aeefd191647f78094022f3b9101"},
"toml": {:hex, :toml, "0.5.2", "e471388a8726d1ce51a6b32f864b8228a1eb8edc907a0edf2bb50eab9321b526", [:mix], [], "hexpm", "f1e3dabef71fb510d015fad18c0e05e7c57281001141504c6b69d94e99750a07"},
}
124 changes: 19 additions & 105 deletions native/exkeccak/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion native/exkeccak/Cargo.toml
Expand Up @@ -10,6 +10,6 @@ path = "src/lib.rs"
crate-type = ["dylib"]

[dependencies]
rustler = "0.21.1"
rustler = "0.22.0-rc.1"
lazy_static = "1.0"
tiny-keccak = { version = "2.0", features = ["keccak"] }
34 changes: 7 additions & 27 deletions native/exkeccak/src/lib.rs
@@ -1,40 +1,20 @@
use rustler::types::binary::{Binary, OwnedBinary};
use rustler::types::Encoder;
use rustler::{Env, Term};
use tiny_keccak::{Hasher, Keccak};
use rustler::types::binary::Binary;
use rustler::types::binary::OwnedBinary;

mod atoms {
rustler::rustler_atoms! {
atom ok;
atom error;
atom invalid_type;
}
}

rustler::rustler_export_nifs! {
"Elixir.ExKeccak.Impl",
[
("hash_256", 1, hash_256, rustler::SchedulerFlags::DirtyCpu)
],
None
}

fn hash_256<'a>(env: Env<'a>, args: &[Term<'a>]) -> Term<'a> {
let data: Binary = match args[0].decode() {
Ok(binary) => binary,
Err(_error) => return (atoms::error(), atoms::invalid_type()).encode(env),
};
let data_slice = data.as_slice();
rustler::init!("Elixir.ExKeccak", [hash_256]);

#[rustler::nif]
fn hash_256<'a>(data: Binary) -> OwnedBinary {
let mut keccak = Keccak::v256();

keccak.update(data_slice);
keccak.update(data.as_slice());

let mut result: [u8; 32] = [0; 32];
keccak.finalize(&mut result);

let mut erl_bin: OwnedBinary = OwnedBinary::new(32).unwrap();
erl_bin.as_mut_slice().copy_from_slice(&result);

(atoms::ok(), erl_bin.release(env)).encode(env)
erl_bin
}
14 changes: 10 additions & 4 deletions test/ex_keccak_test.exs
Expand Up @@ -9,23 +9,29 @@ defmodule ExKeccakTest do
"5d1Ch4jboo9DcCWGj55ZT/tsW4nnG5+DlCFGddOfjTZmfQdzc4yBnodqszjYIaI+8Io41a789cV5rUwzvqxLzw=="
)

{:ok, hashed_data} = ExKeccak.hash_256(data)
hashed_data = ExKeccak.hash_256(data)

assert <<_::binary-12, address::binary-20>> = hashed_data

assert Base.encode16(address, case: :lower) == "73bb50c828fd325c011d740fde78d02528826156"
end

test "fails to decode number" do
assert {:error, :invalid_type} = ExKeccak.hash_256(11)
assert_raise ArgumentError, fn ->
ExKeccak.hash_256(11)
end
end

test "fails to decode nil" do
assert {:error, :invalid_type} = ExKeccak.hash_256(nil)
assert_raise ArgumentError, fn ->
ExKeccak.hash_256(nil)
end
end

test "fails to decode atom" do
assert {:error, :invalid_type} = ExKeccak.hash_256(:atom)
assert_raise ArgumentError, fn ->
ExKeccak.hash_256(:atom)
end
end

@tag :perf
Expand Down

0 comments on commit 2fa8cd0

Please sign in to comment.