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

KZG point eval precompile #4860

Merged
merged 29 commits into from
Jan 23, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
a0aabbd
KZG implementation
jflo Jan 5, 2023
541365d
corrects test that fail if hostname is set to something unresolvable
jflo Jan 6, 2023
685cb10
adds precompile error handling
jflo Jan 15, 2023
86212c8
KZG implementation
jflo Jan 5, 2023
6c9102d
adds precompile error handling
jflo Jan 15, 2023
113e083
checks output in happy path test
jflo Jan 17, 2023
883d7d0
spotless
jflo Jan 17, 2023
ebaacd6
KZG implementation
jflo Jan 5, 2023
95b3b6c
adds precompile error handling
jflo Jan 15, 2023
2bad3ed
Merge branch 'KZGpointEvalPrecompile' of github.com:jflo/besu into KZ…
jflo Jan 19, 2023
5a802e0
KZG implementation
jflo Jan 5, 2023
7d2b70a
adds precompile error handling
jflo Jan 15, 2023
0e2dd0e
checks output in happy path test
jflo Jan 17, 2023
8c5c848
spotless
jflo Jan 17, 2023
8cb5003
KZG implementation
jflo Jan 5, 2023
92a70af
adds precompile error handling
jflo Jan 15, 2023
96fe0e3
KZG implementation
jflo Jan 5, 2023
9a9a51e
adds precompile error handling
jflo Jan 15, 2023
c4b9ece
corrects CHANGELOG
jflo Jan 20, 2023
8942c87
Merge branch 'KZGpointEvalPrecompile' of github.com:jflo/besu into KZ…
jflo Jan 20, 2023
c324a45
Merge branch 'main' into KZGpointEvalPrecompile
jflo Jan 20, 2023
8a5e696
updates from main
jflo Jan 20, 2023
8f5a80d
put humpty dumpty back together again
jflo Jan 20, 2023
ae8523f
expected val needed padding
jflo Jan 20, 2023
4bb9c9c
uses paths when possible
jflo Jan 20, 2023
8f9c1e9
removes kzg setup configurability, adjusts dependencies
jflo Jan 21, 2023
904d463
use a second constructor, since bit length needs to be known at libra…
jflo Jan 21, 2023
e1dd9c1
Merge branch 'main' into KZGpointEvalPrecompile
jflo Jan 23, 2023
3f87644
Merge branch 'main' into KZGpointEvalPrecompile
jflo Jan 23, 2023
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ https://hyperledger.jfrog.io/hyperledger/besu-binaries/besu/23.1.0-RC1/besu-23.1

## 23.1.0-beta

### Additions and Improvements
- Add access list to Transaction Call Object [#4802](https://github.com/hyperledger/besu/issues/4801)
jflo marked this conversation as resolved.
Show resolved Hide resolved
- Add new KZG precompile and option to override the trusted setup being used.

### Breaking Changes
- GoQuorum-compatible privacy is deprecated and will be removed in 23.4
- IBFT 1.0 is deprecated and will be removed in 23.4
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

import picocli.CommandLine;

public class EvmOptions implements CLIOptions<EvmConfiguration> {

public static final String JUMPDEST_CACHE_WEIGHT = "--Xevm-jumpdest-cache-weight-kb";
public static final String KZG_TRUSTED_SETUP_FILE_PATH = "--evm-kzg-trusted-setup-path";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be an --X option. Once the ceremony is done we will have the kzg embedded in the distribution, correct? So this is just transitional for testing?


public static EvmOptions create() {
return new EvmOptions();
Expand All @@ -44,9 +46,18 @@ public static EvmOptions create() {
private Long jumpDestCacheWeightKilobytes =
32_000L; // 10k contracts, (25k max contract size / 8 bit) + 32byte hash

@SuppressWarnings({"FieldCanBeFinal", "FieldMayBeFinal"})
@CommandLine.Option(
names = {KZG_TRUSTED_SETUP_FILE_PATH},
description =
"absolute file path on the host system which points to the trusted KZG setup file to use",
arity = "1")
private String kzgTrustedSetupFilePath = null;
jflo marked this conversation as resolved.
Show resolved Hide resolved

@Override
public EvmConfiguration toDomainObject() {
return new EvmConfiguration(jumpDestCacheWeightKilobytes);
return new EvmConfiguration(
jumpDestCacheWeightKilobytes, Optional.ofNullable(kzgTrustedSetupFilePath));
}

@Override
Expand Down
1 change: 1 addition & 0 deletions besu/src/test/resources/everything_config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,4 @@ Xp2p-tls-crl-file="none.file"

#contracts
Xevm-jumpdest-cache-weight-kb=32000
evm-kzg-trusted-setup-path="/tmp/trusted_setup.txt"
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ allprojects {
}
maven {
url 'https://artifacts.consensys.net/public/maven/maven/'
content { includeGroupByRegex('tech\\.pegasys\\..*') }
content { includeGroupByRegex('tech\\.pegasys.*') }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
content { includeGroupByRegex('tech\\.pegasys.*') }
content { includeGroupByRegex('tech\\.pegasys(\\..*)?') }

Removing the dot opens the door wide to uncontrolled DNS names like tech.pegasys-but-not-really:comprimised-kzg. Instead it should be listed as an optional section.

}
maven {
url 'https://dl.cloudsmith.io/public/consensys/quorum-mainnet-launcher/maven/'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public class Address extends DelegatingBytes implements org.hyperledger.besu.plu
public static final Address BLS12_MAP_FP_TO_G1 = Address.precompiled(0x11);
public static final Address BLS12_MAP_FP2_TO_G2 = Address.precompiled(0x12);

public static final Address KZG_POINT_EVAL = Address.precompiled(0x14);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this address fixed already? Has BLS been "locked in" to those addresses?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as i know it is, the specs have been stable on it.


public static final Address ZERO = Address.fromHexString("0x0");

protected Address(final Bytes bytes) {
Expand Down
5 changes: 5 additions & 0 deletions ethereum/core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@

apply plugin: 'java-library'

repositories {
maven { url "https://artifacts.consensys.net/public/maven/maven/" }
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should not be needed here and should be removed. The repository is already enumerated in the parent build file.

jar {
archiveBaseName = 'besu-core'
manifest {
Expand Down Expand Up @@ -54,6 +58,7 @@ dependencies {
implementation 'org.apache.tuweni:tuweni-concurrent'
implementation 'org.apache.tuweni:tuweni-units'
implementation 'org.apache.tuweni:tuweni-rlp'
implementation 'tech.pegasys:jc-kzg-4844:0.1.0'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Versions should not be listed in child build files. Please add an entry to <root>/gradle/versions.gradle

implementation 'org.hyperledger.besu:bls12-381'
implementation 'org.immutables:value-annotations'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static org.hyperledger.besu.ethereum.core.PrivacyParameters.PLUGIN_PRIVACY;
import static org.hyperledger.besu.evm.precompile.MainnetPrecompiledContracts.populateForBLS12;
import static org.hyperledger.besu.evm.precompile.MainnetPrecompiledContracts.populateForByzantium;
import static org.hyperledger.besu.evm.precompile.MainnetPrecompiledContracts.populateForCancun;
import static org.hyperledger.besu.evm.precompile.MainnetPrecompiledContracts.populateForFrontier;
import static org.hyperledger.besu.evm.precompile.MainnetPrecompiledContracts.populateForIstanbul;

Expand Down Expand Up @@ -60,6 +61,13 @@ public static PrecompileContractRegistry bls12(
return registry;
}

public static PrecompileContractRegistry cancun(
fab-10 marked this conversation as resolved.
Show resolved Hide resolved
final PrecompiledContractConfiguration precompiledContractConfiguration) {
final PrecompileContractRegistry registry = new PrecompileContractRegistry();
populateForCancun(registry, precompiledContractConfiguration.getGasCalculator());
return registry;
}

static void appendPrivacy(
final PrecompileContractRegistry registry,
final PrecompiledContractConfiguration precompiledContractConfiguration) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,7 @@ static ProtocolSpecBuilder cancunDefinition(
(gasCalculator, jdCacheConfig) ->
MainnetEVMs.cancun(
gasCalculator, chainId.orElse(BigInteger.ZERO), evmConfiguration))
.precompileContractRegistryBuilder(MainnetPrecompiledContractRegistries::cancun)
.name("Cancun");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ public void syncStatusPermissioningCheckShouldAllowDNS() throws UnknownHostExcep
.listeningPort(9999)
.disableDiscovery()
.ipAddress(
InetAddress.getLocalHost().getHostName(),
InetAddress.getLocalHost().getCanonicalHostName(),
ImmutableEnodeDnsConfiguration.builder()
.dnsEnabled(true)
.updateEnabled(true)
Expand All @@ -283,7 +283,7 @@ public void syncStatusPermissioningCheckShouldAllowDNS() throws UnknownHostExcep
.listeningPort(9999)
.disableDiscovery()
.ipAddress(
"127.0.0.1",
InetAddress.getLocalHost().getHostAddress(),
ImmutableEnodeDnsConfiguration.builder()
.dnsEnabled(true)
.updateEnabled(true)
Expand Down
5 changes: 5 additions & 0 deletions evm/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ jar {
}
}

repositories {
maven { url "https://artifacts.consensys.net/public/maven/maven/" }
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should not be here, the root gradle should cover this. If it doesn't it should be fixed there.

dependencies {
api 'org.slf4j:slf4j-api'

Expand All @@ -43,9 +46,11 @@ dependencies {
implementation 'org.apache.tuweni:tuweni-bytes'
implementation 'org.hyperledger.besu:arithmetic'
implementation 'org.hyperledger.besu:bls12-381'
implementation 'tech.pegasys:jc-kzg-4844:0.1.0'
jflo marked this conversation as resolved.
Show resolved Hide resolved
implementation 'net.java.dev.jna:jna'
implementation 'com.github.ben-manes.caffeine:caffeine'

testImplementation 'com.fasterxml.jackson.core:jackson-databind'
testImplementation 'info.picocli:picocli'
testImplementation 'junit:junit'
testImplementation 'org.assertj:assertj-core'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,25 @@

package org.hyperledger.besu.evm.internal;

import java.util.Optional;

public class EvmConfiguration {
public static final EvmConfiguration DEFAULT = new EvmConfiguration(32_000L);
public static final EvmConfiguration DEFAULT = new EvmConfiguration(32_000L, Optional.empty());
private final long jumpDestCacheWeightKB;

public EvmConfiguration(final long jumpDestCacheWeightKB) {
private final Optional<String> kzgTrustedSetupPath;
jflo marked this conversation as resolved.
Show resolved Hide resolved

public EvmConfiguration(
final long jumpDestCacheWeightKB, final Optional<String> pathToKZGTrustedSetup) {
this.jumpDestCacheWeightKB = jumpDestCacheWeightKB;
this.kzgTrustedSetupPath = pathToKZGTrustedSetup;
}

public long getJumpDestCacheWeightBytes() {
return jumpDestCacheWeightKB * 1024L;
}

public long getJumpDestCacheWeightKB() {
jflo marked this conversation as resolved.
Show resolved Hide resolved
return jumpDestCacheWeightKB;
public Optional<String> getKzgTrustedSetupPath() {
siladu marked this conversation as resolved.
Show resolved Hide resolved
return this.kzgTrustedSetupPath;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
/*
* Copyright Hyperledger Besu Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
package org.hyperledger.besu.evm.precompile;

import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;

import org.hyperledger.besu.evm.frame.ExceptionalHaltReason;
import org.hyperledger.besu.evm.frame.MessageFrame;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Optional;

import com.google.common.annotations.VisibleForTesting;
import ethereum.ckzg4844.CKZG4844JNI;
import org.apache.tuweni.bytes.Bytes;
import org.jetbrains.annotations.NotNull;

public class KZGPointEvalPrecompiledContract implements PrecompiledContract {

public KZGPointEvalPrecompiledContract(final Optional<String> pathToTrustedSetup) {

String pathToSetup;
CKZG4844JNI.Preset bitLength;
if (pathToTrustedSetup.isPresent()) {
pathToSetup = pathToTrustedSetup.get();
} else {
InputStream is =
KZGPointEvalPrecompiledContract.class.getResourceAsStream(
"mainnet_kzg_trusted_setup_4096.txt");
try {
File jniWillLoadFrom = File.createTempFile("kzgTrustedSetup", "txt");
Fixed Show fixed Hide fixed
jflo marked this conversation as resolved.
Show resolved Hide resolved

Check warning

Code scanning / CodeQL

Local information disclosure in a temporary directory

Local information disclosure vulnerability due to use of file readable by other local users.
Files.copy(is, jniWillLoadFrom.toPath(), REPLACE_EXISTING);
is.close();
pathToSetup = jniWillLoadFrom.getAbsolutePath();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
try {
BufferedReader setupFile =
jflo marked this conversation as resolved.
Show resolved Hide resolved
Files.newBufferedReader(Paths.get(pathToSetup), Charset.defaultCharset());
String firstLine = setupFile.readLine();
if ("4".equals(firstLine)) {
bitLength = CKZG4844JNI.Preset.MINIMAL;
} else if ("4096".equals(firstLine)) {
bitLength = CKZG4844JNI.Preset.MAINNET;
} else {
throw new IllegalArgumentException("provided file not a setup for either 4 or 4096 bits");
}
CKZG4844JNI.loadNativeLibrary(bitLength);
try {
CKZG4844JNI.loadTrustedSetup(pathToSetup);
} catch (RuntimeException alreadyLoaded) {
if (alreadyLoaded.getMessage().contains("Trusted Setup is already loaded")) {
} else {
throw alreadyLoaded;
}
}
siladu marked this conversation as resolved.
Show resolved Hide resolved
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

@VisibleForTesting
public void tearDown() {
CKZG4844JNI.freeTrustedSetup();
}

@Override
public String getName() {
return "KZGPointEval";
}

@Override
public long gasRequirement(final Bytes input) {
// As defined in EIP-4844
return 50000;
}

@NotNull
@Override
public PrecompileContractResult computePrecompile(
final Bytes input, @NotNull final MessageFrame messageFrame) {

if (input.size() != 192) {
return new PrecompileContractResult(
Bytes.EMPTY,
false,
MessageFrame.State.COMPLETED_FAILED,
Optional.of(ExceptionalHaltReason.PRECOMPILE_ERROR));
}
// Bytes versionedHash = input.slice(0, 32);
jflo marked this conversation as resolved.
Show resolved Hide resolved
Bytes z = input.slice(32, 32);
Bytes y = input.slice(64, 32);
Bytes commitment = input.slice(96, 48);
Bytes proof = input.slice(144, 48);

Bytes output = Bytes.EMPTY;
PrecompileContractResult result;
try {
boolean proved =
CKZG4844JNI.verifyKzgProof(
commitment.toArray(), z.toArray(), y.toArray(), proof.toArray());

// # Return FIELD_ELEMENTS_PER_BLOB and BLS_MODULUS as padded 32 byte big endian values
// return Bytes(U256(FIELD_ELEMENTS_PER_BLOB).to_be_bytes32() +
// U256(BLS_MODULUS).to_be_bytes32())
fab-10 marked this conversation as resolved.
Show resolved Hide resolved

if (proved) {
result =
new PrecompileContractResult(
output, false, MessageFrame.State.COMPLETED_SUCCESS, Optional.empty());
} else {
result =
new PrecompileContractResult(
output,
false,
MessageFrame.State.COMPLETED_FAILED,
Optional.of(ExceptionalHaltReason.PRECOMPILE_ERROR));
}
return result;
} catch (RuntimeException kzgFailed) {
System.out.println(kzgFailed.getMessage());
result =
new PrecompileContractResult(
output,
false,
MessageFrame.State.COMPLETED_FAILED,
Optional.of(ExceptionalHaltReason.PRECOMPILE_ERROR));
}
return result;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright ConsenSys AG.
* Copyright Hyperledger Besu Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
Expand All @@ -17,6 +17,8 @@
import org.hyperledger.besu.datatypes.Address;
import org.hyperledger.besu.evm.gascalculator.GasCalculator;

import java.util.Optional;

/** Provides the various precompiled contracts used on mainnet hard forks. */
public abstract class MainnetPrecompiledContracts {

Expand Down Expand Up @@ -86,4 +88,11 @@ public static void populateForBLS12(
registry.put(Address.BLS12_MAP_FP_TO_G1, new BLS12MapFpToG1PrecompiledContract());
registry.put(Address.BLS12_MAP_FP2_TO_G2, new BLS12MapFp2ToG2PrecompiledContract());
}

public static void populateForCancun(
final PrecompileContractRegistry registry, final GasCalculator gasCalculator) {
populateForIstanbul(registry, gasCalculator);
siladu marked this conversation as resolved.
Show resolved Hide resolved
// TODO: allow override to be configured?
siladu marked this conversation as resolved.
Show resolved Hide resolved
registry.put(Address.KZG_POINT_EVAL, new KZGPointEvalPrecompiledContract(Optional.empty()));
}
}
Loading