Skip to content

Commit

Permalink
Feature/use gnark-crypto for eip-2537 (hyperledger#7316)
Browse files Browse the repository at this point in the history
* use gnark-crypto for bls precompiles

Signed-off-by: garyschulte <[email protected]>
Signed-off-by: Daniel Lehrner <[email protected]>
  • Loading branch information
garyschulte authored and daniellehrner committed Jul 16, 2024
1 parent d22c9c6 commit cf878ed
Show file tree
Hide file tree
Showing 20 changed files with 156 additions and 154 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-2537 [#7316](https://github.com/hyperledger/besu/pull/7316)
- Added EIP-7702 [#7237](https://github.com/hyperledger/besu/pull/7237)

### Bug fixes
Expand Down
1 change: 0 additions & 1 deletion ethereum/core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ dependencies {
implementation 'io.tmio:tuweni-concurrent'
implementation 'io.tmio:tuweni-units'
implementation 'io.tmio:tuweni-rlp'
implementation 'org.hyperledger.besu:bls12-381'
implementation 'org.immutables:value-annotations'
implementation 'tech.pegasys:jc-kzg-4844'

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 @@ -14,7 +14,7 @@
*/
package org.hyperledger.besu.evm.precompile;

import org.hyperledger.besu.nativelib.bls12_381.LibEthPairings;
import org.hyperledger.besu.nativelib.gnark.LibGnarkEIP2537;

import org.apache.tuweni.bytes.Bytes;

Expand All @@ -25,7 +25,7 @@ public class BLS12G1AddPrecompiledContract extends AbstractBLS12PrecompiledContr

/** Instantiates a new BLS12G1 Add precompiled contract. */
public BLS12G1AddPrecompiledContract() {
super("BLS12_G1ADD", LibEthPairings.BLS12_G1ADD_OPERATION_RAW_VALUE, PARAMETER_LENGTH);
super("BLS12_G1ADD", LibGnarkEIP2537.BLS12_G1ADD_OPERATION_SHIM_VALUE, PARAMETER_LENGTH);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/
package org.hyperledger.besu.evm.precompile;

import org.hyperledger.besu.nativelib.bls12_381.LibEthPairings;
import org.hyperledger.besu.nativelib.gnark.LibGnarkEIP2537;

import org.apache.tuweni.bytes.Bytes;

Expand All @@ -25,7 +25,7 @@ public class BLS12G1MulPrecompiledContract extends AbstractBLS12PrecompiledContr

/** Instantiates a new BLS12G1 Mul precompiled contract. */
public BLS12G1MulPrecompiledContract() {
super("BLS12_G1MUL", LibEthPairings.BLS12_G1MUL_OPERATION_RAW_VALUE, PARAMETER_LENGTH);
super("BLS12_G1MUL", LibGnarkEIP2537.BLS12_G1MUL_OPERATION_SHIM_VALUE, PARAMETER_LENGTH);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/
package org.hyperledger.besu.evm.precompile;

import org.hyperledger.besu.nativelib.bls12_381.LibEthPairings;
import org.hyperledger.besu.nativelib.gnark.LibGnarkEIP2537;

import org.apache.tuweni.bytes.Bytes;

Expand All @@ -27,7 +27,7 @@ public class BLS12G1MultiExpPrecompiledContract extends AbstractBLS12Precompiled
public BLS12G1MultiExpPrecompiledContract() {
super(
"BLS12_G1MULTIEXP",
LibEthPairings.BLS12_G1MULTIEXP_OPERATION_RAW_VALUE,
LibGnarkEIP2537.BLS12_G1MULTIEXP_OPERATION_SHIM_VALUE,
Integer.MAX_VALUE / PARAMETER_LENGTH * PARAMETER_LENGTH);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/
package org.hyperledger.besu.evm.precompile;

import org.hyperledger.besu.nativelib.bls12_381.LibEthPairings;
import org.hyperledger.besu.nativelib.gnark.LibGnarkEIP2537;

import org.apache.tuweni.bytes.Bytes;

Expand All @@ -25,7 +25,7 @@ public class BLS12G2AddPrecompiledContract extends AbstractBLS12PrecompiledContr

/** Instantiates a new BLS12_G2 Add precompiled contract. */
public BLS12G2AddPrecompiledContract() {
super("BLS12_G2ADD", LibEthPairings.BLS12_G2ADD_OPERATION_RAW_VALUE, PARAMETER_LENGTH);
super("BLS12_G2ADD", LibGnarkEIP2537.BLS12_G2ADD_OPERATION_SHIM_VALUE, PARAMETER_LENGTH);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/
package org.hyperledger.besu.evm.precompile;

import org.hyperledger.besu.nativelib.bls12_381.LibEthPairings;
import org.hyperledger.besu.nativelib.gnark.LibGnarkEIP2537;

import org.apache.tuweni.bytes.Bytes;

Expand All @@ -25,7 +25,7 @@ public class BLS12G2MulPrecompiledContract extends AbstractBLS12PrecompiledContr

/** Instantiates a new BLS12_G2Mul precompiled contract. */
public BLS12G2MulPrecompiledContract() {
super("BLS12_G2MUL", LibEthPairings.BLS12_G2MUL_OPERATION_RAW_VALUE, PARAMETER_LENGTH);
super("BLS12_G2MUL", LibGnarkEIP2537.BLS12_G2MUL_OPERATION_SHIM_VALUE, PARAMETER_LENGTH);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/
package org.hyperledger.besu.evm.precompile;

import org.hyperledger.besu.nativelib.bls12_381.LibEthPairings;
import org.hyperledger.besu.nativelib.gnark.LibGnarkEIP2537;

import org.apache.tuweni.bytes.Bytes;

Expand All @@ -27,7 +27,7 @@ public class BLS12G2MultiExpPrecompiledContract extends AbstractBLS12Precompiled
public BLS12G2MultiExpPrecompiledContract() {
super(
"BLS12_G2MULTIEXP",
LibEthPairings.BLS12_G2MULTIEXP_OPERATION_RAW_VALUE,
LibGnarkEIP2537.BLS12_G2MULTIEXP_OPERATION_SHIM_VALUE,
Integer.MAX_VALUE / PARAMETER_LENGTH * PARAMETER_LENGTH);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/
package org.hyperledger.besu.evm.precompile;

import org.hyperledger.besu.nativelib.bls12_381.LibEthPairings;
import org.hyperledger.besu.nativelib.gnark.LibGnarkEIP2537;

import org.apache.tuweni.bytes.Bytes;

Expand All @@ -27,7 +27,7 @@ public class BLS12MapFp2ToG2PrecompiledContract extends AbstractBLS12Precompiled
public BLS12MapFp2ToG2PrecompiledContract() {
super(
"BLS12_MAP_FIELD_TO_CURVE",
LibEthPairings.BLS12_MAP_FP2_TO_G2_OPERATION_RAW_VALUE,
LibGnarkEIP2537.BLS12_MAP_FP2_TO_G2_OPERATION_SHIM_VALUE,
PARAMETER_LENGTH);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/
package org.hyperledger.besu.evm.precompile;

import org.hyperledger.besu.nativelib.bls12_381.LibEthPairings;
import org.hyperledger.besu.nativelib.gnark.LibGnarkEIP2537;

import org.apache.tuweni.bytes.Bytes;

Expand All @@ -27,7 +27,7 @@ public class BLS12MapFpToG1PrecompiledContract extends AbstractBLS12PrecompiledC
public BLS12MapFpToG1PrecompiledContract() {
super(
"BLS12_MAP_FIELD_TO_CURVE",
LibEthPairings.BLS12_MAP_FP_TO_G1_OPERATION_RAW_VALUE,
LibGnarkEIP2537.BLS12_MAP_FP_TO_G1_OPERATION_SHIM_VALUE,
PARAMETER_LENGTH);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/
package org.hyperledger.besu.evm.precompile;

import org.hyperledger.besu.nativelib.bls12_381.LibEthPairings;
import org.hyperledger.besu.nativelib.gnark.LibGnarkEIP2537;

import org.apache.tuweni.bytes.Bytes;

Expand All @@ -27,7 +27,7 @@ public class BLS12PairingPrecompiledContract extends AbstractBLS12PrecompiledCon
public BLS12PairingPrecompiledContract() {
super(
"BLS12_PAIRING",
LibEthPairings.BLS12_PAIR_OPERATION_RAW_VALUE,
LibGnarkEIP2537.BLS12_PAIR_OPERATION_SHIM_VALUE,
Integer.MAX_VALUE / PARAMETER_LENGTH * PARAMETER_LENGTH);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,5 @@ input,result,gas,notes
000000000000000000000000000000000106df8eba767e90cce0eabdaacc24d8e226c6865012ef8cb1460de5a319d443fdc6b4f4e58fb668943e0528b1809da10000000000000000000000000000000019789f464c95c179af18704c0b67b881991880f75ee7b03b9feafa3eafcd0f7d30a17fdd9cf439ff7fe683adca2083b50000000000000000000000000000000017a81b957a12adf474a2913e8636f169ea9cd10be62c16b88f95f5caf661f158a032a9f7d249fdf2765caa1564bed0570000000000000000000000000000000017fbf2abc62dc2678b65d509e19c9c9c5d961c72565649a078da8dff98be6236ef314e9ff8022f639ff565353345c230,00000000000000000000000000000000002c8bc5f39b2c9fea01372429e92a9c945fad152da67174f4e478fdead734d50f6e2da867c235f1f2f11bdfee67d2a7000000000000000000000000000000000c1dd27aad9f5d48c4824da3071daedf0c7a0e2a0b0ed39c50c9d25e61334a9c96765e049542ccaa00e0eccb316eec08,500,
000000000000000000000000000000000012196c5a43d69224d8713389285f26b98f86ee910ab3dd668e413738282003cc5b7357af9a7af54bb713d62255e80f560000000000000000000000000000000006ba8102bfbeea4416b710c73e8cce3032c31c6269c44906f8ac4f7874ce99fb17559992486528963884ce429a992fee000000000000000000000000000000000001101098f5c39893765766af4512a0c74e1bb89bc7e6fdf14e3e7337d257cc0f94658179d83320b99f31ff94cd2bac0000000000000000000000000000000003e1a9f9f44ca2cdab4f43a1a3ee3470fdf90b2fc228eb3b709fcd72f014838ac82a6d797aeefed9a0804b22ed1ce8f7,,,invalid input parameters, invalid input length for G1 addition
00000000000000000000000000000012196c5a43d69224d8713389285f26b98f86ee910ab3dd668e413738282003cc5b7357af9a7af54bb713d62255e80f560000000000000000000000000000000006ba8102bfbeea4416b710c73e8cce3032c31c6269c44906f8ac4f7874ce99fb17559992486528963884ce429a992fee000000000000000000000000000000000001101098f5c39893765766af4512a0c74e1bb89bc7e6fdf14e3e7337d257cc0f94658179d83320b99f31ff94cd2bac0000000000000000000000000000000003e1a9f9f44ca2cdab4f43a1a3ee3470fdf90b2fc228eb3b709fcd72f014838ac82a6d797aeefed9a0804b22ed1ce8f7,,,invalid input parameters, invalid input length for G1 addition
0000000000000000000000000000000012196c5a43d69224d8713389285f26b98f86ee910ab3dd668e413738282003cc5b7357af9a7af54bb713d62255e80f570000000000000000000000000000000006ba8102bfbeea4416b710c73e8cce3032c31c6269c44906f8ac4f7874ce99fb17559992486528963884ce429a992fee000000000000000000000000000000000001101098f5c39893765766af4512a0c74e1bb89bc7e6fdf14e3e7337d257cc0f94658179d83320b99f31ff94cd2bac0000000000000000000000000000000003e1a9f9f44ca2cdab4f43a1a3ee3470fdf90b2fc228eb3b709fcd72f014838ac82a6d797aeefed9a0804b22ed1ce8f7,,,invalid input parameters, Point 0 is not on curve
0000000000000000000000000000000012196c5a43d69224d8713389285f26b98f86ee910ab3dd668e413738282003cc5b7357af9a7af54bb713d62255e80f560000000000000000000000000000000006ba8102bfbeea4416b710c73e8cce3032c31c6269c44906f8ac4f7874ce99fb17559992486528963884ce429a992fee000000000000000000000000000000000001101098f5c39893765766af4512a0c74e1bb89bc7e6fdf14e3e7337d257cc0f94658179d83320b99f31ff94cd2bad0000000000000000000000000000000003e1a9f9f44ca2cdab4f43a1a3ee3470fdf90b2fc228eb3b709fcd72f014838ac82a6d797aeefed9a0804b22ed1ce8f7,,,invalid input parameters, Point 1 is not on curve
0000000000000000000000000000000012196c5a43d69224d8713389285f26b98f86ee910ab3dd668e413738282003cc5b7357af9a7af54bb713d62255e80f570000000000000000000000000000000006ba8102bfbeea4416b710c73e8cce3032c31c6269c44906f8ac4f7874ce99fb17559992486528963884ce429a992fee000000000000000000000000000000000001101098f5c39893765766af4512a0c74e1bb89bc7e6fdf14e3e7337d257cc0f94658179d83320b99f31ff94cd2bac0000000000000000000000000000000003e1a9f9f44ca2cdab4f43a1a3ee3470fdf90b2fc228eb3b709fcd72f014838ac82a6d797aeefed9a0804b22ed1ce8f7,,,invalid point: point is not on curve
0000000000000000000000000000000012196c5a43d69224d8713389285f26b98f86ee910ab3dd668e413738282003cc5b7357af9a7af54bb713d62255e80f560000000000000000000000000000000006ba8102bfbeea4416b710c73e8cce3032c31c6269c44906f8ac4f7874ce99fb17559992486528963884ce429a992fee000000000000000000000000000000000001101098f5c39893765766af4512a0c74e1bb89bc7e6fdf14e3e7337d257cc0f94658179d83320b99f31ff94cd2bad0000000000000000000000000000000003e1a9f9f44ca2cdab4f43a1a3ee3470fdf90b2fc228eb3b709fcd72f014838ac82a6d797aeefed9a0804b22ed1ce8f7,,,invalid point: point is not on curve
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,5 @@ input,result,gas,notes
000000000000000000000000000000000106df8eba767e90cce0eabdaacc24d8e226c6865012ef8cb1460de5a319d443fdc6b4f4e58fb668943e0528b1809da10000000000000000000000000000000019789f464c95c179af18704c0b67b881991880f75ee7b03b9feafa3eafcd0f7d30a17fdd9cf439ff7fe683adca2083b57cf23dee8d95d94046678f3bdb4b0ea3d4e3a1a2f07f582e2a98ad6eb7562cbf,000000000000000000000000000000000bf700422a382546a74376b0292f3a49ceff5597f0d2b726b1ff099bcda7ba92238a21db12eff5c314a29dd2387bec850000000000000000000000000000000005e22e3c772f3634b1ccf4e311241977eb20e7269540ef22d379de26ab80c58461dfa3b67848e0d584fb11de1917949a,12000,
00000000000000000000000000000012196c5a43d69224d8713389285f26b98f86ee910ab3dd668e413738282003cc5b7357af9a7af54bb713d62255e80f560000000000000000000000000000000006ba8102bfbeea4416b710c73e8cce3032c31c6269c44906f8ac4f7874ce99fb17559992486528963884ce429a992feeb3c940fe79b6966489b527955de7599194a9ac69a6ff58b8d99e7b1084f0464e,,,invalid input parameters, invalid input length for G1 multiplication
000000000000000000000000000000000012196c5a43d69224d8713389285f26b98f86ee910ab3dd668e413738282003cc5b7357af9a7af54bb713d62255e80f560000000000000000000000000000000006ba8102bfbeea4416b710c73e8cce3032c31c6269c44906f8ac4f7874ce99fb17559992486528963884ce429a992feeb3c940fe79b6966489b527955de7599194a9ac69a6ff58b8d99e7b1084f0464e,,,invalid input parameters, invalid input length for G1 multiplication
0000000000000000000000000000000012196c5a43d69224d8713389285f26b98f86ee910ab3dd668e413738282003cc5b7357af9a7af54bb713d62255e80f570000000000000000000000000000000006ba8102bfbeea4416b710c73e8cce3032c31c6269c44906f8ac4f7874ce99fb17559992486528963884ce429a992feeb3c940fe79b6966489b527955de7599194a9ac69a6ff58b8d99e7b1084f0464e,,,invalid input parameters, Point is not on curve
000000000000000000000000000000000123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef00000000000000000000000000000000193fb7cedb32b2c3adc06ec11a96bc0d661869316f5e4a577a9f7c179593987beb4fb2ee424dbb2f5dd891e228b46c4a0000000000000000000000000000000000000000000000000000000000000002,,,Point is not in the expected subgroup
0000000000000000000000000000000012196c5a43d69224d8713389285f26b98f86ee910ab3dd668e413738282003cc5b7357af9a7af54bb713d62255e80f570000000000000000000000000000000006ba8102bfbeea4416b710c73e8cce3032c31c6269c44906f8ac4f7874ce99fb17559992486528963884ce429a992feeb3c940fe79b6966489b527955de7599194a9ac69a6ff58b8d99e7b1084f0464e,,,invalid point: point is not on curve
000000000000000000000000000000000123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef00000000000000000000000000000000193fb7cedb32b2c3adc06ec11a96bc0d661869316f5e4a577a9f7c179593987beb4fb2ee424dbb2f5dd891e228b46c4a0000000000000000000000000000000000000000000000000000000000000002,,,invalid point: subgroup check failed
Loading

0 comments on commit cf878ed

Please sign in to comment.