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 1 commit
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
Prev Previous commit
Next Next commit
put humpty dumpty back together again
Signed-off-by: Justin Florentine <justin+github@florentine.us>
  • Loading branch information
jflo committed Jan 20, 2023
commit 8f5a80deaeffe4f5e7e98a698b09dc57b4ce6ce7
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
### Additions and Improvements
- Added option to evm CLI tool to allow code execution at specific forks [#4913](https://github.com/hyperledger/besu/pull/4913)
- Improve get account performance by using the world state updater cache [#4897](https://github.com/hyperledger/besu/pull/4897)
- Add new KZG precompile and option to override the trusted setup being used.
- Add new KZG precompile and option to override the trusted setup being used [#4822](https://github.com/hyperledger/besu/issues/4822)

### Bug Fixes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,17 @@ static ProtocolSpecBuilder cancunDefinition(
(gasCalculator, jdCacheConfig) ->
MainnetEVMs.cancun(
gasCalculator, chainId.orElse(BigInteger.ZERO), evmConfiguration))
// change contract call creator to accept EOF code
.contractCreationProcessorBuilder(
(gasCalculator, evm) ->
new ContractCreationProcessor(
gasCalculator,
evm,
true,
List.of(
MaxCodeSizeRule.of(contractSizeLimit), EOFValidationCodeRule.of(1, false)),
1,
SPURIOUS_DRAGON_FORCE_DELETE_WHEN_EMPTY_ADDRESSES))
.precompileContractRegistryBuilder(MainnetPrecompiledContractRegistries::cancun)
.name("Cancun");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

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

import org.apache.tuweni.bytes.Bytes32;
import org.hyperledger.besu.evm.frame.ExceptionalHaltReason;
import org.hyperledger.besu.evm.frame.MessageFrame;

Expand All @@ -26,6 +27,7 @@
import java.io.InputStream;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Optional;

Expand All @@ -36,28 +38,29 @@

public class KZGPointEvalPrecompiledContract implements PrecompiledContract {

public KZGPointEvalPrecompiledContract(final Optional<String> pathToTrustedSetup) {
public KZGPointEvalPrecompiledContract(final Optional<Path> pathToTrustedSetup) {

String pathToSetup;
String absolutePathToSetup;
CKZG4844JNI.Preset bitLength;
if (pathToTrustedSetup.isPresent()) {
pathToSetup = pathToTrustedSetup.get();
Path pathToSetup = pathToTrustedSetup.get();
absolutePathToSetup = pathToSetup.toAbsolutePath().toString();
} 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.
jniWillLoadFrom.deleteOnExit();
Files.copy(is, jniWillLoadFrom.toPath(), REPLACE_EXISTING);
is.close();
pathToSetup = jniWillLoadFrom.getAbsolutePath();
absolutePathToSetup = jniWillLoadFrom.getAbsolutePath();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
try {
BufferedReader setupFile =
Files.newBufferedReader(Paths.get(pathToSetup), Charset.defaultCharset());
try (BufferedReader setupFile =
Files.newBufferedReader(Paths.get(absolutePathToSetup), Charset.defaultCharset()); ) {
String firstLine = setupFile.readLine();
if ("4".equals(firstLine)) {
bitLength = CKZG4844JNI.Preset.MINIMAL;
Expand All @@ -68,7 +71,7 @@ public KZGPointEvalPrecompiledContract(final Optional<String> pathToTrustedSetup
}
CKZG4844JNI.loadNativeLibrary(bitLength);
try {
CKZG4844JNI.loadTrustedSetup(pathToSetup);
CKZG4844JNI.loadTrustedSetup(absolutePathToSetup);
} catch (RuntimeException alreadyLoaded) {
if (alreadyLoaded.getMessage().contains("Trusted Setup is already loaded")) {
} else {
Expand Down Expand Up @@ -110,7 +113,6 @@ public PrecompileContractResult computePrecompile(
MessageFrame.State.COMPLETED_FAILED,
Optional.of(ExceptionalHaltReason.PRECOMPILE_ERROR));
}
// Bytes versionedHash = input.slice(0, 32);
Bytes z = input.slice(32, 32);
Bytes y = input.slice(64, 32);
Bytes commitment = input.slice(96, 48);
Expand All @@ -123,11 +125,13 @@ public PrecompileContractResult computePrecompile(
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())

if (proved) {
Bytes fieldElementsPerBlob =
Bytes32.wrap(Bytes.of(CKZG4844JNI.getFieldElementsPerBlob()).xor(Bytes32.ZERO)); // usually 4096
Bytes blsModulus = Bytes32.wrap(Bytes.of(CKZG4844JNI.BLS_MODULUS.toByteArray()).xor(Bytes32.ZERO));

output = Bytes.concatenate(fieldElementsPerBlob, blsModulus);

result =
new PrecompileContractResult(
output, false, MessageFrame.State.COMPLETED_SUCCESS, Optional.empty());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@
import static org.assertj.core.api.Assertions.fail;
import static org.mockito.Mockito.mock;

import ethereum.ckzg4844.CKZG4844JNI;
import org.hyperledger.besu.evm.frame.ExceptionalHaltReason;
import org.hyperledger.besu.evm.frame.MessageFrame;

import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.util.Optional;

import com.fasterxml.jackson.databind.JsonNode;
Expand All @@ -37,13 +40,14 @@ public class KZGPointEvalPrecompileContractTest {
private final MessageFrame toRun = mock(MessageFrame.class);

@BeforeClass
public static void init() {
public static void init() throws URISyntaxException {
contract =
new KZGPointEvalPrecompiledContract(
Optional.of(
KZGPointEvalPrecompileContractTest.class
.getResource("trusted_setup_4.txt")
.getPath()));
Path.of(
KZGPointEvalPrecompileContractTest.class
.getResource("trusted_setup_4.txt")
.toURI())));
}

@Test
Expand All @@ -52,9 +56,12 @@ public void happyPath() {
Bytes.fromHexString(
"013c03613f6fc558fb7e61e75602241ed9a2f04e36d8670aadd286e71b5ca9cc420000000000000000000000000000000000000000000000000000000000000031e5a2356cbc2ef6a733eae8d54bf48719ae3d990017ca787c419c7d369f8e3c83fac17c3f237fc51f90e2c660eb202a438bc2025baded5cd193c1a018c5885bc9281ba704d5566082e851235c7be763b2a99adff965e0a121ee972ebc472d02944a74f5c6243e14052e105124b70bf65faf85ad3a494325e269fad097842cba");

Bytes fieldElementsPerBlob = Bytes.of(CKZG4844JNI.getFieldElementsPerBlob());
Bytes blsModulus = Bytes.of(CKZG4844JNI.BLS_MODULUS.toByteArray());
Bytes expectedOutput = Bytes.concatenate(fieldElementsPerBlob, blsModulus);
// contract input is encoded as follows: versioned_hash | z | y | commitment | proof |
PrecompiledContract.PrecompileContractResult result = contract.computePrecompile(input, toRun);

assertThat(result.getOutput()).isEqualTo(expectedOutput);
MessageFrame.State endState = result.getState();
assertThat(endState).isEqualTo(MessageFrame.State.COMPLETED_SUCCESS);
}
Expand Down