Skip to content

Commit

Permalink
Add elixir bindings (#535)
Browse files Browse the repository at this point in the history
  • Loading branch information
BlazeWasHere authored Mar 10, 2025
1 parent 497de8b commit d800f8f
Show file tree
Hide file tree
Showing 16 changed files with 1,733 additions and 2 deletions.
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 elixir go java nim nodejs python rust

.PHONY: c
c:
Expand All @@ -9,6 +9,10 @@ c:
csharp:
@make -C bindings/csharp

.PHONY: elixir
elixir:
@mix deps.get && mix test

.PHONY: go
go:
@cd bindings/go && go clean -cache && go test
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ 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) |
| Elixir | [README](bindings/elixir/README.md) |
| Go | [README](bindings/go/README.md) |
| Java | [README](bindings/java/README.md) |
| Nim | [README](bindings/nim/README.md) |
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
80 changes: 80 additions & 0 deletions bindings/elixir/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
CC = clang
PRIV_DIR = $(MIX_APP_PATH)/priv
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
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
EXTENSION ?= ".so"

UNAME_S := $(shell uname -s)
UNAME_M := $(shell uname -m)
ifeq ($(UNAME_S),Linux)
ifeq ($(UNAME_M),x86_64)
LOCATION ?= linux-x64
else
LOCATION ?= linux-arm64
endif
endif
ifeq ($(UNAME_S),Darwin)
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:
$(MAKE) -C ../../src blst

.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

0 comments on commit d800f8f

Please sign in to comment.