diff --git a/config/src/main/java/org/hyperledger/besu/config/JsonGenesisConfigOptions.java b/config/src/main/java/org/hyperledger/besu/config/JsonGenesisConfigOptions.java index 8dde977b4dc..5d0370c0c3b 100644 --- a/config/src/main/java/org/hyperledger/besu/config/JsonGenesisConfigOptions.java +++ b/config/src/main/java/org/hyperledger/besu/config/JsonGenesisConfigOptions.java @@ -14,9 +14,9 @@ */ package org.hyperledger.besu.config; -import static java.util.Collections.emptyMap; -import static java.util.Objects.isNull; - +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.google.common.collect.ImmutableMap; +import org.apache.tuweni.units.bigints.UInt256; import org.hyperledger.besu.datatypes.Address; import org.hyperledger.besu.datatypes.Hash; import org.hyperledger.besu.datatypes.Wei; @@ -31,9 +31,8 @@ import java.util.stream.Collectors; import java.util.stream.Stream; -import com.fasterxml.jackson.databind.node.ObjectNode; -import com.google.common.collect.ImmutableMap; -import org.apache.tuweni.units.bigints.UInt256; +import static java.util.Collections.emptyMap; +import static java.util.Objects.isNull; /** The Json genesis config options. */ public class JsonGenesisConfigOptions implements GenesisConfigOptions { @@ -368,6 +367,9 @@ public OptionalLong getThanosBlockNumber() { @Override public OptionalLong getLacchainPostQuantumBlockNumber() { return getOptionalLong("lacchainpqblock"); + } + + @Override public OptionalLong getMagnetoBlockNumber() { return getOptionalLong("magnetoblock"); } diff --git a/config/src/main/java/org/hyperledger/besu/config/StubGenesisConfigOptions.java b/config/src/main/java/org/hyperledger/besu/config/StubGenesisConfigOptions.java index affb9ecae40..c991901a770 100644 --- a/config/src/main/java/org/hyperledger/besu/config/StubGenesisConfigOptions.java +++ b/config/src/main/java/org/hyperledger/besu/config/StubGenesisConfigOptions.java @@ -14,6 +14,8 @@ */ package org.hyperledger.besu.config; +import com.google.common.collect.ImmutableMap; +import org.apache.tuweni.units.bigints.UInt256; import org.hyperledger.besu.datatypes.Address; import org.hyperledger.besu.datatypes.Hash; import org.hyperledger.besu.datatypes.Wei; @@ -26,9 +28,6 @@ import java.util.OptionalInt; import java.util.OptionalLong; -import com.google.common.collect.ImmutableMap; -import org.apache.tuweni.units.bigints.UInt256; - /** The Stub genesis config options. */ public class StubGenesisConfigOptions implements GenesisConfigOptions, Cloneable { @@ -306,6 +305,8 @@ public OptionalLong getThanosBlockNumber() { public OptionalLong getLacchainPostQuantumBlockNumber() { return lacchainPostQuantumBlockNumber; } + + @Override public OptionalLong getMagnetoBlockNumber() { return magnetoBlockNumber; } diff --git a/datatypes/src/main/java/org/hyperledger/besu/datatypes/Address.java b/datatypes/src/main/java/org/hyperledger/besu/datatypes/Address.java index 8862d9461e2..18b973cabc1 100644 --- a/datatypes/src/main/java/org/hyperledger/besu/datatypes/Address.java +++ b/datatypes/src/main/java/org/hyperledger/besu/datatypes/Address.java @@ -14,18 +14,17 @@ */ package org.hyperledger.besu.datatypes; -import static com.google.common.base.Preconditions.checkArgument; -import static org.hyperledger.besu.crypto.Hash.keccak256; - +import com.fasterxml.jackson.annotation.JsonCreator; +import org.apache.tuweni.bytes.Bytes; +import org.apache.tuweni.bytes.Bytes32; +import org.apache.tuweni.bytes.DelegatingBytes; import org.hyperledger.besu.crypto.SECPPublicKey; import org.hyperledger.besu.ethereum.rlp.RLP; import org.hyperledger.besu.ethereum.rlp.RLPException; import org.hyperledger.besu.ethereum.rlp.RLPInput; -import com.fasterxml.jackson.annotation.JsonCreator; -import org.apache.tuweni.bytes.Bytes; -import org.apache.tuweni.bytes.Bytes32; -import org.apache.tuweni.bytes.DelegatingBytes; +import static com.google.common.base.Preconditions.checkArgument; +import static org.hyperledger.besu.crypto.Hash.keccak256; /** A 160-bits account address. */ public class Address extends DelegatingBytes { @@ -70,6 +69,8 @@ public class Address extends DelegatingBytes { public static final Address BLS12_MAP_FP_TO_G1 = Address.precompiled(0x11); /** The constant BLS12_MAP_FP2_TO_G2. */ public static final Address BLS12_MAP_FP2_TO_G2 = Address.precompiled(0x12); + /** Constant for Precompiled Falcon verification signature. */ + public static final Address LACCHAIN_FALCON = Address.precompiled(0x13); /** The constant KZG_POINT_EVAL. */ public static final Address KZG_POINT_EVAL = Address.precompiled(0x14); /** The constant ZERO. */ diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/Address.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/Address.java deleted file mode 100644 index 7cda9e04a68..00000000000 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/Address.java +++ /dev/null @@ -1,202 +0,0 @@ -/* - * Copyright ConsenSys AG. - * - * 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.ethereum.core; - -import static com.google.common.base.Preconditions.checkArgument; - -import org.hyperledger.besu.crypto.SECP256K1.PublicKey; -import org.hyperledger.besu.ethereum.rlp.RLP; -import org.hyperledger.besu.ethereum.rlp.RLPException; -import org.hyperledger.besu.ethereum.rlp.RLPInput; - -import com.fasterxml.jackson.annotation.JsonCreator; -import org.apache.tuweni.bytes.Bytes; -import org.apache.tuweni.bytes.DelegatingBytes; - -/** A 160-bits account address. */ -public class Address extends DelegatingBytes implements org.hyperledger.besu.plugin.data.Address { - - public static final int SIZE = 20; - - /** Specific addresses of the "precompiled" contracts. */ - public static final Address ECREC = Address.precompiled(0x01); - - public static final Address SHA256 = Address.precompiled(0x02); - public static final Address RIPEMD160 = Address.precompiled(0x03); - public static final Address ID = Address.precompiled(0x04); - public static final Address MODEXP = Address.precompiled(0x05); - public static final Address ALTBN128_ADD = Address.precompiled(0x06); - public static final Address ALTBN128_MUL = Address.precompiled(0x07); - public static final Address ALTBN128_PAIRING = Address.precompiled(0x08); - public static final Address BLAKE2B_F_COMPRESSION = Address.precompiled(0x09); - public static final Address BLS12_G1ADD = Address.precompiled(0xA); - public static final Address BLS12_G1MUL = Address.precompiled(0xB); - public static final Address BLS12_G1MULTIEXP = Address.precompiled(0xC); - public static final Address BLS12_G2ADD = Address.precompiled(0xD); - public static final Address BLS12_G2MUL = Address.precompiled(0xE); - public static final Address BLS12_G2MULTIEXP = Address.precompiled(0xF); - public static final Address BLS12_PAIRING = Address.precompiled(0x10); - 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 LACCHAIN_FALCON = Address.precompiled(0x13); - - // Last address that can be generated for a pre-compiled contract - public static final Integer PRIVACY = Byte.MAX_VALUE - 1; - public static final Address DEFAULT_PRIVACY = Address.precompiled(PRIVACY); - public static final Address ONCHAIN_PRIVACY = Address.precompiled(PRIVACY - 1); - - // Onchain privacy management contracts (injected in private state) - public static final Address ONCHAIN_PRIVACY_PROXY = Address.precompiled(PRIVACY - 2); - public static final Address DEFAULT_ONCHAIN_PRIVACY_MANAGEMENT = Address.precompiled(PRIVACY - 3); - - public static final Address ZERO = Address.fromHexString("0x0"); - - protected Address(final Bytes bytes) { - super(bytes); - } - - public static Address wrap(final Bytes value) { - checkArgument( - value.size() == SIZE, - "An account address must be %s bytes long, got %s", - SIZE, - value.size()); - return new Address(value); - } - - /** - * Creates an address from the given RLP-encoded input. - * - * @param input The input to read from - * @return the input's corresponding address - */ - public static Address readFrom(final RLPInput input) { - final Bytes bytes = input.readBytes(); - if (bytes.size() != SIZE) { - throw new RLPException( - String.format("Address unexpected size of %s (needs %s)", bytes.size(), SIZE)); - } - return Address.wrap(bytes); - } - - /** - * Extracts an address from a ECDSARECOVER result hash. - * - * @param hash A hash that has been obtained through hashing the return of the ECDSARECOVER - * function from Appendix F (Signing Transactions) of the Ethereum Yellow Paper. - * @return The ethereum address from the provided hash. - */ - public static Address extract(final Hash hash) { - return wrap(hash.slice(12, 20)); - } - - public static Address extract(final PublicKey publicKey) { - return Address.extract(Hash.hash(publicKey.getEncodedBytes())); - } - - /** - * Parse an hexadecimal string representing an account address. - * - * @param str An hexadecimal string (with or without the leading '0x') representing a valid - * account address. - * @return The parsed address: {@code null} if the provided string is {@code null}. - * @throws IllegalArgumentException if the string is either not hexadecimal, or not the valid - * representation of an address. - */ - @JsonCreator - public static Address fromHexString(final String str) { - if (str == null) return null; - return wrap(Bytes.fromHexStringLenient(str, SIZE)); - } - - /** - * Parse an hexadecimal string representing an account address. - * - * @param str An hexadecimal string representing a valid account address (strictly 20 bytes). - * @return The parsed address. - * @throws IllegalArgumentException if the provided string is {@code null}. - * @throws IllegalArgumentException if the string is either not hexadecimal, or not the valid - * representation of a 20 byte address. - */ - public static Address fromHexStringStrict(final String str) { - checkArgument(str != null); - final Bytes value = Bytes.fromHexString(str); - checkArgument( - value.size() == SIZE, - "An account address must be be %s bytes long, got %s", - SIZE, - value.size()); - return new Address(value); - } - - private static Address precompiled(final int value) { - // Keep it simple while we don't need precompiled above 127. - checkArgument(value < Byte.MAX_VALUE); - final byte[] address = new byte[SIZE]; - address[SIZE - 1] = (byte) value; - return new Address(Bytes.wrap(address)); - } - - public static Address privacyPrecompiled(final int value) { - return precompiled(value); - } - - /** - * Address of the created contract. - * - *

This implement equation (86) in Section 7 of the Yellow Paper (rev. a91c29c). - * - * @param senderAddress the address of the transaction sender. - * @param nonce the nonce of this transaction. - * @return The generated address of the created contract. - */ - public static Address contractAddress(final Address senderAddress, final long nonce) { - return Address.extract( - Hash.hash( - RLP.encode( - out -> { - out.startList(); - out.writeBytes(senderAddress); - out.writeLongScalar(nonce); - out.endList(); - }))); - } - - /** - * Address of the created private contract. - * - * @param senderAddress the address of the transaction sender. - * @param nonce the nonce of this transaction. - * @param privacyGroupId hash of participants list ordered from Enclave response. - * @return The generated address of the created private contract. - */ - public static Address privateContractAddress( - final Address senderAddress, final long nonce, final Bytes privacyGroupId) { - return Address.extract( - Hash.hash( - RLP.encode( - out -> { - out.startList(); - out.writeBytes(senderAddress); - out.writeLongScalar(nonce); - out.writeBytes(privacyGroupId); - out.endList(); - }))); - } - - public static Address fromPlugin(final org.hyperledger.besu.plugin.data.Address logger) { - return logger instanceof Address ? (Address) logger : wrap(logger.copy()); - } -} diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/LacchainProtocolSpecs.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/LacchainProtocolSpecs.java index e0cd009fe0d..0dcfe11129d 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/LacchainProtocolSpecs.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/LacchainProtocolSpecs.java @@ -14,9 +14,10 @@ */ package org.hyperledger.besu.ethereum.mainnet; -import org.hyperledger.besu.ethereum.core.Account; -import org.hyperledger.besu.ethereum.core.Address; -import org.hyperledger.besu.ethereum.mainnet.precompiles.FalconPrecompiledContract; +import org.hyperledger.besu.datatypes.Address; +import org.hyperledger.besu.evm.internal.EvmConfiguration; +import org.hyperledger.besu.evm.precompile.FalconPrecompiledContract; +import org.hyperledger.besu.evm.precompile.PrecompileContractRegistry; import java.math.BigInteger; import java.util.Optional; @@ -27,21 +28,19 @@ public static ProtocolSpecBuilder postQuantumDefinition( final Optional chainId, final OptionalInt contractSizeLimit, final OptionalInt configStackSizeLimit, - final boolean enableRevertReason, - final boolean quorumCompatibilityMode) { + final boolean enableRevertReason) { return MainnetProtocolSpecs.istanbulDefinition( chainId, contractSizeLimit, configStackSizeLimit, enableRevertReason, - quorumCompatibilityMode) + EvmConfiguration.DEFAULT) .precompileContractRegistryBuilder( precompiledContractConfiguration -> { PrecompileContractRegistry lacchainContractsRegistry = MainnetPrecompiledContractRegistries.istanbul(precompiledContractConfiguration); lacchainContractsRegistry.put( Address.LACCHAIN_FALCON, - Account.DEFAULT_VERSION, new FalconPrecompiledContract( precompiledContractConfiguration.getGasCalculator())); return lacchainContractsRegistry; diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/MainnetProtocolSpecFactory.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/MainnetProtocolSpecFactory.java index 3a06d803980..e7aa5953f14 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/MainnetProtocolSpecFactory.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/MainnetProtocolSpecFactory.java @@ -210,6 +210,11 @@ public ProtocolSpecBuilder experimentalEipsDefinition( evmConfiguration); } + public ProtocolSpecBuilder lacchainDefinition() { + return LacchainProtocolSpecs.postQuantumDefinition( + chainId, contractSizeLimit, evmStackSize, isRevertReasonEnabled); + } + //////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////// // Classic Protocol Specs diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/ProtocolScheduleBuilder.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/ProtocolScheduleBuilder.java index 786fdd35976..92f8e8026f8 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/ProtocolScheduleBuilder.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/ProtocolScheduleBuilder.java @@ -309,6 +309,8 @@ private Stream> createMilestones( config.getGrayGlacierBlockNumber(), specFactory.grayGlacierDefinition(config)), blockNumberMilestone( config.getMergeNetSplitBlockNumber(), specFactory.parisDefinition(config)), + blockNumberMilestone( + config.getLacchainPostQuantumBlockNumber(), specFactory.lacchainDefinition()), // Timestamp Forks timestampMilestone(config.getShanghaiTime(), specFactory.shanghaiDefinition(config)), timestampMilestone(config.getCancunTime(), specFactory.cancunDefinition(config)), diff --git a/evm/build.gradle b/evm/build.gradle index dd8760c1079..ec31a153fde 100644 --- a/evm/build.gradle +++ b/evm/build.gradle @@ -40,6 +40,7 @@ dependencies { implementation 'com.github.ben-manes.caffeine:caffeine' implementation 'com.google.guava:guava' implementation 'net.java.dev.jna:jna' + implementation 'org.openquantumsafe:liboqs-java' implementation 'org.apache.tuweni:tuweni-bytes' implementation 'org.apache.tuweni:tuweni-units' implementation 'org.hyperledger.besu:arithmetic' diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/precompiles/FalconPrecompiledContract.java b/evm/src/main/java/org/hyperledger/besu/evm/precompile/FalconPrecompiledContract.java similarity index 85% rename from ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/precompiles/FalconPrecompiledContract.java rename to evm/src/main/java/org/hyperledger/besu/evm/precompile/FalconPrecompiledContract.java index 07d817713db..94b34e0401c 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/precompiles/FalconPrecompiledContract.java +++ b/evm/src/main/java/org/hyperledger/besu/evm/precompile/FalconPrecompiledContract.java @@ -12,21 +12,19 @@ * * SPDX-License-Identifier: Apache-2.0 */ -package org.hyperledger.besu.ethereum.mainnet.precompiles; +package org.hyperledger.besu.evm.precompile; import static java.nio.charset.StandardCharsets.UTF_8; import org.hyperledger.besu.crypto.Hash; -import org.hyperledger.besu.ethereum.core.Gas; -import org.hyperledger.besu.ethereum.mainnet.AbstractPrecompiledContract; -import org.hyperledger.besu.ethereum.vm.GasCalculator; -import org.hyperledger.besu.ethereum.vm.MessageFrame; +import org.hyperledger.besu.evm.frame.MessageFrame; +import org.hyperledger.besu.evm.gascalculator.GasCalculator; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; import org.apache.tuweni.bytes.Bytes; import org.apache.tuweni.bytes.Bytes32; import org.openquantumsafe.Signature; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * note: Liboqs - random number generation defaults to /dev/urandom a better form is to use the @@ -35,7 +33,7 @@ */ public class FalconPrecompiledContract extends AbstractPrecompiledContract { - private static final Logger LOG = LogManager.getLogger(); + private static final Logger LOG = LoggerFactory.getLogger(AbstractBLS12PrecompiledContract.class); private static final Bytes METHOD_ABI = Hash.keccak256(Bytes.of("verify(bytes,bytes,bytes)".getBytes(UTF_8))).slice(0, 4); @@ -47,8 +45,10 @@ public FalconPrecompiledContract(final GasCalculator gasCalculator) { } @Override - public Gas gasRequirement(final Bytes input) { - return gasCalculator().sha256PrecompiledContractGasCost(input); + public long gasRequirement(final Bytes input) { + long value = gasCalculator().sha256PrecompiledContractGasCost(input); + LOG.debug("Gas requirement calculation for Falcon has been called {}", value); + return value; } @Override diff --git a/gradle/versions.gradle b/gradle/versions.gradle index 46ebb9c992b..2036c4300d5 100644 --- a/gradle/versions.gradle +++ b/gradle/versions.gradle @@ -222,6 +222,7 @@ dependencyManagement { dependency 'org.openquantumsafe:liboqs-java:1.1-SNAPSHOT' dependency 'org.testcontainers:testcontainers:1.17.6' + dependency 'org.openquantumsafe:liboqs-java:1.1-SNAPSHOT' dependency 'org.web3j:quorum:4.9.5' dependencySet(group: 'org.web3j', version: '4.9.7') {