Skip to content

Commit

Permalink
Adjust to starknet 0.11 (#255)
Browse files Browse the repository at this point in the history
  • Loading branch information
THenry14 authored Jun 7, 2023
1 parent 6fa6402 commit 8c9e546
Show file tree
Hide file tree
Showing 69 changed files with 1,497 additions and 304 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/Dockerfile.multiarch
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ ENV CC=/usr/bin/gcc-7
ENV CXX=/usr/bin/g++-7
COPY . /build/.
WORKDIR /build/lib
RUN ./build_crypto_cpp.sh
RUN ./build_crypto.sh
5 changes: 0 additions & 5 deletions .github/workflows/build-and-publish-docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@ jobs:
- name: Setup Gradle
uses: gradle/gradle-build-action@v2

# https://www.oracle.com/technical-resources/articles/java/javadoc-tool.html#Descriptions
- name: Replace whitespaces after dots with non breaking spaces.
# Look for '. ' or '.\newline' and replace it with '. '
run: sed -i 's/\(\.\s\|\.$\)/.\ /g' lib/starknet-jvm.md

# Build java and kotlin style docs and copy kotlin output to the java folder
# so that the deployed github page has java docs at `/` and kotlin docs at `/kotlin`
- name: Build docs
Expand Down
20 changes: 14 additions & 6 deletions .github/workflows/build-and-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,14 @@ jobs:
run: echo "os_arch=$(uname -m)" >> $GITHUB_OUTPUT

- name: Build platform specific lib
run: ./gradlew buildCryptoCpp
- name: Upload the shared lib
run: ./gradlew buildCrypto
- name: Upload the shared libs
uses: actions/upload-artifact@v3
with:
name: lib-${{ runner.os }}-${{ steps.vars.outputs.os_arch }}
path: crypto/build/bindings/libcrypto_jni.*
path: |
crypto/pedersen/build/bindings/libcrypto_jni.*
crypto/poseidon/build/bindings/libposeidon_jni.*
- name: Build android's AAR
run: ./gradlew android:build
Expand Down Expand Up @@ -103,13 +105,19 @@ jobs:
- name: Set arch variable
id: vars
run: echo "os_arch=$(docker run --platform linux/${{ matrix.virt_platform }} -t build bash -c 'uname -m')" >> $GITHUB_OUTPUT
- name: Extract lib
run: docker cp $(docker create build):/build/crypto/build/bindings/libcrypto_jni.so libcrypto_jni.so
- name: Extract libs
run: |
container=$(docker create build)
docker cp $container:/build/crypto/pedersen/build/bindings/libcrypto_jni.so libcrypto_jni.so
docker cp $container:/build/crypto/poseidon/build/bindings/libposeidon_jni.so libposeidon_jni.so
- name: Upload the shared lib
uses: actions/upload-artifact@v3
with:
name: lib-Linux-${{ steps.vars.outputs.os_arch }}
path: libcrypto_jni.so
path: |
libcrypto_jni.so
libposeidon_jni.so
outputs:
archs: ${{ steps.vars.outputs.os_arch }}

Expand Down
9 changes: 8 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,11 @@ venv

# Exclude account temp files
lib/src/test/resources/*account
lib/src/test/resources/compiled
lib/src/test/resources/compiled*

# Exclude native builds outputs
crypto/pedersen/crypto-cpp
crypto/poseidon/poseidon
crypto/poseidon/*.dylib
crypto/poseidon/*.so

10 changes: 8 additions & 2 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
[submodule "crypto/crypto-cpp"]
path = crypto/crypto-cpp
[submodule "crypto/pedersen/crypto-cpp"]
path = crypto/pedersen/crypto-cpp
url = [email protected]:software-mansion-labs/crypto-cpp.git
[submodule "crypto/poseidon/poseidon"]
path = crypto/poseidon/poseidon
url = [email protected]:THenry14/poseidon.git
[submodule "cairo"]
path = cairo
url = [email protected]:starkware-libs/cairo.git
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ android {
consumerProguardFiles "consumer-rules.pro"
externalNativeBuild {
cmake {
targets "crypto_jni"
targets "crypto_jni", "poseidon_jni"
arguments "-DCMAKE_BUILD_TYPE=Release"
}
}
Expand Down
2 changes: 1 addition & 1 deletion androiddemo/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ dependencies {
implementation("androidx.appcompat:appcompat:1.4.2")
implementation("com.google.android.material:material:1.6.1")
implementation("androidx.constraintlayout:constraintlayout:2.1.4")
implementation("com.swmansion.starknet:starknet:0.5.3@aar")
implementation("com.swmansion.starknet:starknet:0.6.0@aar")
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.3")
androidTestImplementation("androidx.test.espresso:espresso-core:3.4.0")
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
id("org.jetbrains.kotlin.android") version "1.7.10" apply false
id("org.jetbrains.kotlin.jvm") version "1.7.10" apply false
id("org.jetbrains.kotlin.plugin.serialization") version "1.7.10" apply false
id("org.jetbrains.dokka") version "1.7.10" apply false
id("org.jetbrains.dokka") version "1.8.10" apply false
id("org.jmailen.kotlinter") version "3.11.1"
id("org.jetbrains.kotlinx.kover") version "0.5.1"
}
1 change: 1 addition & 0 deletions cairo
Submodule cairo added at 05867c
72 changes: 8 additions & 64 deletions crypto/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,55 +1,12 @@
cmake_minimum_required (VERSION 3.5)
if (NOT DEFINED CMAKE_CXX_COMPILER)
set(CMAKE_CXX_COMPILER /usr/bin/clang++)
endif()
cmake_minimum_required(VERSION 3.5)

project(StarkwareCryptoLib VERSION 0.1.0 LANGUAGES CXX)
project(CryptoLibs)

set(CMAKE_CXX_STANDARD 17)

set(BOOST_ENABLE_CMAKE ON)

include(FetchContent)
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/609281088cfefc76f9d0ce82e1ff6c30cc3591e5.zip
)
# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)


# Checking if current build supports __uint128_t and including boost if it doesn't.
INCLUDE(CheckCXXSourceCompiles)
check_cxx_source_compiles("
int main() {
__uint128_t a = 0;
return a;
}" HAVE_UINT128)

if (NOT HAVE_UINT128)
FetchContent_Declare(
boostorg
URL https://boostorg.jfrog.io/artifactory/main/release/1.79.0/source/boost_1_79_0.tar.gz
)

FetchContent_GetProperties(boostorg)
if (NOT boostorg_POPULATED)
FetchContent_Populate(boostorg)
endif ()

include_directories(${boostorg_SOURCE_DIR})

# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(boostorg)
endif()

include(CTest)
enable_testing()
include_directories("${CMAKE_SOURCE_DIR}/crypto-cpp/src")
include_directories("${CMAKE_SOURCE_DIR}/bindings")
include_directories("${CMAKE_SOURCE_DIR}/poseidon/poseidon/sources")
include_directories("${CMAKE_SOURCE_DIR}/poseidon/bindings")

include_directories("${CMAKE_SOURCE_DIR}/pedersen/crypto-cpp/src")
include_directories("${CMAKE_SOURCE_DIR}/pedersen/bindings")

set(JAVA_AWT_LIBRARY NotNeeded)
set(JAVA_JVM_LIBRARY NotNeeded)
Expand All @@ -61,18 +18,5 @@ find_package(Java REQUIRED)
find_package(JNI REQUIRED)
include_directories(${JNI_INCLUDE_DIRS})

if (NOT DEFINED CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Debug)
endif()

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 -Werror -Wall -Wextra -fno-strict-aliasing -fPIC")

set(CC_OPTIMIZE "-O3")

set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} ${CC_OPTIMIZE}")

add_subdirectory(crypto-cpp/src)
add_subdirectory(bindings)

add_subdirectory(poseidon)
add_subdirectory(pedersen)
79 changes: 79 additions & 0 deletions crypto/pedersen/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
cmake_minimum_required (VERSION 3.5)
if (NOT DEFINED CMAKE_CXX_COMPILER)
set(CMAKE_CXX_COMPILER /usr/bin/clang++)
endif()

project(StarkwareCryptoLib VERSION 0.1.0 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)

set(BOOST_ENABLE_CMAKE ON)

include(FetchContent)
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/609281088cfefc76f9d0ce82e1ff6c30cc3591e5.zip
)
# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)


# Checking if current build supports __uint128_t and including boost if it doesn't.
INCLUDE(CheckCXXSourceCompiles)
check_cxx_source_compiles("
int main() {
__uint128_t a = 0;
return a;
}" HAVE_UINT128)

if (NOT HAVE_UINT128)
FetchContent_Declare(
boostorg
URL https://boostorg.jfrog.io/artifactory/main/release/1.79.0/source/boost_1_79_0.tar.gz
)

FetchContent_GetProperties(boostorg)
if (NOT boostorg_POPULATED)
FetchContent_Populate(boostorg)
endif ()

include_directories(${boostorg_SOURCE_DIR})

# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(boostorg)
endif()

if (NOT DEFINED JAVA_INCLUDE_PATH)
set(JAVA_AWT_LIBRARY NotNeeded)
set(JAVA_JVM_LIBRARY NotNeeded)
set(JAVA_AWT_INCLUDE_PATH NotNeeded)
set(JAVA_INCLUDE_PATH "$ENV{JAVA_HOME}/include")
set(JAVA_INCLUDE_PATH2 "$ENV{JAVA_HOME}/include/darwin;$ENV{JAVA_HOME}/include/linux")

find_package(Java REQUIRED)
find_package(JNI REQUIRED)
include_directories(${JNI_INCLUDE_DIRS})
endif()

include(CTest)
enable_testing()
include_directories("${CMAKE_SOURCE_DIR}/crypto-cpp/src")
include_directories("${CMAKE_SOURCE_DIR}/bindings")

if (NOT DEFINED CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Debug)
endif()

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 -Werror -Wall -Wextra -fno-strict-aliasing -fPIC")

set(CC_OPTIMIZE "-O3")

set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} ${CC_OPTIMIZE}")

add_subdirectory(crypto-cpp/src)
add_subdirectory(bindings)

File renamed without changes.
2 changes: 2 additions & 0 deletions crypto/build.sh → crypto/pedersen/build_crypto_cpp.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#! /bin/bash

echo "Building crypto-cpp..."
cmake -DCMAKE_BUILD_TYPE=Release -Bbuild -DCMAKE_OSX_ARCHITECTURES="x86_64;arm64"
make -C build
echo "Crypto-cpp built!"
24 changes: 24 additions & 0 deletions crypto/poseidon/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
cmake_minimum_required (VERSION 3.5)

project(poseidon)

include_directories("${CMAKE_SOURCE_DIR}/poseidon/sources")
include_directories("${CMAKE_SOURCE_DIR}/bindings")

if (NOT DEFINED JAVA_INCLUDE_PATH)
set(JAVA_AWT_LIBRARY NotNeeded)
set(JAVA_JVM_LIBRARY NotNeeded)
set(JAVA_AWT_INCLUDE_PATH NotNeeded)
set(JAVA_INCLUDE_PATH "$ENV{JAVA_HOME}/include")
set(JAVA_INCLUDE_PATH2 "$ENV{JAVA_HOME}/include/darwin;$ENV{JAVA_HOME}/include/linux")

find_package(Java REQUIRED)
find_package(JNI REQUIRED)
include_directories(${JNI_INCLUDE_DIRS})
endif()

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Werror -O3 -fPIC")
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

add_subdirectory(poseidon)
add_subdirectory(bindings)
2 changes: 2 additions & 0 deletions crypto/poseidon/bindings/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
add_library(poseidon_jni SHARED starknet_crypto_Poseidon.c)
target_link_libraries(poseidon_jni poseidon)
39 changes: 39 additions & 0 deletions crypto/poseidon/bindings/starknet_crypto_Poseidon.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <jni.h>

#include "./starknet_crypto_Poseidon.h"
#include "f251.h"
#include "poseidon.h"
#include "poseidon_rc.h"

JNIEXPORT jobjectArray JNICALL Java_com_swmansion_starknet_crypto_Poseidon_hades
(JNIEnv *env, jclass obj, jobjectArray values) {

jsize num_rows = (*env)->GetArrayLength(env, values);
jsize num_cols = (*env)->GetArrayLength(env, (*env)->GetObjectArrayElement(env, values, 0));

felt_t converted_values[3] = { {0} };
for (int i = 0; i < num_rows; i++) {
jlongArray row = (*env)->GetObjectArrayElement(env, values, i);
jlong *row_elems = (*env)->GetLongArrayElements(env, row, NULL);
for (int j = 0; j < num_cols; j++) {
converted_values[i][j] = (uint64_t) row_elems[j];
}
(*env)->ReleaseLongArrayElements(env, row, row_elems, JNI_ABORT);
}

permutation_3(converted_values);

jclass longArrayClass = (*env)->FindClass(env, "[J");
jobjectArray result = (*env)->NewObjectArray(env, 3, longArrayClass, NULL);
for (int i = 0; i < 3; i++) {
jlongArray row = (*env)->NewLongArray(env, 4);
jlong* elements = (*env)->GetLongArrayElements(env, row, NULL);
for (int j = 0; j < 4; j++) {
elements[j] = (jlong)converted_values[i][j];
}
(*env)->ReleaseLongArrayElements(env, row, elements, 0);
(*env)->SetObjectArrayElement(env, result, i, row);
}

return result;
}
15 changes: 15 additions & 0 deletions crypto/poseidon/bindings/starknet_crypto_Poseidon.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <jni.h>

#ifndef _Included_com_swmansion_starknet_crypto_Poseidon
#define _Included_com_swmansion_starknet_crypto_Poseidon
#ifdef __cplusplus
extern "C" {
#endif

JNIEXPORT jobjectArray JNICALL Java_com_swmansion_starknet_crypto_Poseidon_hades
(JNIEnv *env, jclass obj, jobjectArray values);

#ifdef __cplusplus
}
#endif
#endif
6 changes: 6 additions & 0 deletions crypto/poseidon/build_poseidon.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#! /bin/bash

echo "Building poseidon..."
cmake -Bbuild -DCMAKE_OSX_ARCHITECTURES="x86_64;arm64"
make -C build
echo "Poseidon built!"
1 change: 1 addition & 0 deletions crypto/poseidon/poseidon
Submodule poseidon added at 927c1e
2 changes: 1 addition & 1 deletion javademo/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ plugins {
}

dependencies {
implementation("com.swmansion.starknet:starknet:0.5.3")
implementation("com.swmansion.starknet:starknet:0.6.0")
}

application {
Expand Down
Loading

0 comments on commit 8c9e546

Please sign in to comment.