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

Support for EIP2537 Constantine Lib #210

Merged
merged 8 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -362,9 +362,11 @@ build_constantine() {

# Compile the native library
if [[ "$OSTYPE" == "darwin"* ]]; then
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
# link against the static libconstantine.a so we do not have to deal with multiple libraries in jni/jna:
clang -I"${JAVA_HOME}/include" -I"${JAVA_HOME}/include/darwin" -shared -o "$SCRIPTDIR/constantine/build/${OSARCH}/lib/libconstantinebindings.dylib" jna_ethereum_evm_precompiles.c -Iconstantine/include -I. constantine/lib/libconstantine.a
elif [[ "$OSTYPE" == "linux-gnu"* ]]; then
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
# link against the static libconstantine.a so we do not have to deal with multiple libraries in jni/jna:
gcc -I"${JAVA_HOME}/include" -I"${JAVA_HOME}/include/linux" -fPIC -shared -o "$SCRIPTDIR/constantine/build/${OSARCH}/lib/libconstantinebindings.so" jna_ethereum_evm_precompiles.c -Iconstantine/include -I. -Lconstantine/lib constantine/lib/libconstantine.a
else
echo "Unsupported OS/architecture: ${OSARCH}"
exit 1
Expand Down
18 changes: 7 additions & 11 deletions constantine/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,35 +26,31 @@ tasks.withType(JavaCompile) {
}

task macArmLibCopy(type: Copy) {
from "build/darwin-aarch64/lib/libconstantineeip196.dylib"
from "build/darwin-aarch64/lib/libconstantinebindings.dylib"
into 'build/resources/main/darwin-aarch64'
}

task macLibCopy(type: Copy) {
from "build/darwin-x86-64/lib/libconstantineeip196.dylib"
from "build/darwin-x86-64/lib/libconstantinebindings.dylib"
into 'build/resources/main/darwin-x86-64'
}

task linuxLibCopy(type: Copy) {
from "build/linux-gnu-x86_64/lib/libconstantineeip196.so"
from "build/linux-gnu-x86_64/lib/libconstantinebindings.so"
into 'build/resources/main/linux-gnu-x86_64'

}

task linuxArm64LibCopy(type: Copy) {
from "build/linux-gnu-aarch64/lib/libconstantineeip196.so"
from "build/linux-gnu-aarch64/lib/libconstantinebindings.so"
into 'build/resources/main/linux-gnu-aarch64'
}

processResources.dependsOn macArmLibCopy, macLibCopy, linuxLibCopy, linuxArm64LibCopy

tasks.named('test', Test) {
description = 'Runs the Java tests'
useJUnit {
include '**/*Test.class'
}
environment 'LD_LIBRARY_PATH', "${System.env.LD_LIBRARY_PATH}:build/resources/main/linux-gnu-x86_64"

test {
// environment 'LD_LIBRARY_PATH', "${System.env.LD_LIBRARY_PATH}:build/resources/main/linux-gnu-x86_64"
systemProperty 'jna.library.path', file('build/resources/main/linux-gnu-x86_64').absolutePath
dependsOn macArmLibCopy, macLibCopy, linuxLibCopy, linuxArm64LibCopy
}

Expand Down
51 changes: 0 additions & 51 deletions constantine/ethereum_evm_precompiles.c

This file was deleted.

37 changes: 0 additions & 37 deletions constantine/ethereum_evm_precompiles.h

This file was deleted.

41 changes: 37 additions & 4 deletions constantine/jna_ethereum_evm_precompiles.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#include "jna_ethereum_evm_precompiles.h"
#include <constantine.h>
#include <stdio.h>

Expand All @@ -13,18 +12,52 @@ void printByteArray(const char* label, const byte* array, size_t len) {
printf("]\n");
}

// BN254 functions
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);
}

// BLS12-381 functions
int bls12381_g1add(byte* r, int r_len, const byte* inputs, int inputs_len) {
return (int) ctt_eth_evm_bls12381_g1add(r, (ptrdiff_t)r_len, inputs, (ptrdiff_t)inputs_len);
}

int bls12381_g2add(byte* r, int r_len, const byte* inputs, int inputs_len) {
return (int) ctt_eth_evm_bls12381_g2add(r, (ptrdiff_t)r_len, inputs, (ptrdiff_t)inputs_len);
}

int bls12381_g1mul(byte* r, int r_len, const byte* inputs, int inputs_len) {
return (int) ctt_eth_evm_bls12381_g1mul(r, (ptrdiff_t)r_len, inputs, (ptrdiff_t)inputs_len);
}

int bls12381_g2mul(byte* r, int r_len, const byte* inputs, int inputs_len) {
return (int) ctt_eth_evm_bls12381_g2mul(r, (ptrdiff_t)r_len, inputs, (ptrdiff_t)inputs_len);
}

int bls12381_g1msm(byte* r, int r_len, const byte* inputs, int inputs_len) {
return (int) ctt_eth_evm_bls12381_g1msm(r, (ptrdiff_t)r_len, inputs, (ptrdiff_t)inputs_len);
}

int bls12381_g2msm(byte* r, int r_len, const byte* inputs, int inputs_len) {
return (int) ctt_eth_evm_bls12381_g2msm(r, (ptrdiff_t)r_len, inputs, (ptrdiff_t)inputs_len);
}

int bls12381_pairingCheck(byte* r, int r_len, const byte* inputs, int inputs_len) {
return (int) ctt_eth_evm_bls12381_pairingcheck(r, (ptrdiff_t)r_len, inputs, (ptrdiff_t)inputs_len);
}

int bls12381_mapFpToG1(byte* r, int r_len, const byte* inputs, int inputs_len) {
return (int) ctt_eth_evm_bls12381_map_fp_to_g1(r, (ptrdiff_t)r_len, inputs, (ptrdiff_t)inputs_len);
}

int bls12381_mapFp2ToG2(byte* r, int r_len, const byte* inputs, int inputs_len) {
return (int) ctt_eth_evm_bls12381_map_fp2_to_g2(r, (ptrdiff_t)r_len, inputs, (ptrdiff_t)inputs_len);
}
34 changes: 0 additions & 34 deletions constantine/jna_ethereum_evm_precompiles.h

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class LibConstantineEIP196 {
static {
boolean enabled;
try {
Native.register(LibConstantineEIP196.class, "constantineeip196");
Native.register(LibConstantineEIP196.class, "constantinebindings");
enabled = true;
} catch (final Throwable t) {
t.printStackTrace();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package org.hyperledger.besu.nativelib.constantine;

import com.sun.jna.Native;

public class LibConstantineEIP2537 {
public static final boolean ENABLED;

static {
boolean enabled;
try {
Native.register(LibConstantineEIP2537.class, "constantinebindings");
enabled = true;
} catch (final Throwable t) {
t.printStackTrace();
enabled = false;
}
ENABLED = enabled;
}

public static native int bls12381_g1add(byte[] r, int r_len, byte[] inputs, int inputs_len);
public static native int bls12381_g2add(byte[] r, int r_len, byte[] inputs, int inputs_len);
public static native int bls12381_g1mul(byte[] r, int r_len, byte[] inputs, int inputs_len);
public static native int bls12381_g2mul(byte[] r, int r_len, byte[] inputs, int inputs_len);
public static native int bls12381_g1msm(byte[] r, int r_len, byte[] inputs, int inputs_len);
public static native int bls12381_g2msm(byte[] r, int r_len, byte[] inputs, int inputs_len);
public static native int bls12381_pairingCheck(byte[] r, int r_len, byte[] inputs, int inputs_len);
public static native int bls12381_mapFpToG1(byte[] r, int r_len, byte[] inputs, int inputs_len);
public static native int bls12381_mapFp2ToG2(byte[] r, int r_len, byte[] inputs, int inputs_len);

public static byte[] g1add(byte[] inputs) {
byte[] result = new byte[128];
int status = bls12381_g1add(result, result.length, inputs, inputs.length);
if (status != 0) {
throw new RuntimeException("eth_evm_bls12381_g1add failed with status: " + status);
}
return result;
}

public static byte[] g2add(byte[] inputs) {
byte[] result = new byte[256];
int status = bls12381_g2add(result, result.length, inputs, inputs.length);
if (status != 0) {
throw new RuntimeException("eth_evm_bls12381_g2add failed with status: " + status);
}
return result;
}

public static byte[] g1mul(byte[] inputs) {
byte[] result = new byte[128];
int status = bls12381_g1mul(result, result.length, inputs, inputs.length);
if (status != 0) {
throw new RuntimeException("eth_evm_bls12381_g1mul failed with status: " + status);
}
return result;
}

public static byte[] g2mul(byte[] inputs) {
byte[] result = new byte[256];
int status = bls12381_g2mul(result, result.length, inputs, inputs.length);
if (status != 0) {
throw new RuntimeException("eth_evm_bls12381_g2mul failed with status: " + status);
}
return result;
}

public static byte[] g1msm(byte[] inputs) {
byte[] result = new byte[128];
int status = bls12381_g1msm(result, result.length, inputs, inputs.length);
if (status != 0) {
throw new RuntimeException("eth_evm_bls12381_g1msm failed with status: " + status);
}
return result;
}

public static byte[] g2msm(byte[] inputs) {
byte[] result = new byte[256];
int status = bls12381_g2msm(result, result.length, inputs, inputs.length);
if (status != 0) {
throw new RuntimeException("eth_evm_bls12381_g2msm failed with status: " + status);
}
return result;
}

public static byte[] pairingCheck(byte[] inputs) {
byte[] result = new byte[32];
int status = bls12381_pairingCheck(result, result.length, inputs, inputs.length);
if (status != 0) {
throw new RuntimeException("eth_evm_bls12381_pairingcheck failed with status: " + status);
}
return result;
}

public static byte[] mapFpToG1(byte[] inputs) {
byte[] result = new byte[128];
int status = bls12381_mapFpToG1(result, result.length, inputs, inputs.length);
if (status != 0) {
throw new RuntimeException("eth_evm_bls12381_map_fp_to_g1 failed with status: " + status);
}
return result;
}

public static byte[] mapFp2ToG2(byte[] inputs) {
byte[] result = new byte[256];
int status = bls12381_mapFp2ToG2(result, result.length, inputs, inputs.length);
if (status != 0) {
throw new RuntimeException("eth_evm_bls12381_map_fp2_to_g2 failed with status: " + status);
}
return result;
}
}
Loading
Loading