Skip to content

Commit

Permalink
chore: initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
vidalpaul committed Aug 20, 2024
1 parent 5ca7cd9 commit 03120dc
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 0 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: test

on:
push:
branches:
- master
- main
pull_request:

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: erlef/setup-beam@v1
with:
otp-version: "26.0.2"
gleam-version: "1.4.1"
rebar3-version: "3"
# elixir-version: "1.15.4"
- run: gleam deps download
- run: gleam test
- run: gleam format --check src test
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
*.beam
*.ez
/build
erl_crash.dump
/target
98 changes: 98 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# Keccaky

Keccaky is a high-performance Keccak (SHA-3) implementation with a Gleam interface, providing easy access to Keccak-256 hashing through a Rust backend. The project leverages Rust's efficient hashing algorithms and integrates them with Gleam for high-level usage.

## Features

- **Keccak-256 Hashing**: Provides Keccak-256 hash functionality using the `tiny-keccak` Rust library.
- **Gleam Integration**: Exposes the hashing functionality to Gleam via an FFI layer.
- **Cross-Language Compatibility**: Utilizes Rust for performance and Gleam for ease of use and safety.

## Installation

### Rust Setup

1. **Clone the Repository**:
```bash
git clone https://github.com/vidalpaul/keccaky.git
cd keccaky
```

2. **Build the Rust Library**:
Ensure you have Rust installed. Build the Rust shared library:
```bash
cargo build --release
```

The shared library will be available in the `target/release` directory.

### Gleam Setup

1. **Install Gleam**:
Follow the [Gleam installation instructions](https://gleam.run/getting-started) if you haven't already.

2. **Build the Gleam Project**:
Ensure the shared library is placed in the correct directory (`src/native` or specified path). Then build the Gleam project:
```bash
gleam build
```

## Usage

### Rust FFI

The Rust function `keccak_256` is exposed to Gleam. The function signature is:
```rust
extern "C" fn keccak_256(input_ptr: *const u8, input_len: usize, output_ptr: *mut u8);
```

### Gleam Integration

In Gleam, you can call the Keccak-256 hash function using:

```gleam
import gleam/erlang
import gleam/ffi
// External function declaration for FFI
@external(erlang, "keccak_ffi", "keccak_256")
fn keccak_256(input: ffi.Buffer, input_len: Int, output: ffi.Buffer) -> Nil
// Function to hash a string using Keccak-256
pub fn hash_keccak_256(input: String) -> Result(String, Nil) {
let input_bytes = erlang.binary_to_list(erlang.bitstring_to_binary(input))
let output_size = 32
let output_bytes = ffi.Buffer.from_binary(erlang.binary_init(output_size, 0))
keccak_256(input_bytes, List.length(input_bytes), output_bytes)
let output_binary = ffi.Buffer.to_binary(output_bytes)
Ok(erlang.binary_to_string(output_binary))
}
```

### Testing

To ensure everything is working correctly, run tests in Rust and Gleam:

- **Rust Tests**:
```bash
cargo test
```

- **Gleam Tests**:
```bash
gleam test
```

## Contributing

Feel free to contribute by submitting issues or pull requests. Ensure all contributions adhere to the project's coding standards and pass existing tests.

## License

This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.

## Contact

For any questions or further information, please contact [[email protected]](mailto:[email protected]).
19 changes: 19 additions & 0 deletions gleam.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name = "keccaky"
version = "1.0.0"

# Fill out these fields if you intend to generate HTML documentation or publish
# your project to the Hex package manager.
#
# description = ""
# licences = ["Apache-2.0"]
# repository = { type = "github", user = "", repo = "" }
# links = [{ title = "Website", href = "" }]
#
# For a full reference of all the available options, you can have a look at
# https://gleam.run/writing-gleam/gleam-toml/.

[dependencies]
gleam_stdlib = ">= 0.34.0 and < 2.0.0"

[dev-dependencies]
gleeunit = ">= 1.0.0 and < 2.0.0"
11 changes: 11 additions & 0 deletions manifest.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# This file was generated by Gleam
# You typically do not need to edit this file

packages = [
{ name = "gleam_stdlib", version = "0.40.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "86606B75A600BBD05E539EB59FABC6E307EEEA7B1E5865AFB6D980A93BCB2181" },
{ name = "gleeunit", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "F7A7228925D3EE7D0813C922E062BFD6D7E9310F0BEE585D3A42F3307E3CFD13" },
]

[requirements]
gleam_stdlib = { version = ">= 0.34.0 and < 2.0.0" }
gleeunit = { version = ">= 1.0.0 and < 2.0.0" }
12 changes: 12 additions & 0 deletions test/keccaky_test.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import gleeunit
import gleeunit/should

pub fn main() {
gleeunit.main()
}

// gleeunit test functions end in `_test`
pub fn hello_world_test() {
1
|> should.equal(1)
}

0 comments on commit 03120dc

Please sign in to comment.