-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #419 from 0xPolygonID/feature/method_channel
SDK refactor, removed layer of DTOs. Added method channel for Android groth16prove
- Loading branch information
Showing
156 changed files
with
2,900 additions
and
8,742 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
Oops, something went wrong.