Skip to content

Commit 021296b

Browse files
committed
fix precompile address decimal/hex, allow precompiles up to 512
Signed-off-by: garyschulte <garyschulte@gmail.com>
1 parent 69c9ede commit 021296b

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

datatypes/src/main/java/org/hyperledger/besu/datatypes/Address.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ public class Address extends DelegatingBytes {
9090
/** The constant BLS12_MAP_FP2_TO_G2. */
9191
public static final Address BLS12_MAP_FP2_TO_G2 = Address.precompiled(0x11);
9292

93+
/** Precompile address for P256_VERIFY. */
94+
public static final Address P256_VERIFY = Address.precompiled(0x0100);
95+
9396
/** The constant ZERO. */
9497
public static final Address ZERO = Address.fromHexString("0x0");
9598

@@ -214,10 +217,11 @@ public static Address fromHexStringStrict(final String str) {
214217
* @return the address
215218
*/
216219
public static Address precompiled(final int value) {
217-
// Keep it simple while we don't need precompiled above 127.
218-
checkArgument(value < Byte.MAX_VALUE);
220+
// Allow values up to 0x01FF (511) to encompass layer2 precompile address space
221+
checkArgument(value < 0x01FF, "Precompiled value must be <= 0x01FF");
219222
final byte[] address = new byte[SIZE];
220-
address[SIZE - 1] = (byte) value;
223+
address[SIZE - 2] = (byte) (value >>> 8); // High byte
224+
address[SIZE - 1] = (byte) (value & 0xFF); // Low byte
221225
return new Address(Bytes.wrap(address));
222226
}
223227

evm/src/main/java/org/hyperledger/besu/evm/gascalculator/OsakaGasCalculator.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@
1515
package org.hyperledger.besu.evm.gascalculator;
1616

1717
import static org.hyperledger.besu.datatypes.Address.BLS12_MAP_FP2_TO_G2;
18+
import static org.hyperledger.besu.datatypes.Address.P256_VERIFY;
1819
import static org.hyperledger.besu.evm.internal.Words.clampedAdd;
1920
import static org.hyperledger.besu.evm.internal.Words.clampedMultiply;
2021
import static org.hyperledger.besu.evm.internal.Words.clampedToInt;
2122

23+
import org.hyperledger.besu.datatypes.Address;
2224
import org.hyperledger.besu.evm.internal.Words;
2325
import org.hyperledger.besu.evm.precompile.BigIntegerModularExponentiationPrecompiledContract;
2426

@@ -37,7 +39,7 @@ public class OsakaGasCalculator extends PragueGasCalculator {
3739

3840
/** Instantiates a new Osaka Gas Calculator. */
3941
public OsakaGasCalculator() {
40-
this(BLS12_MAP_FP2_TO_G2.toArrayUnsafe()[19]);
42+
this(P256_VERIFY.getInt(16));
4143
}
4244

4345
/**
@@ -49,6 +51,26 @@ protected OsakaGasCalculator(final int maxPrecompile) {
4951
super(maxPrecompile);
5052
}
5153

54+
@Override
55+
public boolean isPrecompile(final Address address) {
56+
final byte[] addressBytes = address.toArrayUnsafe();
57+
58+
// First 18 bytes must be zero:
59+
for (int i = 0; i < 18; i++) {
60+
if (addressBytes[i] != 0) {
61+
return false;
62+
}
63+
}
64+
65+
// Interpret last two bytes as big-endian unsigned short.
66+
final int precompileValue =
67+
(Byte.toUnsignedInt(addressBytes[18]) << 8) |
68+
Byte.toUnsignedInt(addressBytes[19]);
69+
70+
// values in range [1, 0x01FF] inclusive to include L1 and L2 precompiles:
71+
return precompileValue > 0 && precompileValue <= 0x01FF;
72+
}
73+
5274
@Override
5375
public long modExpGasCost(final Bytes input) {
5476
final long baseLength = BigIntegerModularExponentiationPrecompiledContract.baseLength(input);

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import org.hyperledger.besu.datatypes.Address;
1818
import org.hyperledger.besu.evm.gascalculator.GasCalculator;
1919

20+
import static org.hyperledger.besu.datatypes.Address.P256_VERIFY;
21+
2022
/** Provides the various precompiled contracts used on mainnet hard forks. */
2123
public interface MainnetPrecompiledContracts {
2224

@@ -197,8 +199,8 @@ static void populateForOsaka(
197199
// EIP-7823 - Set upper bounds for MODEXP
198200
registry.put(
199201
Address.MODEXP, BigIntegerModularExponentiationPrecompiledContract.osaka(gasCalculator));
200-
// RIP-7212 - secp256r1 P256VERIFY
201-
registry.put(Address.precompiled(100), new P256VerifyPrecompiledContract(gasCalculator));
202+
// EIP-7951 - secp256r1 P256VERIFY
203+
registry.put(P256_VERIFY, new P256VerifyPrecompiledContract(gasCalculator));
202204
}
203205

204206
/**

0 commit comments

Comments
 (0)