Skip to content

Commit

Permalink
Merge pull request #419 from 0xPolygonID/feature/method_channel
Browse files Browse the repository at this point in the history
SDK refactor, removed layer of DTOs. Added method channel for Android groth16prove
  • Loading branch information
5eeman authored Jul 4, 2024
2 parents 2c11bce + ed167e4 commit df4f719
Show file tree
Hide file tree
Showing 156 changed files with 2,900 additions and 8,742 deletions.
17 changes: 8 additions & 9 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ buildscript {
}

dependencies {
classpath 'com.android.tools.build:gradle:4.1.3'
classpath 'com.android.tools.build:gradle:7.3.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
Expand All @@ -32,6 +32,7 @@ apply plugin: 'maven-publish'

android {
compileSdkVersion 33
ndkVersion "26.3.11579264"

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
Expand All @@ -46,21 +47,19 @@ android {
main.java.srcDirs += 'src/main/kotlin'
main.jniLibs.srcDirs += 'src/main/jniLibs'
}
externalNativeBuild {
cmake {
path "src/main/cpp/CMakeLists.txt"
}
}

defaultConfig {
minSdkVersion 21
ndk {
// TODO: armeabi-v7a and x86 not supported yet
abiFilters 'arm64-v8a', 'x86_64' /*,'armeabi-v7a', 'x86',*/
abiFilters 'arm64-v8a', 'x86_64'
}
}

/*externalNativeBuild {
ndkBuild {
path "jni/Android.mk"
}
}*/

lintOptions {
disable 'InvalidPackage'
}
Expand Down
67 changes: 67 additions & 0 deletions android/src/main/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
cmake_minimum_required(VERSION 3.4.1)

set(CMAKE_VERBOSE_MAKEFILE ON)
set(CMAKE_CXX_STANDARD 11)

# Define a name for this project. It will be accessible with ${PROJECT_NAME}.
project(rapidsnark_module)

# Create a library with the name defined for the project.
# Set it as SHARED library (will generate a .so file)
# Set the source file
add_library(
${PROJECT_NAME}
SHARED
rapidsnark_module.cpp
)
target_compile_options(${PROJECT_NAME} PRIVATE -Wno-unused-value)

# Import an already existing .so file
# Here, we add a dependency to the shared_library prebuilt
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../jniLibs)

set(
SHARED_PROVER_LIBRARY_SO
${CMAKE_CURRENT_SOURCE_DIR}/../jniLibs/${CMAKE_ANDROID_ARCH_ABI}/librapidsnark.so
)
# IMPORTED allows to depends on a library file outside the project.
add_library(
rapidsnark_prover_lib
SHARED
IMPORTED
)
# IMPORTED_LOCATION specifies the location of the library file on disk
set_target_properties(
rapidsnark_prover_lib
PROPERTIES
IMPORTED_LOCATION ${SHARED_PROVER_LIBRARY_SO}
)

#set(
# SHARED_SDK_LIBRARY_SO
# ${CMAKE_CURRENT_SOURCE_DIR}/../jniLibs/${CMAKE_ANDROID_ARCH_ABI}/libpolygonid.so
#)
## IMPORTED allows to depends on a library file outside the project.
#add_library(
# rapidsnark_sdk_lib
# SHARED
# IMPORTED
#)
## IMPORTED_LOCATION specifies the location of the library file on disk
#set_target_properties(
# rapidsnark_sdk_lib
# PROPERTIES
# IMPORTED_LOCATION ${SHARED_SDK_LIBRARY_SO}
# NO_SONAME 1
#)

# Add libraries to this project
target_link_libraries(
${PROJECT_NAME}
rapidsnark_prover_lib
# rapidsnark_sdk_lib
)

# Add the log library
find_library(log-lib log)
target_link_libraries(${PROJECT_NAME} ${log-lib})
226 changes: 226 additions & 0 deletions android/src/main/cpp/rapidsnark_module.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
#include "rapidsnark_module.h"

#define TAG "RapidsnarkExampleNative"
#define LOGI(...) __android_log_print(ANDROID_LOG_ERROR, TAG, __VA_ARGS__)

#ifdef __cplusplus
extern "C" {
#endif

JNIEXPORT jint JNICALL Java_io_iden3_polygonid_1flutter_1sdk_RapidsnarkJniBridge_groth16Prove(
JNIEnv *env, jobject obj,
jbyteArray zkeyBuffer, jlong zkeySize,
jbyteArray wtnsBuffer, jlong wtnsSize,
jbyteArray proofBuffer, jlongArray proofSize,
jbyteArray publicBuffer, jlongArray publicSize,
jbyteArray errorMsg, jlong errorMsgMaxSize
) {
LOGI("groth16Prover native called");

// Convert jbyteArray to native types
void *nativeZkeyBuffer = env->GetByteArrayElements(zkeyBuffer, nullptr);
void *nativeWtnsBuffer = env->GetByteArrayElements(wtnsBuffer, nullptr);

char *nativeProofBuffer = (char *) env->GetByteArrayElements(proofBuffer, nullptr);
char *nativePublicBuffer = (char *) env->GetByteArrayElements(publicBuffer, nullptr);
char *nativeErrorMsg = (char *) env->GetByteArrayElements(errorMsg, nullptr);

jlong * nativeProofSizeArr = env->GetLongArrayElements(proofSize, 0);
jlong * nativePublicSizeArr = env->GetLongArrayElements(publicSize, 0);

unsigned long nativeProofSize = nativeProofSizeArr[0];
unsigned long nativePublicSize = nativePublicSizeArr[0];

// Call the groth16_prover function
int result = groth16_prover(
nativeZkeyBuffer, zkeySize,
nativeWtnsBuffer, wtnsSize,
nativeProofBuffer, &nativeProofSize,
nativePublicBuffer, &nativePublicSize,
nativeErrorMsg, errorMsgMaxSize
);

// Convert the results back to JNI types
nativeProofSizeArr[0] = nativeProofSize;
nativePublicSizeArr[0] = nativePublicSize;

env->SetLongArrayRegion(proofSize, 0, 1, (jlong * )
nativeProofSizeArr);
env->SetLongArrayRegion(publicSize, 0, 1, (jlong * )
nativePublicSizeArr);

// Release the native buffers
env->ReleaseByteArrayElements(zkeyBuffer, (jbyte *) nativeZkeyBuffer, 0);
env->ReleaseByteArrayElements(wtnsBuffer, (jbyte *) nativeWtnsBuffer, 0);
env->ReleaseByteArrayElements(proofBuffer, (jbyte *) nativeProofBuffer, 0);
env->ReleaseByteArrayElements(publicBuffer, (jbyte *) nativePublicBuffer, 0);
env->ReleaseByteArrayElements(errorMsg, (jbyte *) nativeErrorMsg, 0);

env->ReleaseLongArrayElements(proofSize, (jlong * )
nativeProofSizeArr, 0);
env->ReleaseLongArrayElements(publicSize, (jlong * )
nativePublicSizeArr, 0);

return result;
}

JNIEXPORT jint JNICALL Java_io_iden3_polygonid_1flutter_1sdk_RapidsnarkJniBridge_groth16ProveWithZKeyFilePath(
JNIEnv *env, jobject obj,
jstring zkeyPath,
jbyteArray wtnsBuffer, jlong wtnsSize,
jbyteArray proofBuffer, jlongArray proofSize,
jbyteArray publicBuffer, jlongArray publicSize,
jbyteArray errorMsg, jlong errorMsgMaxSize
) {
LOGI("groth16ProverZkeyFile native called");

// Convert jbyteArray to native types
const char *nativeZkeyPath = env->GetStringUTFChars(zkeyPath, nullptr);

void *nativeWtnsBuffer = env->GetByteArrayElements(wtnsBuffer, nullptr);

char *nativeProofBuffer = (char *) env->GetByteArrayElements(proofBuffer, nullptr);
char *nativePublicBuffer = (char *) env->GetByteArrayElements(publicBuffer, nullptr);
char *nativeErrorMsg = (char *) env->GetByteArrayElements(errorMsg, nullptr);

jlong * nativeProofSizeArr = env->GetLongArrayElements(proofSize, 0);
jlong * nativePublicSizeArr = env->GetLongArrayElements(publicSize, 0);

unsigned long nativeProofSize = nativeProofSizeArr[0];
unsigned long nativePublicSize = nativePublicSizeArr[0];

// Call the groth16_prover function`
int status_code = groth16_prover_zkey_file(
nativeZkeyPath,
nativeWtnsBuffer, wtnsSize,
nativeProofBuffer, &nativeProofSize,
nativePublicBuffer, &nativePublicSize,
nativeErrorMsg, errorMsgMaxSize
);

// Convert the results back to JNI types
nativeProofSizeArr[0] = nativeProofSize;
nativePublicSizeArr[0] = nativePublicSize;

env->SetLongArrayRegion(proofSize, 0, 1, (jlong * )
nativeProofSizeArr);
env->SetLongArrayRegion(publicSize, 0, 1, (jlong * )
nativePublicSizeArr);

// Release the native buffers
env->ReleaseByteArrayElements(wtnsBuffer, (jbyte *) nativeWtnsBuffer, 0);
env->ReleaseByteArrayElements(proofBuffer, (jbyte *) nativeProofBuffer, 0);
env->ReleaseByteArrayElements(publicBuffer, (jbyte *) nativePublicBuffer, 0);
env->ReleaseByteArrayElements(errorMsg, (jbyte *) nativeErrorMsg, 0);

env->ReleaseLongArrayElements(proofSize, (jlong * )
nativeProofSizeArr, 0);
env->ReleaseLongArrayElements(publicSize, (jlong * )
nativePublicSizeArr, 0);

return status_code;
}

JNIEXPORT jint JNICALL Java_io_iden3_polygonid_1flutter_1sdk_RapidsnarkJniBridge_groth16Verify(
JNIEnv *env, jobject obj,
jstring proof, jstring inputs, jstring verificationKey,
jbyteArray errorMsg, jlong errorMsgMaxSize
) {
LOGI("groth16Verifier native called");

// Convert jstring to native types
const char *nativeInputs = env->GetStringUTFChars(inputs, nullptr);
const char *nativeProof = env->GetStringUTFChars(proof, nullptr);
const char *nativeVerificationKey = env->GetStringUTFChars(verificationKey, nullptr);

char *nativeErrorMsg = (char *) env->GetByteArrayElements(errorMsg, nullptr);

// Call the groth16_verify function
int status_code = groth16_verify(
nativeProof,
nativeInputs,
nativeVerificationKey,
nativeErrorMsg, errorMsgMaxSize
);

// Release the native buffers
env->ReleaseStringUTFChars(inputs, nativeInputs);
env->ReleaseStringUTFChars(proof, nativeProof);
env->ReleaseStringUTFChars(verificationKey, nativeVerificationKey);

env->ReleaseByteArrayElements(errorMsg, (jbyte *) nativeErrorMsg, 0);

return status_code;
}

JNIEXPORT jlong JNICALL Java_io_iden3_polygonid_1flutter_1sdk_RapidsnarkJniBridge_groth16PublicSizeForZkeyBuf(
JNIEnv *env, jobject obj,
jbyteArray zkeyBuffer, jlong zkeySize,
jbyteArray errorMsg, jlong errorMsgMaxSize
) {
LOGI("groth16_public_size_for_zkey_buf native called");

void *nativeZkeyBuffer = env->GetByteArrayElements(zkeyBuffer, nullptr);
char *nativeErrorMsg = (char *) env->GetByteArrayElements(errorMsg, nullptr);

jlong nativePublicSize = 0;

// Call the groth16_public_size_for_zkey_buf function
int status_code = groth16_public_size_for_zkey_buf(
nativeZkeyBuffer, zkeySize,
(size_t * ) & nativePublicSize,
nativeErrorMsg, errorMsgMaxSize
);

LOGI("groth16_public_size_for_zkey_buf:%lu", nativePublicSize);

// Release the native buffers
env->ReleaseByteArrayElements(zkeyBuffer, (jbyte *) nativeZkeyBuffer, 0);
env->ReleaseByteArrayElements(errorMsg, (jbyte *) nativeErrorMsg, 0);

return nativePublicSize;
}

JNIEXPORT jlong JNICALL Java_io_iden3_polygonid_1flutter_1sdk_RapidsnarkJniBridge_groth16PublicSizeForZkeyFile(
JNIEnv *env, jobject obj,
jstring zkeyPath,
jbyteArray errorMsg, jlong errorMsgMaxSize
) {
LOGI("groth16_public_size_for_zkey_file native called");

const char *nativeZkeyPath = env->GetStringUTFChars(zkeyPath, nullptr);

char *nativeErrorMsg = (char *) env->GetByteArrayElements(errorMsg, nullptr);

jlong nativePublicSize = 0;

// Call the groth16_public_size_for_zkey_file function
int status_code = groth16_public_size_for_zkey_file(
nativeZkeyPath,
(unsigned long *) &nativePublicSize,
nativeErrorMsg, errorMsgMaxSize
);

LOGI("groth16_public_size_for_zkey_file:%lu", nativePublicSize);

// Release the native buffers
env->ReleaseStringUTFChars(zkeyPath, nativeZkeyPath);
env->ReleaseByteArrayElements(errorMsg, (jbyte *) nativeErrorMsg, 0);

return nativePublicSize;
}

inline const char *ToString(PLGNStatusCode v) {
switch (v) {
case PLGNSTATUSCODE_ERROR:
return "PLGNSTATUSCODE_ERROR";
case PLGNSTATUSCODE_NIL_POINTER:
return "PLGNSTATUSCODE_NIL_POINTER";
default:
return "[Unknown PLGNStatusCode]";
}
}

#ifdef __cplusplus
}
#endif
55 changes: 55 additions & 0 deletions android/src/main/cpp/rapidsnark_module.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#ifndef ANDROID_CMAKE_RAPIDSNARK_MODULE_H
#define ANDROID_CMAKE_RAPIDSNARK_MODULE_H

#include <jni.h>
#include <sys/mman.h>
#include <android/log.h>
#include "libpolygonid.h"
#include "prover.h"
#include "verifier.h"

extern "C" {

JNIEXPORT jint JNICALL Java_io_iden3_polygonid_1flutter_1sdk_RapidsnarkJniBridge_groth16Prove(
JNIEnv *env, jobject obj,
jbyteArray zkeyBuffer, jlong zkeySize,
jbyteArray wtnsBuffer, jlong wtnsSize,
jbyteArray proofBuffer, jlongArray proofSize,
jbyteArray publicBuffer, jlongArray publicSize,
jbyteArray errorMsg, jlong errorMsgMaxSize
);

JNIEXPORT jint JNICALL Java_io_iden3_polygonid_1flutter_1sdk_RapidsnarkJniBridge_groth16ProveWithZKeyFilePath(
JNIEnv *env, jobject obj,
jstring zkeyPath,
jbyteArray wtnsBuffer, jlong wtnsSize,
jbyteArray proofBuffer, jlongArray proofSize,
jbyteArray publicBuffer, jlongArray publicSize,
jbyteArray errorMsg, jlong errorMsgMaxSize
);

JNIEXPORT jint JNICALL Java_io_iden3_polygonid_1flutter_1sdk_RapidsnarkJniBridge_groth16Verify(
JNIEnv *env, jobject obj,
jstring proof, jstring inputs, jstring verificationKey,
jbyteArray errorMsg, jlong errorMsgMaxSize
);

JNIEXPORT jlong JNICALL Java_io_iden3_polygonid_1flutter_1sdk_RapidsnarkJniBridge_groth16PublicSizeForZkeyBuf(
JNIEnv *env, jobject obj,
jbyteArray zkeyBuffer, jlong zkeySize,
jbyteArray errorMsg, jlong errorMsgMaxSize
);

JNIEXPORT jlong JNICALL Java_io_iden3_polygonid_1flutter_1sdk_RapidsnarkJniBridge_groth16PublicSizeForZkeyFile(
JNIEnv *env, jobject obj,
jstring zkeyPath,
jbyteArray errorMsg, jlong errorMsgMaxSize
);

}

#endif //ANDROID_CMAKE_RAPIDSNARK_MODULE_H




Binary file modified android/src/main/jniLibs/arm64-v8a/librapidsnark.so
Binary file not shown.
Loading

0 comments on commit df4f719

Please sign in to comment.