Skip to content

Commit b87ccb2

Browse files
authored
Provide SECP256R1 default to precompile (#9307)
Also remove sigleton pattern from ReferenceTestProtocolSchedules.
1 parent 4693bf5 commit b87ccb2

File tree

7 files changed

+39
-34
lines changed

7 files changed

+39
-34
lines changed

ethereum/evmtool/src/main/java/org/hyperledger/besu/evmtool/CodeValidateSubCommand.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright ConsenSys AG.
2+
* Copyright contributors to Besu.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
55
* the License. You may obtain a copy of the License at
@@ -91,7 +91,7 @@ public CodeValidateSubCommand() {
9191
Suppliers.memoize(
9292
() -> {
9393
if (parentCommand == null) {
94-
return ReferenceTestProtocolSchedules.getInstance().geSpecByName(fork).getEvm();
94+
return ReferenceTestProtocolSchedules.create().geSpecByName(fork).getEvm();
9595
}
9696
return ReferenceTestProtocolSchedules.create(parentCommand.getEvmConfiguration())
9797
.geSpecByName(fork)

ethereum/referencetests/src/main/java/org/hyperledger/besu/ethereum/referencetests/ReferenceTestProtocolSchedules.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@
3939
import java.util.function.Function;
4040
import java.util.stream.Collectors;
4141

42+
/**
43+
* Loads all available protocol schedules into memory and lets users select the appropriate one.
44+
* Beware of the high memory usage and object allocation cost since **all** protocol schedules will
45+
* be created and initialized for each created instance. This might cause your tests to slow down.
46+
*/
4247
public class ReferenceTestProtocolSchedules {
4348

4449
static {
@@ -61,13 +66,8 @@ public class ReferenceTestProtocolSchedules {
6166
"eip158",
6267
"eip158tobyzantiumat5");
6368

64-
private static ReferenceTestProtocolSchedules INSTANCE;
65-
66-
public static ReferenceTestProtocolSchedules getInstance() {
67-
if (INSTANCE == null) {
68-
INSTANCE = create(new StubGenesisConfigOptions(), EvmConfiguration.DEFAULT);
69-
}
70-
return INSTANCE;
69+
public static ReferenceTestProtocolSchedules create() {
70+
return create(new StubGenesisConfigOptions(), EvmConfiguration.DEFAULT);
7171
}
7272

7373
public static ReferenceTestProtocolSchedules create(final EvmConfiguration evmConfiguration) {

ethereum/referencetests/src/reference-test/java/org/hyperledger/besu/ethereum/core/TransactionTest.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright ConsenSys AG.
2+
* Copyright contributors to Besu.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
55
* the License. You may obtain a copy of the License at
@@ -49,13 +49,17 @@
4949
import org.junit.jupiter.params.provider.MethodSource;
5050

5151
public class TransactionTest {
52+
private static final ReferenceTestProtocolSchedules PROTOCOL_SCHEDULES;
53+
54+
static {
55+
PROTOCOL_SCHEDULES = ReferenceTestProtocolSchedules.create();
56+
}
5257

5358
private static TransactionValidator transactionValidator(final String name) {
54-
return ReferenceTestProtocolSchedules.getInstance()
55-
.getByName(name)
56-
.getByBlockHeader(BlockHeaderBuilder.createDefault().buildBlockHeader())
57-
.getTransactionValidatorFactory()
58-
.get();
59+
return PROTOCOL_SCHEDULES.getByName(name)
60+
.getByBlockHeader(BlockHeaderBuilder.createDefault().buildBlockHeader())
61+
.getTransactionValidatorFactory()
62+
.get();
5963
}
6064

6165
private static final String TEST_CONFIG_FILE_DIR_PATH = "TransactionTests/";

ethereum/referencetests/src/reference-test/java/org/hyperledger/besu/ethereum/eof/EOFReferenceTestTools.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright contributors to Hyperledger Besu.
2+
* Copyright contributors to Besu.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
55
* the License. You may obtain a copy of the License at
@@ -40,11 +40,13 @@
4040

4141
public class EOFReferenceTestTools {
4242
private static final List<String> EIPS_TO_RUN;
43+
private static final ReferenceTestProtocolSchedules PROTOCOL_SCHEDULES;
4344

4445
static {
4546
final String eips =
4647
System.getProperty("test.ethereum.eof.eips", "Osaka,Amsterdam,Bogota,Polis,Bangkok");
4748
EIPS_TO_RUN = Arrays.asList(eips.split(","));
49+
PROTOCOL_SCHEDULES = ReferenceTestProtocolSchedules.create();
4850
}
4951

5052
private static final JsonTestParameters<?, ?> params =
@@ -103,7 +105,7 @@ public static void executeTest(
103105
final Bytes code,
104106
final String containerKind,
105107
final EOFTestCaseSpec.TestResult expected) {
106-
EVM evm = ReferenceTestProtocolSchedules.getInstance().geSpecByName(fork).getEvm();
108+
EVM evm = PROTOCOL_SCHEDULES.geSpecByName(fork).getEvm();
107109
assertThat(evm).isNotNull();
108110

109111
// hardwire in the magic byte transaction checks

ethereum/referencetests/src/reference-test/java/org/hyperledger/besu/ethereum/vm/BlockchainReferenceTestTools.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright ConsenSys AG.
2+
* Copyright contributors to Besu.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
55
* the License. You may obtain a copy of the License at
@@ -71,6 +71,7 @@
7171
public class BlockchainReferenceTestTools {
7272

7373
private static final List<String> NETWORKS_TO_RUN;
74+
private static final ReferenceTestProtocolSchedules PROTOCOL_SCHEDULES;
7475

7576
static {
7677
final String networks =
@@ -80,6 +81,7 @@ public class BlockchainReferenceTestTools {
8081
+ "Frontier,Homestead,EIP150,EIP158,Byzantium,Constantinople,ConstantinopleFix,Istanbul,Berlin,"
8182
+ "London,Merge,Paris,Shanghai,Cancun,Prague,Osaka,Amsterdam,Bogota,Polis,Bangkok");
8283
NETWORKS_TO_RUN = Arrays.asList(networks.split(","));
84+
PROTOCOL_SCHEDULES = ReferenceTestProtocolSchedules.create();
8385
}
8486

8587
private static final JsonTestParameters<?, ?> params =
@@ -136,8 +138,7 @@ public static void executeTest(final String name, final BlockchainReferenceTestC
136138
.getWorldState(WorldStateQueryParams.withStateRootAndBlockHashAndUpdateNodeHead(genesisBlockHeader.getStateRoot(), genesisBlockHeader.getHash()))
137139
.orElseThrow();
138140

139-
final ProtocolSchedule schedule =
140-
ReferenceTestProtocolSchedules.getInstance().getByName(spec.getNetwork());
141+
final ProtocolSchedule schedule = PROTOCOL_SCHEDULES.getByName(spec.getNetwork());
141142

142143
final MutableBlockchain blockchain = spec.getBlockchain();
143144
final ProtocolContext context = spec.getProtocolContext();

ethereum/referencetests/src/reference-test/java/org/hyperledger/besu/ethereum/vm/GeneralStateReferenceTestTools.java

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright ConsenSys AG.
2+
* Copyright contributors to Besu.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
55
* the License. You may obtain a copy of the License at
@@ -55,20 +55,16 @@
5555
public class GeneralStateReferenceTestTools {
5656
private static final Logger LOG = LoggerFactory.getLogger(GeneralStateReferenceTestTools.class);
5757

58-
private static final List<String> SPECS_PRIOR_TO_DELETING_EMPTY_ACCOUNTS =
59-
Arrays.asList("Frontier", "Homestead", "EIP150");
60-
6158
private static MainnetTransactionProcessor transactionProcessor(final String name) {
6259
return protocolSpec(name).getTransactionProcessor();
6360
}
6461

6562
private static ProtocolSpec protocolSpec(final String name) {
66-
return ReferenceTestProtocolSchedules.getInstance()
67-
.getByName(name)
68-
.getByBlockHeader(BlockHeaderBuilder.createDefault().buildBlockHeader());
63+
return PROTOCOL_SCHEDULES.getByName(name).getByBlockHeader(BlockHeaderBuilder.createDefault().buildBlockHeader());
6964
}
7065

7166
private static final List<String> EIPS_TO_RUN;
67+
private static final ReferenceTestProtocolSchedules PROTOCOL_SCHEDULES;
7268

7369
static {
7470
final String eips =
@@ -77,6 +73,7 @@ private static ProtocolSpec protocolSpec(final String name) {
7773
"Frontier,Homestead,EIP150,EIP158,Byzantium,Constantinople,ConstantinopleFix,Istanbul,Berlin,"
7874
+ "London,Merge,Paris,Shanghai,Cancun,Prague,Osaka,Amsterdam,Bogota,Polis,Bangkok");
7975
EIPS_TO_RUN = Arrays.asList(eips.split(","));
76+
PROTOCOL_SCHEDULES = ReferenceTestProtocolSchedules.create();
8077
}
8178

8279
private static final JsonTestParameters<?, ?> params =
@@ -184,7 +181,7 @@ public static void executeTest(final GeneralStateTestCaseEipSpec spec) {
184181
.isNull();
185182

186183
final Account coinbase = worldStateUpdater.getOrCreate(spec.getBlockHeader().getCoinbase());
187-
if (coinbase != null && coinbase.isEmpty() && shouldClearEmptyAccounts(spec.getFork())) {
184+
if (coinbase != null && coinbase.isEmpty() && ReferenceTestProtocolSchedules.shouldClearEmptyAccounts(spec.getFork())) {
188185
worldStateUpdater.deleteAccount(coinbase.getAddress());
189186
}
190187
worldStateUpdater.commit();
@@ -222,10 +219,6 @@ public static void executeTest(final GeneralStateTestCaseEipSpec spec) {
222219
});
223220
}
224221

225-
private static boolean shouldClearEmptyAccounts(final String eip) {
226-
return !SPECS_PRIOR_TO_DELETING_EMPTY_ACCOUNTS.contains(eip);
227-
}
228-
229222
private static void logWorldState(final ReferenceTestWorldState worldState) {
230223
ObjectMapper mapper = new ObjectMapper();
231224
ObjectNode worldStateJson = mapper.createObjectNode();

evm/src/main/java/org/hyperledger/besu/evm/precompile/P256VerifyPrecompiledContract.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@
1414
*/
1515
package org.hyperledger.besu.evm.precompile;
1616

17-
import org.hyperledger.besu.crypto.SECP256R1;
17+
import static org.hyperledger.besu.crypto.SignatureAlgorithmType.SECP_256_R1_CURVE_NAME;
18+
1819
import org.hyperledger.besu.crypto.SECPPublicKey;
1920
import org.hyperledger.besu.crypto.SECPSignature;
2021
import org.hyperledger.besu.crypto.SignatureAlgorithm;
22+
import org.hyperledger.besu.crypto.SignatureAlgorithmType;
2123
import org.hyperledger.besu.evm.frame.MessageFrame;
2224
import org.hyperledger.besu.evm.gascalculator.GasCalculator;
2325
import org.hyperledger.besu.nativelib.boringssl.BoringSSLPrecompiles;
@@ -41,6 +43,7 @@ public class P256VerifyPrecompiledContract extends AbstractPrecompiledContract {
4143
private static final Bytes32 VALID = Bytes32.leftPad(Bytes.of(1), (byte) 0);
4244
private static final Bytes INVALID = Bytes.EMPTY;
4345
private static final int SECP256R1_INPUT_LENGTH = 160;
46+
private static final SignatureAlgorithm SECP256R1_SIGNATURE_ALGORITHM_INSTANCE;
4447

4548
static final X9ECParameters R1_PARAMS = SECNamedCurves.getByName("secp256r1");
4649
static final BigInteger N = R1_PARAMS.getN();
@@ -52,6 +55,8 @@ public class P256VerifyPrecompiledContract extends AbstractPrecompiledContract {
5255

5356
static {
5457
maybeEnableNativeBoringSSL();
58+
SECP256R1_SIGNATURE_ALGORITHM_INSTANCE =
59+
SignatureAlgorithmType.create(SECP_256_R1_CURVE_NAME).getInstance();
5560
}
5661

5762
/**
@@ -98,7 +103,7 @@ public static boolean isNativeBoringSSL() {
98103
* @param gasCalculator the gas calculator
99104
*/
100105
public P256VerifyPrecompiledContract(final GasCalculator gasCalculator) {
101-
this(gasCalculator, new SECP256R1());
106+
this(gasCalculator, SECP256R1_SIGNATURE_ALGORITHM_INSTANCE);
102107
}
103108

104109
/**

0 commit comments

Comments
 (0)