Besu native

In Besu, we often use C, Rust, or Go for the cryptographic part. This is mainly because these languages tend to offer better performance.

When we want to add a new native library to Besu, it is relatively straightforward. We simply need to add it to the project.

https://github.com/hyperledger/besu-native

Integrating a library into Besu can be done by following these general steps:

Step 1: Add the Go library

for example this one

https://github.com/hyperledger/besu-native/tree/main/gnark

In the directory, you will find two parts:

  • gnark-jni which will contain the JNI wrapper allowing Java to call the Go library.
  • go.mod which will contain the import link to the Go library.

gnark-jni.go will contain the methods that Java can call, using //export to indicate that the method is exposed.

In this class, you need to perform the necessary conversions to transform objects coming from Java into objects that can be used by the Go library.

  • Add the .java class that call the go exposed methods

https://github.com/hyperledger/besu-native/tree/main/gnark/src/main/java/org/hyperledger/besu/nativelib/gnark

Step 2 Add your library to the build.sh file for cross-platform building

  1. Open the build.sh file of the Besu Native project
  2. Look for a similar section in the file for a library of the same language (C, Rust, Go) as the one you want to add.
  3. Copy and paste that section to create a new section for your library.
  4. Update the file paths to reflect those of your library.
  5. Modify the build commands to match the ones for your library (e.g., the compilation command for the Go library).
  6. Ensure that the necessary environment variables for building your library are correctly set in the build.sh file.

Test your library with JAVA

You can create a Java class to test your code and verify that the JNI part is working correctly.

https://github.com/hyperledger/besu-native/blob/main/gnark/src/test/java/org/hyperledger/besu/nativelib/gnark/LibGnarkTest.java