Skip to content

Commit

Permalink
[no ci] squash commit, use gnark-crypto for eip-196
Browse files Browse the repository at this point in the history
Signed-off-by: garyschulte <[email protected]>
  • Loading branch information
garyschulte committed Jul 12, 2024
1 parent cbb9a6e commit 3d1ad04
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 33 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- Add support to load external profiles using `--profile` [#7265](https://github.com/hyperledger/besu/issues/7265)
- `privacy-nonce-always-increments` option enables private transactions to always increment the nonce, even if the transaction is invalid [#6593](https://github.com/hyperledger/besu/pull/6593)
- Added `block-test` subcommand to the evmtool which runs blockchain reference tests [#7293](https://github.com/hyperledger/besu/pull/7293)
- implement gnark-crypto for eip-196 and eip-2537 [#7262](https://github.com/hyperledger/besu/pull/7262)

### Bug fixes

Expand Down
14 changes: 7 additions & 7 deletions ethereum/referencetests/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -268,13 +268,13 @@ following commands:
processResources.dependsOn('validateReferenceTestSubmodule')

def generateTestFiles(
FileTree jsonPath,
File templateFile,
String pathstrip,
String destination,
String namePrefix,
String packageString,
String ... excludedPath) {
FileTree jsonPath,
File templateFile,
String pathstrip,
String destination,
String namePrefix,
String packageString,
String ... excludedPath) {
mkdir(destination)
def referenceTestTemplate = templateFile.text

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import org.hyperledger.besu.evm.frame.ExceptionalHaltReason;
import org.hyperledger.besu.evm.frame.MessageFrame;
import org.hyperledger.besu.evm.gascalculator.GasCalculator;
import org.hyperledger.besu.nativelib.bls12_381.LibEthPairings;
import org.hyperledger.besu.nativelib.gnark.LibGnarkEIP196;

import java.util.Optional;
import javax.annotation.Nonnull;
Expand Down Expand Up @@ -49,7 +49,7 @@ public abstract class AbstractAltBnPrecompiledContract extends AbstractPrecompil
*/
public static boolean maybeEnableNative() {
try {
useNative = LibEthPairings.ENABLED;
useNative = LibGnarkEIP196.ENABLED;
} catch (UnsatisfiedLinkError | NoClassDefFoundError ule) {
LOG.info("altbn128 native precompile not available: {}", ule.getMessage());
useNative = false;
Expand All @@ -72,7 +72,7 @@ public static boolean isNative() {
}

private final byte operationId;
private final int inputLen;
private final int inputLimit;

/**
* Instantiates a new Abstract alt bn precompiled contract.
Expand All @@ -89,9 +89,9 @@ public static boolean isNative() {
final int inputLen) {
super(name, gasCalculator);
this.operationId = operationId;
this.inputLen = inputLen + 1;
this.inputLimit = inputLen + 1;

if (!LibEthPairings.ENABLED) {
if (!LibGnarkEIP196.ENABLED) {
LOG.info("Native alt bn128 not available");
}
}
Expand All @@ -106,23 +106,24 @@ public static boolean isNative() {
@Nonnull
public PrecompileContractResult computeNative(
final @Nonnull Bytes input, final MessageFrame messageFrame) {
final byte[] result = new byte[LibEthPairings.EIP196_PREALLOCATE_FOR_RESULT_BYTES];
final byte[] error = new byte[LibEthPairings.EIP2537_PREALLOCATE_FOR_ERROR_BYTES];
final byte[] result = new byte[LibGnarkEIP196.EIP196_PREALLOCATE_FOR_RESULT_BYTES];
final byte[] error = new byte[LibGnarkEIP196.EIP196_PREALLOCATE_FOR_ERROR_BYTES];

final IntByReference o_len =
new IntByReference(LibEthPairings.EIP196_PREALLOCATE_FOR_RESULT_BYTES);
new IntByReference(LibGnarkEIP196.EIP196_PREALLOCATE_FOR_RESULT_BYTES);
final IntByReference err_len =
new IntByReference(LibEthPairings.EIP2537_PREALLOCATE_FOR_ERROR_BYTES);
final int inputSize = Math.min(inputLen, input.size());
new IntByReference(LibGnarkEIP196.EIP196_PREALLOCATE_FOR_ERROR_BYTES);
final int inputSize = Math.min(inputLimit, input.size());
final int errorNo =
LibEthPairings.eip196_perform_operation(
LibGnarkEIP196.eip196_perform_operation(
operationId,
input.slice(0, inputSize).toArrayUnsafe(),
inputSize,
result,
o_len,
error,
err_len);

if (errorNo == 0) {
return PrecompileContractResult.success(Bytes.wrap(result, 0, o_len.getValue()));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import org.hyperledger.besu.evm.frame.ExceptionalHaltReason;
import org.hyperledger.besu.evm.frame.MessageFrame;
import org.hyperledger.besu.nativelib.bls12_381.LibEthPairings;
import org.hyperledger.besu.nativelib.gnark.LibGnarkEIP2537;

import java.util.Optional;
import javax.annotation.Nonnull;
Expand Down Expand Up @@ -52,7 +52,7 @@ public abstract class AbstractBLS12PrecompiledContract implements PrecompiledCon

private final String name;
private final byte operationId;
private final int inputLen;
private final int inputLimit;

/**
* Instantiates a new Abstract BLS12 precompiled contract.
Expand All @@ -64,7 +64,7 @@ public abstract class AbstractBLS12PrecompiledContract implements PrecompiledCon
AbstractBLS12PrecompiledContract(final String name, final byte operationId, final int inputLen) {
this.name = name;
this.operationId = operationId;
this.inputLen = inputLen + 1;
this.inputLimit = inputLen + 1;
}

@Override
Expand All @@ -76,23 +76,25 @@ public String getName() {
@Override
public PrecompileContractResult computePrecompile(
final Bytes input, @Nonnull final MessageFrame messageFrame) {
final byte[] result = new byte[LibEthPairings.EIP2537_PREALLOCATE_FOR_RESULT_BYTES];
final byte[] error = new byte[LibEthPairings.EIP2537_PREALLOCATE_FOR_ERROR_BYTES];
final byte[] result = new byte[LibGnarkEIP2537.EIP2537_PREALLOCATE_FOR_RESULT_BYTES];
final byte[] error = new byte[LibGnarkEIP2537.EIP2537_PREALLOCATE_FOR_ERROR_BYTES];

final IntByReference o_len =
new IntByReference(LibEthPairings.EIP2537_PREALLOCATE_FOR_RESULT_BYTES);
new IntByReference(LibGnarkEIP2537.EIP2537_PREALLOCATE_FOR_RESULT_BYTES);
final IntByReference err_len =
new IntByReference(LibEthPairings.EIP2537_PREALLOCATE_FOR_ERROR_BYTES);
final int inputSize = Math.min(inputLen, input.size());
new IntByReference(LibGnarkEIP2537.EIP2537_PREALLOCATE_FOR_ERROR_BYTES);

final int inputSize = Math.min(inputLimit, input.size());
final int errorNo =
LibEthPairings.eip2537_perform_operation(
LibGnarkEIP2537.eip2537_perform_operation(
operationId,
input.slice(0, inputSize).toArrayUnsafe(),
inputSize,
result,
o_len,
error,
err_len);

if (errorNo == 0) {
return PrecompileContractResult.success(Bytes.wrap(result, 0, o_len.getValue()));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import org.hyperledger.besu.evm.frame.ExceptionalHaltReason;
import org.hyperledger.besu.evm.frame.MessageFrame;
import org.hyperledger.besu.evm.gascalculator.GasCalculator;
import org.hyperledger.besu.nativelib.bls12_381.LibEthPairings;
import org.hyperledger.besu.nativelib.gnark.LibGnarkEIP196;

import java.math.BigInteger;
import java.util.Arrays;
Expand All @@ -40,7 +40,7 @@ private AltBN128AddPrecompiledContract(final GasCalculator gasCalculator, final
super(
"AltBN128Add",
gasCalculator,
LibEthPairings.EIP196_ADD_OPERATION_RAW_VALUE,
LibGnarkEIP196.EIP196_ADD_OPERATION_RAW_VALUE,
PARAMETER_LENGTH);
this.gasCost = gasCost;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import org.hyperledger.besu.evm.frame.ExceptionalHaltReason;
import org.hyperledger.besu.evm.frame.MessageFrame;
import org.hyperledger.besu.evm.gascalculator.GasCalculator;
import org.hyperledger.besu.nativelib.bls12_381.LibEthPairings;
import org.hyperledger.besu.nativelib.gnark.LibGnarkEIP196;

import java.math.BigInteger;
import java.util.Arrays;
Expand All @@ -44,7 +44,7 @@ private AltBN128MulPrecompiledContract(final GasCalculator gasCalculator, final
super(
"AltBN128Mul",
gasCalculator,
LibEthPairings.EIP196_MUL_OPERATION_RAW_VALUE,
LibGnarkEIP196.EIP196_MUL_OPERATION_RAW_VALUE,
PARAMETER_LENGTH);
this.gasCost = gasCost;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import org.hyperledger.besu.evm.frame.ExceptionalHaltReason;
import org.hyperledger.besu.evm.frame.MessageFrame;
import org.hyperledger.besu.evm.gascalculator.GasCalculator;
import org.hyperledger.besu.nativelib.bls12_381.LibEthPairings;
import org.hyperledger.besu.nativelib.gnark.LibGnarkEIP196;

import java.math.BigInteger;
import java.util.ArrayList;
Expand Down Expand Up @@ -56,7 +56,7 @@ private AltBN128PairingPrecompiledContract(
super(
"AltBN128Pairing",
gasCalculator,
LibEthPairings.EIP196_PAIR_OPERATION_RAW_VALUE,
LibGnarkEIP196.EIP196_PAIR_OPERATION_RAW_VALUE,
Integer.MAX_VALUE / PARAMETER_LENGTH * PARAMETER_LENGTH);
this.pairingGasCost = pairingGasCost;
this.baseGasCost = baseGasCost;
Expand Down

0 comments on commit 3d1ad04

Please sign in to comment.