Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add elixir bindings #535

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions .github/workflows/elixir-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
name: Elixir
on:
push:
branches:
- main
pull_request:
branches:
- main

defaults:
run:
shell: bash

jobs:
tests:
runs-on: ubuntu-latest
strategy:
matrix:
variation:
- otp: "27.x"
elixir: "1.18"

steps:
- uses: actions/checkout@v3
with:
submodules: recursive

- name: Set up Elixir
uses: erlef/setup-beam@v1
with:
otp-version: ${{matrix.variation.otp}}
elixir-version: ${{matrix.variation.elixir}}

- name: Restore dependencies cache
uses: actions/cache@v3
with:
path: deps
key: ${{ runner.os }}-mix-${{ hashFiles('**/mix.lock') }}
restore-keys: ${{ runner.os }}-mix-

- name: Build BLST
run: |
cd src
make blst

- name: Build CKZG
run: |
cd src
make

- name: Install dependencies
run: mix deps.get

- name: Credo
run: mix credo --strict

- name: Dialyzer
run: mix dialyzer

- name: Test
run: mix test
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.PHONY: all
all: c csharp go java nim nodejs python rust
all: c csharp go java nim nodejs python rust elixir

.PHONY: c
c:
Expand Down Expand Up @@ -34,3 +34,7 @@ rust:
@cargo test --features generate-bindings
@cargo bench --no-run
@cd fuzz && cargo build

.PHONY: elixir
elixir:
@mix deps.get && mix test
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ bindings are intended to be used by Ethereum clients to avoid re-implementation
of crucial cryptographic functions.

| Language | Link |
|----------|--------------------------------------|
| -------- | ------------------------------------ |
| C# | [README](bindings/csharp/README.md) |
| Go | [README](bindings/go/README.md) |
| Java | [README](bindings/java/README.md) |
| Nim | [README](bindings/nim/README.md) |
| Node.js | [README](bindings/node.js/README.md) |
| Python | [README](bindings/python/README.md) |
| Rust | [README](bindings/rust/README.md) |
| Elixir | [README](bindings/elixir/README.md) |

## Interface functions

Expand Down
1 change: 1 addition & 0 deletions bindings/elixir/.clang-format
4 changes: 4 additions & 0 deletions bindings/elixir/.formatter.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Used by "mix format"
[
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
]
29 changes: 29 additions & 0 deletions bindings/elixir/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# The directory Mix will write compiled artifacts to.
/_build/

# If you run "mix test --cover", coverage assets end up here.
/cover/

# The directory Mix downloads your dependencies sources to.
/deps/

# Where third-party dependencies like ExDoc output generated docs.
/doc/

# If the VM crashes, it generates a dump, let's ignore it too.
erl_crash.dump

# Also ignore archive artifacts (built via "mix archive.build").
*.ez

# Ignore package tarball (built via "mix hex.build").
ckzg-*.tar

# Temporary files, for example, from tests.
/tmp/

.cache/
priv/build.txt
priv/ckzg_nif.so
.clangd
compile_commands.json
83 changes: 83 additions & 0 deletions bindings/elixir/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
CC = clang
PRIV_DIR = $(MIX_APP_PATH)/priv
NIF_SO = $(PRIV_DIR)/ckzg_nif.so
SRC_ROOT = $(shell pwd)
C_SRC = $(SRC_ROOT)/native/src

CFLAGS += -Wall -Wextra -shared -std=c11 -fPIC -I"$(ERTS_INCLUDE_DIR)"
UNAME_S := $(shell uname -s)
ifndef TARGET_ABI
ifeq ($(UNAME_S),Darwin)
TARGET_ABI = darwin
endif
endif

ifeq ($(TARGET_ABI),darwin)
CFLAGS += -undefined dynamic_lookup -flat_namespace -undefined suppress
endif

ifeq ($(OS),Windows_NT)
ifneq (,$(findstring Git/,$(SHELL)))
BLST_BUILDSCRIPT = ./build.bat
else
BLST_BUILDSCRIPT = .\build.bat
endif
BLST_OBJ = blst.lib
LOCATION ?= win-x64
EXTENSION ?= ".dll"
CKZG_LIBRARY_PATH = $(PRIV_DIR)/ckzg_nif$(EXTENSION)

else
BLST_BUILDSCRIPT = ./build.sh
BLST_OBJ = libblst.a
CFLAGS += -fPIC

UNAME_S := $(shell uname -s)
UNAME_M := $(shell uname -m)
ifeq ($(UNAME_S),Linux)
EXTENSION ?= ".so"
ifeq ($(UNAME_M),x86_64)
LOCATION ?= linux-x64
else
LOCATION ?= linux-arm64
endif
endif
ifeq ($(UNAME_S),Darwin)
EXTENSION ?= ".dylib"
ifeq ($(UNAME_M),arm64)
LOCATION ?= osx-arm64
else
LOCATION ?= osx-x64
endif
endif

CKZG_LIBRARY_PATH = $(PRIV_DIR)/ckzg_nif$(EXTENSION)
endif

INCLUDE_DIRS = ../../src ../../blst/bindings $(ERTS_INCLUDE_DIR)
TARGETS = $(C_SRC)/ckzg_wrap.c ../../src/ckzg.c ../../blst/$(BLST_OBJ)

CFLAGS += ${addprefix -I,${INCLUDE_DIRS}}
BLST_BUILDSCRIPT_FLAGS += -D__BLST_PORTABLE__
ifdef ARCH
CFLAGS += --target=$(ARCH)
BLST_BUILDSCRIPT_FLAGS += --target=$(ARCH)
endif

ifdef DEBUG
CFLAGS += -g
else
CFLAGS += -O2
endif

.PHONY: all
all: blst ckzg

.PHONY: blst
blst:
cd ../../blst && $(BLST_BUILDSCRIPT) $(BLST_BUILDSCRIPT_FLAGS)

.PHONY: ckzg
ckzg: blst
$(CC) $(CFLAGS) -o $(CKZG_LIBRARY_PATH) $(TARGETS)
@echo "$(CC_PRECOMPILER_CURRENT_TARGET)" > "$(PRIV_DIR)/build.txt"
33 changes: 33 additions & 0 deletions bindings/elixir/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Elixir Bindings for the C-KZG Library

This directory contains Elixir bindings for the C-KZG-4844 library.

## Prerequisites

Make sure `elixir` and `erlang` are installed. You can learn how to do so [here](https://elixir-lang.org/install.html).

## Installation

If [available in Hex](https://hex.pm/docs/publish), the package can be installed
by adding `ckzg` to your list of dependencies in `mix.exs`:

```elixir
def deps do
[
{:ckzg, "~> 0.2.1"}
]
end
```

## Build

```sh
mix deps.get
mix compile
```

## Test

```sh
mix test
```
Loading