Skip to content

Commit

Permalink
integrate into the existing makefile, use openssl RAND_bytes, create …
Browse files Browse the repository at this point in the history
…nif module and expose nif functions
  • Loading branch information
samantehrani committed Jan 4, 2025
1 parent f2cfad7 commit 1a7b205
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 146 deletions.
24 changes: 16 additions & 8 deletions apps/arweave/c_src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ C_SRC_DIR = $(CURDIR)

CFLAGS += -fPIC -I $(ERTS_INCLUDE_DIR) -I $(ERL_INTERFACE_INCLUDE_DIR) -I /usr/local/include -I ../lib/RandomX/src -I $(C_SRC_DIR)
CXXFLAGS += -fPIC -I $(ERTS_INCLUDE_DIR) -I $(ERL_INTERFACE_INCLUDE_DIR) -I ../lib/RandomX/src -std=c++11
LDLIBS += -L $(ERL_INTERFACE_LIB_DIR) -L /usr/local/lib -lei -lssl -lcrypto
LDLIBS += -L $(ERL_INTERFACE_LIB_DIR) -L /usr/local/lib -lei -lssl -lcrypto


RX512_OUTPUT ?= $(CURDIR)/../priv/rx512_arweave.so
Expand Down Expand Up @@ -116,6 +116,19 @@ $(VDF_OUTPUT): $(VDF_OBJECTS)
@mkdir -p $(BASEDIR)/priv/
$(link_verbose) $(CXX) $(VDF_OBJECTS) $(LDFLAGS) $(LDLIBS) -shared -o $(VDF_OUTPUT)

SECP256K1_SOURCES = $(wildcard $(C_SRC_DIR)/*.c $(C_SRC_DIR)/secp256k1/*.c)
SECP256K1_OBJECTS = $(addsuffix .o, $(basename $(SECP256K1_SOURCES)))
SECP256K1_CFLAGS = -fPIC -I $(ERTS_INCLUDE_DIR) -I $(ERL_INTERFACE_INCLUDE_DIR) -I /usr/local/include -I $(CURDIR)/../lib/secp256k1/src -I $(CURDIR)/../lib/secp256k1/include -I $(C_SRC_DIR)
SECP256K1_LDLIBS = -L $(ERL_INTERFACE_LIB_DIR) -L /usr/local/lib -lcrypto
SECP256K1_OUTPUT ?= $(CURDIR)/../priv/secp256k1_arweave.so

$(SECP256K1_OUTPUT): $(SECP256K1_OBJECTS)
@mkdir -p $(BASEDIR)/priv/
$(link_verbose) $(CXX) $(SECP256K1_OBJECTS) $(SECP256K1_LDLIBS) ../lib/secp256k1/build/lib/libsecp256k1.a -shared -o $(SECP256K1_OUTPUT)

%secp256k1_nif.o: %secp256k1_nif.c
$(c_verbose) $(CC) $(SECP256K1_CFLAGS) -c $(OUTPUT_OPTION) $<

%.o: %.c
$(COMPILE_C) $(OUTPUT_OPTION) $<

Expand All @@ -128,12 +141,7 @@ $(VDF_OUTPUT): $(VDF_OBJECTS)
%.o: %.cpp
$(COMPILE_CPP) $(OUTPUT_OPTION) $<

all: $(RX512_OUTPUT) $(RX4096_OUTPUT) $(RXSQUARED_OUTPUT) $(VDF_OUTPUT)
all: $(RX512_OUTPUT) $(RX4096_OUTPUT) $(RXSQUARED_OUTPUT) $(VDF_OUTPUT) $(SECP256K1_OUTPUT)

clean:
@rm -f $(RX512_OUTPUT) $(RX4096_OUTPUT) $(RXSQUARED_OUTPUT) $(VDF_OUTPUT) $(RX512_OBJECTS) $(RX4096_OBJECTS) $(RXSQUARED_OBJECTS) $(VDF_OBJECTS)





@rm -f $(RX512_OUTPUT) $(RX4096_OUTPUT) $(RXSQUARED_OUTPUT) $(VDF_OUTPUT) $(RX512_OBJECTS) $(RX4096_OBJECTS) $(RXSQUARED_OBJECTS) $(VDF_OBJECTS) $(SECP256K1_OUTPUT) $(SECP256K1_OBJECTS)
23 changes: 0 additions & 23 deletions apps/arweave/c_src/secp256k1/Makefile

This file was deleted.

108 changes: 0 additions & 108 deletions apps/arweave/c_src/secp256k1/fill_random.h

This file was deleted.

15 changes: 10 additions & 5 deletions apps/arweave/c_src/secp256k1/secp256k1_nif.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include <openssl/rand.h>
#include <secp256k1.h>

#include "../ar_nif.h"
#include "./fill_random.h"
#include <ar_nif.h>

#define SECP256K1_PUBKEY_UNCOMPRESSED_SIZE 65
#define SECP256K1_PUBKEY_COMPRESSED_SIZE 33
Expand All @@ -14,7 +13,7 @@ static int secp256k1_load(ErlNifEnv* env, void** priv, ERL_NIF_TERM load_info) {

static ERL_NIF_TERM generate_key(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) {
unsigned char seed[SECP256K1_CONTEXT_SEED_SIZE];
if (!fill_random(seed, sizeof(seed))) {
if (!RAND_bytes(seed, sizeof(seed))) {
return error_tuple(env, "Failed to generate random seed for context.");
}
secp256k1_context* ctx = secp256k1_context_create(SECP256K1_CONTEXT_NONE);
Expand All @@ -23,7 +22,7 @@ static ERL_NIF_TERM generate_key(ErlNifEnv* env, int argc, const ERL_NIF_TERM ar
}

unsigned char privbytes[SECP256K1_PRIVKEY_SIZE];
if (!fill_random(privbytes, sizeof(privbytes))) {
if (!RAND_bytes(privbytes, sizeof(privbytes))) {
return error_tuple(env, "Failed to generate random key.");
}
if (!secp256k1_ec_seckey_verify(ctx, privbytes)) {
Expand All @@ -45,3 +44,9 @@ static ERL_NIF_TERM generate_key(ErlNifEnv* env, int argc, const ERL_NIF_TERM ar
ERL_NIF_TERM pubkey_bin = make_output_binary(env, pubbytes, SECP256K1_PUBKEY_UNCOMPRESSED_SIZE);
return ok_tuple2(env, privkey_bin, pubkey_bin);
}

static ErlNifFunc nif_funcs[] = {
{"generate_key", 0, generate_key}
};

ERL_NIF_INIT(secp256k1_nif, nif_funcs, secp256k1_load, NULL, NULL, NULL)
11 changes: 11 additions & 0 deletions apps/arweave/src/secp256k1_nif.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-module(secp256k1_nif).
-export([generate_key/0]).

-on_load(init/0).

init() ->
PrivDir = code:priv_dir(arweave),
ok = erlang:load_nif(filename:join([PrivDir, "secp256k1_arweave"]), 0).

generate_key() ->
erlang:nif_error(not_loaded).
2 changes: 1 addition & 1 deletion rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@
{"(linux)", compile, "make -C apps/arweave/lib/RandomX/buildsquared"},
{"(freebsd|netbsd|openbsd)", compile, "gmake -C apps/arweave/lib/RandomX/buildsquared"},
{"(darwin|linux|freebsd|netbsd|openbsd)", compile, "bash -c \"cd apps/arweave/lib/RandomX/buildsquared && mv librandomx.a librandomxsquared.a\""},
% Build secp256k1
% Build libsecp256k1
{"(darwin|linux|freebsd|netbsd|openbsd)", compile, "./scripts/build_secp256k1.sh > /dev/null"},
% Compile NIFs
{"(linux)", compile, "env AR=gcc-ar make all -C apps/arweave/c_src"},
Expand Down
3 changes: 2 additions & 1 deletion scripts/build_secp256k1.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ CMAKE_OPTIONS="-DBUILD_SHARED_LIBS=OFF \
-DSECP256K1_ENABLE_MODULE_MUSIG=OFF \
-DSECP256K1_ENABLE_MODULE_EXTRAKEYS=OFF \
-DSECP256K1_ENABLE_MODULE_ELLSWIFT=OFF \
-DSECP256K1_ENABLE_MODULE_SCHNORRSIG=OFF"
-DSECP256K1_ENABLE_MODULE_SCHNORRSIG=OFF \
-DSECP256K1_APPEND_CFLAGS=-fPIC"
mkdir -p $BUILD_DIR
cd $BUILD_DIR
cmake $CMAKE_OPTIONS ..
Expand Down

0 comments on commit 1a7b205

Please sign in to comment.