Skip to content
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

feat: add arkworks wrapper for bn128 operations #9

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

AllFi
Copy link

@AllFi AllFi commented Sep 20, 2023

This PR introduces LibarkworksWrapper that uses rust implementation from arkworks to provide necessary functions for BN128Addition, BN128Multiplication, and BN128Pairing precompiles.

This PR is the first step to resolve this issue. A preliminary discussion of this PR can be found here.

1. add rust crate with functions required for bn128 precompiles
2. make the necessary adjustments to include this crate in libsnarkjni
3. implement LibarkworksWrapper
4. add tests
@halibobo1205
Copy link
Contributor

3.10.0-1160.36.2.el7.x86_64 # 1 SMP Wed Jul 21 11:57:15 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

"Could not load library. Reasons: [java.lang.LinkageError: Unable to load library zksnarkjni, java.lang.LinkageError: Unable to load library from /tmp/libzksnarkjni-7289434056439766101.so, java.lang.LinkageError: Unable to load library from /home/java-tron/.hawtjni/zksnarkjni/libzksnarkjni-8327699144273145661.so]" type="java.lang.UnsatisfiedLinkError">java.lang.UnsatisfiedLinkError: Could not load library. Reasons: [java.lang.LinkageError: Unable to load library zksnarkjni, java.lang.LinkageError: Unable to load library from /tmp/libzksnarkjni-7289434056439766101.so, java.lang.LinkageError: Unable to load library from /home/java-tron/.hawtjni/zksnarkjni/libzksnarkjni-8327699144273145661.so]
at org.tron.common.zksnark.LibarkworksTest.libarkworksAddG1(LibarkworksTest.java:89)

@halibobo1205
Copy link
Contributor

@AllFi By the way, can you provide a version for arm64?
One more thing, strip can remove this debugging information and other data included in the executable which is not necessary for execution in order to reduce the size of the executable.
file libzksnarkjni.so
libzksnarkjni.so: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, BuildID[sha1]=a130c4231641dde2848aad767b53b9cf094c5b8c, not stripped
ll libzksnarkjni.so
8914960 Sep 25 15:39 libzksnarkjni.so
After stripped
file libzksnarkjni.so
libzksnarkjni.so: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, BuildID[sha1]=a130c4231641dde2848aad767b53b9cf094c5b8c, stripped
ll libzksnarkjni.so
3002048 Sep 25 15:40 libzksnarkjni.so

@AllFi
Copy link
Author

AllFi commented Sep 25, 2023

Hello, @halibobo1205! Is it necessary to use a specific version of glibc for the arm64 build?

@halibobo1205
Copy link
Contributor

3.10.0-1160.36.2.el7.x86_64 # 1 SMP Wed Jul 21 11:57:15 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

"Could not load library. Reasons: [java.lang.LinkageError: Unable to load library zksnarkjni, java.lang.LinkageError: Unable to load library from /tmp/libzksnarkjni-7289434056439766101.so, java.lang.LinkageError: Unable to load library from /home/java-tron/.hawtjni/zksnarkjni/libzksnarkjni-8327699144273145661.so]" type="java.lang.UnsatisfiedLinkError">java.lang.UnsatisfiedLinkError: Could not load library. Reasons: [java.lang.LinkageError: Unable to load library zksnarkjni, java.lang.LinkageError: Unable to load library from /tmp/libzksnarkjni-7289434056439766101.so, java.lang.LinkageError: Unable to load library from /home/java-tron/.hawtjni/zksnarkjni/libzksnarkjni-8327699144273145661.so] at org.tron.common.zksnark.LibarkworksTest.libarkworksAddG1(LibarkworksTest.java:89)

@AllFi The error occurs on an x86 server.

@halibobo1205
Copy link
Contributor

Hello, @halibobo1205! Is it necessary to use a specific version of glibc for the arm64 build?

@AllFi , not required, because java-tron doesn't support arm64 currently.

1. strip linux64/libzksnarkjni.so
2. fix memcpy.c for aarch64 target
3. update linux64/aarch64/libsnarkjni.so
1. build libzksnarkjni.so on CentOS 7
2. update linux64/libzksnarkjni.so
@AllFi
Copy link
Author

AllFi commented Sep 25, 2023

  1. I managed to reproduce this error on CentOS 7:
[root@centos-s-2vcpu-2gb-lon1-01 zksnark-java-sdk]# uname -s -r
Linux 3.10.0-1160.45.1.el7.x86_64
[root@centos-s-2vcpu-2gb-lon1-01 zksnark-java-sdk]# ldd --version
ldd (GNU libc) 2.17

I rebuilt libzksnarkjni.so on this machine, and now it works properly. It also works on my Linux laptop.

  1. I tried to build the aarch64 version of the library, but encountered this issue:
No symbol version section for versioned symbol `memcpy@GLIBC_2.2.5'

I applied a workaround from https://chromium.googlesource.com/external/github.com/google/protobuf/+/HEAD/ruby/ext/google/protobuf_c/wrap_memcpy.c in the cpp/src/memcpy.c file. With this adjustment, it works fine now.

Could you please confirm if the issue has been resolved?

@halibobo1205
Copy link
Contributor

@AllFi Hi, re-check on CentOS 7 x86_64, it works well.
As for the error, No symbol version section for versioned symbol 'memcpy@GLIBC_2.2.5', you can try it on linux 7 arm64, I've done it before. Feedback is appreciated when you try it!

@r0wdy1
Copy link

r0wdy1 commented Sep 26, 2023

As for the error, No symbol version section for versioned symbol 'memcpy@GLIBC_2.2.5', you can try it on linux 7 arm64, I've done it before. Feedback is appreciated when you try it!

Do you expect any more fixes? It seems that everything works

@halibobo1205
Copy link
Contributor

@r0wdy1 Hi,
system info : Linux 1804bbf650cf 5.10.104-linuxkit #1 SMP PREEMPT Wed Mar 9 19:01:25 UTC 2022 aarch64 aarch64 aarch64 GNU/Linux
docker info : arm64v8/centos:7
error log :

[INFO]
[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ zksnark-java-sdk ---
[INFO]
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running org.tron.common.zksnark.LibsodiumTest
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.087 s <<< FAILURE! - in org.tron.common.zksnark.LibsodiumTest
[ERROR] org.tron.common.zksnark.LibsodiumTest  Time elapsed: 0.086 s  <<< ERROR!
java.lang.UnsatisfiedLinkError: Could not load library. Reasons: [java.lang.LinkageError: Unable to load library zksnarkjni, java.lang.LinkageError: Unable to load library from /tmp/libzksnarkjni-6153043888255349959.so, java.lang.LinkageError: Unable to load library from /root/.hawtjni/zksnarkjni/libzksnarkjni-2395993998654073025.so, java.lang.LinkageError: Unable to load library from /tmp/libzksnarkjni-7507480833451540567.so, java.lang.LinkageError: Unable to load library from /root/.hawtjni/zksnarkjni/libzksnarkjni-9121779718342590029.so]
	at org.tron.common.zksnark.LibsodiumTest.librustzcashInitZksnarkParams(LibsodiumTest.java:44)

[INFO] Running org.tron.common.zksnark.LibarkworksTest
[ERROR] Tests run: 5, Failures: 0, Errors: 5, Skipped: 0, Time elapsed: 0.028 s <<< FAILURE! - in org.tron.common.zksnark.LibarkworksTest
[ERROR] libarkworksAddG1(org.tron.common.zksnark.LibarkworksTest)  Time elapsed: 0.025 s  <<< ERROR!
java.lang.UnsatisfiedLinkError: Could not load library. Reasons: [java.lang.LinkageError: Unable to load library zksnarkjni, java.lang.LinkageError: Unable to load library from /tmp/libzksnarkjni-2045370397893314252.so, java.lang.LinkageError: Unable to load library from /root/.hawtjni/zksnarkjni/libzksnarkjni-1571081008738642458.so, java.lang.LinkageError: Unable to load library from /tmp/libzksnarkjni-51807199268131437.so, java.lang.LinkageError: Unable to load library from /root/.hawtjni/zksnarkjni/libzksnarkjni-256481120846540177.so]
	at org.tron.common.zksnark.LibarkworksTest.libarkworksAddG1(LibarkworksTest.java:89)

[ERROR] libarkworksMulG1(org.tron.common.zksnark.LibarkworksTest)  Time elapsed: 0.001 s  <<< ERROR!
java.lang.NoClassDefFoundError: Could not initialize class org.tron.common.zksnark.LibarkworksWrapper
	at org.tron.common.zksnark.LibarkworksTest.libarkworksMulG1(LibarkworksTest.java:118)

[ERROR] libarkworksG1IsValid(org.tron.common.zksnark.LibarkworksTest)  Time elapsed: 0 s  <<< ERROR!
java.lang.NoClassDefFoundError: Could not initialize class org.tron.common.zksnark.LibarkworksWrapper
	at org.tron.common.zksnark.LibarkworksTest.libarkworksG1IsValid(LibarkworksTest.java:17)

[ERROR] libarkworksPairingCheck(org.tron.common.zksnark.LibarkworksTest)  Time elapsed: 0.002 s  <<< ERROR!
java.lang.NoClassDefFoundError: Could not initialize class org.tron.common.zksnark.LibarkworksWrapper
	at org.tron.common.zksnark.LibarkworksTest.libarkworksPairingCheck(LibarkworksTest.java:138)

[ERROR] libarkworksG2IsValid(org.tron.common.zksnark.LibarkworksTest)  Time elapsed: 0 s  <<< ERROR!
java.lang.NoClassDefFoundError: Could not initialize class org.tron.common.zksnark.LibarkworksWrapper
	at org.tron.common.zksnark.LibarkworksTest.libarkworksG2IsValid(LibarkworksTest.java:44)

[INFO] Running org.tron.common.zksnark.LibrustzcashTest
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.011 s <<< FAILURE! - in org.tron.common.zksnark.LibrustzcashTest
[ERROR] org.tron.common.zksnark.LibrustzcashTest  Time elapsed: 0.011 s  <<< ERROR!
java.lang.NoClassDefFoundError: Could not initialize class org.tron.common.zksnark.LibrustzcashWrapper
	at org.tron.common.zksnark.LibrustzcashTest.librustzcashInitZksnarkParams(LibrustzcashTest.java:17)

[INFO]
[INFO] Results:
[INFO]
[ERROR] Errors:
[ERROR]   LibarkworksTest.libarkworksAddG1:89 ? UnsatisfiedLink Could not load library. ...
[ERROR]   LibarkworksTest.libarkworksG1IsValid:17 NoClassDefFound Could not initialize c...
[ERROR]   LibarkworksTest.libarkworksG2IsValid:44 NoClassDefFound Could not initialize c...
[ERROR]   LibarkworksTest.libarkworksMulG1:118 NoClassDefFound Could not initialize clas...
[ERROR]   LibarkworksTest.libarkworksPairingCheck:138 NoClassDefFound Could not initiali...
[ERROR]   LibrustzcashTest.librustzcashInitZksnarkParams:17 NoClassDefFound Could not in...
[ERROR]   LibsodiumTest.librustzcashInitZksnarkParams:44 ? UnsatisfiedLink Could not loa...
[INFO]
[ERROR] Tests run: 7, Failures: 0, Errors: 7, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------

Then ,i rebuild it, there's still an error.

[INFO]
[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ zksnark-java-sdk ---
[INFO]
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running org.tron.common.zksnark.LibsodiumTest
[INFO] Tests run: 6, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.374 s - in org.tron.common.zksnark.LibsodiumTest
[INFO] Running org.tron.common.zksnark.LibarkworksTest
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 6, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.805s
[INFO] Finished at: Tue Sep 26 08:28:59 UTC 2023
[INFO] Final Memory: 23M/271M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.22.2:test (default-test) on project zksnark-java-sdk: There are test failures.
[ERROR]
[ERROR] Please refer to /tmp/zksnark-java-sdk/target/surefire-reports for the individual test results.
[ERROR] Please refer to dump files (if any exist) [date].dump, [date]-jvmRun[N].dump and [date].dumpstream.
[ERROR] The forked VM terminated without properly saying goodbye. VM crash or System.exit called?
[ERROR] Command was /bin/sh -c cd /tmp/zksnark-java-sdk/target && /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.342.b07-1.el7_9.aarch64/jre/bin/java -javaagent:/root/.m2/repository/org/jacoco/org.jacoco.agent/0.8.8/org.jacoco.agent-0.8.8-runtime.jar=destfile=/tmp/zksnark-java-sdk/target/jacoco.exec -ea -jar /tmp/zksnark-java-sdk/target/surefire/surefirebooter1731445495970835878.jar /tmp/zksnark-java-sdk/target/surefire 2023-09-26T08-28-58_365-jvmRun1 surefire6294069238496356915tmp surefire_01543138630964026371tmp
[ERROR] Error occurred in starting fork, check output in log
[ERROR] Process Exit Code: 127
[ERROR] Crashed tests:
[ERROR] org.tron.common.zksnark.LibarkworksTest
[ERROR] org.apache.maven.surefire.booter.SurefireBooterForkException: The forked VM terminated without properly saying goodbye. VM crash or System.exit called?
[ERROR] Command was /bin/sh -c cd /tmp/zksnark-java-sdk/target && /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.342.b07-1.el7_9.aarch64/jre/bin/java -javaagent:/root/.m2/repository/org/jacoco/org.jacoco.agent/0.8.8/org.jacoco.agent-0.8.8-runtime.jar=destfile=/tmp/zksnark-java-sdk/target/jacoco.exec -ea -jar /tmp/zksnark-java-sdk/target/surefire/surefirebooter1731445495970835878.jar /tmp/zksnark-java-sdk/target/surefire 2023-09-26T08-28-58_365-jvmRun1 surefire6294069238496356915tmp surefire_01543138630964026371tmp
[ERROR] Error occurred in starting fork, check output in log
[ERROR] Process Exit Code: 127
[ERROR] Crashed tests:
[ERROR] org.tron.common.zksnark.LibarkworksTest
[ERROR] at org.apache.maven.plugin.surefire.booterclient.ForkStarter.fork(ForkStarter.java:669)
[ERROR] at org.apache.maven.plugin.surefire.booterclient.ForkStarter.run(ForkStarter.java:282)
[ERROR] at org.apache.maven.plugin.surefire.booterclient.ForkStarter.run(ForkStarter.java:245)
[ERROR] at org.apache.maven.plugin.surefire.AbstractSurefireMojo.executeProvider(AbstractSurefireMojo.java:1183)
[ERROR] at org.apache.maven.plugin.surefire.AbstractSurefireMojo.executeAfterPreconditionsChecked(AbstractSurefireMojo.java:1011)
[ERROR] at org.apache.maven.plugin.surefire.AbstractSurefireMojo.execute(AbstractSurefireMojo.java:857)
[ERROR] at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:101)
[ERROR] at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:209)
[ERROR] at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
[ERROR] at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
[ERROR] at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:84)
[ERROR] at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:59)
[ERROR] at org.apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBuild(LifecycleStarter.java:183)
[ERROR] at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:161)
[ERROR] at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:320)
[ERROR] at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:156)
[ERROR] at org.apache.maven.cli.MavenCli.execute(MavenCli.java:537)
[ERROR] at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:196)
[ERROR] at org.apache.maven.cli.MavenCli.main(MavenCli.java:141)
[ERROR] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[ERROR] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
[ERROR] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[ERROR] at java.lang.reflect.Method.invoke(Method.java:498)
[ERROR] at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:290)
[ERROR] at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:230)
[ERROR] at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:414)
[ERROR] at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:357)
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

@AllFi
Copy link
Author

AllFi commented Sep 26, 2023

Hello, @halibobo1205! I managed to solve this by adding -pthread here:

	SET_TARGET_PROPERTIES( zksnarkjni PROPERTIES LINK_FLAGS "${LINK_FLAGS} -Wl,--wrap=memcpy -pthread" )

But I don't know if this solution is ok. I tried to build master branch in the arm64v8/centos:7 but I also got this error:

/usr/bin/ld: libzksnarkjni.so: No symbol version section for versioned symbol `memcpy@GLIBC_2.2.5'

So it seems the fix in the memcpy.c is still necessary.

What do you think the best way is to build it?

@halibobo1205
Copy link
Contributor

Hello, @halibobo1205! I managed to solve this by adding -pthread here:

	SET_TARGET_PROPERTIES( zksnarkjni PROPERTIES LINK_FLAGS "${LINK_FLAGS} -Wl,--wrap=memcpy -pthread" )

But I don't know if this solution is ok. I tried to build master branch in the arm64v8/centos:7 but I also got this error:

/usr/bin/ld: libzksnarkjni.so: No symbol version section for versioned symbol `memcpy@GLIBC_2.2.5'

So it seems the fix in the memcpy.c is still necessary.

What do you think the best way is to build it?

Sorry, I also tried again with M1 , it also failed. I may have tried to build successfully on M2 before, I can try again on M2, and if it still fails, will build it the way you suggested, but I'm not familiar with c and rust! Here's the repository for my previous build:https://github.com/halibobor/zksnark-java-sdk.

1. add -pthread flag to fix link error on arm
2. rebuild linux libs
@halibobo1205
Copy link
Contributor

@AllFi Sorry, I found the previous build log that shows memcpy@GLIBC_2.2.5 changed to memcpy@GLIBC_2.17.

Copy link
Contributor

@halibobo1205 halibobo1205 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@AllFi
Copy link
Author

AllFi commented Sep 26, 2023

@AllFi Sorry, I found the previous build log that shows memcpy@GLIBC_2.2.5 changed to memcpy@GLIBC_2.17.

No problem, this solution also works for me with -pthread flag. I can replace my solution in memcpy.c with it if necessary. Thank you for your help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants