We wrap the Parity implementation of the Barreto–Naehrig curve introduced in BCTV13, using the Java Native Interface (JNI). This enables arithmetic and pairing operations over the Alt-BN 128 curve from Java programs. The Alt-Bn 128 curve (which is parameterized differently than the BN 128 curve) is completely specified in section E.1 of the Ethereum Yellow-paper.
This repository contains two projects. First, labelled native
, generates the native library, and the second, labelled bench
, tests the JNI functionality.
The native
directory contains a rust project that leverages the rust jni bindings to generate a shared native library. The native library includes the appropriate JNI headers to bridge bytecode running in a JVM and the native code implementing the curve operations.
In order to build the shared library, first, initialize the project submodules. Then, in the native
directory, run the following command:
cargo build --release
The build output (available in the target/release
folder) is libbn_jni.so
. The file extension will be .so
on linux, .dylib
on mac or .dll
on windows.
The bench
folder contains a Java testbench to validate invocation of native library from Java.
Before executing the testbench, make sure to build the native library, since the testbench sets the java.library.path
to the native/target/release/
folder. The project uses gradle, so the tests can be executed by running the following command in the bench
directory:
./gradlew test
The Parity implementation of the Alt-Bn 128 curve was chosen since this implementation boasted the best performance of the well-known open-source implementations for the bn128 curve:
No serious performance benchmarking was done for this JNI wrapper. Overhead incurred in the JVM while calling native libraries through JNI is well understood (see this paper & this paper). Better benchmarking needs to be conducted on this library to determine:
- The real overhead incurred by JNI vs native performance (which was observed not to be preserved from the rudimentary benchmarking conducted), and
- Comparison of a pure-Java implementation of curve operations versus a native implementation accessed via JNI.