forked from tronprotocol/zksnark-java-sdk
-
Notifications
You must be signed in to change notification settings - Fork 1
Implement arkworks wrapper #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
AllFi
wants to merge
16
commits into
master
Choose a base branch
from
arkworks
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
29d8aba
Add draft of arkworks wrapper
AllFi ad750bd
Refactor code, add more test cases
AllFi ffd196e
Minor refactoring
AllFi 47d6f15
Check field elements sizes
AllFi 9106c32
Add left padding
AllFi 12176c4
Remove padding
AllFi dc3b797
Refactor serialize.rs, add pairing test
AllFi a0fe0c3
Reduce lib size
AllFi f421d25
Link libarkworks.so (.dylib) instead of .a
AllFi 158d5d4
adds missing compiled libzksnarkjni.jnilib for Darwin
r0wdy1 66b9766
Set lto = false, return static linking
AllFi 8ebb6a8
Merge branch 'arkworks' of https://github.com/zkBob/zksnark-java-sdk …
AllFi 022efa9
adds build for arm64
r0wdy1 8a9e724
adds x86_64 build
r0wdy1 e326fda
copy built jnilib from Federico2014
r0wdy1 1a09f91
Update linux64 library
AllFi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
[submodule "rust/librustzcash"] | ||
path = rust/librustzcash | ||
url = https://github.com/tronprotocol/librustzcash.git | ||
branch = release_vm_zksnarks_4.0 | ||
url = https://github.com/zkbob/librustzcash.git | ||
branch = lto_false | ||
ignore=dirty |
This file contains hidden or 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 hidden or 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,146 @@ | ||
#include "org_tron_common_zksnark_Libarkworks_LibarkworksJNI.h" | ||
#include "libarkworks.h" | ||
#include <iostream> | ||
|
||
jboolean bool2jbool(bool b) { | ||
if (b) { | ||
return JNI_TRUE; | ||
} else { | ||
return JNI_FALSE; | ||
} | ||
} | ||
|
||
/* | ||
* Class: org_tron_common_zksnark_Libarkworks_LibarkworksJNI | ||
* Method: libarkworksG1IsValid | ||
* Signature: ([B[B)Z | ||
*/ | ||
JNIEXPORT jboolean JNICALL Java_org_tron_common_zksnark_Libarkworks_00024LibarkworksJNI_libarkworksG1IsValid | ||
(JNIEnv * env, jobject, jbyteArray x, jbyteArray y) { | ||
unsigned char * x_p = (unsigned char *) env->GetByteArrayElements(x, nullptr); | ||
unsigned char * y_p = (unsigned char *) env->GetByteArrayElements(y, nullptr); | ||
if (x_p == NULL || y_p == NULL) { | ||
return JNI_FALSE; | ||
} | ||
|
||
bool result = libarkworks_g1_is_valid(x_p, y_p); | ||
env->ReleaseByteArrayElements(x, (jbyte*) x_p, 0); | ||
env->ReleaseByteArrayElements(y, (jbyte*) y_p, 0); | ||
return bool2jbool(result); | ||
} | ||
|
||
/* | ||
* Class: org_tron_common_zksnark_Libarkworks_LibarkworksJNI | ||
* Method: libarkworksG2IsValid | ||
* Signature: ([B[B[B[B)Z | ||
*/ | ||
JNIEXPORT jboolean JNICALL Java_org_tron_common_zksnark_Libarkworks_00024LibarkworksJNI_libarkworksG2IsValid | ||
(JNIEnv * env, jobject, jbyteArray a, jbyteArray b, jbyteArray c, jbyteArray d) { | ||
unsigned char * a_p = (unsigned char *) env->GetByteArrayElements(a, nullptr); | ||
unsigned char * b_p = (unsigned char *) env->GetByteArrayElements(b, nullptr); | ||
unsigned char * c_p = (unsigned char *) env->GetByteArrayElements(c, nullptr); | ||
unsigned char * d_p = (unsigned char *) env->GetByteArrayElements(d, nullptr); | ||
if (a_p == NULL || b_p == NULL || c_p == NULL || d_p == NULL) { | ||
return JNI_FALSE; | ||
} | ||
|
||
bool result = libarkworks_g2_is_valid(a_p, b_p, c_p, d_p); | ||
env->ReleaseByteArrayElements(a, (jbyte*) a_p, 0); | ||
env->ReleaseByteArrayElements(b, (jbyte*) b_p, 0); | ||
env->ReleaseByteArrayElements(c, (jbyte*) c_p, 0); | ||
env->ReleaseByteArrayElements(d, (jbyte*) d_p, 0); | ||
return bool2jbool(result); | ||
} | ||
|
||
/* | ||
* Class: org_tron_common_zksnark_Libarkworks_LibarkworksJNI | ||
* Method: libarkworksAddG1 | ||
* Signature: ([B[B[B)Z | ||
*/ | ||
JNIEXPORT jboolean JNICALL Java_org_tron_common_zksnark_Libarkworks_00024LibarkworksJNI_libarkworksAddG1 | ||
(JNIEnv * env, jobject, jbyteArray a, jbyteArray b, jbyteArray result) { | ||
unsigned char * a_p = (unsigned char *) env->GetByteArrayElements(a, nullptr); | ||
unsigned char * b_p = (unsigned char *) env->GetByteArrayElements(b, nullptr); | ||
unsigned char * result_p = (unsigned char *) env->GetByteArrayElements(result, nullptr); | ||
if (a_p == NULL || b_p == NULL || result_p == NULL) { | ||
return JNI_FALSE; | ||
} | ||
|
||
bool success = libarkworks_add_g1(a_p, b_p, result_p); | ||
env->ReleaseByteArrayElements(a, (jbyte*) a_p, 0); | ||
env->ReleaseByteArrayElements(b, (jbyte*) b_p, 0); | ||
env->ReleaseByteArrayElements(result, (jbyte*) result_p, 0); | ||
return bool2jbool(success); | ||
} | ||
|
||
/* | ||
* Class: org_tron_common_zksnark_Libarkworks_LibarkworksJNI | ||
* Method: libarkworksMulG1 | ||
* Signature: ([B[B[B)Z | ||
*/ | ||
JNIEXPORT jboolean JNICALL Java_org_tron_common_zksnark_Libarkworks_00024LibarkworksJNI_libarkworksMulG1 | ||
(JNIEnv * env, jobject, jbyteArray p, jbyteArray s, jbyteArray result) { | ||
unsigned char * p_p = (unsigned char *) env->GetByteArrayElements(p, nullptr); | ||
unsigned char * s_p = (unsigned char *) env->GetByteArrayElements(s, nullptr); | ||
unsigned char * result_p = (unsigned char *) env->GetByteArrayElements(result, nullptr); | ||
if (p_p == NULL || s_p == NULL || result_p == NULL) { | ||
return JNI_FALSE; | ||
} | ||
|
||
bool success = libarkworks_mul_g1(p_p, s_p, result_p); | ||
env->ReleaseByteArrayElements(p, (jbyte*) p_p, 0); | ||
env->ReleaseByteArrayElements(s, (jbyte*) s_p, 0); | ||
env->ReleaseByteArrayElements(result, (jbyte*) result_p, 0); | ||
return bool2jbool(success); | ||
} | ||
|
||
/* | ||
* Class: org_tron_common_zksnark_Libarkworks_LibarkworksJNI | ||
* Method: libarkworksPairingCheck | ||
* Signature: ([B[BI)Z | ||
*/ | ||
JNIEXPORT jboolean JNICALL Java_org_tron_common_zksnark_Libarkworks_00024LibarkworksJNI_libarkworksPairingCheck | ||
(JNIEnv * env, jobject, jbyteArray g1s, jbyteArray g2s, jint pairs) { | ||
unsigned char * g1s_p = (unsigned char *) env->GetByteArrayElements(g1s, nullptr); | ||
unsigned char * g2s_p = (unsigned char *) env->GetByteArrayElements(g2s, nullptr); | ||
if (g1s_p == NULL || g2s_p == NULL) { | ||
return JNI_FALSE; | ||
} | ||
|
||
bool success = libarkworks_pairing_check(g1s_p, g2s_p, pairs); | ||
env->ReleaseByteArrayElements(g1s, (jbyte*) g1s_p, 0); | ||
env->ReleaseByteArrayElements(g2s, (jbyte*) g2s_p, 0); | ||
return bool2jbool(success); | ||
} | ||
|
||
/* | ||
* Class: org_tron_common_zksnark_Libarkworks_LibarkworksJNI | ||
* Method: libarkworksRandomG1 | ||
* Signature: ([B)V | ||
*/ | ||
JNIEXPORT void JNICALL Java_org_tron_common_zksnark_Libarkworks_00024LibarkworksJNI_libarkworksRandomG1 | ||
(JNIEnv * env, jobject, jbyteArray g1) { | ||
unsigned char * g1_p = (unsigned char *) env->GetByteArrayElements(g1, nullptr); | ||
if (g1_p == NULL) { | ||
return; | ||
} | ||
|
||
libarkworks_random_g1(g1_p); | ||
env->ReleaseByteArrayElements(g1, (jbyte*) g1_p, 0); | ||
} | ||
|
||
/* | ||
* Class: org_tron_common_zksnark_Libarkworks_LibarkworksJNI | ||
* Method: libarkworksRandomG2 | ||
* Signature: ([B)V | ||
*/ | ||
JNIEXPORT void JNICALL Java_org_tron_common_zksnark_Libarkworks_00024LibarkworksJNI_libarkworksRandomG2 | ||
(JNIEnv * env, jobject, jbyteArray g2) { | ||
unsigned char * g2_p = (unsigned char *) env->GetByteArrayElements(g2, nullptr); | ||
if (g2_p == NULL) { | ||
return; | ||
} | ||
|
||
libarkworks_random_g2(g2_p); | ||
env->ReleaseByteArrayElements(g2, (jbyte*)g2_p, 0); | ||
} |
This file contains hidden or 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,2 @@ | ||
/target | ||
/Cargo.lock |
This file contains hidden or 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,22 @@ | ||
[package] | ||
name = "libarkworks" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[lib] | ||
name = "arkworks" | ||
path = "src/lib.rs" | ||
crate-type = ["staticlib", "cdylib"] | ||
|
||
[dependencies] | ||
libc = "0.2" | ||
rand = "0.8.5" | ||
ark-bn254 = "0.4.0" | ||
ark-ff = "0.4.2" | ||
ark-serialize = "0.4.2" | ||
ark-ec = "0.4.2" | ||
|
||
[profile.release] | ||
lto = false | ||
panic = 'abort' | ||
codegen-units = 1 |
This file contains hidden or 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,52 @@ | ||
#ifndef LIBARKWORKS_INCLUDE_H_ | ||
#define LIBARKWORKS_INCLUDE_H_ | ||
|
||
#include <stdint.h> | ||
|
||
extern "C" { | ||
#ifdef WIN32 | ||
typedef uint16_t codeunit; | ||
#else | ||
typedef uint8_t codeunit; | ||
#endif | ||
|
||
bool libarkworks_g1_is_valid( | ||
const unsigned char *x, | ||
const unsigned char *y | ||
); | ||
|
||
bool libarkworks_g2_is_valid( | ||
const unsigned char *a, | ||
const unsigned char *b, | ||
const unsigned char *c, | ||
const unsigned char *d | ||
); | ||
|
||
bool libarkworks_add_g1( | ||
const unsigned char *a, | ||
const unsigned char *b, | ||
unsigned char *result | ||
); | ||
|
||
bool libarkworks_mul_g1( | ||
const unsigned char *p, | ||
const unsigned char *s, | ||
unsigned char *result | ||
); | ||
|
||
bool libarkworks_pairing_check( | ||
const unsigned char *g1s, | ||
const unsigned char *g2s, | ||
const uint32_t pairs | ||
); | ||
|
||
void libarkworks_random_g1( | ||
unsigned char *g1 | ||
); | ||
|
||
void libarkworks_random_g2( | ||
unsigned char *g2 | ||
); | ||
} | ||
|
||
#endif // LIBARKWORKS_INCLUDE_H_ |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.