-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Linux aarch64 support for EIP196 constantine (#203)
* add support for linux aarch64 * convert to JNA, use static libconstantine.a Signed-off-by: Nischal Sharma <[email protected]> Co-authored-by: garyschulte <[email protected]>
- Loading branch information
1 parent
bc48568
commit e996d8e
Showing
11 changed files
with
156 additions
and
75 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,7 @@ on: | |
|
||
jobs: | ||
native-build-linux-x86-64: | ||
runs-on: ubuntu-20.04 | ||
runs-on: ubuntu-22.04 | ||
env: | ||
SKIP_GRADLE: true | ||
steps: | ||
|
@@ -111,6 +111,10 @@ jobs: | |
with: | ||
name: gnark native build artifacts | ||
path: gnark/build/ | ||
- uses: actions/[email protected] | ||
with: | ||
name: constantine native build artifacts | ||
path: constantine/build/ | ||
|
||
native-build-macos: | ||
runs-on: macos-13 | ||
|
@@ -258,7 +262,7 @@ jobs: | |
path: constantine/build/ | ||
|
||
final-assembly: | ||
runs-on: ubuntu-20.04 | ||
runs-on: ubuntu-22.04 | ||
needs: | ||
- native-build-macos | ||
- native-build-m1 | ||
|
@@ -349,6 +353,10 @@ jobs: | |
with: | ||
name: jars | ||
path: gnark/build/libs | ||
- uses: actions/[email protected] | ||
with: | ||
name: jars | ||
path: constantine/build/libs | ||
- name: gradle publish | ||
uses: gradle/gradle-build-action@v2 | ||
if: contains('refs/heads/release-', github.ref) || github.ref == 'refs/heads/main' | ||
|
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 |
---|---|---|
|
@@ -307,6 +307,8 @@ EOF | |
LIBRARY_EXTENSION=so | ||
elif [[ "$OSTYPE" == "darwin"* ]]; then | ||
LIBRARY_EXTENSION=dylib | ||
export GOROOT=$(brew --prefix [email protected])/libexec | ||
export PATH=$GOROOT/bin:$PATH | ||
fi | ||
|
||
go build -buildmode=c-shared -o libgnark_jni.$LIBRARY_EXTENSION gnark-jni.go | ||
|
@@ -324,33 +326,45 @@ build_constantine() { | |
echo "####### build constantine ####" | ||
echo "#############################" | ||
|
||
# Skip if OSARCH is linux-gnu-aarch64 | ||
if [[ "$OSARCH" == "linux-gnu-aarch64" ]]; then | ||
echo "Skipping build for OSARCH: ${OSARCH}" | ||
return | ||
fi | ||
|
||
cd "$SCRIPTDIR/constantine/constantine" | ||
|
||
# delete old build dir, if exists | ||
rm -rf "$SCRIPTDIR/constantine/build" || true | ||
mkdir -p "$SCRIPTDIR/constantine/build/${OSARCH}/lib" | ||
|
||
export PATH=$HOME/.nimble/bin:$PATH | ||
# Check and modify config.nims if on x86_64 | ||
if [[ "$OSARCH" == "linux-gnu-x86_64" ]]; then | ||
# Check if the config.nims already contains the necessary flags | ||
if ! grep -q 'switch("passC", "-fPIC")' config.nims; then | ||
{ | ||
echo 'when defined(linux):' | ||
echo ' switch("passC", "-fPIC")' | ||
echo ' switch("passL", "-fPIC")' | ||
} >> config.nims | ||
fi | ||
fi | ||
|
||
# Build the constantine library | ||
export CTT_LTO=false | ||
nimble make_lib | ||
if [[ "$OSARCH" == "linux-gnu-aarch64" ]]; then | ||
# Download and extract Nim | ||
wget https://github.com/nim-lang/nightlies/releases/download/2024-03-28-version-2-0-b47747d31844c6bd9af4322efe55e24fefea544c/nim-2.0.4-linux_arm64.tar.xz | ||
tar -xf nim-2.0.4-linux_arm64.tar.xz | ||
git config --global --add safe.directory /home/ubuntu/constantine/constantine | ||
export PATH=$(pwd)/nim-2.0.4/bin:$PATH | ||
nimble make_lib | ||
else | ||
export PATH=$HOME/.nimble/bin:$PATH | ||
nimble make_lib | ||
fi | ||
|
||
cd "$SCRIPTDIR/constantine/" | ||
|
||
# Compile the native library | ||
if [[ "$OSTYPE" == "darwin"* ]]; then | ||
cp "$SCRIPTDIR/constantine/constantine/lib/libconstantine.dylib" "$SCRIPTDIR/constantine/build/${OSARCH}/lib/" | ||
clang -I"${JAVA_HOME}/include" -I"${JAVA_HOME}/include/darwin" -shared -o "$SCRIPTDIR/constantine/build/${OSARCH}/lib/libconstantineeip196.jnilib" ethereum_evm_precompiles.c -Iconstantine/include -I. -Lconstantine/lib -lconstantine | ||
clang -I"${JAVA_HOME}/include" -I"${JAVA_HOME}/include/darwin" -shared -o "$SCRIPTDIR/constantine/build/${OSARCH}/lib/libconstantineeip196.dylib" jna_ethereum_evm_precompiles.c -Iconstantine/include -I. constantine/lib/libconstantine.a | ||
elif [[ "$OSTYPE" == "linux-gnu"* ]]; then | ||
cp "$SCRIPTDIR/constantine/constantine/lib/libconstantine.so" "$SCRIPTDIR/constantine/build/${OSARCH}/lib/" | ||
clang -I"${JAVA_HOME}/include" -I"${JAVA_HOME}/include/linux" -fPIC -shared -o "$SCRIPTDIR/constantine/build/${OSARCH}/lib/libconstantineeip196.so" ethereum_evm_precompiles.c -Iconstantine/include -I. -Lconstantine/lib -lconstantine | ||
gcc -I"${JAVA_HOME}/include" -I"${JAVA_HOME}/include/linux" -fPIC -shared -o "$SCRIPTDIR/constantine/build/${OSARCH}/lib/libconstantineeip196.so" jna_ethereum_evm_precompiles.c -Iconstantine/include -I. -Lconstantine/lib constantine/lib/libconstantine.a | ||
else | ||
echo "Unsupported OS/architecture: ${OSARCH}" | ||
exit 1 | ||
|
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,30 @@ | ||
#include "jna_ethereum_evm_precompiles.h" | ||
#include <constantine.h> | ||
#include <stdio.h> | ||
|
||
void printByteArray(const char* label, const byte* array, size_t len) { | ||
printf("%s: [", label); | ||
for (size_t i = 0; i < len; i++) { | ||
printf("%02x", array[i]); | ||
if (i < len - 1) { | ||
printf(", "); | ||
} | ||
} | ||
printf("]\n"); | ||
} | ||
|
||
int bn254_g1add(byte* r, int r_len, const byte* inputs, int inputs_len) { | ||
// Call the original function | ||
return (int) ctt_eth_evm_bn254_g1add(r, (ptrdiff_t)r_len, inputs, (ptrdiff_t)inputs_len); | ||
} | ||
|
||
int bn254_g1mul(byte* r, int r_len, const byte* inputs, int inputs_len) { | ||
// Call the original function | ||
return (int) ctt_eth_evm_bn254_g1mul(r, (ptrdiff_t)r_len, inputs, (ptrdiff_t)inputs_len); | ||
} | ||
|
||
int bn254_pairingCheck(byte* r, int r_len, const byte* inputs, int inputs_len) { | ||
// Call the original function | ||
return (int) ctt_eth_evm_bn254_ecpairingcheck(r, (ptrdiff_t)r_len, inputs, (ptrdiff_t)inputs_len); | ||
} | ||
|
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,34 @@ | ||
#ifndef _Included_LibConstantineEIP196_Wrapper | ||
#define _Included_LibConstantineEIP196_Wrapper | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
// Define byte as unsigned char | ||
typedef unsigned char byte; | ||
|
||
/* | ||
* Function: ctt_eth_evm_bn254_g1add_wrapper | ||
* Signature: (byte*, int, const byte*, int) -> int | ||
*/ | ||
int bn254_g1add(byte* r, int r_len, const byte* inputs, int inputs_len); | ||
|
||
/* | ||
* Function: ctt_eth_evm_bn254_g1mul_wrapper | ||
* Signature: (byte*, int, const byte*, int) -> int | ||
*/ | ||
int bn254_g1mul(byte* r, int r_len, const byte* inputs, int inputs_len); | ||
|
||
/* | ||
* Function: ctt_eth_evm_bn254_pairingCheck_wrapper | ||
* Signature: (byte*, int, const byte*, int) -> int | ||
*/ | ||
int bn254_pairingCheck(byte* r, int r_len, const byte* inputs, int inputs_len); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#endif /* _Included_LibConstantineEIP196_Wrapper */ | ||
|
38 changes: 23 additions & 15 deletions
38
...antine/src/main/java/org/hyperledger/besu/nativelib/constantine/LibConstantineEIP196.java
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 |
---|---|---|
@@ -1,42 +1,50 @@ | ||
package org.hyperledger.besu.nativelib.constantine; | ||
|
||
import com.sun.jna.Native; | ||
|
||
public class LibConstantineEIP196 { | ||
public static final boolean ENABLED; | ||
|
||
static { | ||
System.loadLibrary("constantineeip196"); | ||
boolean enabled; | ||
try { | ||
Native.register(LibConstantineEIP196.class, "constantineeip196"); | ||
enabled = true; | ||
} catch (final Throwable t) { | ||
t.printStackTrace(); | ||
enabled = false; | ||
} | ||
ENABLED = enabled; | ||
} | ||
|
||
public native int ctt_eth_evm_bn254_g1add(byte[] r, int r_len, byte[] inputs, int inputs_len); | ||
public native int ctt_eth_evm_bn254_g1mul(byte[] r, int r_len, byte[] inputs, int inputs_len); | ||
public native int ctt_eth_evm_bn254_pairingCheck(byte[] r, int r_len, byte[] inputs, int inputs_len); | ||
|
||
public static void loadNativeLibrary() { | ||
System.loadLibrary("constantineeip196"); | ||
} | ||
public static native int bn254_g1add(byte[] r, int r_len, byte[] inputs, int inputs_len); | ||
public static native int bn254_g1mul(byte[] r, int r_len, byte[] inputs, int inputs_len); | ||
public static native int bn254_pairingCheck(byte[] r, int r_len, byte[] inputs, int inputs_len); | ||
|
||
public byte[] add(byte[] inputs) { | ||
public static byte[] add(byte[] inputs) { | ||
byte[] result = new byte[64]; | ||
int status = ctt_eth_evm_bn254_g1add(result, result.length, inputs, inputs.length); | ||
int status = bn254_g1add(result, result.length, inputs, inputs.length); | ||
if (status != 0) { | ||
throw new RuntimeException("ctt_eth_evm_bn254_g1add failed with status: " + status); | ||
} | ||
return result; | ||
} | ||
|
||
public byte[] mul(byte[] inputs) { | ||
public static byte[] mul(byte[] inputs) { | ||
byte[] result = new byte[64]; | ||
int status = ctt_eth_evm_bn254_g1mul(result, result.length, inputs, inputs.length); | ||
int status = bn254_g1mul(result, result.length, inputs, inputs.length); | ||
if (status != 0) { | ||
throw new RuntimeException("ctt_eth_evm_bn254_g1mul failed with status: " + status); | ||
} | ||
return result; | ||
} | ||
|
||
public byte[] pairingCheck(byte[] inputs) { | ||
public static byte[] pairingCheck(byte[] inputs) { | ||
byte[] result = new byte[32]; | ||
int status = ctt_eth_evm_bn254_pairingCheck(result, result.length, inputs, inputs.length); | ||
int status = bn254_pairingCheck(result, result.length, inputs, inputs.length); | ||
if (status != 0) { | ||
throw new RuntimeException("ctt_eth_evm_bn254_pairingCheck failed with status: " + status); | ||
} | ||
return result; | ||
} | ||
} | ||
} |
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
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
Oops, something went wrong.