Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package org.hyperledger.besu.cli.config;

import org.hyperledger.besu.crypto.SECP256K1;
import org.hyperledger.besu.crypto.SECP256R1;
import org.hyperledger.besu.evm.precompile.AltBN128PairingPrecompiledContract;
import org.hyperledger.besu.evm.precompile.BLS12PairingPrecompiledContract;

Expand Down Expand Up @@ -48,6 +49,15 @@ record NativeRequirementResult(Boolean present, String libname, Optional<String>
? Optional.empty()
: Optional.of("secp256k1: Native secp256k1 failed to load")));

var secp256r1 = new SECP256R1();
requirements.add(
new NativeRequirementResult(
secp256r1.maybeEnableNative(),
"secp256r1",
secp256r1.maybeEnableNative()
? Optional.empty()
: Optional.of("secp256r1: Native secp256r1 failed to load")));

requirements.add(
new NativeRequirementResult(
AltBN128PairingPrecompiledContract.isNative(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,17 @@ public SECPSignature sign(final Bytes32 dataHash, final KeyPair keyPair) {
@Override
public boolean verify(final Bytes data, final SECPSignature signature, final SECPPublicKey pub) {
if (useNative) {
return verifyNative(data, signature, pub);
return verifyNative(data, signature, pub, false);
} else {
return super.verify(data, signature, pub);
}
}

@Override
public boolean verifyMalleable(
final Bytes data, final SECPSignature signature, final SECPPublicKey pub) {
if (useNative) {
return verifyNative(data, signature, pub, true);
} else {
return super.verify(data, signature, pub);
}
Expand Down Expand Up @@ -164,12 +174,16 @@ private Optional<SECPPublicKey> recoverPublicKeyFromSignatureNative(
}

private boolean verifyNative(
final Bytes data, final SECPSignature signature, final SECPPublicKey pub) {
final Bytes data,
final SECPSignature signature,
final SECPPublicKey pub,
final boolean allowMalleable) {

return libSECP256R1.verify(
data.toArrayUnsafe(),
signature.getR().toByteArray(),
signature.getS().toByteArray(),
pub.getEncoded());
pub.getEncoded(),
allowMalleable);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public interface SignatureAlgorithm {
SECPSignature sign(final Bytes32 dataHash, final KeyPair keyPair);

/**
* Verify given data.
* Verify given message digest data, signature and public key.
*
* @param data the data
* @param signature the signature
Expand All @@ -65,6 +65,21 @@ public interface SignatureAlgorithm {
*/
boolean verify(final Bytes data, final SECPSignature signature, final SECPPublicKey pub);

/**
* Verify given message digest data, signature and public key, allowing for malleable signatures.
*
* @param data the data
* @param signature the signature
* @param pub the pub
* @return the boolean
*/
default boolean verifyMalleable(
final Bytes data, final SECPSignature signature, final SECPPublicKey pub) {
throw new UnsupportedOperationException(
"Malleable signatures are not supported for this curve " + getCurveName());
}
;

/**
* Verify given data.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ public class Address extends DelegatingBytes {
/** The constant BLS12_MAP_FP2_TO_G2. */
public static final Address BLS12_MAP_FP2_TO_G2 = Address.precompiled(0x11);

/** Precompile address for P256_VERIFY. */
public static final Address P256_VERIFY = Address.precompiled(0x0100);

/** The constant ZERO. */
public static final Address ZERO = Address.fromHexString("0x0");

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.hyperledger.besu.evmtool.benchmarks.BenchmarkExecutor;
import org.hyperledger.besu.evmtool.benchmarks.ECRecoverBenchmark;
import org.hyperledger.besu.evmtool.benchmarks.ModExpBenchmark;
import org.hyperledger.besu.evmtool.benchmarks.P256VerifyBenchmark;
import org.hyperledger.besu.evmtool.benchmarks.Secp256k1Benchmark;
import org.hyperledger.besu.util.BesuVersionUtils;
import org.hyperledger.besu.util.LogConfigurator;
Expand Down Expand Up @@ -65,7 +66,8 @@ enum Benchmark {
ModExp(ModExpBenchmark::new),
Secp256k1(Secp256k1Benchmark::new),
// bls12
Bls12(BLS12Benchmark::new);
Bls12(BLS12Benchmark::new),
p256Verify(P256VerifyBenchmark::new);

private final BenchmarkExecutor.Builder executorBuilder;

Expand Down
Loading
Loading